@htilabs/platform-common-models 0.23.0 → 0.25.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/README.md +104 -0
- package/dist/authz/index.cjs +143 -12
- package/dist/authz/index.cjs.map +1 -1
- package/dist/authz/index.d.cts +121 -12
- package/dist/authz/index.d.ts +121 -12
- package/dist/authz/index.js +136 -12
- package/dist/authz/index.js.map +1 -1
- package/dist/constants/index.cjs +30 -0
- package/dist/constants/index.cjs.map +1 -1
- package/dist/constants/index.d.cts +1 -1
- package/dist/constants/index.d.ts +1 -1
- package/dist/constants/index.js +26 -0
- package/dist/constants/index.js.map +1 -1
- package/dist/{cvcEntry-0BIB9sen.d.cts → cvcEntry-Asg7mxjB.d.cts} +12 -9
- package/dist/{cvcEntry-0BIB9sen.d.ts → cvcEntry-Asg7mxjB.d.ts} +12 -9
- package/dist/{index-CDoqrfc1.d.ts → index-ClTjjJ_S.d.ts} +1 -1
- package/dist/{index-Zu_mOJRn.d.cts → index-Pt82NxQg.d.cts} +1 -1
- package/dist/index.cjs +604 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -5
- package/dist/index.d.ts +7 -5
- package/dist/index.js +577 -15
- package/dist/index.js.map +1 -1
- package/dist/information-scope/index.d.cts +3 -350
- package/dist/information-scope/index.d.ts +3 -350
- package/dist/informationScopeResolver-BzYu97DE.d.cts +351 -0
- package/dist/informationScopeResolver-BzYu97DE.d.ts +351 -0
- package/dist/models/index.cjs +26 -1
- package/dist/models/index.cjs.map +1 -1
- package/dist/models/index.d.cts +2 -2
- package/dist/models/index.d.ts +2 -2
- package/dist/models/index.js +26 -1
- package/dist/models/index.js.map +1 -1
- package/dist/{platform-auth-B49Ly57B.d.cts → platform-auth-DJO-r9Pf.d.cts} +48 -1
- package/dist/{platform-auth-B49Ly57B.d.ts → platform-auth-DJO-r9Pf.d.ts} +48 -1
- package/dist/schemas/index.cjs +26 -1
- package/dist/schemas/index.cjs.map +1 -1
- package/dist/schemas/index.d.cts +25 -25
- package/dist/schemas/index.d.ts +25 -25
- package/dist/schemas/index.js +26 -1
- package/dist/schemas/index.js.map +1 -1
- package/dist/services/index.cjs +26 -1
- package/dist/services/index.cjs.map +1 -1
- package/dist/services/index.d.cts +2 -1
- package/dist/services/index.d.ts +2 -1
- package/dist/services/index.js +26 -1
- package/dist/services/index.js.map +1 -1
- package/dist/sharing-ceiling/index.cjs +497 -0
- package/dist/sharing-ceiling/index.cjs.map +1 -0
- package/dist/sharing-ceiling/index.d.cts +318 -0
- package/dist/sharing-ceiling/index.d.ts +318 -0
- package/dist/sharing-ceiling/index.js +451 -0
- package/dist/sharing-ceiling/index.js.map +1 -0
- package/dist/types/index.d.cts +11 -3
- package/dist/types/index.d.ts +11 -3
- package/dist/validation/index.cjs +22 -0
- package/dist/validation/index.cjs.map +1 -1
- package/dist/validation/index.js +22 -0
- package/dist/validation/index.js.map +1 -1
- package/package.json +7 -1
- package/src/__sot__/README.md +2 -1
- package/src/__sot__/coreFunctionFields.ts +88 -4
- package/src/__sot__/coreFunctions.pin.json +1040 -113
- package/src/__sot__/sharingStarter.pin.json +229 -0
- package/src/__tests__/authz-resolve.test.ts +115 -31
- package/src/__tests__/core-function-default-bounds.test.ts +374 -0
- package/src/__tests__/core-functions-case-close.test.ts +82 -0
- package/src/__tests__/sharing-starter-canon.test.ts +178 -0
- package/src/authz/defaultBounds.ts +69 -0
- package/src/authz/index.ts +12 -3
- package/src/authz/resolveEffectiveFunctions.ts +69 -29
- package/src/constants/platform-auth.ts +60 -0
- package/src/generated/coreFunctionDefaultBounds.ts +109 -0
- package/src/generated/sharingPlatformStarter.ts +165 -0
- package/src/index.ts +5 -0
- package/src/schemas/platform-role.ts +4 -1
- package/src/sharingCeiling/__tests__/sharingCeiling.test.ts +678 -0
- package/src/sharingCeiling/chain.ts +269 -0
- package/src/sharingCeiling/coverage.ts +346 -0
- package/src/sharingCeiling/index.ts +14 -0
- package/src/sharingCeiling/starter.ts +89 -0
- package/src/types/platform-auth.ts +9 -2
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Inter-Agency Sharing ceiling read chain (co-2sx): the account's stored
|
|
3
|
+
* ceiling CELLS folded into canon's `sharingScopeEdits`, and the ONE chain
|
|
4
|
+
* that turns them, the platform starter and the purpose switches into the
|
|
5
|
+
* list a share for one recipient × purpose may carry.
|
|
6
|
+
*
|
|
7
|
+
* Pure, total on stored data, no I/O. Throws only on a caller's programming
|
|
8
|
+
* error (a missing / reserved row key, a missing purpose).
|
|
9
|
+
*
|
|
10
|
+
* Canon — prototype `HTI-LEADS-PAVE-Thread-prototype.html` @ d85f05b7:
|
|
11
|
+
* - `shareEffectiveCeiling` L327966-L328090; the per-purpose rung
|
|
12
|
+
* L328040-L328082, the chain itself L328053-L328058:
|
|
13
|
+
*
|
|
14
|
+
* const rowKey = r.__external ? "__external__" : r.id;
|
|
15
|
+
* const per = edits[rowKey] && edits[rowKey][opts.purposeCode];
|
|
16
|
+
* const seeded = (starter.counterpartyOverrides[rowKey] || {})[opts.purposeCode];
|
|
17
|
+
* const def = edits.__default__ && edits.__default__[opts.purposeCode];
|
|
18
|
+
* const platformDefault = starter.scopeByPurpose[opts.purposeCode];
|
|
19
|
+
* const allowed = per || seeded || def || platformDefault || null;
|
|
20
|
+
*
|
|
21
|
+
* - the purpose switch L327999-L328005 (an explicit `false` only).
|
|
22
|
+
* Platform prototype `src/data.js` @ a6f1f3a — the contract of record
|
|
23
|
+
* (GDR § 5.79), `HTI_SHARE_POLICY_API.effectiveCeiling` L51324ff:
|
|
24
|
+
* - clamp 1 L51352-L51354: a purpose is off when the account switched it
|
|
25
|
+
* off, OR when its platform row is `enabled: false` and the account has
|
|
26
|
+
* not switched it on (`records_request`, L32527). This module follows the
|
|
27
|
+
* contract of record; the monolith reads only the explicit `false`.
|
|
28
|
+
* - clamp 5 L51516-L51525: the same chain as the monolith.
|
|
29
|
+
* - `EXTERNAL_RECIPIENT_KEY = "__external__"` L50434: an off-platform
|
|
30
|
+
* recipient resolves through the same chain under that key, so an
|
|
31
|
+
* un-authored off-platform purpose follows the account default, then the
|
|
32
|
+
* platform default (L50427-L50430).
|
|
33
|
+
*
|
|
34
|
+
* One JavaScript detail of canon's `||` chain is kept: an EMPTY authored
|
|
35
|
+
* list is truthy, so it BINDS (it lets nothing but System metadata leave).
|
|
36
|
+
*
|
|
37
|
+
* STRICTER THAN CANON (ruling, co-2sx review): a purpose with no rung at all
|
|
38
|
+
* fails CLOSED — `allowed: []`, rung `none` — where canon's `|| null` reads
|
|
39
|
+
* no ceiling (`if (allowed && …)`, L328059). Every one of the eight core
|
|
40
|
+
* CVC-84 purposes has a platform row, so `none` can only come from a caller
|
|
41
|
+
* error (a misspelled purpose, a LEADS-wing purpose on a core share, a
|
|
42
|
+
* replaced starter missing a row); an unknown purpose must never mean
|
|
43
|
+
* "anything may leave".
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
import {
|
|
47
|
+
HTI_SHARE_PLATFORM_PURPOSE_ENABLED,
|
|
48
|
+
HTI_SHARE_PLATFORM_STARTER,
|
|
49
|
+
SHARING_CEILING_SCOPE_VERSION,
|
|
50
|
+
type SharingPlatformStarter,
|
|
51
|
+
} from "./starter.js";
|
|
52
|
+
|
|
53
|
+
/** The account default row key — canon `sharingScopeEdits.__default__` (L328056; data.js L51519). */
|
|
54
|
+
export const SHARING_CEILING_DEFAULT_ROW_KEY = "__default__" as const;
|
|
55
|
+
|
|
56
|
+
/** The off-platform / unspecified recipients row key (canon L328053; data.js `EXTERNAL_RECIPIENT_KEY`, L50434). */
|
|
57
|
+
export const SHARING_CEILING_EXTERNAL_ROW_KEY = "__external__" as const;
|
|
58
|
+
|
|
59
|
+
/** rowKey (partner id · `__default__` · `__external__`) → purpose → v4 row codes. */
|
|
60
|
+
export type SharingScopeEdits = Readonly<
|
|
61
|
+
Record<string, Readonly<Record<string, readonly string[]>>>
|
|
62
|
+
>;
|
|
63
|
+
|
|
64
|
+
/** The fields of one stored ceiling cell this read looks at (cells keyed agencyId × rowKey × purpose). */
|
|
65
|
+
export interface SharingCeilingCellLike {
|
|
66
|
+
readonly rowKey?: unknown;
|
|
67
|
+
readonly purpose?: unknown;
|
|
68
|
+
readonly scopeVersion?: unknown;
|
|
69
|
+
readonly informationScope?: unknown;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const own = <V>(record: unknown, key: string): V | undefined =>
|
|
73
|
+
record !== null &&
|
|
74
|
+
typeof record === "object" &&
|
|
75
|
+
Object.prototype.hasOwnProperty.call(record, key)
|
|
76
|
+
? ((record as Record<string, V>)[key] as V)
|
|
77
|
+
: undefined;
|
|
78
|
+
|
|
79
|
+
const isStringList = (v: unknown): v is readonly string[] =>
|
|
80
|
+
Array.isArray(v) && v.every((c) => typeof c === "string");
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The list one PRESENT cell carries. A cell is a rule by being there: stamped
|
|
84
|
+
* 4 with an array of strings → that list (a copy); anything else (unstamped,
|
|
85
|
+
* a v3 stamp, a missing list, a non-array, a non-string entry) → `[]`, which
|
|
86
|
+
* binds and covers nothing. A malformed rule never widens a share by falling
|
|
87
|
+
* through to a broader rung.
|
|
88
|
+
*/
|
|
89
|
+
export function sharingCeilingCellList(cell: SharingCeilingCellLike): string[] {
|
|
90
|
+
if (cell.scopeVersion === SHARING_CEILING_SCOPE_VERSION && isStringList(cell.informationScope)) {
|
|
91
|
+
return [...cell.informationScope];
|
|
92
|
+
}
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const sameSet = (a: readonly string[], b: readonly string[]): boolean => {
|
|
97
|
+
const sa = new Set(a);
|
|
98
|
+
const sb = new Set(b);
|
|
99
|
+
return sa.size === sb.size && [...sa].every((c) => sb.has(c));
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Fold ONE sending account's ceiling cells into canon's `sharingScopeEdits`.
|
|
104
|
+
*
|
|
105
|
+
* - A cell is present or absent; there is no status. An ABSENT cell is no
|
|
106
|
+
* rule, and the chain falls through.
|
|
107
|
+
* - A present cell binds with {@link sharingCeilingCellList}: `[]` binds; an
|
|
108
|
+
* unstamped, v3-stamped or malformed list reads as `[]`.
|
|
109
|
+
* - A cell without a string `rowKey` and `purpose` cannot be attributed to a
|
|
110
|
+
* cell and is skipped (it is not a rule for any cell).
|
|
111
|
+
* - Two cells for the same (rowKey, purpose) — the store's key forbids it —
|
|
112
|
+
* keep the list when they agree as sets, else read `[]` (fail closed).
|
|
113
|
+
*
|
|
114
|
+
* The caller passes one account's cells; the fold does not filter by agency.
|
|
115
|
+
*/
|
|
116
|
+
export function sharingScopeEditsFromCells(
|
|
117
|
+
cells: Iterable<SharingCeilingCellLike | null | undefined> | null | undefined,
|
|
118
|
+
): SharingScopeEdits {
|
|
119
|
+
const byRow = new Map<string, Map<string, string[]>>();
|
|
120
|
+
if (cells === null || cells === undefined || typeof (cells as Iterable<unknown>)[Symbol.iterator] !== "function") {
|
|
121
|
+
return {};
|
|
122
|
+
}
|
|
123
|
+
for (const cell of cells) {
|
|
124
|
+
if (cell === null || typeof cell !== "object") continue;
|
|
125
|
+
const { rowKey, purpose } = cell;
|
|
126
|
+
if (typeof rowKey !== "string" || rowKey === "" || typeof purpose !== "string" || purpose === "") continue;
|
|
127
|
+
const list = sharingCeilingCellList(cell);
|
|
128
|
+
const row = byRow.get(rowKey) ?? new Map<string, string[]>();
|
|
129
|
+
const prior = row.get(purpose);
|
|
130
|
+
row.set(purpose, prior === undefined || sameSet(prior, list) ? list : []);
|
|
131
|
+
byRow.set(rowKey, row);
|
|
132
|
+
}
|
|
133
|
+
// Object.fromEntries defines OWN properties, so a row key such as
|
|
134
|
+
// `__proto__` is a key, never the prototype.
|
|
135
|
+
return Object.fromEntries([...byRow].map(([k, row]) => [k, Object.fromEntries(row)]));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Which rung of the chain answered. */
|
|
139
|
+
export type SharingCeilingRung =
|
|
140
|
+
| "purpose_disabled"
|
|
141
|
+
| "partner_rule"
|
|
142
|
+
| "seeded_partner_rule"
|
|
143
|
+
| "account_default"
|
|
144
|
+
| "platform_default"
|
|
145
|
+
| "none";
|
|
146
|
+
|
|
147
|
+
export interface EffectiveSharingCeiling {
|
|
148
|
+
/**
|
|
149
|
+
* The v4 row codes a share may carry (read with `scopeRowWithinCeiling`).
|
|
150
|
+
* `[]` when the purpose is disabled, and when no rung exists for the
|
|
151
|
+
* purpose (rung `none` — fails closed; see the module header).
|
|
152
|
+
*/
|
|
153
|
+
readonly allowed: string[];
|
|
154
|
+
readonly rung: SharingCeilingRung;
|
|
155
|
+
/** The purpose is switched off for the account (`allowed` is then `[]`). */
|
|
156
|
+
readonly disabled: boolean;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface EffectiveSharingCeilingInputs {
|
|
160
|
+
/** The sending account's authored lists ({@link sharingScopeEditsFromCells}). */
|
|
161
|
+
readonly edits: SharingScopeEdits | null | undefined;
|
|
162
|
+
/** Defaults to {@link HTI_SHARE_PLATFORM_STARTER}. */
|
|
163
|
+
readonly starter?: SharingPlatformStarter;
|
|
164
|
+
/** The account's Share Purposes switches: `false` disables, `true` re-enables a platform-disabled purpose. */
|
|
165
|
+
readonly purposeOverrides?: Readonly<Record<string, boolean | undefined>> | null;
|
|
166
|
+
/**
|
|
167
|
+
* Platform purpose `enabled` flags, merged OVER
|
|
168
|
+
* {@link HTI_SHARE_PLATFORM_PURPOSE_ENABLED} (a purpose the map does not
|
|
169
|
+
* name keeps the platform row). `null` / `undefined` = the platform rows.
|
|
170
|
+
*/
|
|
171
|
+
readonly purposeEnabled?: Readonly<Record<string, boolean | undefined>> | null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* One rung's list as the chain reads it: absent (`undefined` / `null`) falls
|
|
176
|
+
* through, a list of strings binds (copied), anything else binds as `[]`.
|
|
177
|
+
*/
|
|
178
|
+
function rungList(value: unknown): string[] | undefined {
|
|
179
|
+
if (value === undefined || value === null) return undefined;
|
|
180
|
+
return isStringList(value) ? [...value] : [];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Is `purpose` switched off? The contract of record (data.js clamp 1,
|
|
185
|
+
* L51352-L51354): the account's explicit `false`, or a platform row
|
|
186
|
+
* `enabled: false` the account has not overridden `true`. `purposeEnabled`
|
|
187
|
+
* is merged over the platform rows (a purpose it does not name, or names
|
|
188
|
+
* with a non-boolean, keeps the platform row); `null` = the platform rows.
|
|
189
|
+
*/
|
|
190
|
+
export function isSharingPurposeDisabled(
|
|
191
|
+
purpose: string,
|
|
192
|
+
purposeOverrides?: Readonly<Record<string, boolean | undefined>> | null,
|
|
193
|
+
purposeEnabled?: Readonly<Record<string, boolean | undefined>> | null,
|
|
194
|
+
): boolean {
|
|
195
|
+
const override = own<unknown>(purposeOverrides, purpose);
|
|
196
|
+
if (override === false) return true;
|
|
197
|
+
const callerRow = own<unknown>(purposeEnabled, purpose);
|
|
198
|
+
const enabled =
|
|
199
|
+
typeof callerRow === "boolean"
|
|
200
|
+
? callerRow
|
|
201
|
+
: own<unknown>(HTI_SHARE_PLATFORM_PURPOSE_ENABLED, purpose);
|
|
202
|
+
return enabled === false && override !== true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The ceiling row key for one recipient: `__external__` for an off-platform
|
|
207
|
+
* recipient, else its partner id (canon L328053). Throws on a non-external
|
|
208
|
+
* recipient with no id, or whose id is a reserved row key.
|
|
209
|
+
*/
|
|
210
|
+
export function sharingCeilingRowKey(recipient: {
|
|
211
|
+
readonly external?: boolean;
|
|
212
|
+
readonly id?: string | null;
|
|
213
|
+
}): string {
|
|
214
|
+
if (recipient.external === true) return SHARING_CEILING_EXTERNAL_ROW_KEY;
|
|
215
|
+
const id = recipient.id;
|
|
216
|
+
if (typeof id !== "string" || id === "") {
|
|
217
|
+
throw new TypeError("sharing ceiling: an on-platform recipient needs a partner id");
|
|
218
|
+
}
|
|
219
|
+
if (id === SHARING_CEILING_DEFAULT_ROW_KEY || id === SHARING_CEILING_EXTERNAL_ROW_KEY) {
|
|
220
|
+
throw new RangeError(`sharing ceiling: "${id}" is a reserved row key, not a partner id`);
|
|
221
|
+
}
|
|
222
|
+
return id;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The ceiling for one recipient row and one purpose, by canon's chain:
|
|
227
|
+
*
|
|
228
|
+
* purpose disabled (clamp 1) → [] (purpose_disabled)
|
|
229
|
+
* edits[rowKey][purpose] (partner_rule)
|
|
230
|
+
* || starter.counterpartyOverrides[rowKey][purpose] (seeded_partner_rule)
|
|
231
|
+
* || edits.__default__[purpose] (account_default)
|
|
232
|
+
* || starter.scopeByPurpose[purpose] (platform_default)
|
|
233
|
+
* || [] (none — fails closed)
|
|
234
|
+
*
|
|
235
|
+
* `rowKey` is a partner id or `__external__` ({@link sharingCeilingRowKey});
|
|
236
|
+
* `__default__` is refused (it is the account default, not a recipient).
|
|
237
|
+
*/
|
|
238
|
+
export function effectiveCeiling(
|
|
239
|
+
rowKey: string,
|
|
240
|
+
purpose: string,
|
|
241
|
+
inputs: EffectiveSharingCeilingInputs,
|
|
242
|
+
): EffectiveSharingCeiling {
|
|
243
|
+
if (typeof rowKey !== "string" || rowKey === "") {
|
|
244
|
+
throw new TypeError("sharing ceiling: rowKey must be a partner id or __external__");
|
|
245
|
+
}
|
|
246
|
+
if (rowKey === SHARING_CEILING_DEFAULT_ROW_KEY) {
|
|
247
|
+
throw new RangeError("sharing ceiling: __default__ is the account default, not a recipient row key");
|
|
248
|
+
}
|
|
249
|
+
if (typeof purpose !== "string" || purpose === "") {
|
|
250
|
+
throw new TypeError("sharing ceiling: purpose must be a share purpose code");
|
|
251
|
+
}
|
|
252
|
+
const starter = inputs.starter ?? HTI_SHARE_PLATFORM_STARTER;
|
|
253
|
+
if (isSharingPurposeDisabled(purpose, inputs.purposeOverrides, inputs.purposeEnabled)) {
|
|
254
|
+
return { allowed: [], rung: "purpose_disabled", disabled: true };
|
|
255
|
+
}
|
|
256
|
+
const edits = inputs.edits;
|
|
257
|
+
const rungs: readonly [SharingCeilingRung, unknown][] = [
|
|
258
|
+
["partner_rule", own(own(edits, rowKey), purpose)],
|
|
259
|
+
["seeded_partner_rule", own(own(starter.counterpartyOverrides, rowKey), purpose)],
|
|
260
|
+
["account_default", own(own(edits, SHARING_CEILING_DEFAULT_ROW_KEY), purpose)],
|
|
261
|
+
["platform_default", own(starter.scopeByPurpose, purpose)],
|
|
262
|
+
];
|
|
263
|
+
for (const [rung, value] of rungs) {
|
|
264
|
+
const allowed = rungList(value);
|
|
265
|
+
if (allowed !== undefined) return { allowed, rung, disabled: false };
|
|
266
|
+
}
|
|
267
|
+
// Stricter than canon's `|| null` (module header): fail closed.
|
|
268
|
+
return { allowed: [], rung: "none", disabled: false };
|
|
269
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE ceiling coverage rule (co-2sx): ONE answer to "does this ceiling list
|
|
3
|
+
* let this v4 row leave?", read the same way by the composer's mask
|
|
4
|
+
* ({@link rowsOutsideCeiling} / {@link rowsExpandedByCeiling} /
|
|
5
|
+
* {@link maskSelectionToCeiling}) and by the send door
|
|
6
|
+
* ({@link rowsOverCeiling}), so the list a composer masks to is the list a
|
|
7
|
+
* door accepts (pinned by a property test over every live row × generated
|
|
8
|
+
* ceilings).
|
|
9
|
+
*
|
|
10
|
+
* A row `code` is WITHIN a ceiling list when
|
|
11
|
+
* 1. it or an ANCESTOR is listed — canon's dumb read, `scopeListCovers`
|
|
12
|
+
* (prototype `HTI-LEADS-PAVE-Thread-prototype.html` @ d85f05b7
|
|
13
|
+
* L327953-L327964); or
|
|
14
|
+
* 2. it carries no content of its own (below), has at least one coverage
|
|
15
|
+
* child, and EVERY coverage child is within (recursively) — canon's door
|
|
16
|
+
* expands a branch / kind pick to the grain the ceiling is written at
|
|
17
|
+
* before clamping (`shareItemsWithinCeiling`, L328204-L328230), so a
|
|
18
|
+
* ceiling naming every child of `identifying` but not `identifying`
|
|
19
|
+
* permits the `identifying` pick.
|
|
20
|
+
*
|
|
21
|
+
* OWN CONTENT (ruling, strict rule 2): a row with fields HOMED directly on it
|
|
22
|
+
* (canon's field placements, {@link INFORMATION_SCOPE_FIELD_HOME_ROWS}; a
|
|
23
|
+
* per-kind row reads its `kindRow`, since rule 1 routes another person's
|
|
24
|
+
* record there) and NO rest row is within ONLY when it or an ancestor is
|
|
25
|
+
* listed — listing its children does not carry its own fields. Examples:
|
|
26
|
+
* `instruments` (`capture.assessment*`) and `commercial_activity`. A row with
|
|
27
|
+
* a rest row (`identifying` + `identifying.other`) keeps rule 2: its own
|
|
28
|
+
* fields are its rest row's.
|
|
29
|
+
*
|
|
30
|
+
* A COVERAGE child is live, not under the platform safety floor (never
|
|
31
|
+
* shared) and not System metadata (always rides). A row is LIVE when it and
|
|
32
|
+
* every row on its ancestor path are non-retired; a code outside the tree, or
|
|
33
|
+
* not live, is never within.
|
|
34
|
+
*
|
|
35
|
+
* Deliberately NOT the resolver's `isCovered`, under which a listed rest row
|
|
36
|
+
* (`identifying.other`) would cover its whole category.
|
|
37
|
+
*
|
|
38
|
+
* `allowed`: a list (or set) of v4 row codes; `null` is NO ceiling (canon
|
|
39
|
+
* `if (allowed && …)`), under which every live row is within. Any other value
|
|
40
|
+
* reads as `[]` (fail closed). `effectiveCeiling` never answers `null`.
|
|
41
|
+
*
|
|
42
|
+
* The tree arrives as an information-scope resolver (or anything with its
|
|
43
|
+
* four read methods), so this module carries no SOT and no I/O.
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
import {
|
|
47
|
+
INFORMATION_SCOPE_SYSTEM_INFO,
|
|
48
|
+
type InformationScopeResolver,
|
|
49
|
+
} from "../services/informationScopeResolver.js";
|
|
50
|
+
import { INFORMATION_SCOPE_FIELD_HOME_ROWS } from "./starter.js";
|
|
51
|
+
|
|
52
|
+
/** What the coverage rule reads of a tree. */
|
|
53
|
+
export type SharingCeilingScope = Pick<
|
|
54
|
+
InformationScopeResolver,
|
|
55
|
+
"rows" | "row" | "childrenOf" | "ancestorsOf"
|
|
56
|
+
>;
|
|
57
|
+
|
|
58
|
+
/** A ceiling list as callers hold it; `null` = no ceiling. */
|
|
59
|
+
export type SharingCeilingAllowed = readonly string[] | ReadonlySet<string> | null;
|
|
60
|
+
|
|
61
|
+
interface TreeFacts {
|
|
62
|
+
readonly isLive: (code: string) => boolean;
|
|
63
|
+
readonly isSystem: (code: string) => boolean;
|
|
64
|
+
readonly underFloor: (code: string) => boolean;
|
|
65
|
+
readonly isCoverageRow: (code: string) => boolean;
|
|
66
|
+
readonly coverageChildren: (code: string) => readonly string[];
|
|
67
|
+
readonly hasOwnContent: (code: string) => boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const factsCache = new WeakMap<object, TreeFacts>();
|
|
71
|
+
|
|
72
|
+
function memoize<V>(fn: (code: string) => V): (code: string) => V {
|
|
73
|
+
const memo = new Map<string, V>();
|
|
74
|
+
return (code) => {
|
|
75
|
+
if (memo.has(code)) return memo.get(code) as V;
|
|
76
|
+
const out = fn(code);
|
|
77
|
+
memo.set(code, out);
|
|
78
|
+
return out;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function treeFacts(scope: SharingCeilingScope): TreeFacts {
|
|
83
|
+
const hit = factsCache.get(scope);
|
|
84
|
+
if (hit) return hit;
|
|
85
|
+
const systemPath = scope.ancestorsOf(INFORMATION_SCOPE_SYSTEM_INFO);
|
|
86
|
+
const systemRoot = systemPath.length > 0 ? systemPath[systemPath.length - 1] : undefined;
|
|
87
|
+
const isLive = memoize(
|
|
88
|
+
(code) =>
|
|
89
|
+
scope.row(code) !== undefined &&
|
|
90
|
+
scope.ancestorsOf(code).every((c) => scope.row(c)?.status !== "retired"),
|
|
91
|
+
);
|
|
92
|
+
const isSystem = memoize(
|
|
93
|
+
(code) => systemRoot !== undefined && scope.ancestorsOf(code).includes(systemRoot),
|
|
94
|
+
);
|
|
95
|
+
const underFloor = memoize((code) =>
|
|
96
|
+
scope.ancestorsOf(code).some((c) => scope.row(c)?.platformFloor === true),
|
|
97
|
+
);
|
|
98
|
+
const isCoverageRow = memoize((code) => isLive(code) && !isSystem(code) && !underFloor(code));
|
|
99
|
+
const coverageChildren = memoize((code) =>
|
|
100
|
+
Object.freeze(scope.childrenOf(code).filter(isCoverageRow)),
|
|
101
|
+
);
|
|
102
|
+
const hasOwnContent = memoize((code) => {
|
|
103
|
+
const row = scope.row(code);
|
|
104
|
+
if (row === undefined) return false;
|
|
105
|
+
const homed =
|
|
106
|
+
INFORMATION_SCOPE_FIELD_HOME_ROWS.has(code) ||
|
|
107
|
+
(row.kindRow !== undefined && INFORMATION_SCOPE_FIELD_HOME_ROWS.has(row.kindRow));
|
|
108
|
+
if (!homed) return false;
|
|
109
|
+
// A rest row stands for the row's own fields; then rule 2 still applies.
|
|
110
|
+
return !scope
|
|
111
|
+
.childrenOf(code)
|
|
112
|
+
.some((c) => scope.row(c)?.rowType === "category_rest" && isLive(c));
|
|
113
|
+
});
|
|
114
|
+
const facts: TreeFacts = {
|
|
115
|
+
isLive,
|
|
116
|
+
isSystem,
|
|
117
|
+
underFloor,
|
|
118
|
+
isCoverageRow,
|
|
119
|
+
coverageChildren,
|
|
120
|
+
hasOwnContent,
|
|
121
|
+
};
|
|
122
|
+
factsCache.set(scope, facts);
|
|
123
|
+
return facts;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function asCeilingSet(allowed: unknown): ReadonlySet<string> | null {
|
|
127
|
+
if (allowed === null) return null;
|
|
128
|
+
if (allowed instanceof Set) return allowed as ReadonlySet<string>;
|
|
129
|
+
if (Array.isArray(allowed)) {
|
|
130
|
+
return new Set(allowed.filter((c): c is string => typeof c === "string"));
|
|
131
|
+
}
|
|
132
|
+
return new Set(); // malformed: covers nothing
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Does the coverage rule require `code` for its parent to be covered? Live,
|
|
137
|
+
* not under the platform floor, not System metadata.
|
|
138
|
+
*/
|
|
139
|
+
export function isCeilingCoverageRow(scope: SharingCeilingScope, code: string): boolean {
|
|
140
|
+
return treeFacts(scope).isCoverageRow(code);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Does `code` carry content of its own that children-only coverage cannot
|
|
145
|
+
* carry (fields homed directly on it, no rest row)? See the module header.
|
|
146
|
+
*/
|
|
147
|
+
export function hasOwnScopeContent(scope: SharingCeilingScope, code: string): boolean {
|
|
148
|
+
return treeFacts(scope).hasOwnContent(code);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The coverage of ONE ceiling list, memoised per row — build it once and ask
|
|
153
|
+
* it about many rows. See the module header for the rule.
|
|
154
|
+
*/
|
|
155
|
+
export function ceilingCoverage(
|
|
156
|
+
scope: SharingCeilingScope,
|
|
157
|
+
allowed: SharingCeilingAllowed,
|
|
158
|
+
): (code: string) => boolean {
|
|
159
|
+
const facts = treeFacts(scope);
|
|
160
|
+
const set = asCeilingSet(allowed);
|
|
161
|
+
const memo = new Map<string, boolean>();
|
|
162
|
+
const within = (code: string): boolean => {
|
|
163
|
+
const hit = memo.get(code);
|
|
164
|
+
if (hit !== undefined) return hit;
|
|
165
|
+
let out: boolean;
|
|
166
|
+
if (!facts.isLive(code)) out = false;
|
|
167
|
+
else if (set === null) out = true;
|
|
168
|
+
else if (scope.ancestorsOf(code).some((c) => set.has(c))) out = true;
|
|
169
|
+
else if (facts.hasOwnContent(code)) out = false;
|
|
170
|
+
else {
|
|
171
|
+
const kids = facts.coverageChildren(code);
|
|
172
|
+
out = kids.length > 0 && kids.every(within);
|
|
173
|
+
}
|
|
174
|
+
memo.set(code, out);
|
|
175
|
+
return out;
|
|
176
|
+
};
|
|
177
|
+
return within;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** THE coverage rule for one row (see the module header). */
|
|
181
|
+
export function scopeRowWithinCeiling(
|
|
182
|
+
scope: SharingCeilingScope,
|
|
183
|
+
allowed: SharingCeilingAllowed,
|
|
184
|
+
code: string,
|
|
185
|
+
): boolean {
|
|
186
|
+
return ceilingCoverage(scope, allowed)(code);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The DOOR reader: the codes of a declared list the ceiling does not let
|
|
191
|
+
* leave, in list order, each once. `system_info` and every System-metadata
|
|
192
|
+
* row ride regardless (canon L327974), and rows under the platform floor are
|
|
193
|
+
* never included on any share, so neither is reported. A code outside the
|
|
194
|
+
* tree, or not live, is reported (fail closed).
|
|
195
|
+
*/
|
|
196
|
+
export function rowsOverCeiling(
|
|
197
|
+
scope: SharingCeilingScope,
|
|
198
|
+
declared: readonly string[] | null | undefined,
|
|
199
|
+
allowed: SharingCeilingAllowed,
|
|
200
|
+
): string[] {
|
|
201
|
+
const facts = treeFacts(scope);
|
|
202
|
+
const within = ceilingCoverage(scope, allowed);
|
|
203
|
+
const out: string[] = [];
|
|
204
|
+
for (const code of new Set(Array.isArray(declared) ? declared : [])) {
|
|
205
|
+
if (code === INFORMATION_SCOPE_SYSTEM_INFO) continue;
|
|
206
|
+
if (!facts.isLive(code)) {
|
|
207
|
+
out.push(code);
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (facts.isSystem(code) || facts.underFloor(code)) continue;
|
|
211
|
+
if (!within(code)) out.push(code);
|
|
212
|
+
}
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
interface CeilingWalk {
|
|
217
|
+
/** Rows wholly outside the ceiling, each at its highest such grain. */
|
|
218
|
+
readonly outside: string[];
|
|
219
|
+
/** Own-content rows not within that hold within rows: a pick of one is its children. */
|
|
220
|
+
readonly expanded: string[];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** One top-down walk of the live tree against a ceiling (System metadata skipped). */
|
|
224
|
+
function walkCeiling(scope: SharingCeilingScope, allowed: SharingCeilingAllowed): CeilingWalk {
|
|
225
|
+
const facts = treeFacts(scope);
|
|
226
|
+
if (asCeilingSet(allowed) === null) return { outside: [], expanded: [] };
|
|
227
|
+
const within = ceilingCoverage(scope, allowed);
|
|
228
|
+
const holdsMemo = new Map<string, boolean>();
|
|
229
|
+
const holdsWithin = (code: string): boolean => {
|
|
230
|
+
const hit = holdsMemo.get(code);
|
|
231
|
+
if (hit !== undefined) return hit;
|
|
232
|
+
// Only a coverage row counts as content held beneath: a listed FLOOR row
|
|
233
|
+
// (never included) does not make its parent partly permitted.
|
|
234
|
+
const out = scope
|
|
235
|
+
.childrenOf(code)
|
|
236
|
+
.some((c) => (facts.isCoverageRow(c) && within(c)) || holdsWithin(c));
|
|
237
|
+
holdsMemo.set(code, out);
|
|
238
|
+
return out;
|
|
239
|
+
};
|
|
240
|
+
const outside: string[] = [];
|
|
241
|
+
const expanded: string[] = [];
|
|
242
|
+
const visit = (code: string): void => {
|
|
243
|
+
if (!facts.isLive(code) || within(code)) return;
|
|
244
|
+
if (!holdsWithin(code)) {
|
|
245
|
+
outside.push(code);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (facts.hasOwnContent(code)) expanded.push(code);
|
|
249
|
+
for (const child of scope.childrenOf(code)) visit(child);
|
|
250
|
+
};
|
|
251
|
+
for (const r of scope.rows) {
|
|
252
|
+
if (r.parentScopeCode !== null && r.parentScopeCode !== "") continue;
|
|
253
|
+
if (facts.isSystem(r.scopeCode)) continue;
|
|
254
|
+
visit(r.scopeCode);
|
|
255
|
+
}
|
|
256
|
+
return { outside, expanded };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The COMPOSER-MASK reader: the rows a ceiling restricts, each at the
|
|
261
|
+
* highest grain that is wholly outside it — a row not within and holding no
|
|
262
|
+
* within coverage row beneath. A row that is not within but holds one that
|
|
263
|
+
* is, is not restricted itself; its children are walked. System metadata is
|
|
264
|
+
* never restricted. `null` (no ceiling) restricts nothing.
|
|
265
|
+
*/
|
|
266
|
+
export function rowsOutsideCeiling(
|
|
267
|
+
scope: SharingCeilingScope,
|
|
268
|
+
allowed: SharingCeilingAllowed,
|
|
269
|
+
): string[] {
|
|
270
|
+
return walkCeiling(scope, allowed).outside;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* The rows with content of their own (strict rule 2) that are not within the
|
|
275
|
+
* ceiling but hold rows that are: the row itself cannot be sent, so a pick of
|
|
276
|
+
* it reads as a pick of its children (then masked). Top-down order.
|
|
277
|
+
*/
|
|
278
|
+
export function rowsExpandedByCeiling(
|
|
279
|
+
scope: SharingCeilingScope,
|
|
280
|
+
allowed: SharingCeilingAllowed,
|
|
281
|
+
): string[] {
|
|
282
|
+
return walkCeiling(scope, allowed).expanded;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** `system_info` first, then each code once, in order. */
|
|
286
|
+
function withSystemInfo(codes: readonly string[]): string[] {
|
|
287
|
+
return [...new Set([INFORMATION_SCOPE_SYSTEM_INFO, ...codes])];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Take one row out of a selection at its own grain: drop it and everything
|
|
292
|
+
* beneath it, and replace each selected ancestor by its other children, all
|
|
293
|
+
* the way down the path (a category's rest row is one of those children, so
|
|
294
|
+
* it is written too).
|
|
295
|
+
*/
|
|
296
|
+
function subtractRow(
|
|
297
|
+
scope: SharingCeilingScope,
|
|
298
|
+
selection: readonly string[],
|
|
299
|
+
code: string,
|
|
300
|
+
): string[] {
|
|
301
|
+
const path = scope.ancestorsOf(code); // code first, then up to the root
|
|
302
|
+
let sel = selection.filter((c) => !scope.ancestorsOf(c).includes(code));
|
|
303
|
+
for (;;) {
|
|
304
|
+
const coverAt = path.findIndex((c, i) => i > 0 && sel.includes(c));
|
|
305
|
+
if (coverAt === -1) break;
|
|
306
|
+
sel = sel.filter((c) => c !== path[coverAt]);
|
|
307
|
+
for (let i = coverAt; i >= 1; i -= 1) {
|
|
308
|
+
const parent = path[i] as string;
|
|
309
|
+
const onPath = path[i - 1];
|
|
310
|
+
for (const child of scope.childrenOf(parent)) {
|
|
311
|
+
if (child !== onPath && !sel.includes(child)) sel.push(child);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return sel;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* The COMPOSER MASK: a selection with every row the ceiling restricts taken
|
|
320
|
+
* out at its own grain. First each covered own-content row the ceiling does
|
|
321
|
+
* not permit ({@link rowsExpandedByCeiling}) is replaced by its children,
|
|
322
|
+
* then every row wholly outside ({@link rowsOutsideCeiling}) is subtracted.
|
|
323
|
+
* Only live rows survive (plus `system_info`, always first). A row within
|
|
324
|
+
* the ceiling is never removed, and {@link rowsOverCeiling} accepts every
|
|
325
|
+
* result.
|
|
326
|
+
*/
|
|
327
|
+
export function maskSelectionToCeiling(
|
|
328
|
+
scope: SharingCeilingScope,
|
|
329
|
+
selection: readonly string[] | null | undefined,
|
|
330
|
+
allowed: SharingCeilingAllowed,
|
|
331
|
+
): string[] {
|
|
332
|
+
const facts = treeFacts(scope);
|
|
333
|
+
const { outside, expanded } = walkCeiling(scope, allowed);
|
|
334
|
+
let sel = (Array.isArray(selection) ? selection : []).filter(
|
|
335
|
+
(c): c is string => typeof c === "string" && facts.isLive(c),
|
|
336
|
+
);
|
|
337
|
+
for (const code of expanded) {
|
|
338
|
+
if (!scope.ancestorsOf(code).some((c) => sel.includes(c))) continue;
|
|
339
|
+
sel = subtractRow(scope, sel, code);
|
|
340
|
+
for (const child of scope.childrenOf(code)) if (!sel.includes(child)) sel.push(child);
|
|
341
|
+
}
|
|
342
|
+
for (const code of outside) sel = subtractRow(scope, sel, code);
|
|
343
|
+
// Siblings put back by subtractRow / the expansion come from childrenOf,
|
|
344
|
+
// which a caller's tree may populate with retired rows: keep live ones only.
|
|
345
|
+
return withSystemInfo(sel.filter((c) => facts.isLive(c)));
|
|
346
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @htilabs/platform-common-models/sharing-ceiling
|
|
3
|
+
*
|
|
4
|
+
* Browser-safe entry for the Inter-Agency Sharing CEILING on the CVC-236 v4
|
|
5
|
+
* tree (co-2sx): the platform starter (generated, gated canon data), the
|
|
6
|
+
* cell fold, the ONE read chain (`effectiveCeiling`) and the ONE coverage
|
|
7
|
+
* rule (`scopeRowWithinCeiling`, with its door and composer-mask readers).
|
|
8
|
+
* Pure TypeScript, no mongoose, no I/O, no SOT import — pass the v4 tree as
|
|
9
|
+
* an information-scope resolver. Also re-exported from the package root.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export * from "./starter.js";
|
|
13
|
+
export * from "./chain.js";
|
|
14
|
+
export * from "./coverage.js";
|