@ultimat3/manifest 31.50.0 → 31.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +8 -8
  2. package/src/diff-input.ts +78 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/manifest",
3
- "version": "31.50.0",
3
+ "version": "31.52.0",
4
4
  "description": "x.manifest.json: deterministic generated facts, contract diff, AGENTS.md budget",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -43,12 +43,12 @@
43
43
  "test": "bun test"
44
44
  },
45
45
  "dependencies": {
46
- "@ultimat3/action": "31.50.0",
47
- "@ultimat3/core": "31.50.0",
48
- "@ultimat3/entity": "31.50.0",
49
- "@ultimat3/jobs": "31.50.0",
50
- "@ultimat3/query": "31.50.0",
51
- "@ultimat3/realtime": "31.50.0",
52
- "@ultimat3/schema": "31.50.0"
46
+ "@ultimat3/action": "31.52.0",
47
+ "@ultimat3/core": "31.52.0",
48
+ "@ultimat3/entity": "31.52.0",
49
+ "@ultimat3/jobs": "31.52.0",
50
+ "@ultimat3/query": "31.52.0",
51
+ "@ultimat3/realtime": "31.52.0",
52
+ "@ultimat3/schema": "31.52.0"
53
53
  }
54
54
  }
package/src/diff-input.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // An input schema's change, classified (`diffActions`, `diffQueries`, `diffJobs`): byte-identical
2
- // is no change; only ADDING optional top-level properties to an object schema is `additive`, since
3
- // every payload the old (closed) schema accepted the new one accepts; anything else is `breaking`.
2
+ // is no change, and so is an `enum` reordered; ADDING optional top-level properties to a closed
3
+ // object schema, or ADDING values to an `enum` in a positive position, is `additive`, since every
4
+ // payload the old schema accepted the new one accepts; anything else is `breaking`.
4
5
  //
5
6
  // Deliberately shallow: only the top level is compared, so an optional property added INSIDE a
6
7
  // nested object still reads as breaking — over-reporting is the safe side of a contract check.
@@ -26,7 +27,7 @@ export function onlyAddsOptionalProperties(before: unknown, after: unknown): boo
26
27
  if (canonicalJson(beforeRest) !== canonicalJson(afterRest)) return false;
27
28
  // No property removed or changed.
28
29
  for (const [name, schema] of Object.entries(beforeProps)) {
29
- if (!(name in afterProps)) return false;
30
+ if (!Object.hasOwn(afterProps, name)) return false;
30
31
  if (canonicalJson(schema) !== canonicalJson(afterProps[name])) return false;
31
32
  }
32
33
  // Nothing newly required — an old property or a new one.
@@ -34,6 +35,74 @@ export function onlyAddsOptionalProperties(before: unknown, after: unknown): boo
34
35
  return names(afterRequired).every((name) => wasRequired.has(name));
35
36
  }
36
37
 
38
+ /** How `after` compares to `before` under {@link enumWidening}. */
39
+ export type EnumWidening = 'same' | 'wider' | 'other';
40
+
41
+ /** Keywords whose value is a schema (or a list of schemas) a payload must satisfy, not exclude. */
42
+ const SCHEMA_KEYWORDS = new Set(['items', 'additionalProperties', 'prefixItems', 'anyOf', 'allOf']);
43
+ /** Keywords whose value maps names to schemas, each one a positive position too. */
44
+ const SCHEMA_MAP_KEYWORDS = new Set(['properties', 'patternProperties']);
45
+
46
+ /** A reference keyword: it can reach a widened node from anywhere, a negative position too. */
47
+ const REF_KEYWORDS = new Set(['$ref', '$dynamicRef', '$recursiveRef']);
48
+
49
+ const refers = (value: unknown): boolean =>
50
+ Array.isArray(value)
51
+ ? value.some(refers)
52
+ : isJsonObject(value) &&
53
+ Object.keys(value).some((key) => REF_KEYWORDS.has(key) || refers(value[key]));
54
+
55
+ const joined = (results: readonly EnumWidening[]): EnumWidening =>
56
+ results.includes('other') ? 'other' : results.includes('wider') ? 'wider' : 'same';
57
+
58
+ const ownKeys = (value: Record<string, unknown>): string[] => Object.keys(value).sort();
59
+
60
+ const sameKeys = (before: Record<string, unknown>, after: Record<string, unknown>): boolean =>
61
+ canonicalJson(ownKeys(before)) === canonicalJson(ownKeys(after));
62
+
63
+ /** A schema, or a list of schemas, compared position by position. */
64
+ const schemaOrList = (before: unknown, after: unknown): EnumWidening => {
65
+ if (Array.isArray(before) && Array.isArray(after)) {
66
+ if (before.length !== after.length) return 'other';
67
+ return joined(before.map((item, index) => enumWidening(item, after[index])));
68
+ }
69
+ return enumWidening(before, after);
70
+ };
71
+
72
+ /**
73
+ * Whether `after` is `before` with values ADDED to `enum` lists and nothing else changed. Only a
74
+ * positive position is followed — `properties`, `items`, `anyOf`, `allOf` and their like — where
75
+ * accepting more at the node accepts more overall. `oneOf`, `not`, `if`, `const` and every other
76
+ * keyword must match exactly: widening inside one can refuse a payload the old schema took. An
77
+ * enum holding the same values in another order is `same`. A schema that refers (`$ref`) is never
78
+ * judged wider by `diffInput`: the reference can reach the widened node from a negative position.
79
+ */
80
+ export function enumWidening(before: unknown, after: unknown): EnumWidening {
81
+ if (!isJsonObject(before) || !isJsonObject(after)) {
82
+ return canonicalJson(before) === canonicalJson(after) ? 'same' : 'other';
83
+ }
84
+ if (!sameKeys(before, after)) return 'other';
85
+ const results: EnumWidening[] = [];
86
+ for (const key of ownKeys(before)) {
87
+ const was = before[key];
88
+ const now = after[key];
89
+ if (key === 'enum' && Array.isArray(was) && Array.isArray(now)) {
90
+ const old = new Set(was.map((value) => canonicalJson(value)));
91
+ const offered = new Set(now.map((value) => canonicalJson(value)));
92
+ if (![...old].every((value) => offered.has(value))) return 'other';
93
+ results.push(offered.size > old.size ? 'wider' : 'same');
94
+ } else if (SCHEMA_KEYWORDS.has(key)) {
95
+ results.push(schemaOrList(was, now));
96
+ } else if (SCHEMA_MAP_KEYWORDS.has(key) && isJsonObject(was) && isJsonObject(now)) {
97
+ if (!sameKeys(was, now)) return 'other';
98
+ results.push(joined(ownKeys(was).map((name) => enumWidening(was[name], now[name]))));
99
+ } else {
100
+ results.push(canonicalJson(was) === canonicalJson(now) ? 'same' : 'other');
101
+ }
102
+ }
103
+ return joined(results);
104
+ }
105
+
37
106
  /** The change between two input schemas at `path`, or none. */
38
107
  export function diffInput(
39
108
  path: string,
@@ -45,5 +114,11 @@ export function diffInput(
45
114
  if (onlyAddsOptionalProperties(before, after)) {
46
115
  return [{ kind: 'additive', path, detail: 'input gained optional properties' }];
47
116
  }
117
+ const widening = enumWidening(before, after);
118
+ if (widening === 'same') return [];
119
+ // A `$ref` may reach the widened enum from a `not` or a `oneOf`: not judged, so breaking.
120
+ if (widening === 'wider' && !refers(before)) {
121
+ return [{ kind: 'additive', path, detail: 'input accepts more enum values' }];
122
+ }
48
123
  return [{ kind: 'breaking', path, detail: breakingDetail }];
49
124
  }