@geonosis/policy 2.12.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 +6 -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
package/dist/read.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// src/read.ts
|
|
2
|
+
import { POLICY_FALLBACK, POLICY_INVALID_VALUE } from "./types.js";
|
|
3
|
+
var attempt = async (what) => {
|
|
4
|
+
try {
|
|
5
|
+
return await what();
|
|
6
|
+
} catch {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var accepts = (policy, kind, value) => {
|
|
11
|
+
const schema = policy.kindOf(kind)?.schema;
|
|
12
|
+
return schema ? schema.safeParse(value).success : true;
|
|
13
|
+
};
|
|
14
|
+
var record = async (policy, journal, entry) => {
|
|
15
|
+
const key = `${entry.event}::${entry.kind}::${entry.op ?? ""}`;
|
|
16
|
+
if (policy.journalledOnce.has(key))
|
|
17
|
+
return;
|
|
18
|
+
policy.journalledOnce.add(key);
|
|
19
|
+
await attempt(() => journal?.write(entry));
|
|
20
|
+
};
|
|
21
|
+
var readPolicy = async (policy, kind, query = {}, options) => {
|
|
22
|
+
const rules = options.rule ? await attempt(() => options.rule?.rules(kind, query)) ?? [] : policy.rules;
|
|
23
|
+
const decision = policy.resolve(kind, query, rules);
|
|
24
|
+
if (decision) {
|
|
25
|
+
const spec = policy.kindOf(kind);
|
|
26
|
+
if (spec?.isReference?.(decision.value)) {
|
|
27
|
+
const referenced = await attempt(() => spec.dereference?.(decision.value, { config: options.config, query }));
|
|
28
|
+
if (referenced !== undefined) {
|
|
29
|
+
return { decision, source: "rule", usedFallback: false, value: referenced };
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
return { decision, source: "rule", usedFallback: false, value: decision.value };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const configured = await attempt(() => options.config?.get(kind, query));
|
|
36
|
+
if (configured !== undefined && accepts(policy, kind, configured)) {
|
|
37
|
+
return { source: "config", usedFallback: false, value: configured };
|
|
38
|
+
}
|
|
39
|
+
if (!options.silent) {
|
|
40
|
+
await record(policy, options.journal, {
|
|
41
|
+
event: POLICY_FALLBACK,
|
|
42
|
+
fellBackTo: options.fallback,
|
|
43
|
+
kind,
|
|
44
|
+
...options.op ? { op: options.op } : {},
|
|
45
|
+
...options.subject ? { subject: options.subject } : {}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return { source: "fallback", usedFallback: true, value: options.fallback };
|
|
49
|
+
};
|
|
50
|
+
var policyOrDefault = async (policy, input, apply) => {
|
|
51
|
+
try {
|
|
52
|
+
return apply(input.value);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
await record(policy, input.journal, {
|
|
55
|
+
event: POLICY_INVALID_VALUE,
|
|
56
|
+
fellBackTo: input.fallback,
|
|
57
|
+
kind: input.kind,
|
|
58
|
+
op: input.op,
|
|
59
|
+
rejected: {
|
|
60
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
61
|
+
value: input.value
|
|
62
|
+
},
|
|
63
|
+
...input.subject ? { subject: input.subject } : {}
|
|
64
|
+
});
|
|
65
|
+
return apply(input.fallback);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
export {
|
|
69
|
+
policyOrDefault,
|
|
70
|
+
readPolicy
|
|
71
|
+
};
|
package/dist/rows.cjs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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/rows.ts
|
|
40
|
+
var exports_rows = {};
|
|
41
|
+
__export(exports_rows, {
|
|
42
|
+
toPolicyRule: () => toPolicyRule,
|
|
43
|
+
toPolicyRules: () => toPolicyRules
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(exports_rows);
|
|
46
|
+
var toPolicyRule = (row) => ({
|
|
47
|
+
constraint: { kind: row.constraint_kind, value: row.constraint_value },
|
|
48
|
+
id: row.rule_key,
|
|
49
|
+
...row.overridable === false ? { overridable: false } : {},
|
|
50
|
+
reason: row.reason,
|
|
51
|
+
...row.scope ? { scope: row.scope } : {},
|
|
52
|
+
tier: row.tier
|
|
53
|
+
});
|
|
54
|
+
var toPolicyRules = (rows) => rows.filter((row) => (row.status ?? "active") === "active").map(toPolicyRule);
|
package/dist/rows.d.cts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { PolicyRule, PolicyRuleRow } from './types.cjs';
|
|
2
|
+
/**
|
|
3
|
+
* A stored row, as a rule the lattice can use.
|
|
4
|
+
*
|
|
5
|
+
* This is the one part of a storage adapter that is not the store's own vocabulary, so it lives
|
|
6
|
+
* once: two adapters that each did their own mapping would drift on what `overridable: true` means,
|
|
7
|
+
* or on whether a null scope is a scope of nothing.
|
|
8
|
+
*/
|
|
9
|
+
export declare const toPolicyRule: (row: PolicyRuleRow) => PolicyRule;
|
|
10
|
+
/** The rules still in force. A retired row stays readable — it just stops deciding. */
|
|
11
|
+
export declare const toPolicyRules: (rows: readonly PolicyRuleRow[]) => PolicyRule[];
|
package/dist/rows.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { PolicyRule, PolicyRuleRow } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A stored row, as a rule the lattice can use.
|
|
4
|
+
*
|
|
5
|
+
* This is the one part of a storage adapter that is not the store's own vocabulary, so it lives
|
|
6
|
+
* once: two adapters that each did their own mapping would drift on what `overridable: true` means,
|
|
7
|
+
* or on whether a null scope is a scope of nothing.
|
|
8
|
+
*/
|
|
9
|
+
export declare const toPolicyRule: (row: PolicyRuleRow) => PolicyRule;
|
|
10
|
+
/** The rules still in force. A retired row stays readable — it just stops deciding. */
|
|
11
|
+
export declare const toPolicyRules: (rows: readonly PolicyRuleRow[]) => PolicyRule[];
|
|
@@ -2,15 +2,12 @@
|
|
|
2
2
|
var toPolicyRule = (row) => ({
|
|
3
3
|
constraint: { kind: row.constraint_kind, value: row.constraint_value },
|
|
4
4
|
id: row.rule_key,
|
|
5
|
-
// Only `false` is worth carrying: `true` is the default a policy declares, and writing it back
|
|
6
|
-
// would make a rule that never said anything indistinguishable from one that agreed.
|
|
7
5
|
...row.overridable === false ? { overridable: false } : {},
|
|
8
6
|
reason: row.reason,
|
|
9
7
|
...row.scope ? { scope: row.scope } : {},
|
|
10
8
|
tier: row.tier
|
|
11
9
|
});
|
|
12
10
|
var toPolicyRules = (rows) => rows.filter((row) => (row.status ?? "active") === "active").map(toPolicyRule);
|
|
13
|
-
|
|
14
11
|
export {
|
|
15
12
|
toPolicyRule,
|
|
16
13
|
toPolicyRules
|
package/dist/seed.cjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
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/seed.ts
|
|
40
|
+
var exports_seed = {};
|
|
41
|
+
__export(exports_seed, {
|
|
42
|
+
rulesToSeed: () => rulesToSeed
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(exports_seed);
|
|
45
|
+
var sameScope = (left, right) => {
|
|
46
|
+
const a = left ?? {};
|
|
47
|
+
const b = right ?? {};
|
|
48
|
+
return [...new Set([...Object.keys(a), ...Object.keys(b)])].every((key) => a[key] === b[key]);
|
|
49
|
+
};
|
|
50
|
+
var rulesToSeed = (seed, existing) => {
|
|
51
|
+
const active = existing.filter((rule) => (rule.status ?? "active") === "active");
|
|
52
|
+
return seed.filter((rule) => !active.some((row) => row.id === rule.id || row.constraint.kind === rule.constraint.kind && sameScope(row.scope, rule.scope)));
|
|
53
|
+
};
|
package/dist/seed.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PolicyRule } from './types.cjs';
|
|
2
|
+
/** A rule as a store hands it back: the kernel's shape, plus whether it is still in force. */
|
|
3
|
+
export type ExistingPolicyRule = PolicyRule & {
|
|
4
|
+
status?: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Which seed rules to author, given what is already there. Idempotent by ID and by COVERAGE.
|
|
8
|
+
*
|
|
9
|
+
* The coverage half is the one that was learned the hard way: a first seeding run produced two
|
|
10
|
+
* active rules for one constraint with identical scope and value — the seed's, and one an admin had
|
|
11
|
+
* authored by hand two days earlier. Same effect, two keys, and a conflict the lattice reports and
|
|
12
|
+
* a human has to unpick.
|
|
13
|
+
*
|
|
14
|
+
* A seed provides DEFAULTS. If the business has already said something about a constraint at the
|
|
15
|
+
* same scope, it has spoken, and the seed's opinion is not wanted. A RETIRED rule is not a current
|
|
16
|
+
* opinion, so it does not block.
|
|
17
|
+
*/
|
|
18
|
+
export declare const rulesToSeed: <T extends PolicyRule>(seed: readonly T[], existing: readonly ExistingPolicyRule[]) => T[];
|
package/dist/seed.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PolicyRule } from './types.js';
|
|
2
|
+
/** A rule as a store hands it back: the kernel's shape, plus whether it is still in force. */
|
|
3
|
+
export type ExistingPolicyRule = PolicyRule & {
|
|
4
|
+
status?: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Which seed rules to author, given what is already there. Idempotent by ID and by COVERAGE.
|
|
8
|
+
*
|
|
9
|
+
* The coverage half is the one that was learned the hard way: a first seeding run produced two
|
|
10
|
+
* active rules for one constraint with identical scope and value — the seed's, and one an admin had
|
|
11
|
+
* authored by hand two days earlier. Same effect, two keys, and a conflict the lattice reports and
|
|
12
|
+
* a human has to unpick.
|
|
13
|
+
*
|
|
14
|
+
* A seed provides DEFAULTS. If the business has already said something about a constraint at the
|
|
15
|
+
* same scope, it has spoken, and the seed's opinion is not wanted. A RETIRED rule is not a current
|
|
16
|
+
* opinion, so it does not block.
|
|
17
|
+
*/
|
|
18
|
+
export declare const rulesToSeed: <T extends PolicyRule>(seed: readonly T[], existing: readonly ExistingPolicyRule[]) => T[];
|
package/dist/seed.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/seed.ts
|
|
2
|
+
var sameScope = (left, right) => {
|
|
3
|
+
const a = left ?? {};
|
|
4
|
+
const b = right ?? {};
|
|
5
|
+
return [...new Set([...Object.keys(a), ...Object.keys(b)])].every((key) => a[key] === b[key]);
|
|
6
|
+
};
|
|
7
|
+
var rulesToSeed = (seed, existing) => {
|
|
8
|
+
const active = existing.filter((rule) => (rule.status ?? "active") === "active");
|
|
9
|
+
return seed.filter((rule) => !active.some((row) => row.id === rule.id || row.constraint.kind === rule.constraint.kind && sameScope(row.scope, rule.scope)));
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
rulesToSeed
|
|
13
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
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/threshold.ts
|
|
40
|
+
var exports_threshold = {};
|
|
41
|
+
__export(exports_threshold, {
|
|
42
|
+
declareThreshold: () => declareThreshold,
|
|
43
|
+
describeThreshold: () => describeThreshold,
|
|
44
|
+
expiredThresholds: () => expiredThresholds,
|
|
45
|
+
isThreshold: () => isThreshold
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(exports_threshold);
|
|
48
|
+
var DECLARED = Symbol.for("com.microcompanies.geonosis.threshold");
|
|
49
|
+
var ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
50
|
+
var refuse = (what) => {
|
|
51
|
+
throw new Error(`declareThreshold: ${what}. Next: give it one, or leave it as a plain constant and say why it needs no authority.`);
|
|
52
|
+
};
|
|
53
|
+
var filled = (value) => typeof value === "string" && value.trim() !== "";
|
|
54
|
+
var declareThreshold = (input) => {
|
|
55
|
+
if (!filled(input.authority))
|
|
56
|
+
refuse("a threshold whose authority is nobody is a magic number");
|
|
57
|
+
if (!filled(input.reason))
|
|
58
|
+
refuse("a threshold with no reason cannot teach anyone what it is for");
|
|
59
|
+
if (input.value === undefined)
|
|
60
|
+
refuse("a threshold with no value decides nothing");
|
|
61
|
+
if (input.revisit !== undefined && !ISO_DATE.test(input.revisit)) {
|
|
62
|
+
refuse(`revisit "${input.revisit}" is not an ISO date — a date nobody can compare never expires`);
|
|
63
|
+
}
|
|
64
|
+
return Object.freeze({ ...input, [DECLARED]: true });
|
|
65
|
+
};
|
|
66
|
+
var isThreshold = (value) => typeof value === "object" && value !== null && (DECLARED in value);
|
|
67
|
+
var expiredThresholds = (thresholds, now) => thresholds.filter((one) => one.revisit !== undefined && new Date(one.revisit) <= now);
|
|
68
|
+
var describeThreshold = (threshold) => [
|
|
69
|
+
`${String(threshold.value)}`,
|
|
70
|
+
threshold.subject === undefined ? "" : ` for ${threshold.subject}`,
|
|
71
|
+
` — ${threshold.authority}: ${threshold.reason}`,
|
|
72
|
+
threshold.revisit === undefined ? "" : ` (revisit ${threshold.revisit})`
|
|
73
|
+
].join("");
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The compile-time tier: a threshold that no tenant sets and no config row overrides, declared where
|
|
3
|
+
* it is used.
|
|
4
|
+
*
|
|
5
|
+
* The runtime lattice answers "what does this tenant, in this region, for this order, get?" — and it
|
|
6
|
+
* needs a store, a boot and an await to answer it. Most thresholds are not that question.
|
|
7
|
+
* `LIVENESS_TIMEOUT_MS = 3000` decides behaviour in code and will never be tenant-settable; what it
|
|
8
|
+
* is missing is not a lattice, it is an authority. That is all this tier adds, and the reason it
|
|
9
|
+
* adds nothing else: a threshold whose declaration cost an async call and a port would go back to
|
|
10
|
+
* being a number.
|
|
11
|
+
*/
|
|
12
|
+
export interface ThresholdInput<Value> {
|
|
13
|
+
/** Who or what set it: a person and a date, a contract, a standard, an incident. */
|
|
14
|
+
readonly authority: string;
|
|
15
|
+
/** Why this number and not another. The sentence the person who was just blocked needs. */
|
|
16
|
+
readonly reason: string;
|
|
17
|
+
/** ISO date. When somebody should look at it again; absent means the authority is permanent. */
|
|
18
|
+
readonly revisit?: string;
|
|
19
|
+
/** What it reaches. Absent where the name says it. */
|
|
20
|
+
readonly subject?: string;
|
|
21
|
+
readonly value: Value;
|
|
22
|
+
}
|
|
23
|
+
declare const DECLARED: unique symbol;
|
|
24
|
+
export type Threshold<Value> = {
|
|
25
|
+
readonly [DECLARED]: true;
|
|
26
|
+
} & Readonly<ThresholdInput<Value>>;
|
|
27
|
+
/**
|
|
28
|
+
* Declared, frozen, and refused where it is incomplete.
|
|
29
|
+
*
|
|
30
|
+
* The refusal is at the declaration and not at the first read, because a declaration runs when the
|
|
31
|
+
* module loads: a threshold missing its authority breaks the boot of whatever imports it, which is
|
|
32
|
+
* the earliest anyone can be told and the last moment it is cheap.
|
|
33
|
+
*/
|
|
34
|
+
export declare const declareThreshold: <Value>(input: ThresholdInput<Value>) => Threshold<Value>;
|
|
35
|
+
export declare const isThreshold: (value: unknown) => value is Threshold<unknown>;
|
|
36
|
+
/**
|
|
37
|
+
* The thresholds whose revisit date has passed.
|
|
38
|
+
*
|
|
39
|
+
* Reported, never thrown. A declaration that started failing the build on a date would have its
|
|
40
|
+
* date deleted by the first person it stopped, and the tier would be back to numbers with prose
|
|
41
|
+
* beside them.
|
|
42
|
+
*/
|
|
43
|
+
export declare const expiredThresholds: (thresholds: readonly Threshold<unknown>[], now: Date) => Threshold<unknown>[];
|
|
44
|
+
export declare const describeThreshold: (threshold: Threshold<unknown>) => string;
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The compile-time tier: a threshold that no tenant sets and no config row overrides, declared where
|
|
3
|
+
* it is used.
|
|
4
|
+
*
|
|
5
|
+
* The runtime lattice answers "what does this tenant, in this region, for this order, get?" — and it
|
|
6
|
+
* needs a store, a boot and an await to answer it. Most thresholds are not that question.
|
|
7
|
+
* `LIVENESS_TIMEOUT_MS = 3000` decides behaviour in code and will never be tenant-settable; what it
|
|
8
|
+
* is missing is not a lattice, it is an authority. That is all this tier adds, and the reason it
|
|
9
|
+
* adds nothing else: a threshold whose declaration cost an async call and a port would go back to
|
|
10
|
+
* being a number.
|
|
11
|
+
*/
|
|
12
|
+
export interface ThresholdInput<Value> {
|
|
13
|
+
/** Who or what set it: a person and a date, a contract, a standard, an incident. */
|
|
14
|
+
readonly authority: string;
|
|
15
|
+
/** Why this number and not another. The sentence the person who was just blocked needs. */
|
|
16
|
+
readonly reason: string;
|
|
17
|
+
/** ISO date. When somebody should look at it again; absent means the authority is permanent. */
|
|
18
|
+
readonly revisit?: string;
|
|
19
|
+
/** What it reaches. Absent where the name says it. */
|
|
20
|
+
readonly subject?: string;
|
|
21
|
+
readonly value: Value;
|
|
22
|
+
}
|
|
23
|
+
declare const DECLARED: unique symbol;
|
|
24
|
+
export type Threshold<Value> = {
|
|
25
|
+
readonly [DECLARED]: true;
|
|
26
|
+
} & Readonly<ThresholdInput<Value>>;
|
|
27
|
+
/**
|
|
28
|
+
* Declared, frozen, and refused where it is incomplete.
|
|
29
|
+
*
|
|
30
|
+
* The refusal is at the declaration and not at the first read, because a declaration runs when the
|
|
31
|
+
* module loads: a threshold missing its authority breaks the boot of whatever imports it, which is
|
|
32
|
+
* the earliest anyone can be told and the last moment it is cheap.
|
|
33
|
+
*/
|
|
34
|
+
export declare const declareThreshold: <Value>(input: ThresholdInput<Value>) => Threshold<Value>;
|
|
35
|
+
export declare const isThreshold: (value: unknown) => value is Threshold<unknown>;
|
|
36
|
+
/**
|
|
37
|
+
* The thresholds whose revisit date has passed.
|
|
38
|
+
*
|
|
39
|
+
* Reported, never thrown. A declaration that started failing the build on a date would have its
|
|
40
|
+
* date deleted by the first person it stopped, and the tier would be back to numbers with prose
|
|
41
|
+
* beside them.
|
|
42
|
+
*/
|
|
43
|
+
export declare const expiredThresholds: (thresholds: readonly Threshold<unknown>[], now: Date) => Threshold<unknown>[];
|
|
44
|
+
export declare const describeThreshold: (threshold: Threshold<unknown>) => string;
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/threshold.ts
|
|
2
|
+
var DECLARED = Symbol.for("com.microcompanies.geonosis.threshold");
|
|
3
|
+
var ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
4
|
+
var refuse = (what) => {
|
|
5
|
+
throw new Error(`declareThreshold: ${what}. Next: give it one, or leave it as a plain constant and say why it needs no authority.`);
|
|
6
|
+
};
|
|
7
|
+
var filled = (value) => typeof value === "string" && value.trim() !== "";
|
|
8
|
+
var declareThreshold = (input) => {
|
|
9
|
+
if (!filled(input.authority))
|
|
10
|
+
refuse("a threshold whose authority is nobody is a magic number");
|
|
11
|
+
if (!filled(input.reason))
|
|
12
|
+
refuse("a threshold with no reason cannot teach anyone what it is for");
|
|
13
|
+
if (input.value === undefined)
|
|
14
|
+
refuse("a threshold with no value decides nothing");
|
|
15
|
+
if (input.revisit !== undefined && !ISO_DATE.test(input.revisit)) {
|
|
16
|
+
refuse(`revisit "${input.revisit}" is not an ISO date — a date nobody can compare never expires`);
|
|
17
|
+
}
|
|
18
|
+
return Object.freeze({ ...input, [DECLARED]: true });
|
|
19
|
+
};
|
|
20
|
+
var isThreshold = (value) => typeof value === "object" && value !== null && (DECLARED in value);
|
|
21
|
+
var expiredThresholds = (thresholds, now) => thresholds.filter((one) => one.revisit !== undefined && new Date(one.revisit) <= now);
|
|
22
|
+
var describeThreshold = (threshold) => [
|
|
23
|
+
`${String(threshold.value)}`,
|
|
24
|
+
threshold.subject === undefined ? "" : ` for ${threshold.subject}`,
|
|
25
|
+
` — ${threshold.authority}: ${threshold.reason}`,
|
|
26
|
+
threshold.revisit === undefined ? "" : ` (revisit ${threshold.revisit})`
|
|
27
|
+
].join("");
|
|
28
|
+
export {
|
|
29
|
+
declareThreshold,
|
|
30
|
+
describeThreshold,
|
|
31
|
+
expiredThresholds,
|
|
32
|
+
isThreshold
|
|
33
|
+
};
|
package/dist/types.cjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
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/types.ts
|
|
40
|
+
var exports_types = {};
|
|
41
|
+
__export(exports_types, {
|
|
42
|
+
POLICY_FALLBACK: () => POLICY_FALLBACK,
|
|
43
|
+
POLICY_INVALID_VALUE: () => POLICY_INVALID_VALUE
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(exports_types);
|
|
46
|
+
var POLICY_FALLBACK = "policy.fallback";
|
|
47
|
+
var POLICY_INVALID_VALUE = "policy.invalid_value";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Something a port may answer with, or answer with later. */
|
|
2
|
-
type Awaitable<T> = Promise<T> | T;
|
|
2
|
+
export type Awaitable<T> = Promise<T> | T;
|
|
3
3
|
/**
|
|
4
4
|
* A schema, recognised by what it does rather than by what it is an instance of.
|
|
5
5
|
*
|
|
@@ -9,16 +9,16 @@ type Awaitable<T> = Promise<T> | T;
|
|
|
9
9
|
* object, so a schema from another copy is not an instance of ours: such a guard passes every test
|
|
10
10
|
* written against the kit's own zod and refuses every schema a consumer hands it.
|
|
11
11
|
*/
|
|
12
|
-
type SchemaLike = {
|
|
12
|
+
export type SchemaLike = {
|
|
13
13
|
safeParse: (value: unknown) => {
|
|
14
14
|
success: boolean;
|
|
15
15
|
};
|
|
16
16
|
};
|
|
17
17
|
/** What a rule is aimed at, and what a decision is being made about. */
|
|
18
|
-
type PolicyScope = Record<string, unknown>;
|
|
19
|
-
type PolicyQuery = PolicyScope;
|
|
18
|
+
export type PolicyScope = Record<string, unknown>;
|
|
19
|
+
export type PolicyQuery = PolicyScope;
|
|
20
20
|
/** One authored constraint: a value, plus who said it, who it reaches and why it exists. */
|
|
21
|
-
type PolicyRule = {
|
|
21
|
+
export type PolicyRule = {
|
|
22
22
|
constraint: {
|
|
23
23
|
kind: string;
|
|
24
24
|
value: unknown;
|
|
@@ -33,7 +33,7 @@ type PolicyRule = {
|
|
|
33
33
|
tier: string;
|
|
34
34
|
};
|
|
35
35
|
/** One authority, in a lattice declared strongest first. */
|
|
36
|
-
type PolicyTierSpec = {
|
|
36
|
+
export type PolicyTierSpec = {
|
|
37
37
|
/** Fills silence only. A promoted learning enters here and can never outrank an authority. */
|
|
38
38
|
advisory?: boolean;
|
|
39
39
|
name: string;
|
|
@@ -43,7 +43,7 @@ type PolicyTierSpec = {
|
|
|
43
43
|
overrides?: boolean;
|
|
44
44
|
};
|
|
45
45
|
/** One thing policy may constrain, and what is true of its values. */
|
|
46
|
-
type PolicyKindSpec = {
|
|
46
|
+
export type PolicyKindSpec = {
|
|
47
47
|
/**
|
|
48
48
|
* Read the value this one POINTS at. Called only when `isReference` says so; answering
|
|
49
49
|
* `undefined` means the thing it points at is unset, which is a gap and not a number — the read
|
|
@@ -68,7 +68,7 @@ type PolicyKindSpec = {
|
|
|
68
68
|
schema?: SchemaLike;
|
|
69
69
|
};
|
|
70
70
|
/** What policy decided, and the whole trace of why. */
|
|
71
|
-
type PolicyDecision<V = unknown> = {
|
|
71
|
+
export type PolicyDecision<V = unknown> = {
|
|
72
72
|
/** Same authority, same specificity, DIFFERENT value. Reported, never guessed. */
|
|
73
73
|
conflicts: PolicyRule[];
|
|
74
74
|
explain: string;
|
|
@@ -81,25 +81,25 @@ type PolicyDecision<V = unknown> = {
|
|
|
81
81
|
winner: PolicyRule;
|
|
82
82
|
};
|
|
83
83
|
/** A rule this build cannot honour, and the reason nobody has to guess at. */
|
|
84
|
-
type UnusableRule = {
|
|
84
|
+
export type UnusableRule = {
|
|
85
85
|
id: string;
|
|
86
86
|
reason: string;
|
|
87
87
|
};
|
|
88
88
|
/** Where the authored rules come from. */
|
|
89
|
-
type PolicyRuleReader = {
|
|
89
|
+
export type PolicyRuleReader = {
|
|
90
90
|
rules: (kind: string, query: PolicyQuery) => Awaitable<readonly PolicyRule[]>;
|
|
91
91
|
};
|
|
92
92
|
/** The layer an admin can edit. `undefined` means no value is set. */
|
|
93
|
-
type PolicyConfigReader = {
|
|
93
|
+
export type PolicyConfigReader = {
|
|
94
94
|
get: (kind: string, query: PolicyQuery) => Awaitable<unknown>;
|
|
95
95
|
};
|
|
96
96
|
/** Where a fallback is recorded. Best-effort by contract: it may never change a decision. */
|
|
97
|
-
type PolicyJournal = {
|
|
97
|
+
export type PolicyJournal = {
|
|
98
98
|
write: (entry: PolicyJournalEntry) => Awaitable<void>;
|
|
99
99
|
};
|
|
100
|
-
declare const POLICY_FALLBACK = "policy.fallback";
|
|
101
|
-
declare const POLICY_INVALID_VALUE = "policy.invalid_value";
|
|
102
|
-
type PolicyJournalEntry = {
|
|
100
|
+
export declare const POLICY_FALLBACK = "policy.fallback";
|
|
101
|
+
export declare const POLICY_INVALID_VALUE = "policy.invalid_value";
|
|
102
|
+
export type PolicyJournalEntry = {
|
|
103
103
|
event: typeof POLICY_FALLBACK | typeof POLICY_INVALID_VALUE;
|
|
104
104
|
/** The value that decided instead. */
|
|
105
105
|
fellBackTo: unknown;
|
|
@@ -113,7 +113,7 @@ type PolicyJournalEntry = {
|
|
|
113
113
|
subject?: string;
|
|
114
114
|
};
|
|
115
115
|
/** The data half of a policy — everything the lattice needs and nothing it does not. */
|
|
116
|
-
type PolicyData = {
|
|
116
|
+
export type PolicyData = {
|
|
117
117
|
kinds: readonly PolicyKindSpec[];
|
|
118
118
|
overridableByDefault: boolean;
|
|
119
119
|
rules: readonly PolicyRule[];
|
|
@@ -122,7 +122,7 @@ type PolicyData = {
|
|
|
122
122
|
/** Strongest first. */
|
|
123
123
|
tiers: readonly PolicyTierSpec[];
|
|
124
124
|
};
|
|
125
|
-
type Policy = PolicyData & {
|
|
125
|
+
export type Policy = PolicyData & {
|
|
126
126
|
/** The fallbacks and unusable values this process has already recorded, keyed per boot. */
|
|
127
127
|
journalledOnce: Set<string>;
|
|
128
128
|
kindOf: (kind: string) => PolicyKindSpec | undefined;
|
|
@@ -143,7 +143,7 @@ type Policy = PolicyData & {
|
|
|
143
143
|
usableRules: (rules?: readonly PolicyRule[]) => PolicyRule[];
|
|
144
144
|
};
|
|
145
145
|
/** One recorded thing that happened, as the learning loop reads it. */
|
|
146
|
-
type JournalObservationEntry = {
|
|
146
|
+
export type JournalObservationEntry = {
|
|
147
147
|
actor: {
|
|
148
148
|
id?: string;
|
|
149
149
|
kind: 'agent' | 'system' | 'user';
|
|
@@ -164,7 +164,7 @@ type JournalObservationEntry = {
|
|
|
164
164
|
subject?: string;
|
|
165
165
|
};
|
|
166
166
|
/** What a read answered with, and which layer answered. */
|
|
167
|
-
type PolicyReading<V> = {
|
|
167
|
+
export type PolicyReading<V> = {
|
|
168
168
|
/** Absent when no rule spoke and a lower layer decided. */
|
|
169
169
|
decision?: PolicyDecision<V>;
|
|
170
170
|
source: 'config' | 'fallback' | 'rule';
|
|
@@ -179,7 +179,7 @@ type PolicyReading<V> = {
|
|
|
179
179
|
* because the surrogate id is the store's and the KEY is the rule's: it is what a refusal quotes,
|
|
180
180
|
* and what lets evidence accumulate against one rule across environments.
|
|
181
181
|
*/
|
|
182
|
-
type PolicyRuleRow = {
|
|
182
|
+
export type PolicyRuleRow = {
|
|
183
183
|
constraint_kind: string;
|
|
184
184
|
constraint_value: unknown;
|
|
185
185
|
overridable?: boolean | null;
|
|
@@ -191,12 +191,10 @@ type PolicyRuleRow = {
|
|
|
191
191
|
tier: string;
|
|
192
192
|
};
|
|
193
193
|
/** What a store offers the kernel. Every port is optional except the rules themselves. */
|
|
194
|
-
type PolicyAdapter = {
|
|
194
|
+
export type PolicyAdapter = {
|
|
195
195
|
config?: PolicyConfigReader;
|
|
196
196
|
journal?: PolicyJournal;
|
|
197
197
|
/** For a diagnostic — a report naming "the adapter" helps nobody. */
|
|
198
198
|
name: string;
|
|
199
199
|
rule: PolicyRuleReader;
|
|
200
200
|
};
|
|
201
|
-
|
|
202
|
-
export { type Awaitable as A, type JournalObservationEntry as J, type PolicyQuery as P, type SchemaLike as S, type UnusableRule as U, type PolicyRuleRow as a, type PolicyAdapter as b, type PolicyKindSpec as c, type PolicyRule as d, type PolicyTierSpec as e, type Policy as f, type PolicyData as g, type PolicyDecision as h, type PolicyScope as i, type PolicyJournal as j, type PolicyConfigReader as k, type PolicyRuleReader as l, type PolicyReading as m, POLICY_FALLBACK as n, POLICY_INVALID_VALUE as o, type PolicyJournalEntry as p };
|