@fleetless/contracts 13.3.0 → 15.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/CHANGELOG.md +304 -0
  2. package/artifacts/openapi.json +20156 -10513
  3. package/artifacts/routes.json +1429 -292
  4. package/artifacts/schema/access-warning.schema.json +51 -0
  5. package/artifacts/schema/action-config.schema.json +35 -0
  6. package/artifacts/schema/addon-catalogue-entry.schema.json +3 -1
  7. package/artifacts/schema/admin-plan-change-request.schema.json +26 -0
  8. package/artifacts/schema/app-access-response.schema.json +136 -0
  9. package/artifacts/schema/app-access.schema.json +80 -0
  10. package/artifacts/schema/app-deletion-summary.schema.json +15 -8
  11. package/artifacts/schema/app-invitation-list-response.schema.json +13 -6
  12. package/artifacts/schema/app-invitation.schema.json +5 -5
  13. package/artifacts/schema/app-list-response.schema.json +6 -6
  14. package/artifacts/schema/app-lock.schema.json +30 -0
  15. package/artifacts/schema/app-user-list-response.schema.json +13 -6
  16. package/artifacts/schema/app-user.schema.json +13 -6
  17. package/artifacts/schema/app.schema.json +6 -6
  18. package/artifacts/schema/asset-plan-limit-details.schema.json +3 -1
  19. package/artifacts/schema/auth-ok.schema.json +20 -6
  20. package/artifacts/schema/camera-list-response.schema.json +1 -1
  21. package/artifacts/schema/client-identity.schema.json +21 -6
  22. package/artifacts/schema/cloud-config.schema.json +359 -5
  23. package/artifacts/schema/collection-actor.schema.json +35 -0
  24. package/artifacts/schema/collection-definitions-response.schema.json +612 -0
  25. package/artifacts/schema/collection-event.schema.json +216 -0
  26. package/artifacts/schema/collection-export-query.schema.json +56 -0
  27. package/artifacts/schema/collection-filter.schema.json +9 -0
  28. package/artifacts/schema/collection-import-preview.schema.json +143 -0
  29. package/artifacts/schema/collection-import-request.schema.json +105 -0
  30. package/artifacts/schema/collection-import-result.schema.json +38 -0
  31. package/artifacts/schema/collection-list-response.schema.json +119 -0
  32. package/artifacts/schema/collection-record-get-query.schema.json +12 -0
  33. package/artifacts/schema/collection-record-history-query.schema.json +35 -0
  34. package/artifacts/schema/collection-record-history-response.schema.json +389 -0
  35. package/artifacts/schema/collection-record-list-query.schema.json +67 -0
  36. package/artifacts/schema/collection-record-list-response.schema.json +212 -0
  37. package/artifacts/schema/collection-record-restore-request.schema.json +14 -0
  38. package/artifacts/schema/collection-record-write.schema.json +31 -0
  39. package/artifacts/schema/collection-record.schema.json +180 -0
  40. package/artifacts/schema/collection-revision.schema.json +295 -0
  41. package/artifacts/schema/collection-subscribe-error.schema.json +31 -0
  42. package/artifacts/schema/collection-subscribe.schema.json +48 -0
  43. package/artifacts/schema/collection-subscribed.schema.json +22 -0
  44. package/artifacts/schema/collection-summary.schema.json +92 -0
  45. package/artifacts/schema/collection-unsubscribe.schema.json +22 -0
  46. package/artifacts/schema/collections-access-change.schema.json +34 -0
  47. package/artifacts/schema/collections-document.schema.json +732 -0
  48. package/artifacts/schema/collections-draft-response.schema.json +845 -0
  49. package/artifacts/schema/collections-effect.schema.json +384 -0
  50. package/artifacts/schema/collections-issue.schema.json +36 -0
  51. package/artifacts/schema/collections-publish-request.schema.json +18 -0
  52. package/artifacts/schema/collections-publish-response.schema.json +450 -0
  53. package/artifacts/schema/collections-purge-preview.schema.json +60 -0
  54. package/artifacts/schema/collections-purge-request.schema.json +27 -0
  55. package/artifacts/schema/collections-purge-response.schema.json +30 -0
  56. package/artifacts/schema/collections-restore-preview-query.schema.json +20 -0
  57. package/artifacts/schema/collections-restore-preview.schema.json +581 -0
  58. package/artifacts/schema/collections-restore-request.schema.json +27 -0
  59. package/artifacts/schema/collections-restore-response.schema.json +455 -0
  60. package/artifacts/schema/collections-schema-preview.schema.json +519 -0
  61. package/artifacts/schema/collections-schema-published.schema.json +22 -0
  62. package/artifacts/schema/collections-version-response.schema.json +869 -0
  63. package/artifacts/schema/collections-version-summary.schema.json +139 -0
  64. package/artifacts/schema/collections-versions-response.schema.json +167 -0
  65. package/artifacts/schema/config-draft-response.schema.json +359 -5
  66. package/artifacts/schema/config-version-response.schema.json +359 -5
  67. package/artifacts/schema/confirmation-required-details.schema.json +13 -0
  68. package/artifacts/schema/create-app-invitation-request.schema.json +4 -4
  69. package/artifacts/schema/create-app-user-request.schema.json +4 -4
  70. package/artifacts/schema/create-org-role-request.schema.json +33 -0
  71. package/artifacts/schema/dangling-reference.schema.json +130 -0
  72. package/artifacts/schema/datapoint-config.schema.json +35 -0
  73. package/artifacts/schema/datapoint-list-response.schema.json +2 -2
  74. package/artifacts/schema/effective-capability.schema.json +104 -0
  75. package/artifacts/schema/effective-rights-query.schema.json +19 -0
  76. package/artifacts/schema/effective-rights-response.schema.json +396 -0
  77. package/artifacts/schema/effective-robot-rights.schema.json +266 -0
  78. package/artifacts/schema/invoke-or-service-response.schema.json +1 -1
  79. package/artifacts/schema/invoke-response.schema.json +1 -1
  80. package/artifacts/schema/journal-intent-v2-body.schema.json +74 -0
  81. package/artifacts/schema/journal-intent-v2.schema.json +84 -0
  82. package/artifacts/schema/journal-replay-receipt-body.schema.json +1 -0
  83. package/artifacts/schema/journal-replay-receipt.schema.json +1 -0
  84. package/artifacts/schema/mcp-robot-datasheet.schema.json +2 -2
  85. package/artifacts/schema/org-plan.schema.json +29 -1
  86. package/artifacts/schema/org-quota-usage-counts.schema.json +18 -0
  87. package/artifacts/schema/org-quota-usage.schema.json +47 -1
  88. package/artifacts/schema/org-quotas.schema.json +29 -1
  89. package/artifacts/schema/org-role-list-response.schema.json +206 -0
  90. package/artifacts/schema/org-role.schema.json +59 -0
  91. package/artifacts/schema/patch-app-user-request.schema.json +4 -4
  92. package/artifacts/schema/patch-org-role-request.schema.json +38 -0
  93. package/artifacts/schema/plan-catalogue-entry.schema.json +29 -1
  94. package/artifacts/schema/plan-change-decision-status-response.schema.json +29 -1
  95. package/artifacts/schema/plan-limit-details.schema.json +3 -1
  96. package/artifacts/schema/plan-limits.schema.json +29 -1
  97. package/artifacts/schema/plan-overrides.schema.json +26 -0
  98. package/artifacts/schema/publish-config-response.schema.json +48 -1
  99. package/artifacts/schema/publisher-config.schema.json +35 -0
  100. package/artifacts/schema/put-collections-draft-request.schema.json +16 -0
  101. package/artifacts/schema/put-role-defaults-request.schema.json +127 -0
  102. package/artifacts/schema/record-changed-details.schema.json +16 -0
  103. package/artifacts/schema/record-referenced-details.schema.json +35 -0
  104. package/artifacts/schema/reference-invalid-details.schema.json +25 -0
  105. package/artifacts/schema/reference-place.schema.json +112 -0
  106. package/artifacts/schema/rename-slug-response.schema.json +4 -3
  107. package/artifacts/schema/required-missing-details.schema.json +19 -0
  108. package/artifacts/schema/robot-config-doc.schema.json +359 -5
  109. package/artifacts/schema/robot-deletion-summary.schema.json +33 -1
  110. package/artifacts/schema/role-collection-rights.schema.json +82 -0
  111. package/artifacts/schema/role-cycle-details.schema.json +18 -0
  112. package/artifacts/schema/role-defaults-response.schema.json +59 -0
  113. package/artifacts/schema/role-rename-preview.schema.json +147 -0
  114. package/artifacts/schema/service-config.schema.json +35 -0
  115. package/artifacts/schema/slug-usage-response.schema.json +4 -3
  116. package/artifacts/schema/update-app-request.schema.json +0 -12
  117. package/dist/app-users.d.ts +11 -8
  118. package/dist/app-users.js +18 -10
  119. package/dist/apps.d.ts +258 -71
  120. package/dist/apps.js +215 -151
  121. package/dist/audit.d.ts +35 -5
  122. package/dist/audit.js +40 -2
  123. package/dist/billing.d.ts +10 -10
  124. package/dist/client-auth.d.ts +5 -2
  125. package/dist/client-auth.js +11 -3
  126. package/dist/client-robots.d.ts +1 -1
  127. package/dist/client-robots.js +1 -1
  128. package/dist/collections-issues.d.ts +67 -0
  129. package/dist/collections-issues.js +340 -0
  130. package/dist/collections-records.d.ts +562 -0
  131. package/dist/collections-records.js +525 -0
  132. package/dist/collections-rights.d.ts +17 -0
  133. package/dist/collections-rights.js +41 -0
  134. package/dist/collections-schema.d.ts +1064 -0
  135. package/dist/collections-schema.js +386 -0
  136. package/dist/collections-templates.d.ts +55 -0
  137. package/dist/collections-templates.js +137 -0
  138. package/dist/collections-transfer.d.ts +101 -0
  139. package/dist/collections-transfer.js +206 -0
  140. package/dist/collections-values.d.ts +67 -0
  141. package/dist/collections-values.js +284 -0
  142. package/dist/collections.d.ts +568 -0
  143. package/dist/collections.js +332 -0
  144. package/dist/common.d.ts +1 -1
  145. package/dist/common.js +1 -1
  146. package/dist/config-issues.d.ts +1 -1
  147. package/dist/config-issues.js +1 -1
  148. package/dist/config.d.ts +78 -12
  149. package/dist/config.js +109 -15
  150. package/dist/errors.d.ts +63 -5
  151. package/dist/errors.js +152 -24
  152. package/dist/index.d.ts +40 -14
  153. package/dist/index.js +27 -7
  154. package/dist/journal-intents.d.ts +30 -0
  155. package/dist/journal-intents.js +3 -0
  156. package/dist/legal-acceptances.d.ts +2 -2
  157. package/dist/mcp.d.ts +5 -37
  158. package/dist/mcp.js +4 -14
  159. package/dist/org-roles.d.ts +186 -0
  160. package/dist/org-roles.js +199 -0
  161. package/dist/plans.d.ts +25 -7
  162. package/dist/plans.js +21 -1
  163. package/dist/protocol.d.ts +19 -1
  164. package/dist/realtime.d.ts +172 -3
  165. package/dist/realtime.js +164 -1
  166. package/dist/rest.d.ts +104 -53
  167. package/dist/rest.js +79 -55
  168. package/dist/rights.d.ts +197 -0
  169. package/dist/rights.js +396 -0
  170. package/dist/routes.d.ts +1 -1
  171. package/dist/routes.js +675 -150
  172. package/package.json +1 -1
  173. package/artifacts/schema/mcp-role-preview-response.schema.json +0 -134
  174. package/artifacts/schema/put-robot-details-request.schema.json +0 -41
  175. package/artifacts/schema/put-robot-details-response.schema.json +0 -43
  176. package/artifacts/schema/robot-details-doc.schema.json +0 -33
  177. package/artifacts/schema/role-delete-query.schema.json +0 -13
  178. package/artifacts/schema/role-in-use-details.schema.json +0 -28
  179. package/artifacts/schema/role-list-response.schema.json +0 -48
  180. package/artifacts/schema/role-permissions.schema.json +0 -61
  181. package/artifacts/schema/role-rename-request.schema.json +0 -16
  182. package/artifacts/schema/role.schema.json +0 -35
package/dist/rights.js ADDED
@@ -0,0 +1,396 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ import { z } from 'zod';
3
+ /**
4
+ * Roles and the rights they resolve to — the part that has to be one piece of
5
+ * code for every layer that asks.
6
+ *
7
+ * A role is a key in the organization. Which role may use which capability of
8
+ * a robot is written in that robot's `fleetless.yaml` (`role` on an entry,
9
+ * `access.rules`, `access.assets`, `access.job_history`) and falls back to a
10
+ * default per kind that the organization sets. The cloud answers "may this
11
+ * caller do that?" from these functions, the console editor answers "what
12
+ * does this document say about roles?" from the same ones, and an assistant
13
+ * can read the same sentences back — a second implementation would be a
14
+ * second account of who may do what, which is the one thing that must not
15
+ * drift.
16
+ *
17
+ * **A leaf module on purpose.** It imports only types from `config.ts` and
18
+ * `config-issues.ts`, so `config.ts` can import the zod parts below (`roleRef`)
19
+ * without a cycle. Nothing here parses a document; every function takes one
20
+ * the caller has already validated, or `null` for none.
21
+ *
22
+ * **Lenient by construction.** A role key that names no role is never an
23
+ * error: it grants nothing, and `roleIssues` says so as a warning. Inheritance
24
+ * through a missing parent lapses; the role's other parents still count.
25
+ */
26
+ /* ------------------------------------------------------------- role keys */
27
+ /**
28
+ * A role key: lower-case, letter-initial, letters, digits and underscores, at
29
+ * most 63 characters. `defaults` is reserved: it is the path segment of the
30
+ * organization's defaults (`/api/org/roles/defaults`), and a role with that
31
+ * key would sit next to it. The rule is in the pattern, so the JSON Schema
32
+ * artifacts state it too.
33
+ */
34
+ export const ROLE_KEY_PATTERN = /^(?!defaults$)[a-z][a-z0-9_]{0,62}$/;
35
+ export const ROLE_KEY_RULE = 'A role key is lower-case: it starts with a letter and continues with letters, digits and underscores, up to 63 characters in all — `operate`, `night_shift`. A capital, a dash, a dot, a space or a leading digit is refused, and `defaults` is reserved.';
36
+ /** The most roles one organization holds, and the most keys a single `role` list names. */
37
+ export const MAX_ORG_ROLES = 50;
38
+ /** The longest display name of a role. */
39
+ export const ROLE_NAME_MAX = 60;
40
+ /**
41
+ * One role key. The sentence is both what the parser says and the
42
+ * `patternErrorMessage` of the exported JSON Schema: one rule, one wording.
43
+ */
44
+ export const roleKey = z.string()
45
+ .regex(ROLE_KEY_PATTERN, ROLE_KEY_RULE)
46
+ .meta({ patternErrorMessage: ROLE_KEY_RULE });
47
+ /**
48
+ * The value of a `role` in `fleetless.yaml`: one key, or a list of keys when
49
+ * several roles qualify. A list is non-empty — an empty one would say "nobody",
50
+ * which is what leaving the role out of every rule and default already does.
51
+ */
52
+ export const roleRef = z.union([
53
+ roleKey,
54
+ z.array(roleKey)
55
+ .min(1, 'A list of roles names at least one role.')
56
+ .max(MAX_ORG_ROLES, `A list of roles names at most ${MAX_ORG_ROLES} roles.`),
57
+ ], {
58
+ error: (issue) => (issue.code === 'invalid_union'
59
+ ? 'A role is one key, `operate`, or a list of keys, `[operate, admin]`.'
60
+ : undefined),
61
+ }).meta({
62
+ defaultSnippets: [
63
+ {
64
+ label: 'one role',
65
+ description: 'The role that may use this; a role that inherits from it may too.',
66
+ body: '${1:operate}',
67
+ },
68
+ {
69
+ label: 'any of several roles',
70
+ description: 'A list of roles; holding one of them, or inheriting from one, is enough.',
71
+ body: ['${1:operate}', '${2:admin}'],
72
+ },
73
+ ],
74
+ });
75
+ /* ----------------------------------------------------------------- kinds */
76
+ /**
77
+ * The kinds of access a default or a lock can name. `read` covers datapoints
78
+ * and cameras, `invoke` services and actions, `publish` publishers (driving);
79
+ * `assets` and `job_history` belong to the robot as a whole.
80
+ */
81
+ export const ACCESS_KINDS = ['read', 'invoke', 'publish', 'assets', 'job_history'];
82
+ export const accessKind = z.enum(ACCESS_KINDS);
83
+ /** The kinds an entry of `fleetless.yaml` has, and the only ones a rule can filter by. */
84
+ export const ENTRY_KINDS = ['read', 'invoke', 'publish'];
85
+ /* ------------------------------------------------------------------ seed */
86
+ /** What a new organization starts with. Roles are ordinary rows from then on. */
87
+ export const SEED_ROLES = [
88
+ { key: 'observe', name: 'Observe', inherits: [] },
89
+ { key: 'operate', name: 'Operate', inherits: ['observe'] },
90
+ { key: 'admin', name: 'Admin', inherits: ['operate'] },
91
+ ];
92
+ /** The defaults a new organization starts with: who holds each kind when a robot's document says nothing. */
93
+ export const SEED_DEFAULTS = {
94
+ read: ['observe'],
95
+ invoke: ['operate'],
96
+ publish: ['operate'],
97
+ assets: ['observe'],
98
+ job_history: ['operate'],
99
+ };
100
+ /**
101
+ * The key and every role it inherits from, transitively. Parents that are no
102
+ * role are skipped, and so is whatever only they would have led to. `[]` for a
103
+ * key that is no role. A cycle in the graph ends the walk; each key appears once.
104
+ */
105
+ export function roleClosure(graph, key) {
106
+ if (!graph.has(key))
107
+ return [];
108
+ const out = [key];
109
+ const seen = new Set(out);
110
+ for (let i = 0; i < out.length; i += 1) {
111
+ for (const parent of graph.get(out[i]).inherits) {
112
+ if (graph.has(parent) && !seen.has(parent)) {
113
+ seen.add(parent);
114
+ out.push(parent);
115
+ }
116
+ }
117
+ }
118
+ return out;
119
+ }
120
+ /**
121
+ * Whether the held role meets a requirement: it, or any role it inherits from,
122
+ * is named. `via` is the required key that was met when that is not the held
123
+ * role itself, so a caller can say "admin, through operate": `null` when the
124
+ * held role is named, otherwise the nearest required ancestor, in the order
125
+ * `roleClosure` walks (the parents of the held role first, and in the order it
126
+ * lists them), whatever the order of `required`. `unknown` lists the required
127
+ * keys that are no role — they were skipped, not failed.
128
+ *
129
+ * A held role that is unknown or `null`, and an empty requirement, never
130
+ * satisfy: nobody holds what nobody was given.
131
+ */
132
+ export function roleSatisfies(graph, held, required) {
133
+ const unknown = [...new Set(required.filter((key) => !graph.has(key)))];
134
+ if (held === null || required.length === 0)
135
+ return { satisfied: false, via: null, unknown };
136
+ const wanted = new Set(required);
137
+ const met = roleClosure(graph, held).find((key) => wanted.has(key));
138
+ if (met === undefined)
139
+ return { satisfied: false, via: null, unknown };
140
+ return { satisfied: true, via: met === held ? null : met, unknown };
141
+ }
142
+ /**
143
+ * The cycle that giving `key` these parents would create, as `[key, …, key]`,
144
+ * or `null`. The graph is taken as it is with that one edit applied, so it
145
+ * answers for a role that does not exist yet as well. Cycles that do not pass
146
+ * through `key` are not its doing and are ignored.
147
+ */
148
+ export function findCycle(graph, key, inherits) {
149
+ const parentsOf = (node) => (node === key ? inherits : graph.get(node)?.inherits ?? []);
150
+ const path = [key];
151
+ const dead = new Set();
152
+ const visit = (node) => {
153
+ for (const parent of parentsOf(node)) {
154
+ if (parent === key)
155
+ return [...path, key];
156
+ if (dead.has(parent) || path.includes(parent))
157
+ continue;
158
+ path.push(parent);
159
+ const found = visit(parent);
160
+ if (found !== null)
161
+ return found;
162
+ path.pop();
163
+ dead.add(parent);
164
+ }
165
+ return null;
166
+ };
167
+ return visit(key);
168
+ }
169
+ /* ----------------------------------------------------------- exposures */
170
+ /** The kind of access an entry of each section needs. Declared as a record so a sixth section cannot be forgotten. */
171
+ const ENTRY_KIND_OF = {
172
+ datapoints: 'read',
173
+ actions: 'invoke',
174
+ services: 'invoke',
175
+ publishers: 'publish',
176
+ cameras: 'read',
177
+ };
178
+ const SECTIONS = Object.keys(ENTRY_KIND_OF);
179
+ const asRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value) ? value : null;
180
+ const asString = (value) => (typeof value === 'string' ? value : null);
181
+ /**
182
+ * The ROS name an entry is matched by: the `topic` of a datapoint or a
183
+ * publisher, the `ros_name` of a service or an action, the `source.topic` of a
184
+ * camera whose source is a ROS topic. `null` for every other camera — an
185
+ * address on the network or a device is not a ROS name, so no rule can fit it.
186
+ */
187
+ function rosNameOf(section, entry) {
188
+ const record = asRecord(entry);
189
+ if (record === null)
190
+ return null;
191
+ switch (section) {
192
+ case 'datapoints':
193
+ case 'publishers':
194
+ return asString(record.topic);
195
+ case 'services':
196
+ case 'actions':
197
+ return asString(record.ros_name);
198
+ case 'cameras': {
199
+ const source = asRecord(record.source);
200
+ return source !== null && source.kind === 'ros' ? asString(source.topic) : null;
201
+ }
202
+ }
203
+ }
204
+ /** Whether one name segment fits a pattern segment: `*` stands for any run of characters, none included. */
205
+ function segmentMatches(pattern, segment) {
206
+ if (pattern === '*')
207
+ return segment.length > 0;
208
+ let p = 0;
209
+ let s = 0;
210
+ let star = -1;
211
+ let mark = 0;
212
+ while (s < segment.length) {
213
+ if (p < pattern.length && pattern[p] === '*') {
214
+ star = p;
215
+ p += 1;
216
+ mark = s;
217
+ }
218
+ else if (p < pattern.length && pattern[p] === segment[s]) {
219
+ p += 1;
220
+ s += 1;
221
+ }
222
+ else if (star !== -1) {
223
+ p = star + 1;
224
+ mark += 1;
225
+ s = mark;
226
+ }
227
+ else {
228
+ return false;
229
+ }
230
+ }
231
+ while (p < pattern.length && pattern[p] === '*')
232
+ p += 1;
233
+ return p === pattern.length;
234
+ }
235
+ /**
236
+ * Whether an absolute match pattern fits an absolute ROS name. A segment of
237
+ * `*` is any one segment; a segment of `**` is any number of segments, none
238
+ * included, so `/arm/**` fits `/arm` as well as `/arm/home`; `*` inside a
239
+ * segment (`/joint_*`) stands for any characters of that segment. Anything
240
+ * relative, with an empty segment or with a trailing slash fits nothing.
241
+ */
242
+ export function matchRosGlob(pattern, rosName) {
243
+ if (!pattern.startsWith('/') || !rosName.startsWith('/'))
244
+ return false;
245
+ const parts = pattern.slice(1).split('/');
246
+ const names = rosName.slice(1).split('/');
247
+ if (parts.includes('') || names.includes(''))
248
+ return false;
249
+ // fits[i][j]: parts[i..] fit names[j..]. Filled from the end, so `**` costs a scan, not a branch per attempt.
250
+ const fits = Array.from({ length: parts.length + 1 }, () => new Array(names.length + 1).fill(false));
251
+ fits[parts.length][names.length] = true;
252
+ for (let i = parts.length - 1; i >= 0; i -= 1) {
253
+ for (let j = names.length; j >= 0; j -= 1) {
254
+ if (parts[i] === '**') {
255
+ fits[i][j] = fits[i + 1][j] || (j < names.length && fits[i][j + 1]);
256
+ }
257
+ else {
258
+ fits[i][j] = j < names.length && segmentMatches(parts[i], names[j]) && fits[i + 1][j + 1];
259
+ }
260
+ }
261
+ }
262
+ return fits[0][0];
263
+ }
264
+ /** Whether an `access.rules` rule applies to an entry of this kind and ROS name: its `kinds` include the kind when it names any, and its glob fits. */
265
+ function ruleFits(rule, kind, rosName) {
266
+ return (rule.kinds === undefined || rule.kinds.includes(kind)) && matchRosGlob(rule.match, rosName);
267
+ }
268
+ /** The entries of one section, in document order. A prototype key is never an entry. */
269
+ function entriesOf(doc, section) {
270
+ const table = asRecord(doc[section]);
271
+ return table === null ? [] : Object.entries(table).map(([slug, entry]) => [slug, (asRecord(entry) ?? {})]);
272
+ }
273
+ /** Every exposed entry of the document, section by section in the order of `EXPOSURE_SECTIONS`. `[]` for no document. */
274
+ export function listExposures(doc) {
275
+ if (doc === null)
276
+ return [];
277
+ return SECTIONS.flatMap((section) => entriesOf(doc, section).map(([slug, entry]) => ({
278
+ slug,
279
+ section,
280
+ kind: ENTRY_KIND_OF[section],
281
+ rosName: rosNameOf(section, entry),
282
+ })));
283
+ }
284
+ const keysOf = (value) => (typeof value === 'string' ? [value] : [...value]);
285
+ /**
286
+ * What a robot's published document, with the organization's defaults, asks of
287
+ * a caller for one target.
288
+ *
289
+ * For an entry: its own `role`, else the first `access.rules` rule that fits
290
+ * (by ROS name, and by kind when the rule names kinds), else the default of its
291
+ * kind. For `assets` and `job_history`: `access.<capability>` of the document,
292
+ * else the default. A kind with no default asks for nobody (`roles: []`).
293
+ *
294
+ * `null` when the document has no entry of that slug — including no document
295
+ * at all, which has no entries. A capability always has a requirement.
296
+ */
297
+ export function requirementOf(doc, defaults, target) {
298
+ if ('capability' in target) {
299
+ const own = doc?.access?.[target.capability];
300
+ return own !== undefined
301
+ ? { roles: keysOf(own), source: { kind: 'robot' } }
302
+ : { roles: [...(defaults[target.capability] ?? [])], source: { kind: 'org_default' } };
303
+ }
304
+ if (doc === null)
305
+ return null;
306
+ for (const section of SECTIONS) {
307
+ // By key, not by scanning the section: asked once per exposure of a robot, a scan makes a document quadratic in its entries.
308
+ const table = asRecord(doc[section]);
309
+ if (table === null || !Object.hasOwn(table, target.slug))
310
+ continue;
311
+ const entry = (asRecord(table[target.slug]) ?? {});
312
+ if (entry.role !== undefined)
313
+ return { roles: keysOf(entry.role), source: { kind: 'entry' } };
314
+ const kind = ENTRY_KIND_OF[section];
315
+ const rosName = rosNameOf(section, entry);
316
+ const rules = doc.access?.rules ?? [];
317
+ if (rosName !== null) {
318
+ for (const [index, rule] of rules.entries()) {
319
+ if (ruleFits(rule, kind, rosName)) {
320
+ return { roles: keysOf(rule.role), source: { kind: 'rule', index, match: rule.match } };
321
+ }
322
+ }
323
+ }
324
+ return { roles: [...(defaults[kind] ?? [])], source: { kind: 'org_default' } };
325
+ }
326
+ return null;
327
+ }
328
+ /** Every role key the document names: on entries, in rules, and on `access.assets` and `access.job_history`. */
329
+ export function collectRoleRefs(doc) {
330
+ if (doc === null)
331
+ return [];
332
+ const refs = [];
333
+ const add = (value, path, slug) => {
334
+ if (value === undefined)
335
+ return;
336
+ if (typeof value === 'string')
337
+ refs.push({ key: value, path, slug });
338
+ else
339
+ value.forEach((key, i) => refs.push({ key, path: `${path}[${i}]`, slug }));
340
+ };
341
+ for (const section of SECTIONS) {
342
+ for (const [slug, entry] of entriesOf(doc, section))
343
+ add(entry.role, `${section}.${slug}.role`, slug);
344
+ }
345
+ doc.access?.rules?.forEach((rule, i) => add(rule.role, `access.rules[${i}].role`, null));
346
+ add(doc.access?.assets, 'access.assets', null);
347
+ add(doc.access?.job_history, 'access.job_history', null);
348
+ return refs;
349
+ }
350
+ /** What a reference belongs to, for a sentence: the entry, the rule, or the robot-level key. */
351
+ const subjectOf = (path) => {
352
+ const onField = path.replace(/\.role(?:\[\d+\])?$/, '');
353
+ return onField !== path ? onField : path.replace(/\[\d+\]$/, '');
354
+ };
355
+ /**
356
+ * The warnings a document earns against the organization's roles. Never an
357
+ * error: both are published as they are, and the sentences are the ones the
358
+ * cloud answers and the editor shows.
359
+ *
360
+ * - `unknown_role`: a key that is no role of the organization. It grants
361
+ * nothing, and neither does inheritance through it.
362
+ * - `rule_matches_nothing`: an `access.rules` rule that fits no exposed entry
363
+ * of this document, so it changes nothing. A rule's `kinds` are honoured,
364
+ * and an entry without a ROS name — a camera that is not a ROS topic — is
365
+ * never matched. The built-in datapoint, `bridge_state`, is not an entry.
366
+ */
367
+ export function roleIssues(doc, graph) {
368
+ if (doc === null)
369
+ return [];
370
+ const issues = [];
371
+ for (const ref of collectRoleRefs(doc)) {
372
+ if (graph.has(ref.key))
373
+ continue;
374
+ issues.push({
375
+ path: ref.path,
376
+ slug: ref.slug,
377
+ code: 'unknown_role',
378
+ message: `${subjectOf(ref.path)} names \`${ref.key}\`, which is no role of this organization.`,
379
+ severity: 'warning',
380
+ });
381
+ }
382
+ const exposures = listExposures(doc);
383
+ for (const [index, rule] of (doc.access?.rules ?? []).entries()) {
384
+ const fits = exposures.some((exposure) => exposure.rosName !== null && ruleFits(rule, exposure.kind, exposure.rosName));
385
+ if (fits)
386
+ continue;
387
+ issues.push({
388
+ path: `access.rules[${index}].match`,
389
+ slug: null,
390
+ code: 'rule_matches_nothing',
391
+ message: `Rule ${index + 1} matches nothing, so it changes nothing.`,
392
+ severity: 'warning',
393
+ });
394
+ }
395
+ return issues;
396
+ }
package/dist/routes.d.ts CHANGED
@@ -36,7 +36,7 @@ export type RouteAudience = 'developer' | 'client' | 'internal';
36
36
  */
37
37
  export type RouteAuth = 'developer' | 'developer_or_client' | 'none' | 'robot_upload' | 'in_handler' | 'ops';
38
38
  export type RouteTransport = 'http' | 'websocket';
39
- export type RouteSection = 'health' | 'developer-auth' | 'client-auth' | 'org' | 'billing' | 'users' | 'apps' | 'robots' | 'config' | 'alerts' | 'commands' | 'cameras' | 'assets' | 'mcp' | 'transports';
39
+ export type RouteSection = 'health' | 'developer-auth' | 'client-auth' | 'org' | 'billing' | 'users' | 'apps' | 'collections' | 'robots' | 'config' | 'alerts' | 'commands' | 'cameras' | 'assets' | 'mcp' | 'transports';
40
40
  export interface RouteParam {
41
41
  readonly name: string;
42
42
  /** One sentence: what the segment identifies and where a caller gets it. */