@geonosis/policy 2.13.0 → 3.0.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.
- package/CHANGELOG.md +4 -0
- package/dist/conformance.cjs +169 -0
- package/dist/conformance.d.cts +35 -0
- package/dist/conformance.d.ts +35 -0
- package/dist/conformance.js +129 -0
- package/dist/define.cjs +118 -0
- package/dist/define.d.cts +34 -0
- package/dist/define.d.ts +34 -0
- package/dist/define.js +85 -0
- package/dist/drizzle/index.cjs +37 -32
- package/dist/drizzle/index.d.cts +20 -23
- package/dist/drizzle/index.d.ts +20 -23
- package/dist/drizzle/index.js +2 -5
- package/dist/index.cjs +79 -856
- package/dist/index.d.cts +19 -428
- package/dist/index.d.ts +19 -428
- package/dist/index.js +35 -797
- package/dist/journal-events.cjs +68 -0
- package/dist/journal-events.d.cts +26 -0
- package/dist/journal-events.d.ts +26 -0
- package/dist/journal-events.js +28 -0
- package/dist/lattice.cjs +150 -0
- package/dist/lattice.d.cts +48 -0
- package/dist/lattice.d.ts +48 -0
- package/dist/lattice.js +110 -0
- package/dist/learning.cjs +246 -0
- package/dist/learning.d.cts +121 -0
- package/dist/learning.d.ts +121 -0
- package/dist/learning.js +206 -0
- package/dist/markdown.cjs +118 -0
- package/dist/markdown.d.cts +35 -0
- package/dist/markdown.d.ts +35 -0
- package/dist/markdown.js +78 -0
- package/dist/medusa/index.cjs +39 -36
- package/dist/medusa/index.d.cts +14 -16
- package/dist/medusa/index.d.ts +14 -16
- package/dist/medusa/index.js +4 -9
- package/dist/permissions/index.cjs +49 -40
- package/dist/permissions/index.d.cts +18 -20
- package/dist/permissions/index.d.ts +18 -20
- package/dist/permissions/index.js +15 -25
- package/dist/read.cjs +111 -0
- package/dist/read.d.cts +51 -0
- package/dist/read.d.ts +51 -0
- package/dist/read.js +71 -0
- package/dist/rows.cjs +54 -0
- package/dist/rows.d.cts +11 -0
- package/dist/rows.d.ts +11 -0
- package/dist/{chunk-JICJ4XEX.js → rows.js} +0 -3
- package/dist/seed.cjs +53 -0
- package/dist/seed.d.cts +18 -0
- package/dist/seed.d.ts +18 -0
- package/dist/seed.js +13 -0
- package/dist/threshold.cjs +73 -0
- package/dist/threshold.d.cts +45 -0
- package/dist/threshold.d.ts +45 -0
- package/dist/threshold.js +33 -0
- package/dist/types.cjs +47 -0
- package/dist/{types-C-Q3r-In.d.cts → types.d.cts} +21 -23
- package/dist/{types-C-Q3r-In.d.ts → types.d.ts} +21 -23
- package/dist/types.js +7 -0
- package/package.json +3 -3
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/journal-events.ts
|
|
40
|
+
var exports_journal_events = {};
|
|
41
|
+
__export(exports_journal_events, {
|
|
42
|
+
journalRowForEvent: () => journalRowForEvent
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(exports_journal_events);
|
|
45
|
+
var firstString = (data, keys) => {
|
|
46
|
+
for (const key of keys) {
|
|
47
|
+
const value = data[key];
|
|
48
|
+
if (typeof value === "string" && value.length > 0)
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
};
|
|
53
|
+
var journalRowForEvent = (eventName, data, options) => {
|
|
54
|
+
const payload = Array.isArray(data) ? data[0] : data;
|
|
55
|
+
if (!payload || typeof payload !== "object")
|
|
56
|
+
return null;
|
|
57
|
+
const subject = firstString(payload, options.subjectKeys);
|
|
58
|
+
if (!subject)
|
|
59
|
+
return null;
|
|
60
|
+
const actorId = firstString(payload, options.actorKeys ?? []);
|
|
61
|
+
return {
|
|
62
|
+
actor_id: actorId,
|
|
63
|
+
actor_kind: actorId ? "user" : "system",
|
|
64
|
+
event_name: eventName,
|
|
65
|
+
op: eventName.split(".")[0] ?? eventName,
|
|
66
|
+
subject
|
|
67
|
+
};
|
|
68
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** One journal row derived from something that happened. */
|
|
2
|
+
export type DomainJournalRow = {
|
|
3
|
+
actor_id: null | string;
|
|
4
|
+
actor_kind: 'system' | 'user';
|
|
5
|
+
event_name: string;
|
|
6
|
+
op: string;
|
|
7
|
+
subject: string;
|
|
8
|
+
};
|
|
9
|
+
export type JournalRowOptions = {
|
|
10
|
+
/** Where this build puts the human who caused it, when there is one. */
|
|
11
|
+
actorKeys?: readonly string[];
|
|
12
|
+
/** Where this build puts the id of the thing an event is about. */
|
|
13
|
+
subjectKeys: readonly string[];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The journal row for one domain event, or `null` when there is nothing countable to say.
|
|
17
|
+
*
|
|
18
|
+
* The learning loop reads four signals: a human reversing an automated decision, repeated refusals
|
|
19
|
+
* of the same rule, readers falling back to their own defaults, and the same operation run over and
|
|
20
|
+
* over. A reader journals its own fallbacks, so the third arrives on its own — the other three are
|
|
21
|
+
* computed from things that HAPPENED, and without this they are computed over an empty table.
|
|
22
|
+
*
|
|
23
|
+
* The keys are the caller's, with no default: which field carries the id of the thing an event is
|
|
24
|
+
* about is a fact about one system, and guessing would be quietly wrong rather than loudly absent.
|
|
25
|
+
*/
|
|
26
|
+
export declare const journalRowForEvent: (eventName: string, data: unknown, options: JournalRowOptions) => DomainJournalRow | null;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** One journal row derived from something that happened. */
|
|
2
|
+
export type DomainJournalRow = {
|
|
3
|
+
actor_id: null | string;
|
|
4
|
+
actor_kind: 'system' | 'user';
|
|
5
|
+
event_name: string;
|
|
6
|
+
op: string;
|
|
7
|
+
subject: string;
|
|
8
|
+
};
|
|
9
|
+
export type JournalRowOptions = {
|
|
10
|
+
/** Where this build puts the human who caused it, when there is one. */
|
|
11
|
+
actorKeys?: readonly string[];
|
|
12
|
+
/** Where this build puts the id of the thing an event is about. */
|
|
13
|
+
subjectKeys: readonly string[];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The journal row for one domain event, or `null` when there is nothing countable to say.
|
|
17
|
+
*
|
|
18
|
+
* The learning loop reads four signals: a human reversing an automated decision, repeated refusals
|
|
19
|
+
* of the same rule, readers falling back to their own defaults, and the same operation run over and
|
|
20
|
+
* over. A reader journals its own fallbacks, so the third arrives on its own — the other three are
|
|
21
|
+
* computed from things that HAPPENED, and without this they are computed over an empty table.
|
|
22
|
+
*
|
|
23
|
+
* The keys are the caller's, with no default: which field carries the id of the thing an event is
|
|
24
|
+
* about is a fact about one system, and guessing would be quietly wrong rather than loudly absent.
|
|
25
|
+
*/
|
|
26
|
+
export declare const journalRowForEvent: (eventName: string, data: unknown, options: JournalRowOptions) => DomainJournalRow | null;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/journal-events.ts
|
|
2
|
+
var firstString = (data, keys) => {
|
|
3
|
+
for (const key of keys) {
|
|
4
|
+
const value = data[key];
|
|
5
|
+
if (typeof value === "string" && value.length > 0)
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
return null;
|
|
9
|
+
};
|
|
10
|
+
var journalRowForEvent = (eventName, data, options) => {
|
|
11
|
+
const payload = Array.isArray(data) ? data[0] : data;
|
|
12
|
+
if (!payload || typeof payload !== "object")
|
|
13
|
+
return null;
|
|
14
|
+
const subject = firstString(payload, options.subjectKeys);
|
|
15
|
+
if (!subject)
|
|
16
|
+
return null;
|
|
17
|
+
const actorId = firstString(payload, options.actorKeys ?? []);
|
|
18
|
+
return {
|
|
19
|
+
actor_id: actorId,
|
|
20
|
+
actor_kind: actorId ? "user" : "system",
|
|
21
|
+
event_name: eventName,
|
|
22
|
+
op: eventName.split(".")[0] ?? eventName,
|
|
23
|
+
subject
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export {
|
|
27
|
+
journalRowForEvent
|
|
28
|
+
};
|
package/dist/lattice.cjs
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/lattice.ts
|
|
40
|
+
var exports_lattice = {};
|
|
41
|
+
__export(exports_lattice, {
|
|
42
|
+
appliesTo: () => appliesTo,
|
|
43
|
+
resolveConstraint: () => resolveConstraint,
|
|
44
|
+
specificityOf: () => specificityOf,
|
|
45
|
+
unsetKindsOf: () => unsetKindsOf,
|
|
46
|
+
unsupportedScopesOf: () => unsupportedScopesOf,
|
|
47
|
+
unusableRulesOf: () => unusableRulesOf,
|
|
48
|
+
usableRulesOf: () => usableRulesOf
|
|
49
|
+
});
|
|
50
|
+
module.exports = __toCommonJS(exports_lattice);
|
|
51
|
+
var specificityOf = (scopeKeys, scope) => {
|
|
52
|
+
if (!scope)
|
|
53
|
+
return 0;
|
|
54
|
+
let score = 0;
|
|
55
|
+
for (const [index, key] of scopeKeys.entries()) {
|
|
56
|
+
if (scope[key] !== undefined)
|
|
57
|
+
score += 2 ** index;
|
|
58
|
+
}
|
|
59
|
+
return score;
|
|
60
|
+
};
|
|
61
|
+
var appliesTo = (rule, query, scopeKeys) => {
|
|
62
|
+
if (!rule.scope)
|
|
63
|
+
return true;
|
|
64
|
+
for (const [key, want] of Object.entries(rule.scope)) {
|
|
65
|
+
if (want === undefined)
|
|
66
|
+
continue;
|
|
67
|
+
if (!scopeKeys.includes(key))
|
|
68
|
+
return false;
|
|
69
|
+
if (query[key] !== want)
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
};
|
|
74
|
+
var unsupportedScopesOf = (rules, scopeKeys) => {
|
|
75
|
+
const out = [];
|
|
76
|
+
for (const rule of rules) {
|
|
77
|
+
const scope = rule.scope;
|
|
78
|
+
if (!scope)
|
|
79
|
+
continue;
|
|
80
|
+
const keys = Object.keys(scope).filter((key) => scope[key] !== undefined && !scopeKeys.includes(key));
|
|
81
|
+
if (keys.length > 0)
|
|
82
|
+
out.push({ id: rule.id, keys });
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
};
|
|
86
|
+
var unusableBecause = (rule, data) => {
|
|
87
|
+
if (!data.tiers.some((tier) => tier.name === rule.tier))
|
|
88
|
+
return `unknown tier "${rule.tier}"`;
|
|
89
|
+
const kind = data.kinds.find((spec) => spec.kind === rule.constraint.kind);
|
|
90
|
+
if (!kind)
|
|
91
|
+
return `unknown kind "${rule.constraint.kind}"`;
|
|
92
|
+
if (kind.schema && !kind.schema.safeParse(rule.constraint.value).success) {
|
|
93
|
+
return `value ${JSON.stringify(rule.constraint.value)} is not a ${kind.kind}`;
|
|
94
|
+
}
|
|
95
|
+
return;
|
|
96
|
+
};
|
|
97
|
+
var unusableRulesOf = (rules, data) => rules.flatMap((rule) => {
|
|
98
|
+
const reason = unusableBecause(rule, data);
|
|
99
|
+
return reason ? [{ id: rule.id, reason }] : [];
|
|
100
|
+
});
|
|
101
|
+
var usableRulesOf = (rules, data) => rules.filter((rule) => unusableBecause(rule, data) === undefined);
|
|
102
|
+
var unsetKindsOf = (rules, kinds) => {
|
|
103
|
+
const set = new Set(rules.map((rule) => rule.constraint.kind));
|
|
104
|
+
return kinds.map((kind) => kind.kind).filter((kind) => !set.has(kind));
|
|
105
|
+
};
|
|
106
|
+
var sameValue = (kind, a, b) => kind?.sameValue ? kind.sameValue(a, b) : JSON.stringify(a) === JSON.stringify(b);
|
|
107
|
+
var explainDecision = (winner, overrode, conflicts) => {
|
|
108
|
+
const parts = [`${winner.id} (${winner.tier}) — ${winner.reason}`];
|
|
109
|
+
if (overrode)
|
|
110
|
+
parts.push(`overrides ${overrode.id} (${overrode.tier}, overridable)`);
|
|
111
|
+
if (conflicts.length > 0) {
|
|
112
|
+
parts.push(`CONFLICT with equal authority: ${conflicts.map((rule) => rule.id).join(", ")} — resolve in policy, not in code`);
|
|
113
|
+
}
|
|
114
|
+
return parts.join("; ");
|
|
115
|
+
};
|
|
116
|
+
var resolveConstraint = (data, kind, query = {}, rules = data.rules) => {
|
|
117
|
+
const rank = (tier) => data.tiers.findIndex((spec) => spec.name === tier);
|
|
118
|
+
const specificity = (scope) => specificityOf(data.scopeKeys, scope);
|
|
119
|
+
const pool = usableRulesOf(rules, data).filter((rule) => rule.constraint.kind === kind && appliesTo(rule, query, data.scopeKeys));
|
|
120
|
+
if (pool.length === 0)
|
|
121
|
+
return;
|
|
122
|
+
const sorted = pool.map((rule, index) => ({ index, rule })).toSorted((a, b) => {
|
|
123
|
+
const byTier = rank(a.rule.tier) - rank(b.rule.tier);
|
|
124
|
+
if (byTier !== 0)
|
|
125
|
+
return byTier;
|
|
126
|
+
const bySpecificity = specificity(b.rule.scope) - specificity(a.rule.scope);
|
|
127
|
+
return bySpecificity === 0 ? a.index - b.index : bySpecificity;
|
|
128
|
+
}).map((entry) => entry.rule);
|
|
129
|
+
const strongest = sorted[0];
|
|
130
|
+
if (!strongest)
|
|
131
|
+
return;
|
|
132
|
+
const tierOf = (rule) => data.tiers.find((spec) => spec.name === rule.tier);
|
|
133
|
+
const mayBeOverridden = (rule) => tierOf(rule)?.overridable === true && (rule.overridable ?? data.overridableByDefault);
|
|
134
|
+
const overrideTier = data.tiers.find((tier) => tier.overrides)?.name;
|
|
135
|
+
const override = overrideTier ? sorted.find((rule) => rule.tier === overrideTier) : undefined;
|
|
136
|
+
const overrode = override && override !== strongest && mayBeOverridden(strongest) ? strongest : undefined;
|
|
137
|
+
const winner = overrode ? override : strongest;
|
|
138
|
+
const suppressed = sorted.filter((rule) => rule !== winner);
|
|
139
|
+
const kindSpec = data.kinds.find((spec) => spec.kind === kind);
|
|
140
|
+
const conflicts = suppressed.filter((rule) => rank(rule.tier) === rank(winner.tier) && specificity(rule.scope) === specificity(winner.scope) && !sameValue(kindSpec, rule.constraint.value, winner.constraint.value));
|
|
141
|
+
return {
|
|
142
|
+
conflicts,
|
|
143
|
+
explain: explainDecision(winner, overrode, conflicts),
|
|
144
|
+
kind,
|
|
145
|
+
...overrode ? { overrode } : {},
|
|
146
|
+
suppressed,
|
|
147
|
+
value: winner.constraint.value,
|
|
148
|
+
winner
|
|
149
|
+
};
|
|
150
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { PolicyData, PolicyDecision, PolicyKindSpec, PolicyQuery, PolicyRule, PolicyScope, UnusableRule } from './types.cjs';
|
|
2
|
+
/**
|
|
3
|
+
* The precedence lattice — deciding which authority wins when several speak at once.
|
|
4
|
+
*
|
|
5
|
+
* A business is governed by several at a time: law, a signed agreement, its own SOP, a buyer's
|
|
6
|
+
* stated preference, the strategy currently running, an operator override typed at 6pm on a Friday.
|
|
7
|
+
* They contradict each other constantly, so "which one wins" must not be an accident of evaluation
|
|
8
|
+
* order — it is a declared ordering, and every decision carries a TRACE naming the winner and what
|
|
9
|
+
* it suppressed.
|
|
10
|
+
*
|
|
11
|
+
* Nothing in this file names a tier, a kind or a scope key. Two consumers wrote this lattice
|
|
12
|
+
* independently and disagreed on all three (a Medusa storefront's strongest tier is `regulatory`,
|
|
13
|
+
* musa's is `physics`), which is why every one of them is data.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* The weight of a scope key, derived from where it was declared.
|
|
17
|
+
*
|
|
18
|
+
* Powers of two in declaration order, so a narrower key can never be outvoted by an accumulation of
|
|
19
|
+
* vaguer ones: an order-scoped rule beats one scoped to region + category + group together, which
|
|
20
|
+
* is the intent every time someone writes an order-scoped exception. Both consumers wrote exactly
|
|
21
|
+
* that by hand and each needed a test to catch a key that had been given no weight at all — a rule
|
|
22
|
+
* that matches but always loses. Deriving it designs the failure out.
|
|
23
|
+
*/
|
|
24
|
+
export declare const specificityOf: (scopeKeys: readonly string[], scope: PolicyScope | undefined) => number;
|
|
25
|
+
/**
|
|
26
|
+
* Does this rule reach this subject?
|
|
27
|
+
*
|
|
28
|
+
* A rule scoped on a key the query does not carry does NOT apply, and neither does one scoped on a
|
|
29
|
+
* key this build has never heard of. That is the safe direction — a rule cannot reach a subject we
|
|
30
|
+
* know nothing about — but refusing quietly is the same failure in a new coat, which is what
|
|
31
|
+
* `unsupportedScopesOf` is for.
|
|
32
|
+
*/
|
|
33
|
+
export declare const appliesTo: (rule: PolicyRule, query: PolicyQuery, scopeKeys: readonly string[]) => boolean;
|
|
34
|
+
export declare const unsupportedScopesOf: (rules: readonly PolicyRule[], scopeKeys: readonly string[]) => {
|
|
35
|
+
id: string;
|
|
36
|
+
keys: string[];
|
|
37
|
+
}[];
|
|
38
|
+
export declare const unusableRulesOf: (rules: readonly PolicyRule[], data: Pick<PolicyData, 'kinds' | 'tiers'>) => UnusableRule[];
|
|
39
|
+
export declare const usableRulesOf: (rules: readonly PolicyRule[], data: Pick<PolicyData, 'kinds' | 'tiers'>) => PolicyRule[];
|
|
40
|
+
export declare const unsetKindsOf: (rules: readonly PolicyRule[], kinds: readonly PolicyKindSpec[]) => string[];
|
|
41
|
+
/**
|
|
42
|
+
* Resolve one kind for one subject, with the whole trace.
|
|
43
|
+
*
|
|
44
|
+
* `undefined` when no rule speaks — silence is a legitimate answer and the caller's own default
|
|
45
|
+
* takes over. Never a thrown error: a missing rule is the normal state of most subjects, and of
|
|
46
|
+
* every subject in a build nobody has written a policy for yet.
|
|
47
|
+
*/
|
|
48
|
+
export declare const resolveConstraint: <V = unknown>(data: PolicyData, kind: string, query?: PolicyQuery, rules?: readonly PolicyRule[]) => PolicyDecision<V> | undefined;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { PolicyData, PolicyDecision, PolicyKindSpec, PolicyQuery, PolicyRule, PolicyScope, UnusableRule } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The precedence lattice — deciding which authority wins when several speak at once.
|
|
4
|
+
*
|
|
5
|
+
* A business is governed by several at a time: law, a signed agreement, its own SOP, a buyer's
|
|
6
|
+
* stated preference, the strategy currently running, an operator override typed at 6pm on a Friday.
|
|
7
|
+
* They contradict each other constantly, so "which one wins" must not be an accident of evaluation
|
|
8
|
+
* order — it is a declared ordering, and every decision carries a TRACE naming the winner and what
|
|
9
|
+
* it suppressed.
|
|
10
|
+
*
|
|
11
|
+
* Nothing in this file names a tier, a kind or a scope key. Two consumers wrote this lattice
|
|
12
|
+
* independently and disagreed on all three (a Medusa storefront's strongest tier is `regulatory`,
|
|
13
|
+
* musa's is `physics`), which is why every one of them is data.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* The weight of a scope key, derived from where it was declared.
|
|
17
|
+
*
|
|
18
|
+
* Powers of two in declaration order, so a narrower key can never be outvoted by an accumulation of
|
|
19
|
+
* vaguer ones: an order-scoped rule beats one scoped to region + category + group together, which
|
|
20
|
+
* is the intent every time someone writes an order-scoped exception. Both consumers wrote exactly
|
|
21
|
+
* that by hand and each needed a test to catch a key that had been given no weight at all — a rule
|
|
22
|
+
* that matches but always loses. Deriving it designs the failure out.
|
|
23
|
+
*/
|
|
24
|
+
export declare const specificityOf: (scopeKeys: readonly string[], scope: PolicyScope | undefined) => number;
|
|
25
|
+
/**
|
|
26
|
+
* Does this rule reach this subject?
|
|
27
|
+
*
|
|
28
|
+
* A rule scoped on a key the query does not carry does NOT apply, and neither does one scoped on a
|
|
29
|
+
* key this build has never heard of. That is the safe direction — a rule cannot reach a subject we
|
|
30
|
+
* know nothing about — but refusing quietly is the same failure in a new coat, which is what
|
|
31
|
+
* `unsupportedScopesOf` is for.
|
|
32
|
+
*/
|
|
33
|
+
export declare const appliesTo: (rule: PolicyRule, query: PolicyQuery, scopeKeys: readonly string[]) => boolean;
|
|
34
|
+
export declare const unsupportedScopesOf: (rules: readonly PolicyRule[], scopeKeys: readonly string[]) => {
|
|
35
|
+
id: string;
|
|
36
|
+
keys: string[];
|
|
37
|
+
}[];
|
|
38
|
+
export declare const unusableRulesOf: (rules: readonly PolicyRule[], data: Pick<PolicyData, 'kinds' | 'tiers'>) => UnusableRule[];
|
|
39
|
+
export declare const usableRulesOf: (rules: readonly PolicyRule[], data: Pick<PolicyData, 'kinds' | 'tiers'>) => PolicyRule[];
|
|
40
|
+
export declare const unsetKindsOf: (rules: readonly PolicyRule[], kinds: readonly PolicyKindSpec[]) => string[];
|
|
41
|
+
/**
|
|
42
|
+
* Resolve one kind for one subject, with the whole trace.
|
|
43
|
+
*
|
|
44
|
+
* `undefined` when no rule speaks — silence is a legitimate answer and the caller's own default
|
|
45
|
+
* takes over. Never a thrown error: a missing rule is the normal state of most subjects, and of
|
|
46
|
+
* every subject in a build nobody has written a policy for yet.
|
|
47
|
+
*/
|
|
48
|
+
export declare const resolveConstraint: <V = unknown>(data: PolicyData, kind: string, query?: PolicyQuery, rules?: readonly PolicyRule[]) => PolicyDecision<V> | undefined;
|
package/dist/lattice.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// src/lattice.ts
|
|
2
|
+
var specificityOf = (scopeKeys, scope) => {
|
|
3
|
+
if (!scope)
|
|
4
|
+
return 0;
|
|
5
|
+
let score = 0;
|
|
6
|
+
for (const [index, key] of scopeKeys.entries()) {
|
|
7
|
+
if (scope[key] !== undefined)
|
|
8
|
+
score += 2 ** index;
|
|
9
|
+
}
|
|
10
|
+
return score;
|
|
11
|
+
};
|
|
12
|
+
var appliesTo = (rule, query, scopeKeys) => {
|
|
13
|
+
if (!rule.scope)
|
|
14
|
+
return true;
|
|
15
|
+
for (const [key, want] of Object.entries(rule.scope)) {
|
|
16
|
+
if (want === undefined)
|
|
17
|
+
continue;
|
|
18
|
+
if (!scopeKeys.includes(key))
|
|
19
|
+
return false;
|
|
20
|
+
if (query[key] !== want)
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
};
|
|
25
|
+
var unsupportedScopesOf = (rules, scopeKeys) => {
|
|
26
|
+
const out = [];
|
|
27
|
+
for (const rule of rules) {
|
|
28
|
+
const scope = rule.scope;
|
|
29
|
+
if (!scope)
|
|
30
|
+
continue;
|
|
31
|
+
const keys = Object.keys(scope).filter((key) => scope[key] !== undefined && !scopeKeys.includes(key));
|
|
32
|
+
if (keys.length > 0)
|
|
33
|
+
out.push({ id: rule.id, keys });
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
};
|
|
37
|
+
var unusableBecause = (rule, data) => {
|
|
38
|
+
if (!data.tiers.some((tier) => tier.name === rule.tier))
|
|
39
|
+
return `unknown tier "${rule.tier}"`;
|
|
40
|
+
const kind = data.kinds.find((spec) => spec.kind === rule.constraint.kind);
|
|
41
|
+
if (!kind)
|
|
42
|
+
return `unknown kind "${rule.constraint.kind}"`;
|
|
43
|
+
if (kind.schema && !kind.schema.safeParse(rule.constraint.value).success) {
|
|
44
|
+
return `value ${JSON.stringify(rule.constraint.value)} is not a ${kind.kind}`;
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
};
|
|
48
|
+
var unusableRulesOf = (rules, data) => rules.flatMap((rule) => {
|
|
49
|
+
const reason = unusableBecause(rule, data);
|
|
50
|
+
return reason ? [{ id: rule.id, reason }] : [];
|
|
51
|
+
});
|
|
52
|
+
var usableRulesOf = (rules, data) => rules.filter((rule) => unusableBecause(rule, data) === undefined);
|
|
53
|
+
var unsetKindsOf = (rules, kinds) => {
|
|
54
|
+
const set = new Set(rules.map((rule) => rule.constraint.kind));
|
|
55
|
+
return kinds.map((kind) => kind.kind).filter((kind) => !set.has(kind));
|
|
56
|
+
};
|
|
57
|
+
var sameValue = (kind, a, b) => kind?.sameValue ? kind.sameValue(a, b) : JSON.stringify(a) === JSON.stringify(b);
|
|
58
|
+
var explainDecision = (winner, overrode, conflicts) => {
|
|
59
|
+
const parts = [`${winner.id} (${winner.tier}) — ${winner.reason}`];
|
|
60
|
+
if (overrode)
|
|
61
|
+
parts.push(`overrides ${overrode.id} (${overrode.tier}, overridable)`);
|
|
62
|
+
if (conflicts.length > 0) {
|
|
63
|
+
parts.push(`CONFLICT with equal authority: ${conflicts.map((rule) => rule.id).join(", ")} — resolve in policy, not in code`);
|
|
64
|
+
}
|
|
65
|
+
return parts.join("; ");
|
|
66
|
+
};
|
|
67
|
+
var resolveConstraint = (data, kind, query = {}, rules = data.rules) => {
|
|
68
|
+
const rank = (tier) => data.tiers.findIndex((spec) => spec.name === tier);
|
|
69
|
+
const specificity = (scope) => specificityOf(data.scopeKeys, scope);
|
|
70
|
+
const pool = usableRulesOf(rules, data).filter((rule) => rule.constraint.kind === kind && appliesTo(rule, query, data.scopeKeys));
|
|
71
|
+
if (pool.length === 0)
|
|
72
|
+
return;
|
|
73
|
+
const sorted = pool.map((rule, index) => ({ index, rule })).toSorted((a, b) => {
|
|
74
|
+
const byTier = rank(a.rule.tier) - rank(b.rule.tier);
|
|
75
|
+
if (byTier !== 0)
|
|
76
|
+
return byTier;
|
|
77
|
+
const bySpecificity = specificity(b.rule.scope) - specificity(a.rule.scope);
|
|
78
|
+
return bySpecificity === 0 ? a.index - b.index : bySpecificity;
|
|
79
|
+
}).map((entry) => entry.rule);
|
|
80
|
+
const strongest = sorted[0];
|
|
81
|
+
if (!strongest)
|
|
82
|
+
return;
|
|
83
|
+
const tierOf = (rule) => data.tiers.find((spec) => spec.name === rule.tier);
|
|
84
|
+
const mayBeOverridden = (rule) => tierOf(rule)?.overridable === true && (rule.overridable ?? data.overridableByDefault);
|
|
85
|
+
const overrideTier = data.tiers.find((tier) => tier.overrides)?.name;
|
|
86
|
+
const override = overrideTier ? sorted.find((rule) => rule.tier === overrideTier) : undefined;
|
|
87
|
+
const overrode = override && override !== strongest && mayBeOverridden(strongest) ? strongest : undefined;
|
|
88
|
+
const winner = overrode ? override : strongest;
|
|
89
|
+
const suppressed = sorted.filter((rule) => rule !== winner);
|
|
90
|
+
const kindSpec = data.kinds.find((spec) => spec.kind === kind);
|
|
91
|
+
const conflicts = suppressed.filter((rule) => rank(rule.tier) === rank(winner.tier) && specificity(rule.scope) === specificity(winner.scope) && !sameValue(kindSpec, rule.constraint.value, winner.constraint.value));
|
|
92
|
+
return {
|
|
93
|
+
conflicts,
|
|
94
|
+
explain: explainDecision(winner, overrode, conflicts),
|
|
95
|
+
kind,
|
|
96
|
+
...overrode ? { overrode } : {},
|
|
97
|
+
suppressed,
|
|
98
|
+
value: winner.constraint.value,
|
|
99
|
+
winner
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
export {
|
|
103
|
+
appliesTo,
|
|
104
|
+
resolveConstraint,
|
|
105
|
+
specificityOf,
|
|
106
|
+
unsetKindsOf,
|
|
107
|
+
unsupportedScopesOf,
|
|
108
|
+
unusableRulesOf,
|
|
109
|
+
usableRulesOf
|
|
110
|
+
};
|