@htilabs/platform-common-models 0.19.0 → 0.20.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/dist/cvcs/index.cjs +4769 -643
- package/dist/cvcs/index.cjs.map +1 -1
- package/dist/cvcs/index.d.cts +5036 -606
- package/dist/cvcs/index.d.ts +5036 -606
- package/dist/cvcs/index.js +4769 -643
- package/dist/cvcs/index.js.map +1 -1
- package/dist/enums/index.cjs +74 -1
- package/dist/enums/index.cjs.map +1 -1
- package/dist/enums/index.d.cts +77 -4
- package/dist/enums/index.d.ts +77 -4
- package/dist/enums/index.js +74 -1
- package/dist/enums/index.js.map +1 -1
- package/dist/index.cjs +5199 -588
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5189 -588
- package/dist/index.js.map +1 -1
- package/dist/information-scope/index.cjs +440 -0
- package/dist/information-scope/index.cjs.map +1 -0
- package/dist/information-scope/index.d.cts +313 -0
- package/dist/information-scope/index.d.ts +313 -0
- package/dist/information-scope/index.js +404 -0
- package/dist/information-scope/index.js.map +1 -0
- package/dist/services/index.cjs +414 -2
- package/dist/services/index.cjs.map +1 -1
- package/dist/services/index.d.cts +1 -0
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +403 -1
- package/dist/services/index.js.map +1 -1
- package/package.json +32 -26
- package/src/__tests__/__snapshots__/cvc-sot.snap +3 -3
- package/src/__tests__/core-information-scope-invariant.test.ts +709 -456
- package/src/__tests__/cvc-sot-snapshot.test.ts +16 -17
- package/src/__tests__/cvc-structural.test.ts +2 -2
- package/src/__tests__/location-type-routing-invariant.test.ts +25 -8
- package/src/cvcs/coreInformationScope.v2.0.0.json +5396 -0
- package/src/cvcs/index.ts +2 -2
- package/src/cvcs/roiScope.v1.3.0.json +360 -0
- package/src/generated/cvc-enums.ts +77 -4
- package/src/services/__tests__/fixtures/infoscope-v4/information_scope_v4.migrations.csv +106 -0
- package/src/services/__tests__/fixtures/infoscope-v4/information_scope_v4.rows.csv +117 -0
- package/src/services/__tests__/fixtures/infoscope-v4/loadInfoscopeV4Fixtures.ts +116 -0
- package/src/services/__tests__/informationScopeResolver.test.ts +964 -0
- package/src/services/index.ts +10 -3
- package/src/services/informationScope.ts +10 -0
- package/src/services/informationScopeResolver.ts +672 -0
- package/src/services/informationScopeSot.ts +161 -0
- package/src/cvcs/coreInformationScope.v1.1.0.json +0 -1290
- package/src/cvcs/roiScope.v1.2.0.json +0 -340
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @htilabs/platform-common-models — Information scope resolver (CVC-236 v4)
|
|
3
|
+
*
|
|
4
|
+
* The ONE implementation of how a stored information-scope selection reads
|
|
5
|
+
* against the v4 tree, so Thread and PAVE (preview and send, picker and door)
|
|
6
|
+
* cannot disagree. Pure and dependency-free: no imports, no I/O, never
|
|
7
|
+
* mutates its inputs. Browser-safe entry: `@htilabs/platform-common-models/information-scope`.
|
|
8
|
+
*
|
|
9
|
+
* Spec (canon, read-only): htilabstech/prototype
|
|
10
|
+
* docs/share-roi/INFOSCOPE_V4_DEV_HANDOFF_2026-10-09.md
|
|
11
|
+
* @ 00179edcb3f0c00a8b55beeffd1618c8a5c98937 (signed; the handoff and its
|
|
12
|
+
* CSVs are unchanged from 6dab8a95)
|
|
13
|
+
* - § 2 rules 1–7 ("the resolver contract — build exactly this, nothing
|
|
14
|
+
* cleverer") — every behaviour below cites its rule;
|
|
15
|
+
* - § 3 the migration rules (identity · row · category · subtract ·
|
|
16
|
+
* retired before v4) and `scopeVersion`; a signed release keeps its list
|
|
17
|
+
* as signed and its current reading is DERIVED at read time; a retired
|
|
18
|
+
* code "stays recognisable in stored data forever";
|
|
19
|
+
* - § 7 row 6 a branch-grain pick is expanded to the ceiling's grain
|
|
20
|
+
* before clamping; a sender can never widen;
|
|
21
|
+
* - § 8 the enums.
|
|
22
|
+
* Reference implementation of rule-1 kind-row resolution:
|
|
23
|
+
* tools/infoscope/phase4_stress_test.py `resolve_home` (same SHA).
|
|
24
|
+
*
|
|
25
|
+
* WHAT A SELECTION STORES. `information_scope: string[]` holds canon's scope
|
|
26
|
+
* codes as canon spells them — `names`, `identifying.other`,
|
|
27
|
+
* `offenders.risk` (handoff § 8: "lowercase snake, or `<kind>.<row>`") —
|
|
28
|
+
* NOT the package's SOT keys (`offenders__risk`) and NOT seven-segment wire
|
|
29
|
+
* codes (`236-016-001-002-…`). `ScopeRow.scopeCode` / `parentScopeCode` are
|
|
30
|
+
* in that space; `scopeRowsFromSot` maps the SOT into it.
|
|
31
|
+
*
|
|
32
|
+
* ALWAYS COVERED. `system_info` and its ancestor `system` ("System metadata —
|
|
33
|
+
* always included, never a choice", handoff § 1; the field placements home
|
|
34
|
+
* the system fields on `system`) are covered by every selection.
|
|
35
|
+
*
|
|
36
|
+
* Two kinds of call:
|
|
37
|
+
* - READS (`isCovered`, `isIncludedOnShare`, `shareVerdict`,
|
|
38
|
+
* `isVisibleAtLevel`, `readSelection`, `readRestriction`,
|
|
39
|
+
* `expandBranchPickToGrain`, `clampToCeiling`) are total: they never
|
|
40
|
+
* throw on stored data. An unknown code, a null list or an unsupported
|
|
41
|
+
* `scopeVersion` (absent, 2, or anything but 3 / 4) includes nothing
|
|
42
|
+
* (fail closed).
|
|
43
|
+
* - PICKER WRITES (`selectAll`, `deselectChild`, `expandSelectAll`), the
|
|
44
|
+
* build-time `rowForSubject`, and the constructor throw on programming
|
|
45
|
+
* errors (an unknown or non-selectable code, a malformed tree).
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
// ─── Constants ───────────────────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
/** The tree version this resolver reads to (handoff § 3: "today 3, after this round 4"). */
|
|
51
|
+
export const INFORMATION_SCOPE_VERSION = 4 as const;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The `scopeVersion` stamps a stored list may carry and still be read: 3
|
|
55
|
+
* (through the v3 → v4 table) and 4. A 2 list, and an UNSTAMPED list (which
|
|
56
|
+
* is v2-era), must go through canon's conjunctive v2 → v3 table first (SOT
|
|
57
|
+
* v2.0.0 `migrations.readAt` / `migrations.unstamped`: the `retired before
|
|
58
|
+
* v4` lines name "the flattened v3 reading, not an unconditional one"); this
|
|
59
|
+
* package ships no v2 → v3 table, so those lists are unsupported and include
|
|
60
|
+
* nothing (fail closed). None was ever persisted (`migrations.unstamped`).
|
|
61
|
+
*/
|
|
62
|
+
export const INFORMATION_SCOPE_READABLE_VERSIONS: readonly number[] = Object.freeze([3, 4]);
|
|
63
|
+
|
|
64
|
+
/** Required on every stored selection and always included (handoff § 2 rule 1; § 6). */
|
|
65
|
+
export const INFORMATION_SCOPE_SYSTEM_INFO = "system_info" as const;
|
|
66
|
+
|
|
67
|
+
// ─── Row and migration input shapes ─────────────────────────────────────────
|
|
68
|
+
|
|
69
|
+
/** A kind of other named person (handoff § 1; § 8 `kind_branch`). */
|
|
70
|
+
export type ScopePersonKind = "offenders" | "family" | "others";
|
|
71
|
+
|
|
72
|
+
/** Whose record a record is: the client, or another named person's kind (rule 1). */
|
|
73
|
+
export type ScopeSubjectKind = "client" | ScopePersonKind;
|
|
74
|
+
|
|
75
|
+
/** `kind_branch` (§ 8); `null` on System and Notes rows (blank in the CSV). */
|
|
76
|
+
export type ScopeKindBranch = ScopeSubjectKind | "leads" | null;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* `row_type` (§ 8). The tree also carries `branch` (`primary_client`,
|
|
80
|
+
* `other_named_persons`) and the LEADS spelling `sub-category`, which § 8's
|
|
81
|
+
* list omits; both are accepted as written.
|
|
82
|
+
*/
|
|
83
|
+
export type ScopeRowType =
|
|
84
|
+
| "branch"
|
|
85
|
+
| "category"
|
|
86
|
+
| "subcategory"
|
|
87
|
+
| "sub-category"
|
|
88
|
+
| "person_type"
|
|
89
|
+
| "category_rest"
|
|
90
|
+
| "instrument row";
|
|
91
|
+
|
|
92
|
+
/** `status` (§ 8). */
|
|
93
|
+
export type ScopeRowStatus = "as_today" | "new" | "retired";
|
|
94
|
+
|
|
95
|
+
/** `special_data_type` (§ 8, plus `evidence`, which the tree carries on `case_attachments`). */
|
|
96
|
+
export type ScopeSpecialDataType =
|
|
97
|
+
| "none"
|
|
98
|
+
| "survivor_confidential"
|
|
99
|
+
| "criminal_intelligence"
|
|
100
|
+
| "evidence";
|
|
101
|
+
|
|
102
|
+
/** The three app-relevance columns (§ 4). */
|
|
103
|
+
export type ScopeApp = "pave" | "thread" | "leads";
|
|
104
|
+
export type ScopeApps = Readonly<Record<ScopeApp, boolean>>;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* One tree row, in the selection-code space (see header). Bind the SOT with
|
|
108
|
+
* `scopeRowsFromSot`. Extra fields on a caller's row type are carried through
|
|
109
|
+
* (`row(code)` returns the caller's row).
|
|
110
|
+
*/
|
|
111
|
+
export interface ScopeRow {
|
|
112
|
+
/** The code a selection stores (canon's spelling: `offenders.risk`). */
|
|
113
|
+
scopeCode: string;
|
|
114
|
+
parentScopeCode: string | null;
|
|
115
|
+
rowType: ScopeRowType;
|
|
116
|
+
kindBranch: ScopeKindBranch;
|
|
117
|
+
/** On a per-kind row (`offenders.risk`): the client row it copies (`risk`). Required there. */
|
|
118
|
+
kindRow?: string;
|
|
119
|
+
/** App relevance (§ 4). Informational: no resolver decision reads it. */
|
|
120
|
+
apps: ScopeApps;
|
|
121
|
+
selectable: boolean;
|
|
122
|
+
/** `shareable_default = on` (rule 4: `false` = off by default). */
|
|
123
|
+
shareableDefault: boolean;
|
|
124
|
+
platformFloor: boolean;
|
|
125
|
+
levelFloor: 1 | 3;
|
|
126
|
+
specialDataType: ScopeSpecialDataType;
|
|
127
|
+
/** A `retired` row is never covered, offered or written. */
|
|
128
|
+
status: ScopeRowStatus;
|
|
129
|
+
replacesV3?: readonly string[];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The four migration rules of handoff § 3, plus the pre-v4 retirement reading. */
|
|
133
|
+
export type ScopeMigrationRule = "identity" | "row" | "category" | "subtract" | "retired before v4";
|
|
134
|
+
|
|
135
|
+
/** One from-to line (selection-code space); `to` is empty for "(nothing)". */
|
|
136
|
+
export interface ScopeMigration {
|
|
137
|
+
from: string;
|
|
138
|
+
to: readonly string[];
|
|
139
|
+
rule: ScopeMigrationRule;
|
|
140
|
+
note?: string;
|
|
141
|
+
/** The line reads WIDER than v3 did (SOT `widens: true`; consent widening is an open question). */
|
|
142
|
+
widens?: boolean;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ─── Output shapes ───────────────────────────────────────────────────────────
|
|
146
|
+
|
|
147
|
+
/** How one stored code was read. `unknown` = no row, no table line: it includes nothing. */
|
|
148
|
+
export type ScopeReadingRule = ScopeMigrationRule | "unknown";
|
|
149
|
+
|
|
150
|
+
export interface ScopeCodeReading {
|
|
151
|
+
readonly from: string;
|
|
152
|
+
readonly rule: ScopeReadingRule;
|
|
153
|
+
readonly reads: readonly string[];
|
|
154
|
+
/** The table line it read through is flagged as reading wider than v3. */
|
|
155
|
+
readonly widens: boolean;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The current (v4) reading of a stored list. `stored` and `storedVersion` are
|
|
160
|
+
* the list as signed, copied, never rewritten (§ 3: "a signed release keeps
|
|
161
|
+
* its list as signed, at the version signed; its current reading is derived
|
|
162
|
+
* through the table at read time").
|
|
163
|
+
*/
|
|
164
|
+
export interface ScopeReading {
|
|
165
|
+
/** The stamp as stored; `null` when it was absent or not a number. */
|
|
166
|
+
readonly storedVersion: number | null;
|
|
167
|
+
readonly stored: readonly string[];
|
|
168
|
+
readonly version: typeof INFORMATION_SCOPE_VERSION;
|
|
169
|
+
/** The stamp is not in `INFORMATION_SCOPE_READABLE_VERSIONS`: every code reads `unknown`. */
|
|
170
|
+
readonly unsupportedVersion: boolean;
|
|
171
|
+
/**
|
|
172
|
+
* The derived v4 list: unique, in tree order. A selection reading always
|
|
173
|
+
* contains `system_info`; a restriction reading contains only what it restricts.
|
|
174
|
+
*/
|
|
175
|
+
readonly informationScope: readonly string[];
|
|
176
|
+
/** One entry per distinct stored code, in stored order. */
|
|
177
|
+
readonly codes: readonly ScopeCodeReading[];
|
|
178
|
+
/** Stored codes that are retired (read by `row`, `subtract` or `retired before v4`). */
|
|
179
|
+
readonly retired: readonly string[];
|
|
180
|
+
/** Stored codes with no live row and no table line; they include nothing. */
|
|
181
|
+
readonly unknown: readonly string[];
|
|
182
|
+
/** Stored codes whose reading is wider than v3's (`widens`): surface, never hide. */
|
|
183
|
+
readonly widened: readonly string[];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export type ScopeShareVerdict =
|
|
187
|
+
| { readonly included: true; readonly reason: "included" }
|
|
188
|
+
| { readonly included: false; readonly reason: "not_covered" | "platform_floor" | "unknown_code" }
|
|
189
|
+
| {
|
|
190
|
+
readonly included: false;
|
|
191
|
+
readonly reason: "off_by_default";
|
|
192
|
+
/** The off-by-default rows in the chain the account has not enabled. */
|
|
193
|
+
readonly notEnabled: readonly string[];
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
export type ScopeShareReason = ScopeShareVerdict["reason"];
|
|
197
|
+
|
|
198
|
+
export interface ScopeShareOptions {
|
|
199
|
+
/** The off-by-default rows the account's policy has enabled for sharing (rule 4). */
|
|
200
|
+
enabledOffByDefault?: ReadonlySet<string> | readonly string[];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export interface CreateInformationScopeResolverInput<R extends ScopeRow> {
|
|
204
|
+
rows: readonly R[];
|
|
205
|
+
migrations?: readonly ScopeMigration[];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** A stored list as it may arrive from storage (reads are total). */
|
|
209
|
+
export type StoredScopeList = readonly string[] | null | undefined;
|
|
210
|
+
|
|
211
|
+
export interface InformationScopeResolver<R extends ScopeRow = ScopeRow> {
|
|
212
|
+
readonly version: typeof INFORMATION_SCOPE_VERSION;
|
|
213
|
+
/** Every row in input order. */
|
|
214
|
+
readonly rows: readonly R[];
|
|
215
|
+
row(code: string): R | undefined;
|
|
216
|
+
/** The live (non-retired) children of `code`, in input order (a copy). */
|
|
217
|
+
childrenOf(code: string): string[];
|
|
218
|
+
/** `code` first, then its parent, up to the root (a copy). Empty for an unknown code. */
|
|
219
|
+
ancestorsOf(code: string): string[];
|
|
220
|
+
|
|
221
|
+
/** Rules 2 + 3. */
|
|
222
|
+
isCovered(selection: StoredScopeList, code: string): boolean;
|
|
223
|
+
/** Rules 2–5. */
|
|
224
|
+
isIncludedOnShare(selection: StoredScopeList, code: string, options?: ScopeShareOptions): boolean;
|
|
225
|
+
/** `isIncludedOnShare` with the reason, for "held back and counted". */
|
|
226
|
+
shareVerdict(
|
|
227
|
+
selection: StoredScopeList,
|
|
228
|
+
code: string,
|
|
229
|
+
options?: ScopeShareOptions,
|
|
230
|
+
): ScopeShareVerdict;
|
|
231
|
+
/** Rule 7 (the viewing side). */
|
|
232
|
+
isVisibleAtLevel(level: number, code: string): boolean;
|
|
233
|
+
|
|
234
|
+
/** Rules 3 + 6: the codes a picker writes when it expands `code` (its live children, rest row included). */
|
|
235
|
+
expandSelectAll(code: string): string[];
|
|
236
|
+
/** Rule 6: select-all at `code` (the code itself is stored; its now-redundant descendants drop). */
|
|
237
|
+
selectAll(selection: readonly string[], code: string): string[];
|
|
238
|
+
/** Rules 3 + 6: de-select one child; every selected ancestor is replaced by its other children. */
|
|
239
|
+
deselectChild(selection: readonly string[], childCode: string): string[];
|
|
240
|
+
|
|
241
|
+
/** § 3: the current v4 reading of a stored selection. */
|
|
242
|
+
readSelection(list: StoredScopeList, scopeVersion: number | null | undefined): ScopeReading;
|
|
243
|
+
/** § 3 `subtract`: the current v4 reading of a stored RESTRICTION. */
|
|
244
|
+
readRestriction(list: StoredScopeList, scopeVersion: number | null | undefined): ScopeReading;
|
|
245
|
+
|
|
246
|
+
/** Rule 1: the row a record is included under, given its field's home row and its subject. */
|
|
247
|
+
rowForSubject(homeRow: string, kind: ScopeSubjectKind): string;
|
|
248
|
+
|
|
249
|
+
/** § 7 row 6: expand every pick that sits above the ceiling's grain into its children. */
|
|
250
|
+
expandBranchPickToGrain(selection: StoredScopeList, ceiling: StoredScopeList): string[];
|
|
251
|
+
/** § 7 row 6: expand, then keep only what the ceiling covers. A sender can never widen. */
|
|
252
|
+
clampToCeiling(selection: StoredScopeList, ceiling: StoredScopeList): string[];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// ─── Pure row guards ─────────────────────────────────────────────────────────
|
|
256
|
+
|
|
257
|
+
/** A row a picker offers as a choice (§ 8 `selectable`; never a retired row). */
|
|
258
|
+
export const isSelectableScopeRow = (row: ScopeRow | undefined): boolean =>
|
|
259
|
+
row !== undefined && row.selectable && row.status !== "retired";
|
|
260
|
+
|
|
261
|
+
/** The `category_rest` row (rule 3): never offered, covered with its category. */
|
|
262
|
+
export const isScopeRestRow = (row: ScopeRow | undefined): boolean =>
|
|
263
|
+
row !== undefined && row.rowType === "category_rest";
|
|
264
|
+
|
|
265
|
+
/** One of the 28 `summary_<x>` / `full_<x>` rows (§ 8 `instrument row`). */
|
|
266
|
+
export const isScopeInstrumentRow = (row: ScopeRow | undefined): boolean =>
|
|
267
|
+
row !== undefined && row.rowType === "instrument row";
|
|
268
|
+
|
|
269
|
+
// ─── Person kind (rule 1, Phase 3 D5) ───────────────────────────────────────
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Which kind of other named person someone is (handoff § 2 rule 1; § 8; Phase
|
|
273
|
+
* 3 D5): "This person caused harm" wins; "Eligible family member" (asserted
|
|
274
|
+
* by the advocate, never derived from the relationship) otherwise; neither →
|
|
275
|
+
* `others`. A person with both switches is listed under `offenders`; the
|
|
276
|
+
* caller keeps the family-member VAWA handling, which rides `efmAsserted` on
|
|
277
|
+
* the person, not the kind.
|
|
278
|
+
*/
|
|
279
|
+
export function kindForPerson(person: {
|
|
280
|
+
causedHarm: boolean;
|
|
281
|
+
efmAsserted: boolean;
|
|
282
|
+
}): ScopePersonKind {
|
|
283
|
+
if (person.causedHarm) return "offenders";
|
|
284
|
+
if (person.efmAsserted) return "family";
|
|
285
|
+
return "others";
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// ─── The resolver ────────────────────────────────────────────────────────────
|
|
289
|
+
|
|
290
|
+
const PERSON_KINDS: ReadonlySet<string> = new Set<ScopePersonKind>(["offenders", "family", "others"]);
|
|
291
|
+
const SPECIAL_DATA_TYPES: ReadonlySet<string> = new Set<ScopeSpecialDataType>([
|
|
292
|
+
"none",
|
|
293
|
+
"survivor_confidential",
|
|
294
|
+
"criminal_intelligence",
|
|
295
|
+
"evidence",
|
|
296
|
+
]);
|
|
297
|
+
const MIGRATION_RULES: ReadonlySet<string> = new Set<ScopeMigrationRule>([
|
|
298
|
+
"identity",
|
|
299
|
+
"row",
|
|
300
|
+
"category",
|
|
301
|
+
"subtract",
|
|
302
|
+
"retired before v4",
|
|
303
|
+
]);
|
|
304
|
+
const RETIRED_RULES: ReadonlySet<ScopeReadingRule> = new Set<ScopeReadingRule>([
|
|
305
|
+
"row",
|
|
306
|
+
"subtract",
|
|
307
|
+
"retired before v4",
|
|
308
|
+
]);
|
|
309
|
+
|
|
310
|
+
/** Reading precedence per code (§ 3): a stated category line, then identity, row, pre-v4. */
|
|
311
|
+
const SELECTION_PRECEDENCE: readonly ScopeMigrationRule[] = [
|
|
312
|
+
"category",
|
|
313
|
+
"identity",
|
|
314
|
+
"row",
|
|
315
|
+
"retired before v4",
|
|
316
|
+
];
|
|
317
|
+
const RESTRICTION_PRECEDENCE: readonly ScopeMigrationRule[] = [
|
|
318
|
+
"subtract",
|
|
319
|
+
...SELECTION_PRECEDENCE,
|
|
320
|
+
];
|
|
321
|
+
|
|
322
|
+
const asList = (list: StoredScopeList): readonly string[] => (Array.isArray(list) ? list : []);
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Bind the resolver to one tree (and its migration table). Throws on a
|
|
326
|
+
* malformed tree: a duplicate code, a parent that is not a row, a cycle, no
|
|
327
|
+
* `system_info` row, a live row whose `specialDataType` is missing or not in
|
|
328
|
+
* § 8 (rule 1 routes on it — absence fails closed by refusing to bind), a
|
|
329
|
+
* per-kind row without a `kindRow` that names a row, an unknown migration
|
|
330
|
+
* rule, a migration successor that is not a live row, or two different
|
|
331
|
+
* readings for the same code and rule.
|
|
332
|
+
*/
|
|
333
|
+
export function createInformationScopeResolver<R extends ScopeRow>(
|
|
334
|
+
input: CreateInformationScopeResolverInput<R>,
|
|
335
|
+
): InformationScopeResolver<R> {
|
|
336
|
+
const rows = Object.freeze([...input.rows]);
|
|
337
|
+
const byCode = new Map<string, R>();
|
|
338
|
+
const order = new Map<string, number>();
|
|
339
|
+
for (const r of rows) {
|
|
340
|
+
if (byCode.has(r.scopeCode)) {
|
|
341
|
+
throw new Error(`information scope: duplicate code "${r.scopeCode}"`);
|
|
342
|
+
}
|
|
343
|
+
byCode.set(r.scopeCode, r);
|
|
344
|
+
order.set(r.scopeCode, order.size);
|
|
345
|
+
}
|
|
346
|
+
const isLive = (code: string): boolean => {
|
|
347
|
+
const r = byCode.get(code);
|
|
348
|
+
return r !== undefined && r.status !== "retired";
|
|
349
|
+
};
|
|
350
|
+
const children = new Map<string, string[]>();
|
|
351
|
+
for (const r of rows) {
|
|
352
|
+
if (r.status !== "retired" && !SPECIAL_DATA_TYPES.has(r.specialDataType)) {
|
|
353
|
+
throw new Error(
|
|
354
|
+
`information scope: "${r.scopeCode}" has specialDataType ${JSON.stringify(r.specialDataType)}`,
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
if (r.parentScopeCode === null || r.parentScopeCode === "") continue;
|
|
358
|
+
if (!byCode.has(r.parentScopeCode)) {
|
|
359
|
+
throw new Error(
|
|
360
|
+
`information scope: "${r.scopeCode}" names unknown parent "${r.parentScopeCode}"`,
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
if (r.status === "retired") continue;
|
|
364
|
+
const list = children.get(r.parentScopeCode) ?? [];
|
|
365
|
+
list.push(r.scopeCode);
|
|
366
|
+
children.set(r.parentScopeCode, list);
|
|
367
|
+
}
|
|
368
|
+
const ancestorCache = new Map<string, readonly string[]>();
|
|
369
|
+
const ancestors = (code: string): readonly string[] => {
|
|
370
|
+
const hit = ancestorCache.get(code);
|
|
371
|
+
if (hit) return hit;
|
|
372
|
+
const out: string[] = [];
|
|
373
|
+
let cur: string | null = code;
|
|
374
|
+
while (cur !== null && cur !== "") {
|
|
375
|
+
const r = byCode.get(cur);
|
|
376
|
+
if (!r) break;
|
|
377
|
+
if (out.includes(cur)) throw new Error(`information scope: cycle at "${cur}"`);
|
|
378
|
+
out.push(cur);
|
|
379
|
+
cur = r.parentScopeCode;
|
|
380
|
+
}
|
|
381
|
+
ancestorCache.set(code, out);
|
|
382
|
+
return out;
|
|
383
|
+
};
|
|
384
|
+
for (const r of rows) ancestors(r.scopeCode);
|
|
385
|
+
if (!isLive(INFORMATION_SCOPE_SYSTEM_INFO)) {
|
|
386
|
+
throw new Error(`information scope: the tree has no "${INFORMATION_SCOPE_SYSTEM_INFO}" row`);
|
|
387
|
+
}
|
|
388
|
+
/** `system_info` and its ancestors (`system`): covered by every selection. */
|
|
389
|
+
const alwaysCovered: ReadonlySet<string> = new Set(ancestors(INFORMATION_SCOPE_SYSTEM_INFO));
|
|
390
|
+
|
|
391
|
+
const kids = (code: string): readonly string[] => children.get(code) ?? [];
|
|
392
|
+
const descendantsOf = (code: string): string[] => {
|
|
393
|
+
const out: string[] = [];
|
|
394
|
+
const stack = [...kids(code)];
|
|
395
|
+
while (stack.length > 0) {
|
|
396
|
+
const c = stack.pop() as string;
|
|
397
|
+
out.push(c);
|
|
398
|
+
stack.push(...kids(c));
|
|
399
|
+
}
|
|
400
|
+
return out;
|
|
401
|
+
};
|
|
402
|
+
const restRowOf = (code: string): string | undefined =>
|
|
403
|
+
kids(code).find((c) => isScopeRestRow(byCode.get(c)));
|
|
404
|
+
|
|
405
|
+
// Rule 1 kind rows, read off the tree: each kind's `person_type` root, and
|
|
406
|
+
// each per-kind row by (kind, the client row it copies).
|
|
407
|
+
const kindRoot = new Map<string, string>();
|
|
408
|
+
const perKindRow = new Map<string, string>();
|
|
409
|
+
for (const r of rows) {
|
|
410
|
+
if (r.status === "retired" || r.kindBranch === null || !PERSON_KINDS.has(r.kindBranch)) continue;
|
|
411
|
+
if (r.rowType === "person_type") {
|
|
412
|
+
kindRoot.set(r.kindBranch, r.scopeCode);
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
if (r.kindRow === undefined || !isLive(r.kindRow)) {
|
|
416
|
+
throw new Error(
|
|
417
|
+
`information scope: per-kind row "${r.scopeCode}" has no live kindRow (${String(r.kindRow)})`,
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
perKindRow.set(`${r.kindBranch}\u0000${r.kindRow}`, r.scopeCode);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Migration table, indexed by code then rule.
|
|
424
|
+
const migrations = new Map<string, Map<ScopeMigrationRule, ScopeMigration>>();
|
|
425
|
+
for (const m of input.migrations ?? []) {
|
|
426
|
+
if (!MIGRATION_RULES.has(m.rule)) {
|
|
427
|
+
throw new Error(`information scope: unknown migration rule "${m.rule}" on "${m.from}"`);
|
|
428
|
+
}
|
|
429
|
+
for (const to of m.to) {
|
|
430
|
+
if (!isLive(to)) {
|
|
431
|
+
throw new Error(
|
|
432
|
+
`information scope: migration "${m.from}" (${m.rule}) reads as "${to}", not a live row`,
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
const perCode = migrations.get(m.from) ?? new Map<ScopeMigrationRule, ScopeMigration>();
|
|
437
|
+
const prior = perCode.get(m.rule);
|
|
438
|
+
if (prior && prior.to.join(" ") !== m.to.join(" ")) {
|
|
439
|
+
throw new Error(`information scope: two "${m.rule}" readings for "${m.from}"`);
|
|
440
|
+
}
|
|
441
|
+
perCode.set(m.rule, {
|
|
442
|
+
from: m.from,
|
|
443
|
+
rule: m.rule,
|
|
444
|
+
to: Object.freeze([...m.to]),
|
|
445
|
+
widens: m.widens === true || prior?.widens === true,
|
|
446
|
+
});
|
|
447
|
+
migrations.set(m.from, perCode);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Unique, tree order for known codes, unknown codes kept after in first-seen
|
|
452
|
+
* order; a selection always carries `system_info` (a restriction does not).
|
|
453
|
+
*/
|
|
454
|
+
const normalize = (codes: Iterable<string>, withSystemInfo = true): string[] => {
|
|
455
|
+
const seen = new Set<string>();
|
|
456
|
+
const known: string[] = [];
|
|
457
|
+
const unknown: string[] = [];
|
|
458
|
+
const all = withSystemInfo ? [INFORMATION_SCOPE_SYSTEM_INFO, ...codes] : [...codes];
|
|
459
|
+
for (const c of all) {
|
|
460
|
+
if (seen.has(c)) continue;
|
|
461
|
+
seen.add(c);
|
|
462
|
+
(byCode.has(c) ? known : unknown).push(c);
|
|
463
|
+
}
|
|
464
|
+
known.sort((a, b) => (order.get(a) as number) - (order.get(b) as number));
|
|
465
|
+
return [...known, ...unknown];
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
const requirePickable = (code: string, op: string): void => {
|
|
469
|
+
if (!byCode.has(code)) throw new RangeError(`information scope ${op}: unknown code "${code}"`);
|
|
470
|
+
if (!isSelectableScopeRow(byCode.get(code))) {
|
|
471
|
+
throw new RangeError(`information scope ${op}: "${code}" is not selectable`);
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
// Rule 2 (self or any ancestor in the flat list) + rule 3 (a record homed on a
|
|
476
|
+
// category is covered when the category OR its rest row is). `system_info` and
|
|
477
|
+
// `system` are always covered (header).
|
|
478
|
+
const isCovered = (selection: StoredScopeList, code: string): boolean => {
|
|
479
|
+
if (!isLive(code)) return false;
|
|
480
|
+
if (alwaysCovered.has(code)) return true;
|
|
481
|
+
const sel = new Set(asList(selection));
|
|
482
|
+
if (ancestors(code).some((a) => sel.has(a))) return true;
|
|
483
|
+
const rest = restRowOf(code);
|
|
484
|
+
return rest !== undefined && sel.has(rest);
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
const shareVerdict = (
|
|
488
|
+
selection: StoredScopeList,
|
|
489
|
+
code: string,
|
|
490
|
+
options: ScopeShareOptions = {},
|
|
491
|
+
): ScopeShareVerdict => {
|
|
492
|
+
if (!isLive(code)) return { included: false, reason: "unknown_code" };
|
|
493
|
+
if (!isCovered(selection, code)) return { included: false, reason: "not_covered" };
|
|
494
|
+
const chain = ancestors(code).map((c) => byCode.get(c) as R);
|
|
495
|
+
// Rule 5: the floor (or a floor ancestor) never leaves, whatever the list says.
|
|
496
|
+
if (chain.some((r) => r.platformFloor)) return { included: false, reason: "platform_floor" };
|
|
497
|
+
// Rule 4: every off-by-default row in the chain must be enabled by the account
|
|
498
|
+
// (the reference runner's `included`: enabling an ancestor does not enable an
|
|
499
|
+
// off-by-default child of it).
|
|
500
|
+
const enabled = new Set(options.enabledOffByDefault ?? []);
|
|
501
|
+
const notEnabled = chain
|
|
502
|
+
.filter((r) => !r.shareableDefault && !enabled.has(r.scopeCode))
|
|
503
|
+
.map((r) => r.scopeCode);
|
|
504
|
+
if (notEnabled.length > 0) return { included: false, reason: "off_by_default", notEnabled };
|
|
505
|
+
return { included: true, reason: "included" };
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
const expandSelectAll = (code: string): string[] => {
|
|
509
|
+
if (!byCode.has(code)) throw new RangeError(`information scope expand: unknown code "${code}"`);
|
|
510
|
+
return [...kids(code)];
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
const selectAll = (selection: readonly string[], code: string): string[] => {
|
|
514
|
+
requirePickable(code, "selectAll");
|
|
515
|
+
const drop = new Set(descendantsOf(code));
|
|
516
|
+
return normalize([...selection.filter((c) => !drop.has(c)), code]);
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const deselectChild = (selection: readonly string[], childCode: string): string[] => {
|
|
520
|
+
requirePickable(childCode, "deselectChild");
|
|
521
|
+
if (alwaysCovered.has(childCode)) {
|
|
522
|
+
throw new RangeError(`information scope deselectChild: "${childCode}" is always included`);
|
|
523
|
+
}
|
|
524
|
+
const sel = new Set(selection);
|
|
525
|
+
const path = ancestors(childCode); // [child, parent, …, root]
|
|
526
|
+
let top = 0;
|
|
527
|
+
for (let i = 1; i < path.length; i += 1) if (sel.has(path[i])) top = i;
|
|
528
|
+
// Replace every selected ancestor by its OTHER children, all the way down the
|
|
529
|
+
// path (rule 3: the rest row is one of those children, so it is written too).
|
|
530
|
+
for (let i = 1; i <= top; i += 1) {
|
|
531
|
+
sel.delete(path[i]);
|
|
532
|
+
for (const c of kids(path[i])) if (c !== path[i - 1]) sel.add(c);
|
|
533
|
+
}
|
|
534
|
+
sel.delete(childCode);
|
|
535
|
+
for (const d of descendantsOf(childCode)) sel.delete(d);
|
|
536
|
+
return normalize(sel);
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
const readOne = (
|
|
540
|
+
from: string,
|
|
541
|
+
scopeVersion: number,
|
|
542
|
+
precedence: readonly ScopeMigrationRule[],
|
|
543
|
+
): ScopeCodeReading => {
|
|
544
|
+
// A v4 list names live codes as written.
|
|
545
|
+
if (scopeVersion === INFORMATION_SCOPE_VERSION && isLive(from)) {
|
|
546
|
+
return { from, rule: "identity", reads: [from], widens: false };
|
|
547
|
+
}
|
|
548
|
+
// Every other code reads through the table — including a retired code in a
|
|
549
|
+
// v4 list (§ 3 (c): it "stays recognisable in stored data forever").
|
|
550
|
+
const lines = migrations.get(from);
|
|
551
|
+
if (lines) {
|
|
552
|
+
for (const rule of precedence) {
|
|
553
|
+
if (scopeVersion === INFORMATION_SCOPE_VERSION && (rule === "identity" || rule === "category")) {
|
|
554
|
+
continue; // a v4 code that is not live has no identity reading
|
|
555
|
+
}
|
|
556
|
+
const line = lines.get(rule);
|
|
557
|
+
if (line) return { from, rule, reads: [...line.to], widens: line.widens === true };
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return { from, rule: "unknown", reads: [], widens: false };
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
const read = (
|
|
564
|
+
list: StoredScopeList,
|
|
565
|
+
scopeVersion: number | null | undefined,
|
|
566
|
+
precedence: readonly ScopeMigrationRule[],
|
|
567
|
+
isRestriction: boolean,
|
|
568
|
+
): ScopeReading => {
|
|
569
|
+
const stored = [...asList(list)];
|
|
570
|
+
const supported =
|
|
571
|
+
typeof scopeVersion === "number" && INFORMATION_SCOPE_READABLE_VERSIONS.includes(scopeVersion);
|
|
572
|
+
const codes: ScopeCodeReading[] = [];
|
|
573
|
+
const seen = new Set<string>();
|
|
574
|
+
for (const from of stored) {
|
|
575
|
+
if (seen.has(from)) continue;
|
|
576
|
+
seen.add(from);
|
|
577
|
+
codes.push(
|
|
578
|
+
supported
|
|
579
|
+
? readOne(from, scopeVersion as number, precedence)
|
|
580
|
+
: { from, rule: "unknown", reads: [], widens: false },
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
return {
|
|
584
|
+
storedVersion: typeof scopeVersion === "number" ? scopeVersion : null,
|
|
585
|
+
stored,
|
|
586
|
+
version: INFORMATION_SCOPE_VERSION,
|
|
587
|
+
unsupportedVersion: !supported,
|
|
588
|
+
informationScope: normalize(
|
|
589
|
+
codes.flatMap((c) => c.reads),
|
|
590
|
+
!isRestriction,
|
|
591
|
+
),
|
|
592
|
+
codes,
|
|
593
|
+
retired: codes.filter((c) => RETIRED_RULES.has(c.rule)).map((c) => c.from),
|
|
594
|
+
unknown: codes.filter((c) => c.rule === "unknown").map((c) => c.from),
|
|
595
|
+
widened: codes.filter((c) => c.widens).map((c) => c.from),
|
|
596
|
+
};
|
|
597
|
+
};
|
|
598
|
+
|
|
599
|
+
const rowForSubject = (homeRow: string, kind: ScopeSubjectKind): string => {
|
|
600
|
+
const home = byCode.get(homeRow);
|
|
601
|
+
if (!home) throw new RangeError(`information scope rowForSubject: unknown home "${homeRow}"`);
|
|
602
|
+
if (kind === "client") return homeRow;
|
|
603
|
+
const chain = ancestors(homeRow);
|
|
604
|
+
// System, Notes and the LEADS groups sit under no `branch` row: the record stays home.
|
|
605
|
+
if (byCode.get(chain[chain.length - 1])?.rowType !== "branch") return homeRow;
|
|
606
|
+
// A criminal-intelligence row under the client (`derived_risk_signals`) stays home:
|
|
607
|
+
// every per-kind row is survivor_confidential, so re-branching would move CI
|
|
608
|
+
// content across a handling class. The reference keeps it home the same way
|
|
609
|
+
// (`resolve_home` / `leads_codes`).
|
|
610
|
+
if (home.specialDataType === "criminal_intelligence") return homeRow;
|
|
611
|
+
const root = kindRoot.get(kind);
|
|
612
|
+
if (!root) throw new RangeError(`information scope rowForSubject: the tree has no "${kind}" kind`);
|
|
613
|
+
if (chain.includes(root)) return homeRow;
|
|
614
|
+
// The deepest row the kind defines for the home (D15).
|
|
615
|
+
for (const anc of chain) {
|
|
616
|
+
const perKind = perKindRow.get(`${kind}\u0000${anc}`);
|
|
617
|
+
if (perKind !== undefined) return perKind;
|
|
618
|
+
}
|
|
619
|
+
return root;
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
const expandBranchPickToGrain = (
|
|
623
|
+
selection: StoredScopeList,
|
|
624
|
+
ceiling: StoredScopeList,
|
|
625
|
+
): string[] => {
|
|
626
|
+
const ceil = asList(ceiling);
|
|
627
|
+
const ceilSet = new Set(ceil);
|
|
628
|
+
const isWithin = (c: string): boolean => ancestors(c).some((a) => ceilSet.has(a));
|
|
629
|
+
const isAbove = (c: string): boolean => ceil.some((k) => k !== c && ancestors(k).includes(c));
|
|
630
|
+
const out: string[] = [];
|
|
631
|
+
const stack = [...asList(selection)];
|
|
632
|
+
while (stack.length > 0) {
|
|
633
|
+
const c = stack.pop() as string;
|
|
634
|
+
if (isLive(c) && !isWithin(c) && isAbove(c)) stack.push(...kids(c));
|
|
635
|
+
else out.push(c);
|
|
636
|
+
}
|
|
637
|
+
return normalize(out);
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
const clampToCeiling = (selection: StoredScopeList, ceiling: StoredScopeList): string[] => {
|
|
641
|
+
const ceil = new Set(asList(ceiling));
|
|
642
|
+
return normalize(
|
|
643
|
+
expandBranchPickToGrain(selection, ceiling).filter(
|
|
644
|
+
(c) => isLive(c) && ancestors(c).some((a) => ceil.has(a)),
|
|
645
|
+
),
|
|
646
|
+
);
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
return {
|
|
650
|
+
version: INFORMATION_SCOPE_VERSION,
|
|
651
|
+
rows,
|
|
652
|
+
row: (code) => byCode.get(code),
|
|
653
|
+
childrenOf: (code) => [...kids(code)],
|
|
654
|
+
ancestorsOf: (code) => [...ancestors(code)],
|
|
655
|
+
isCovered,
|
|
656
|
+
isIncludedOnShare: (selection, code, options) =>
|
|
657
|
+
shareVerdict(selection, code, options).included,
|
|
658
|
+
shareVerdict,
|
|
659
|
+
// Rule 7: the lowest internal viewing level that sees the row. Level 2
|
|
660
|
+
// (autocomplete) sees the level-1 stub (§ 5). Sharing ignores this.
|
|
661
|
+
isVisibleAtLevel: (level, code) =>
|
|
662
|
+
typeof level === "number" && isLive(code) && level >= (byCode.get(code) as R).levelFloor,
|
|
663
|
+
expandSelectAll,
|
|
664
|
+
selectAll,
|
|
665
|
+
deselectChild,
|
|
666
|
+
readSelection: (list, scopeVersion) => read(list, scopeVersion, SELECTION_PRECEDENCE, false),
|
|
667
|
+
readRestriction: (list, scopeVersion) => read(list, scopeVersion, RESTRICTION_PRECEDENCE, true),
|
|
668
|
+
rowForSubject,
|
|
669
|
+
expandBranchPickToGrain,
|
|
670
|
+
clampToCeiling,
|
|
671
|
+
};
|
|
672
|
+
}
|