@fabrika/control-contract 0.9.0-dev.3 → 0.9.0-dev.30

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/control-contract",
3
- "version": "0.9.0-dev.3",
3
+ "version": "0.9.0-dev.30",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,12 +22,12 @@
22
22
  }
23
23
  },
24
24
  "scripts": {
25
- "typecheck": "tsc --noEmit"
25
+ "typecheck": "tsrs --build tsconfig.test.json"
26
26
  },
27
27
  "dependencies": {
28
28
  "zod": "^3.25.76",
29
- "@fabrika/auth-core": "0.9.0-dev.3",
30
- "@fabrika/app": "0.9.0-dev.3",
31
- "@fabrika/provider-contract": "0.9.0-dev.3"
29
+ "@fabrika/auth-core": "0.9.0-dev.30",
30
+ "@fabrika/app": "0.9.0-dev.30",
31
+ "@fabrika/provider-contract": "0.9.0-dev.30"
32
32
  }
33
33
  }
package/src/access.ts CHANGED
@@ -45,6 +45,18 @@ export const CONTROL_ACTIONS = Object.freeze({
45
45
  MAIL_CONTENT_READ: 'mail.content.read',
46
46
  /** Release or drop a held message, requeue a final one, and change the gate's rules. Global. */
47
47
  MAIL_MANAGE: 'mail.manage',
48
+ /**
49
+ * Read an application environment's data as a metadata projection: sizes, counts, state and the backup list
50
+ * (ADR-0069 §2). Scoped by app/environment.
51
+ */
52
+ DATA_READ: 'data.read',
53
+ /**
54
+ * Read row contents, exports, backup downloads and content fields of every tenant of an environment. Checked as a
55
+ * global grant only, so a grant scoped to an app never carries it: content is for the installation's operators.
56
+ */
57
+ DATA_CONTENT_READ: 'data.content.read',
58
+ /** Run maintenance transitions, backups and restores, and recover an interrupted operation. Scoped by app/environment. */
59
+ DATA_MANAGE: 'data.manage',
48
60
  })
49
61
 
50
62
  /**
@@ -0,0 +1,335 @@
1
+ // How the approval page shows one access change (ADR-0050 §7): the classification's facts grouped by who reaches an
2
+ // endpoint and by what else they are about, one row per subject with all its lines, ranked by effect, review and
3
+ // reach, and quiet subjects that changed alike folded into one row. Pure, and it decides nothing: whether a run waits is
4
+ // `waitsForApproval` of its lines and its environment's protection alone.
5
+
6
+ import {
7
+ type AccessChangeDto,
8
+ CHANGE_SUBJECT_KINDS,
9
+ type ChangeAudienceDto,
10
+ type ChangeSubjectDto,
11
+ type ChangeSubjectKind,
12
+ SURFACE_REACHES,
13
+ type SurfaceReach,
14
+ } from './index.js'
15
+
16
+ /**
17
+ * Whether a change waits for a person on a protected environment (ADR-0050 §3): it has a line that needs closer review,
18
+ * a step off the paved road, something anyone sets off, people becoming visible or a migration that may change stored
19
+ * data. Any other change publishes at once, its lines recorded.
20
+ */
21
+ export function waitsForApproval(changes: readonly Pick<AccessChangeDto, 'review'>[]): boolean {
22
+ return changes.some((change) => change.review === 'closer')
23
+ }
24
+
25
+ /**
26
+ * The endpoints of one caller group: everyone a reach admits, or the holders of one link mount's share links. `people`
27
+ * marks the machine endpoints that signed-in people reach too, a group of their own, so its title names both.
28
+ */
29
+ export interface CallerGroup {
30
+ readonly kind: 'caller'
31
+ readonly key: string
32
+ readonly reach: SurfaceReach
33
+ readonly mount?: string
34
+ readonly people?: true
35
+ }
36
+
37
+ /** The lines about one kind of subject other than an endpoint: tables, roles, migrations, the application. */
38
+ export interface SubjectGroup {
39
+ readonly kind: 'subject'
40
+ readonly key: string
41
+ readonly subject: ChangeSubjectKind
42
+ }
43
+
44
+ /** One subject and its lines, most important first. */
45
+ export interface SubjectRow {
46
+ readonly kind: 'subject'
47
+ readonly subject: ChangeSubjectDto
48
+ readonly audience?: ChangeAudienceDto
49
+ readonly changes: readonly AccessChangeDto[]
50
+ }
51
+
52
+ /**
53
+ * Quiet subjects that changed alike: each has lines of exactly these rules and effects, and none of them widens or needs
54
+ * closer review (`rules` in order, as `rule` and `effect`).
55
+ */
56
+ export interface FoldedRow {
57
+ readonly kind: 'folded'
58
+ readonly rules: readonly { readonly rule: string; readonly effect: 'narrows' | 'neutral' }[]
59
+ readonly subjects: readonly SubjectRow[]
60
+ }
61
+
62
+ export type ChangeRow = SubjectRow | FoldedRow
63
+
64
+ export interface EffectTally {
65
+ readonly widens: number
66
+ readonly narrows: number
67
+ readonly neutral: number
68
+ readonly closer: number
69
+ }
70
+
71
+ /** A group's subjects by what their lines do: how many there are, how many widen access, how many need closer review. */
72
+ export interface SubjectTally {
73
+ readonly subjects: number
74
+ readonly widening: number
75
+ readonly closer: number
76
+ }
77
+
78
+ export type ChangeGroup = (CallerGroup | SubjectGroup) & {
79
+ readonly rows: readonly ChangeRow[]
80
+ /** How many subjects the group holds, folded ones included. */
81
+ readonly subjects: number
82
+ /** The group's lines by effect. */
83
+ readonly tally: EffectTally
84
+ /** The group's subjects (its rows' endpoints, tables, …) by effect: what its heading counts. */
85
+ readonly standing: SubjectTally
86
+ /** Whether the page collapses the group by default: only when it holds no widening line. */
87
+ readonly collapsed: boolean
88
+ }
89
+
90
+ export interface ApprovalView {
91
+ /**
92
+ * The change in a sentence or two, which the page leads with: the endpoints it adds, changes and removes, and what to
93
+ * check first. `check` is every line that needs closer review, the most important first; without one, the widening
94
+ * line most worth a look: a new endpoint the most exposed callers reach, else the most exposed widening. Empty when
95
+ * nothing widens or needs closer review.
96
+ */
97
+ readonly summary: {
98
+ readonly added: number
99
+ readonly changed: number
100
+ readonly removed: number
101
+ readonly check: readonly AccessChangeDto[]
102
+ }
103
+ /**
104
+ * Per caller group the endpoints that change, apart from the removed endpoints and mounts its paths reach (a removal
105
+ * that widens stands with whoever its paths reach now), and the migration files the change adds or edits.
106
+ */
107
+ readonly impact: {
108
+ readonly callers: readonly {
109
+ readonly key: string
110
+ readonly reach: SurfaceReach
111
+ readonly mount?: string
112
+ readonly people?: true
113
+ readonly endpoints: number
114
+ readonly removedEndpoints: number
115
+ readonly removedMounts: number
116
+ }[]
117
+ readonly migrations: number
118
+ }
119
+ readonly callers: readonly ChangeGroup[]
120
+ readonly data: readonly ChangeGroup[]
121
+ /** The lines of a run classified before lines carried facts, in their stored order. */
122
+ readonly unsorted: readonly AccessChangeDto[]
123
+ }
124
+
125
+ /** Quiet subjects that changed alike fold from this many on. */
126
+ export const FOLD_AT = 3
127
+
128
+ /**
129
+ * A line's rank: a widening for closer review first, then a widening, a narrowing for closer review, a line that leaves
130
+ * access unchanged, a narrowing. Nothing else enters it, so it cannot disagree with the classification.
131
+ */
132
+ export function changeRank(change: Pick<AccessChangeDto, 'effect' | 'review'>): number {
133
+ const closer = change.review === 'closer'
134
+ if (change.effect === 'widens') return closer ? 0 : 1
135
+ if (closer) return 2
136
+ return change.effect === 'neutral' ? 3 : 4
137
+ }
138
+
139
+ export function approvalView(changes: readonly AccessChangeDto[]): ApprovalView {
140
+ const callers = new Map<string, { readonly head: CallerGroup; readonly changes: AccessChangeDto[] }>()
141
+ const data = new Map<ChangeSubjectKind, AccessChangeDto[]>()
142
+ const unsorted: AccessChangeDto[] = []
143
+ // A line with an audience is about who reaches something: an endpoint, or a removed mount whose paths now reach a
144
+ // more exposed endpoint. Every other line is about data and runtime.
145
+ for (const change of changes) {
146
+ const subject = change.subject
147
+ if (subject === undefined) unsorted.push(change)
148
+ else if (change.audience !== undefined) {
149
+ const { reach, mount, callers: kinds } = change.audience
150
+ // An endpoint machines and signed-in people both reach is listed under machines, its most exposed caller, in a
151
+ // group of its own whose title names both.
152
+ const people = reach === 'machine' && kinds?.includes('human') === true
153
+ const key = mount !== undefined ? `${reach}:${mount}` : people ? `${reach}+human` : reach
154
+ const group = callers.get(key)
155
+ const head: CallerGroup = { kind: 'caller', key, reach, ...(mount === undefined ? {} : { mount }), ...(people ? { people: true } : {}) }
156
+ if (group === undefined) callers.set(key, { head, changes: [change] })
157
+ else group.changes.push(change)
158
+ } else {
159
+ const kind = data.get(subject.kind)
160
+ if (kind === undefined) data.set(subject.kind, [change])
161
+ else kind.push(change)
162
+ }
163
+ }
164
+ const callerGroups = sortGroups([...callers.values()].map(({ head, changes }) => group(head, changes)), reachIndex)
165
+ const dataGroups = sortGroups([...data].map(([subject, changes]) => group({ kind: 'subject', key: subject, subject }, changes)), subjectIndex)
166
+ // A migration file the change adds or edits; a removed file runs nowhere.
167
+ const migrations = new Set(
168
+ changes.flatMap((change) =>
169
+ change.subject?.kind === 'migration' && (change.rule === 'migration.added' || change.rule === 'migration.changed') ? [change.subject.name] : []
170
+ ),
171
+ )
172
+ const counted = (entry: ChangeGroup, kind: 'endpoint' | 'mount', removed: boolean) =>
173
+ entry.rows.flatMap(subjectRows).filter((row) =>
174
+ row.subject.kind === kind && row.changes.some((change) => change.rule === 'endpoint.removed' || change.rule === 'mount.removed') === removed
175
+ ).length
176
+ // Each endpoint once, by what its lines say happened to it.
177
+ const fates = new Map<string, 'added' | 'removed' | 'changed'>()
178
+ for (const change of changes) {
179
+ if (change.subject?.kind !== 'endpoint') continue
180
+ const fate = change.rule === 'endpoint.added' ? 'added' : change.rule === 'endpoint.removed' ? 'removed' : 'changed'
181
+ if (fate !== 'changed' || !fates.has(change.subject.name)) fates.set(change.subject.name, fate)
182
+ }
183
+ const fateCount = (fate: 'added' | 'removed' | 'changed') => [...fates.values()].filter((each) => each === fate).length
184
+ return {
185
+ summary: { added: fateCount('added'), changed: fateCount('changed'), removed: fateCount('removed'), check: checkFirst(changes) },
186
+ impact: {
187
+ callers: callerGroups.flatMap((entry) =>
188
+ entry.kind === 'caller'
189
+ ? [{
190
+ key: entry.key,
191
+ reach: entry.reach,
192
+ ...(entry.mount === undefined ? {} : { mount: entry.mount }),
193
+ ...(entry.people === true ? { people: true as const } : {}),
194
+ endpoints: counted(entry, 'endpoint', false),
195
+ removedEndpoints: counted(entry, 'endpoint', true),
196
+ removedMounts: counted(entry, 'mount', true),
197
+ }]
198
+ : []
199
+ ),
200
+ migrations: migrations.size,
201
+ },
202
+ callers: callerGroups,
203
+ data: dataGroups,
204
+ unsorted,
205
+ }
206
+ }
207
+
208
+ /**
209
+ * What the approver checks first: every line for closer review, by rank; without one, the one widening line most worth a
210
+ * look: a new endpoint before any other line, the most exposed callers first, then the line's place.
211
+ */
212
+ function checkFirst(changes: readonly AccessChangeDto[]): AccessChangeDto[] {
213
+ const placed = changes.map((change, index) => ({ change, index }))
214
+ const closer = placed.filter(({ change }) => change.review === 'closer')
215
+ if (closer.length > 0) return closer.sort((a, b) => changeRank(a.change) - changeRank(b.change) || a.index - b.index).map(({ change }) => change)
216
+ const reach = (change: AccessChangeDto) => (change.audience === undefined ? SURFACE_REACHES.length : SURFACE_REACHES.indexOf(change.audience.reach))
217
+ const added = (change: AccessChangeDto) => (change.rule === 'endpoint.added' ? 0 : 1)
218
+ const first = placed.filter(({ change }) => change.effect === 'widens')
219
+ .sort((a, b) => added(a.change) - added(b.change) || reach(a.change) - reach(b.change) || a.index - b.index)[0]
220
+ return first === undefined ? [] : [first.change]
221
+ }
222
+
223
+ /** Every line a row holds. */
224
+ export function rowChanges(row: ChangeRow): readonly AccessChangeDto[] {
225
+ return row.kind === 'subject' ? row.changes : row.subjects.flatMap((subject) => subject.changes)
226
+ }
227
+
228
+ /** Every subject a row holds. */
229
+ function subjectRows(row: ChangeRow): readonly SubjectRow[] {
230
+ return row.kind === 'subject' ? [row] : row.subjects
231
+ }
232
+
233
+ /** Groups in rank order: the group whose most important line ranks first, then by `index` (reach or subject kind). */
234
+ function sortGroups(groups: readonly ChangeGroup[], index: (entry: ChangeGroup) => number): ChangeGroup[] {
235
+ const ranked = groups.map((entry) => ({ entry, rank: Math.min(...entry.rows.flatMap(rowChanges).map(changeRank)) }))
236
+ return ranked.sort((a, b) => a.rank - b.rank || index(a.entry) - index(b.entry) || compareText(a.entry.key, b.entry.key)).map(({ entry }) => entry)
237
+ }
238
+
239
+ function group(head: CallerGroup | SubjectGroup, changes: readonly AccessChangeDto[]): ChangeGroup {
240
+ const bySubject = new Map<string, { subject: ChangeSubjectDto; audience?: ChangeAudienceDto; changes: AccessChangeDto[] }>()
241
+ for (const change of changes) {
242
+ const subject = change.subject
243
+ if (subject === undefined) continue
244
+ // A removed mount and an endpoint may share a name; a subject is its kind and its name.
245
+ const key = `${subject.kind}:${subject.name}`
246
+ const entry = bySubject.get(key)
247
+ if (entry === undefined) bySubject.set(key, { subject, ...(change.audience === undefined ? {} : { audience: change.audience }), changes: [change] })
248
+ else entry.changes.push(change)
249
+ }
250
+ const rows = [...bySubject.values()].map((entry): SubjectRow => ({
251
+ kind: 'subject',
252
+ subject: entry.subject,
253
+ ...(entry.audience === undefined ? {} : { audience: entry.audience }),
254
+ changes: entry.changes.map((change, index) => ({ change, index })).sort((a, b) => changeRank(a.change) - changeRank(b.change) || a.index - b.index)
255
+ .map(({ change }) => change),
256
+ }))
257
+ return {
258
+ ...head,
259
+ rows: fold(rows.sort((a, b) => rowRank(a) - rowRank(b) || compareText(a.subject.name, b.subject.name))),
260
+ subjects: rows.length,
261
+ tally: tallyOf(changes),
262
+ standing: {
263
+ subjects: rows.length,
264
+ widening: rows.filter((row) => row.changes.some((change) => change.effect === 'widens')).length,
265
+ closer: rows.filter((row) => row.changes.some((change) => change.review === 'closer')).length,
266
+ },
267
+ collapsed: !changes.some((change) => change.effect === 'widens'),
268
+ }
269
+ }
270
+
271
+ /**
272
+ * A subject is quiet when none of its lines widens or needs closer review. Quiet subjects whose lines have the same rules
273
+ * and effects become one row, from `FOLD_AT` of them on, at the place of the first; every other subject keeps its row,
274
+ * so every widening and every line for closer review is in view without expanding anything.
275
+ */
276
+ function fold(rows: readonly SubjectRow[]): ChangeRow[] {
277
+ const keys = new Map(rows.map((row) => [row, quietKey(row)]))
278
+ const families = new Map<string, SubjectRow[]>()
279
+ for (const [row, key] of keys) {
280
+ if (key === undefined) continue
281
+ const family = families.get(key)
282
+ if (family === undefined) families.set(key, [row])
283
+ else family.push(row)
284
+ }
285
+ const folded: ChangeRow[] = []
286
+ const placed = new Set<string>()
287
+ for (const row of rows) {
288
+ const key = keys.get(row)
289
+ const family = key === undefined ? [] : (families.get(key) ?? [])
290
+ if (key === undefined || family.length < FOLD_AT) folded.push(row)
291
+ else if (!placed.has(key)) {
292
+ placed.add(key)
293
+ folded.push({ kind: 'folded', rules: quietRules(row), subjects: family })
294
+ }
295
+ }
296
+ return folded
297
+ }
298
+
299
+ function quietRules(row: SubjectRow): { rule: string; effect: 'narrows' | 'neutral' }[] {
300
+ const rules = new Map<string, { rule: string; effect: 'narrows' | 'neutral' }>()
301
+ for (const change of row.changes) {
302
+ if (change.rule !== undefined && change.effect !== 'widens') {
303
+ rules.set(`${change.rule} ${change.effect}`, { rule: change.rule, effect: change.effect })
304
+ }
305
+ }
306
+ return [...rules.values()]
307
+ }
308
+
309
+ function quietKey(row: SubjectRow): string | undefined {
310
+ const quiet = row.changes.every((change) => change.effect !== 'widens' && change.review !== 'closer' && change.rule !== undefined)
311
+ return quiet ? quietRules(row).map(({ rule, effect }) => `${rule} ${effect}`).join('|') : undefined
312
+ }
313
+
314
+ function tallyOf(changes: readonly AccessChangeDto[]): EffectTally {
315
+ return {
316
+ widens: changes.filter((change) => change.effect === 'widens').length,
317
+ narrows: changes.filter((change) => change.effect === 'narrows').length,
318
+ neutral: changes.filter((change) => change.effect === 'neutral').length,
319
+ closer: changes.filter((change) => change.review === 'closer').length,
320
+ }
321
+ }
322
+
323
+ function rowRank(row: SubjectRow): number {
324
+ return Math.min(...row.changes.map(changeRank))
325
+ }
326
+
327
+ function reachIndex(entry: ChangeGroup): number {
328
+ return entry.kind === 'caller' ? SURFACE_REACHES.indexOf(entry.reach) : 0
329
+ }
330
+
331
+ function subjectIndex(entry: ChangeGroup): number {
332
+ return entry.kind === 'subject' ? CHANGE_SUBJECT_KINDS.indexOf(entry.subject) : 0
333
+ }
334
+
335
+ const compareText = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0)