@fuzdev/fuz_app 0.181.0 → 0.182.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 (114) hide show
  1. package/dist/actions/CLAUDE.md +24 -6
  2. package/dist/actions/action_rpc.d.ts +11 -1
  3. package/dist/actions/action_rpc.d.ts.map +1 -1
  4. package/dist/actions/action_rpc.js +3 -2
  5. package/dist/actions/action_types.d.ts +16 -1
  6. package/dist/actions/action_types.d.ts.map +1 -1
  7. package/dist/actions/action_types.js +1 -1
  8. package/dist/actions/notification_sender.d.ts +83 -0
  9. package/dist/actions/notification_sender.d.ts.map +1 -0
  10. package/dist/actions/notification_sender.js +85 -0
  11. package/dist/actions/perform_action.d.ts +9 -0
  12. package/dist/actions/perform_action.d.ts.map +1 -1
  13. package/dist/actions/perform_action.js +22 -13
  14. package/dist/actions/register_action_ws.d.ts +14 -0
  15. package/dist/actions/register_action_ws.d.ts.map +1 -1
  16. package/dist/actions/register_action_ws.js +3 -2
  17. package/dist/actions/ws_socket_session.d.ts +3 -0
  18. package/dist/actions/ws_socket_session.d.ts.map +1 -1
  19. package/dist/actions/ws_socket_session.js +2 -1
  20. package/dist/auth/CLAUDE.md +60 -41
  21. package/dist/auth/account_actions.d.ts.map +1 -1
  22. package/dist/auth/account_actions.js +13 -3
  23. package/dist/auth/account_queries.d.ts +158 -16
  24. package/dist/auth/account_queries.d.ts.map +1 -1
  25. package/dist/auth/account_queries.js +189 -18
  26. package/dist/auth/account_routes.d.ts +1 -1
  27. package/dist/auth/account_routes.d.ts.map +1 -1
  28. package/dist/auth/account_routes.js +27 -26
  29. package/dist/auth/admin_actions.d.ts.map +1 -1
  30. package/dist/auth/admin_actions.js +71 -28
  31. package/dist/auth/api_token_queries.d.ts +13 -5
  32. package/dist/auth/api_token_queries.d.ts.map +1 -1
  33. package/dist/auth/api_token_queries.js +24 -9
  34. package/dist/auth/bearer_auth.d.ts.map +1 -1
  35. package/dist/auth/bearer_auth.js +3 -2
  36. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  37. package/dist/auth/daemon_token_middleware.js +12 -1
  38. package/dist/auth/deps.d.ts +10 -0
  39. package/dist/auth/deps.d.ts.map +1 -1
  40. package/dist/auth/request_context.d.ts +73 -73
  41. package/dist/auth/request_context.d.ts.map +1 -1
  42. package/dist/auth/request_context.js +78 -96
  43. package/dist/auth/resolved_auth.d.ts +10 -5
  44. package/dist/auth/resolved_auth.d.ts.map +1 -1
  45. package/dist/auth/resolved_auth.js +2 -2
  46. package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
  47. package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
  48. package/dist/auth/role_grant_offer_action_specs.js +2 -1
  49. package/dist/auth/role_grant_offer_actions.d.ts +7 -11
  50. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  51. package/dist/auth/role_grant_offer_actions.js +61 -93
  52. package/dist/auth/role_grant_offer_notifications.d.ts +87 -38
  53. package/dist/auth/role_grant_offer_notifications.d.ts.map +1 -1
  54. package/dist/auth/role_grant_offer_notifications.js +75 -22
  55. package/dist/auth/role_grant_offer_queries.d.ts +96 -15
  56. package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
  57. package/dist/auth/role_grant_offer_queries.js +185 -70
  58. package/dist/auth/role_grant_offer_schema.js +1 -1
  59. package/dist/auth/role_grant_queries.d.ts +52 -12
  60. package/dist/auth/role_grant_queries.d.ts.map +1 -1
  61. package/dist/auth/role_grant_queries.js +75 -24
  62. package/dist/auth/role_grant_sql.d.ts +21 -0
  63. package/dist/auth/role_grant_sql.d.ts.map +1 -1
  64. package/dist/auth/role_grant_sql.js +21 -0
  65. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  66. package/dist/auth/self_service_role_actions.js +9 -2
  67. package/dist/auth/session_queries.d.ts +34 -13
  68. package/dist/auth/session_queries.d.ts.map +1 -1
  69. package/dist/auth/session_queries.js +48 -18
  70. package/dist/auth/standard_rpc_actions.d.ts +6 -19
  71. package/dist/auth/standard_rpc_actions.d.ts.map +1 -1
  72. package/dist/auth/standard_rpc_actions.js +5 -6
  73. package/dist/auth/token_scope.d.ts +1 -1
  74. package/dist/auth/token_scope.js +1 -1
  75. package/dist/http/CLAUDE.md +7 -6
  76. package/dist/http/auth_shape.d.ts +1 -1
  77. package/dist/http/error_schemas.d.ts +9 -21
  78. package/dist/http/error_schemas.d.ts.map +1 -1
  79. package/dist/http/error_schemas.js +14 -24
  80. package/dist/http/route_spec.d.ts +2 -2
  81. package/dist/server/app_backend.d.ts.map +1 -1
  82. package/dist/server/app_backend.js +3 -1
  83. package/dist/server/app_server.d.ts +26 -16
  84. package/dist/server/app_server.d.ts.map +1 -1
  85. package/dist/server/app_server.js +17 -7
  86. package/dist/server/serve_fact_route.d.ts +3 -3
  87. package/dist/server/serve_fact_route.js +12 -12
  88. package/dist/testing/CLAUDE.md +5 -5
  89. package/dist/testing/app_server.d.ts.map +1 -1
  90. package/dist/testing/app_server.js +3 -1
  91. package/dist/testing/auth_apps.js +1 -1
  92. package/dist/testing/cross_backend/account_lifecycle.d.ts.map +1 -1
  93. package/dist/testing/cross_backend/account_lifecycle.js +43 -0
  94. package/dist/testing/cross_backend/default_spine_surface.d.ts +1 -26
  95. package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
  96. package/dist/testing/cross_backend/default_spine_surface.js +2 -6
  97. package/dist/testing/cross_backend/full_spine_mount.d.ts +3 -10
  98. package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
  99. package/dist/testing/cross_backend/full_spine_mount.js +4 -4
  100. package/dist/testing/cross_backend/role_grant_offer_notification_ws.d.ts +5 -4
  101. package/dist/testing/cross_backend/role_grant_offer_notification_ws.d.ts.map +1 -1
  102. package/dist/testing/cross_backend/role_grant_offer_notification_ws.js +183 -30
  103. package/dist/testing/cross_backend/session_cap.d.ts.map +1 -1
  104. package/dist/testing/cross_backend/session_cap.js +26 -5
  105. package/dist/testing/middleware.d.ts +1 -1
  106. package/dist/testing/middleware.js +5 -5
  107. package/dist/testing/resolved_auth.d.ts +3 -1
  108. package/dist/testing/resolved_auth.d.ts.map +1 -1
  109. package/dist/testing/resolved_auth.js +4 -4
  110. package/dist/testing/stubs.d.ts.map +1 -1
  111. package/dist/testing/stubs.js +5 -2
  112. package/dist/testing/ws_round_trip.d.ts.map +1 -1
  113. package/dist/testing/ws_round_trip.js +8 -4
  114. package/package.json +1 -1
@@ -115,6 +115,18 @@ export interface RevokeRoleGrantResult {
115
115
  * offer becomes terminal at revoke time. A fresh post-revoke grant
116
116
  * requires the grantor to call `query_role_grant_offer_create` again.
117
117
  *
118
+ * The revokee's account row is locked first (`FOR NO KEY UPDATE`, the lock
119
+ * `query_account_lock_for_revoke` takes), so no offer to or from the account is
120
+ * made, and none accepted, while the revoke runs: the offers it locks are the
121
+ * offers it writes. Then those offers, in id order, before the grant is
122
+ * updated: an accept claims its offers before it inserts its grant, and that
123
+ * insert waits on an uncommitted update of the same grant, so the grant first
124
+ * and the offers second would close a cycle with it. A caller that supersedes
125
+ * the revokee's outgoing offers too (`role_grant_revoke`) takes the account row
126
+ * and then every offer it writes in one statement first
127
+ * (`query_role_grant_offer_lock_for_revoke`); the locks here then find rows
128
+ * the transaction already holds.
129
+ *
118
130
  * @param deps - query dependencies
119
131
  * @param role_grant_id - the role_grant to revoke
120
132
  * @param actor_id - the actor that must own the role_grant
@@ -159,7 +171,7 @@ export declare const query_account_has_global_role: (deps: QueryDeps, account_id
159
171
  * Like `query_account_has_global_role`, but only counts the grant when the
160
172
  * **account and the granting actor are both active** (`deleted_at IS NULL`).
161
173
  * Used by the last-admin branch of the removability guard: a soft-deleted admin
162
- * can't log in and is excluded from `query_count_active_accounts_with_global_role_for_update`,
174
+ * can't log in and is excluded from `query_count_active_accounts_with_global_role_locked`,
163
175
  * so the guard must use the same active predicate when testing whether the
164
176
  * *target* is an admin — otherwise removing an already-tombstoned admin is
165
177
  * falsely blocked as `cannot_delete_last_admin`. Soft-delete does **not** revoke
@@ -205,16 +217,37 @@ export declare const query_actor_has_active_global_role: (deps: QueryDeps, actor
205
217
  * guard could fail-open and let the last usable admin be removed).
206
218
  *
207
219
  * **Locks** every counted `role_grant`, `actor`, and `account` row
208
- * (`FOR UPDATE`) until the caller's transaction ends, so two guards racing to
209
- * remove the last two admins serialize: the second waits for the first to
210
- * commit, re-reads the rows it changed, and counts one. Run it inside the
211
- * transaction that does the removal. Twin of the Rust
212
- * `query_count_active_accounts_with_global_role_for_update`.
220
+ * `FOR NO KEY UPDATE` until the caller's transaction ends. That conflicts with
221
+ * another guard, so two guards racing to remove the last two admins serialize:
222
+ * the second waits for the first to commit, re-reads the rows it changed, and
223
+ * counts one. It conflicts with every write that lowers the count — a revoke's
224
+ * or a soft delete's `UPDATE` of a counted row, a purge's `DELETE` — and with
225
+ * the locks a credential mint, an offer, an invite and an offer accept (its
226
+ * recipient's row and its grantor's, `query_account_lock_for_offer_accept`)
227
+ * take on the account row. Run it inside the transaction that does the
228
+ * removal.
229
+ *
230
+ * It does not conflict with a foreign key's `FOR KEY SHARE` — the lock an
231
+ * insert or update naming a counted row takes (an accept's new grant naming its
232
+ * grantor, a cell's `created_by`). Such a write only adds a reference: it never
233
+ * lowers the count, so the guard fails closed — what it counted is at most what
234
+ * will stand, and a removal it allows leaves an admin. A purge that deletes a
235
+ * row such a write names still takes `FOR UPDATE` and waits for it.
236
+ *
237
+ * A purge's cascade writes admin rows too — it sets null the `granted_by` of
238
+ * every grant the purged account's actors granted, admins' grants among them,
239
+ * in the order its plan walks them — so a purge takes every admin's account
240
+ * row first, with its own, in one id-ordered statement
241
+ * (`query_account_lock_admins_for_purge`), and runs this count after it only
242
+ * when its target is an admin: another guard then waits at its first account
243
+ * holding nothing.
244
+ * Twin of the Rust `query_count_active_accounts_with_global_role_locked`.
213
245
  *
214
246
  * The rows are locked in id order — account, then actor, then grant — as the
215
247
  * sort returns them: each account's row, its actors' and its grants' before
216
- * the next account's. `query_account_lock_live_many` takes account rows in the
217
- * same order, so an offer between two admins and an admin's removal never
248
+ * the next account's. `query_account_lock_live_many` and
249
+ * `query_account_lock_for_offer_accept` take account rows in the same order,
250
+ * so an offer or an accept between two admins and an admin's removal never
218
251
  * wait on each other's rows in opposite orders. Ids never change, so no
219
252
  * concurrent update can move a row the sort has placed.
220
253
  *
@@ -230,7 +263,7 @@ export declare const query_actor_has_active_global_role: (deps: QueryDeps, actor
230
263
  * @returns the number of distinct active accounts with an active global `role` grant
231
264
  * @mutates `role_grant` / `actor` / `account` rows - row-locks the counted rows for the transaction
232
265
  */
233
- export declare const query_count_active_accounts_with_global_role_for_update: (deps: QueryDeps, role: string, exclude_role_grant_id?: Uuid | null) => Promise<number>;
266
+ export declare const query_count_active_accounts_with_global_role_locked: (deps: QueryDeps, role: string, exclude_role_grant_id?: Uuid | null) => Promise<number>;
234
267
  /**
235
268
  * Find the account ID of an account that holds an active **global** role_grant
236
269
  * for a given role.
@@ -273,7 +306,9 @@ export interface RevokeForScopeResult {
273
306
  /**
274
307
  * Every pending offer at the scope — tuple-matched and orphan, undifferentiated
275
308
  * — superseded in the same cascade. Each entry carries its grantor's
276
- * `from_account_id` for `role_grant_offer_supersede` notification fan-out.
309
+ * `from_account_id`, as every supersede cascade's does; no
310
+ * `role_grant_offer_supersede` notification carries `scope_destroyed`, so a
311
+ * scope teardown tells no one.
277
312
  *
278
313
  * The caller is responsible for emitting `role_grant_offer_supersede` audit
279
314
  * events with `reason: 'scope_destroyed'` and `cause_id: <destroyed scope row id>`
@@ -294,7 +329,12 @@ export interface RevokeForScopeResult {
294
329
  * attached to the destroyed scope is cleaned up.
295
330
  *
296
331
  * Both updates run as separate statements inside the caller's transaction,
297
- * the `role_grant` revoke first. The two halves are
332
+ * the scope's pending offers locked in id order first, then the `role_grant`
333
+ * revoke, then the supersede of the offers that lock returned — offers before
334
+ * grants, as `query_revoke_role_grant` takes them. No account row keeps a new
335
+ * offer at the scope out (the scope's offers span accounts), so the supersede
336
+ * writes only the rows it locked: an offer made at the scope after the lock is
337
+ * left pending rather than taken out of order. The two halves are
298
338
  * independent — orphan pending offers can exist at a scope with no active
299
339
  * role_grants, so the supersede half always runs even when no role_grant was
300
340
  * revoked.
@@ -305,7 +345,7 @@ export interface RevokeForScopeResult {
305
345
  * @param reason - optional free-form reason, stamped on `role_grant.revoked_reason`.
306
346
  * @returns the revoked role_grants (with `account_id` for fan-out) and superseded offers (with `from_account_id` for fan-out)
307
347
  * @mutates `role_grant` table - sets `revoked_at`/`revoked_by`/`revoked_reason` on every active row at `scope_id`
308
- * @mutates `role_grant_offer` table - stamps `superseded_at` on every pending row at `scope_id`
348
+ * @mutates `role_grant_offer` table - stamps `superseded_at` on every pending row at `scope_id` its lock took
309
349
  */
310
350
  export declare const query_role_grant_revoke_for_scope: (deps: QueryDeps, scope_id: Uuid, revoked_by: Uuid | null, reason?: string | null) => Promise<RevokeForScopeResult>;
311
351
  //# sourceMappingURL=role_grant_queries.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"role_grant_queries.d.ts","sourceRoot":"","sources":["../../src/lib/auth/role_grant_queries.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAEpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAS3E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAGpE;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,0KAaoB,CAAC;AAcpD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,uBAAuB,GACnC,MAAM,SAAS,EACf,OAAO,oBAAoB,KACzB,OAAO,CAAC,SAAS,CA8CnB,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,GAClC,MAAM,SAAS,EACf,I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AAQ,EAAE,IAAI,CAAC;QACf,UAAU,EAAE,IAAI,CAAC;KACjB,CAAC,CAAC;IACH;;;;;;;;;;;;OAYG;IACH,iBAAiB,EAAE,KAAK,CAAC,eAAe,CAAC,CAAC;CAC1C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,eAAO,MAAM,iCAAiC,GAC7C,MAAM,SAAS,EACf,UAAU,IAAI,EACd,YAAY,IAAI,GAAG,IAAI,EACvB,SAAS,MAAM,GAAG,IAAI,KACpB,OAAO,CAAC,oBAAoB,CAkD9B,CAAC"}
@@ -9,7 +9,7 @@
9
9
  import { assert_row } from '../db/assert_row.js';
10
10
  import { columns_sql, iso8601_timestamp_expr, qualify_columns } from '../db/sql_columns.js';
11
11
  import { ROLE_GRANT_OFFER_COLUMNS, ROLE_GRANT_OFFER_SCOPE_KIND_GLOBAL_TOKEN, ROLE_GRANT_OFFER_SCOPE_SENTINEL_UUID, ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT } from './role_grant_offer_ddl.js';
12
- import { offer_pending_predicate, role_grant_active_sql } from './role_grant_sql.js';
12
+ import { offer_pending_predicate, offer_rows_locked_in_id_order, role_grant_active_sql } from './role_grant_sql.js';
13
13
  /**
14
14
  * The full `role_grant` column set, named explicitly so a row read fails loud
15
15
  * on schema drift (see `ACCOUNT_COLUMNS` in `auth/account_queries.ts` for the
@@ -162,6 +162,18 @@ export const query_role_grant_find_active_role_for_actor = async (deps, role_gra
162
162
  * offer becomes terminal at revoke time. A fresh post-revoke grant
163
163
  * requires the grantor to call `query_role_grant_offer_create` again.
164
164
  *
165
+ * The revokee's account row is locked first (`FOR NO KEY UPDATE`, the lock
166
+ * `query_account_lock_for_revoke` takes), so no offer to or from the account is
167
+ * made, and none accepted, while the revoke runs: the offers it locks are the
168
+ * offers it writes. Then those offers, in id order, before the grant is
169
+ * updated: an accept claims its offers before it inserts its grant, and that
170
+ * insert waits on an uncommitted update of the same grant, so the grant first
171
+ * and the offers second would close a cycle with it. A caller that supersedes
172
+ * the revokee's outgoing offers too (`role_grant_revoke`) takes the account row
173
+ * and then every offer it writes in one statement first
174
+ * (`query_role_grant_offer_lock_for_revoke`); the locks here then find rows
175
+ * the transaction already holds.
176
+ *
165
177
  * @param deps - query dependencies
166
178
  * @param role_grant_id - the role_grant to revoke
167
179
  * @param actor_id - the actor that must own the role_grant
@@ -171,6 +183,21 @@ export const query_role_grant_find_active_role_for_actor = async (deps, role_gra
171
183
  * @mutates `role_grant_offer` rows - stamps `superseded_at` on every pending sibling for the same `(account, role, scope)`
172
184
  */
173
185
  export const query_revoke_role_grant = async (deps, role_grant_id, actor_id, revoked_by, reason) => {
186
+ // the revokee's account row first, as `query_account_lock_for_revoke` takes it
187
+ await deps.db.query(`SELECT a.id FROM actor act JOIN account a ON a.id = act.account_id
188
+ WHERE act.id = $1
189
+ FOR NO KEY UPDATE OF a`, [actor_id]);
190
+ // the role and scope name the offers to lock; written once with the grant
191
+ const target = await deps.db.query_one(`SELECT role, scope_id FROM role_grant WHERE id = $1 AND actor_id = $2 AND revoked_at IS NULL`, [role_grant_id, actor_id]);
192
+ if (!target)
193
+ return null;
194
+ const incoming = `to_account_id = (SELECT account_id FROM actor WHERE id = $1)
195
+ AND role = $2 AND scope_id IS NOT DISTINCT FROM $3 AND ${offer_pending_predicate('')}`;
196
+ await deps.db.query(`SELECT id FROM role_grant_offer WHERE ${incoming} ORDER BY id FOR UPDATE`, [
197
+ actor_id,
198
+ target.role,
199
+ target.scope_id
200
+ ]);
174
201
  const rows = await deps.db.query(`UPDATE role_grant SET revoked_at = NOW(), revoked_by = $3, revoked_reason = $4
175
202
  WHERE id = $1 AND actor_id = $2 AND revoked_at IS NULL
176
203
  RETURNING id, role, scope_kind, scope_id`, [role_grant_id, actor_id, revoked_by ?? null, reason ?? null]);
@@ -186,12 +213,7 @@ export const query_revoke_role_grant = async (deps, role_grant_id, actor_id, rev
186
213
  const superseded_offers = await deps.db.query(`WITH updated AS (
187
214
  UPDATE role_grant_offer o
188
215
  SET superseded_at = NOW()
189
- FROM actor a
190
- WHERE a.id = $1
191
- AND o.to_account_id = a.account_id
192
- AND o.role = $2
193
- AND o.scope_id IS NOT DISTINCT FROM $3
194
- AND ${offer_pending_predicate('o')}
216
+ WHERE ${offer_rows_locked_in_id_order('o', incoming)}
195
217
  -- raw columns; the outer grantor select formats them
196
218
  RETURNING ${qualify_columns(ROLE_GRANT_OFFER_COLUMNS, 'o')}
197
219
  )
@@ -253,7 +275,7 @@ export const query_account_has_global_role = async (deps, account_id, role) => {
253
275
  * Like `query_account_has_global_role`, but only counts the grant when the
254
276
  * **account and the granting actor are both active** (`deleted_at IS NULL`).
255
277
  * Used by the last-admin branch of the removability guard: a soft-deleted admin
256
- * can't log in and is excluded from `query_count_active_accounts_with_global_role_for_update`,
278
+ * can't log in and is excluded from `query_count_active_accounts_with_global_role_locked`,
257
279
  * so the guard must use the same active predicate when testing whether the
258
280
  * *target* is an admin — otherwise removing an already-tombstoned admin is
259
281
  * falsely blocked as `cannot_delete_last_admin`. Soft-delete does **not** revoke
@@ -325,16 +347,37 @@ export const query_actor_has_active_global_role = async (deps, actor_id, role) =
325
347
  * guard could fail-open and let the last usable admin be removed).
326
348
  *
327
349
  * **Locks** every counted `role_grant`, `actor`, and `account` row
328
- * (`FOR UPDATE`) until the caller's transaction ends, so two guards racing to
329
- * remove the last two admins serialize: the second waits for the first to
330
- * commit, re-reads the rows it changed, and counts one. Run it inside the
331
- * transaction that does the removal. Twin of the Rust
332
- * `query_count_active_accounts_with_global_role_for_update`.
350
+ * `FOR NO KEY UPDATE` until the caller's transaction ends. That conflicts with
351
+ * another guard, so two guards racing to remove the last two admins serialize:
352
+ * the second waits for the first to commit, re-reads the rows it changed, and
353
+ * counts one. It conflicts with every write that lowers the count — a revoke's
354
+ * or a soft delete's `UPDATE` of a counted row, a purge's `DELETE` — and with
355
+ * the locks a credential mint, an offer, an invite and an offer accept (its
356
+ * recipient's row and its grantor's, `query_account_lock_for_offer_accept`)
357
+ * take on the account row. Run it inside the transaction that does the
358
+ * removal.
359
+ *
360
+ * It does not conflict with a foreign key's `FOR KEY SHARE` — the lock an
361
+ * insert or update naming a counted row takes (an accept's new grant naming its
362
+ * grantor, a cell's `created_by`). Such a write only adds a reference: it never
363
+ * lowers the count, so the guard fails closed — what it counted is at most what
364
+ * will stand, and a removal it allows leaves an admin. A purge that deletes a
365
+ * row such a write names still takes `FOR UPDATE` and waits for it.
366
+ *
367
+ * A purge's cascade writes admin rows too — it sets null the `granted_by` of
368
+ * every grant the purged account's actors granted, admins' grants among them,
369
+ * in the order its plan walks them — so a purge takes every admin's account
370
+ * row first, with its own, in one id-ordered statement
371
+ * (`query_account_lock_admins_for_purge`), and runs this count after it only
372
+ * when its target is an admin: another guard then waits at its first account
373
+ * holding nothing.
374
+ * Twin of the Rust `query_count_active_accounts_with_global_role_locked`.
333
375
  *
334
376
  * The rows are locked in id order — account, then actor, then grant — as the
335
377
  * sort returns them: each account's row, its actors' and its grants' before
336
- * the next account's. `query_account_lock_live_many` takes account rows in the
337
- * same order, so an offer between two admins and an admin's removal never
378
+ * the next account's. `query_account_lock_live_many` and
379
+ * `query_account_lock_for_offer_accept` take account rows in the same order,
380
+ * so an offer or an accept between two admins and an admin's removal never
338
381
  * wait on each other's rows in opposite orders. Ids never change, so no
339
382
  * concurrent update can move a row the sort has placed.
340
383
  *
@@ -350,9 +393,9 @@ export const query_actor_has_active_global_role = async (deps, actor_id, role) =
350
393
  * @returns the number of distinct active accounts with an active global `role` grant
351
394
  * @mutates `role_grant` / `actor` / `account` rows - row-locks the counted rows for the transaction
352
395
  */
353
- export const query_count_active_accounts_with_global_role_for_update = async (deps, role, exclude_role_grant_id = null) => {
354
- // `FOR UPDATE` refuses an aggregate, so the rows come back and are counted
355
- // here. The excluded grant is dropped in code, not SQL: filtering it in the
396
+ export const query_count_active_accounts_with_global_role_locked = async (deps, role, exclude_role_grant_id = null) => {
397
+ // A locking clause refuses an aggregate, so the rows come back and are
398
+ // counted here. The excluded grant is dropped in code, not SQL: filtering it in the
356
399
  // WHERE would leave it unlocked, and two cross-revokes (each locking only the
357
400
  // other's grant, then updating it) would deadlock instead of serializing
358
401
  const rows = await deps.db.query(`SELECT a.id AS account_id, rg.id AS role_grant_id
@@ -365,7 +408,7 @@ export const query_count_active_accounts_with_global_role_for_update = async (de
365
408
  AND rg.scope_id IS NULL
366
409
  AND ${role_grant_active_sql('rg')}
367
410
  ORDER BY a.id, act.id, rg.id
368
- FOR UPDATE`, [role]);
411
+ FOR NO KEY UPDATE`, [role]);
369
412
  const account_ids = new Set();
370
413
  for (const row of rows) {
371
414
  if (row.role_grant_id !== exclude_role_grant_id)
@@ -417,7 +460,12 @@ export const query_role_grant_find_account_id_for_role = async (deps, role) => {
417
460
  * attached to the destroyed scope is cleaned up.
418
461
  *
419
462
  * Both updates run as separate statements inside the caller's transaction,
420
- * the `role_grant` revoke first. The two halves are
463
+ * the scope's pending offers locked in id order first, then the `role_grant`
464
+ * revoke, then the supersede of the offers that lock returned — offers before
465
+ * grants, as `query_revoke_role_grant` takes them. No account row keeps a new
466
+ * offer at the scope out (the scope's offers span accounts), so the supersede
467
+ * writes only the rows it locked: an offer made at the scope after the lock is
468
+ * left pending rather than taken out of order. The two halves are
421
469
  * independent — orphan pending offers can exist at a scope with no active
422
470
  * role_grants, so the supersede half always runs even when no role_grant was
423
471
  * revoked.
@@ -428,13 +476,17 @@ export const query_role_grant_find_account_id_for_role = async (deps, role) => {
428
476
  * @param reason - optional free-form reason, stamped on `role_grant.revoked_reason`.
429
477
  * @returns the revoked role_grants (with `account_id` for fan-out) and superseded offers (with `from_account_id` for fan-out)
430
478
  * @mutates `role_grant` table - sets `revoked_at`/`revoked_by`/`revoked_reason` on every active row at `scope_id`
431
- * @mutates `role_grant_offer` table - stamps `superseded_at` on every pending row at `scope_id`
479
+ * @mutates `role_grant_offer` table - stamps `superseded_at` on every pending row at `scope_id` its lock took
432
480
  */
433
481
  export const query_role_grant_revoke_for_scope = async (deps, scope_id, revoked_by, reason) => {
434
482
  // Revoke every active role_grant at the scope. CTE returns `actor_id` directly
435
483
  // from the role_grant row (drives `target_actor_id` audit envelopes); a join
436
484
  // against `actor` resolves `account_id` for `target_account_id`
437
485
  // + WS/SSE socket-close fan-out, all in one round-trip.
486
+ // The scope's pending offers first, in id order — before the grants, for
487
+ // the reason `query_revoke_role_grant` locks its offers before its grant
488
+ const at_scope = `scope_id = $1 AND ${offer_pending_predicate('')}`;
489
+ const locked_ids = (await deps.db.query(`SELECT id FROM role_grant_offer WHERE ${at_scope} ORDER BY id FOR UPDATE`, [scope_id])).map((row) => row.id);
438
490
  const revoked = await deps.db.query(`WITH updated AS (
439
491
  UPDATE role_grant
440
492
  SET revoked_at = NOW(), revoked_by = $2, revoked_reason = $3
@@ -452,11 +504,10 @@ export const query_role_grant_revoke_for_scope = async (deps, scope_id, revoked_
452
504
  const superseded_offers = await deps.db.query(`WITH updated AS (
453
505
  UPDATE role_grant_offer o
454
506
  SET superseded_at = NOW()
455
- WHERE o.scope_id = $1
456
- AND ${offer_pending_predicate('o')}
507
+ WHERE o.id = ANY($2) AND o.scope_id = $1 AND ${offer_pending_predicate('o')}
457
508
  -- raw columns; the outer grantor select formats them
458
509
  RETURNING ${qualify_columns(ROLE_GRANT_OFFER_COLUMNS, 'o')}
459
510
  )
460
- ${ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT}`, [scope_id]);
511
+ ${ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT}`, [scope_id, locked_ids]);
461
512
  return { revoked, superseded_offers };
462
513
  };
@@ -39,4 +39,25 @@ export declare const OFFER_TERMINAL_COLUMNS: readonly ["accepted_at", "declined_
39
39
  * @returns the predicate, with no surrounding parentheses
40
40
  */
41
41
  export declare const offer_pending_predicate: (alias: string) => string;
42
+ /**
43
+ * The predicate a multi-row write of `role_grant_offer` takes its rows
44
+ * through: `id` in an id-ordered locking subselect over `where`.
45
+ *
46
+ * Every transaction that writes several offer rows locks them in id order —
47
+ * in one statement like this, or, when it writes them across statements or
48
+ * after another table's row, all of them first in one id-ordered lock, and
49
+ * then writes only rows that lock took: it either holds the account rows that
50
+ * keep a new offer out (an offer create takes both parties' rows — a removal
51
+ * holds its account's, a revoke its revokee's, an accept its recipient's), or
52
+ * names the locked ids (a scope teardown) — so two such transactions never
53
+ * hold rows the other waits on, whatever plan the database picks. A row
54
+ * changed by a transaction it waited on is re-checked against `where` and
55
+ * drops out when it no longer matches. Twin of the Rust
56
+ * `offer_rows_locked_in_id_order`.
57
+ *
58
+ * @param alias - the alias qualifying the written table's `id`, or `''`
59
+ * @param where - the rows' condition, over unqualified `role_grant_offer` columns
60
+ * @returns the predicate, with no surrounding parentheses
61
+ */
62
+ export declare const offer_rows_locked_in_id_order: (alias: string, where: string) => string;
42
63
  //# sourceMappingURL=role_grant_sql.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"role_grant_sql.d.ts","sourceRoot":"","sources":["../../src/lib/auth/role_grant_sql.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAQH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,qBAAqB,GAAI,OAAO,MAAM,KAAG,MAIrD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,0EAKzB,CAAC;AAEX;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,GAAI,OAAO,MAAM,KAAG,MACkC,CAAC"}
1
+ {"version":3,"file":"role_grant_sql.d.ts","sourceRoot":"","sources":["../../src/lib/auth/role_grant_sql.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAQH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,qBAAqB,GAAI,OAAO,MAAM,KAAG,MAIrD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,0EAKzB,CAAC;AAEX;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,GAAI,OAAO,MAAM,KAAG,MACkC,CAAC;AAE3F;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,6BAA6B,GAAI,OAAO,MAAM,EAAE,OAAO,MAAM,KAAG,MACwB,CAAC"}
@@ -52,3 +52,24 @@ export const OFFER_TERMINAL_COLUMNS = [
52
52
  * @returns the predicate, with no surrounding parentheses
53
53
  */
54
54
  export const offer_pending_predicate = (alias) => OFFER_TERMINAL_COLUMNS.map((column) => `${qualify(alias, column)} IS NULL`).join(' AND ');
55
+ /**
56
+ * The predicate a multi-row write of `role_grant_offer` takes its rows
57
+ * through: `id` in an id-ordered locking subselect over `where`.
58
+ *
59
+ * Every transaction that writes several offer rows locks them in id order —
60
+ * in one statement like this, or, when it writes them across statements or
61
+ * after another table's row, all of them first in one id-ordered lock, and
62
+ * then writes only rows that lock took: it either holds the account rows that
63
+ * keep a new offer out (an offer create takes both parties' rows — a removal
64
+ * holds its account's, a revoke its revokee's, an accept its recipient's), or
65
+ * names the locked ids (a scope teardown) — so two such transactions never
66
+ * hold rows the other waits on, whatever plan the database picks. A row
67
+ * changed by a transaction it waited on is re-checked against `where` and
68
+ * drops out when it no longer matches. Twin of the Rust
69
+ * `offer_rows_locked_in_id_order`.
70
+ *
71
+ * @param alias - the alias qualifying the written table's `id`, or `''`
72
+ * @param where - the rows' condition, over unqualified `role_grant_offer` columns
73
+ * @returns the predicate, with no surrounding parentheses
74
+ */
75
+ export const offer_rows_locked_in_id_order = (alias, where) => `${qualify(alias, 'id')} IN (SELECT id FROM role_grant_offer WHERE ${where} ORDER BY id FOR UPDATE)`;
@@ -1 +1 @@
1
- {"version":3,"file":"self_service_role_actions.d.ts","sourceRoot":"","sources":["../../src/lib/auth/self_service_role_actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAIH,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAGjG,OAAO,EAKN,KAAK,YAAY,EACjB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAW3D;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,oCAAoC,GAAI,OAAO,QAAQ,CAAC,MAAM,CAAC,KAAG,IAW9E,CAAC;AAEF,sDAAsD;AACtD,MAAM,WAAW,6BAA6B;IAC7C;;;;;;;;;;;OAWG;IACH,cAAc,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IACvC;;;;OAIG;IACH,KAAK,CAAC,EAAE,YAAY,CAAC;CACrB;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,gCAAgC,GAC5C,MAAM,yBAAyB,EAC/B,UAAS,6BAAkC,KACzC,KAAK,CAAC,SAAS,CA0JjB,CAAC"}
1
+ {"version":3,"file":"self_service_role_actions.d.ts","sourceRoot":"","sources":["../../src/lib/auth/self_service_role_actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAIH,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAGjG,OAAO,EAKN,KAAK,YAAY,EACjB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAe3D;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,oCAAoC,GAAI,OAAO,QAAQ,CAAC,MAAM,CAAC,KAAG,IAW9E,CAAC;AAEF,sDAAsD;AACtD,MAAM,WAAW,6BAA6B;IAC7C;;;;;;;;;;;OAWG;IACH,cAAc,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IACvC;;;;OAIG;IACH,KAAK,CAAC,EAAE,YAAY,CAAC;CACrB;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,gCAAgC,GAC5C,MAAM,yBAAyB,EAC/B,UAAS,6BAAkC,KACzC,KAAK,CAAC,SAAS,CAgKjB,CAAC"}
@@ -45,6 +45,7 @@ import { GRANT_PATH_SELF_SERVICE } from './grant_path_schema.js';
45
45
  import { query_create_role_grant, query_revoke_role_grant } from './role_grant_queries.js';
46
46
  import { is_role_grant_active } from './account_schema.js';
47
47
  import { has_scoped_role } from './request_context.js';
48
+ import { queue_supersede_notifications } from './role_grant_offer_notifications.js';
48
49
  import { ERROR_ROLE_NOT_SELF_SERVICE_ELIGIBLE, self_service_role_set_action_spec } from './self_service_role_action_specs.js';
49
50
  /**
50
51
  * Validate a deployment's self-service eligible-role set at construction.
@@ -192,8 +193,9 @@ export const create_self_service_role_actions = (deps, options = {}) => {
192
193
  // for the same `(account, role, scope)` superseded as a side effect, and
193
194
  // each gets its forensic row like the admin revoke's (empty in practice:
194
195
  // offer creation gates on the role's `grant_paths`, so only a role that is
195
- // both offerable and self-service-eligible has siblings). The WS supersede
196
- // notification to each grantor is not sent (twin of the Rust path).
196
+ // both offerable and self-service-eligible has siblings). Both parties are
197
+ // told, as the admin revoke tells them: the recipient is the caller, but
198
+ // its answer lists no offers, so its other connections learn of them here.
197
199
  for (const offer of result.superseded_offers) {
198
200
  await deps.audit.emit_role_grant_target(ctx, auth, {
199
201
  event_type: 'role_grant_offer_supersede',
@@ -208,6 +210,11 @@ export const create_self_service_role_actions = (deps, options = {}) => {
208
210
  }
209
211
  });
210
212
  }
213
+ const revoked_grant = { kind: 'id', id: result.id };
214
+ queue_supersede_notifications(ctx, result.superseded_offers, 'role_grant_revoked', {
215
+ grantor: revoked_grant,
216
+ recipient: revoked_grant
217
+ });
211
218
  return { ok: true, enabled: false, changed: true };
212
219
  };
213
220
  return [rpc_action(self_service_role_set_action_spec, handler)];
@@ -128,6 +128,13 @@ export declare const query_session_revoke_for_account: (deps: QueryDeps, token_h
128
128
  /**
129
129
  * Revoke all sessions for an account.
130
130
  *
131
+ * Locks its rows in id order before it deletes them (`ORDER BY id FOR
132
+ * UPDATE`), as every multi-row delete of `auth_session` rows does — the cap's
133
+ * eviction, the expiry sweep, the revoke-all — so two of them reaching the
134
+ * same rows wait one on the other instead of deadlocking in the opposite
135
+ * orders their plans would otherwise walk. Twin of the Rust
136
+ * `fuz_auth::query_session_revoke_all_for_account`.
137
+ *
131
138
  * @returns the number of sessions revoked
132
139
  * @mutates `auth_session` table - deletes every row for `account_id`
133
140
  */
@@ -142,21 +149,21 @@ export declare const query_session_list_for_account: (deps: QueryDeps, account_i
142
149
  * Keeps the newest `max_sessions` sessions and deletes the rest.
143
150
  *
144
151
  * Race safety: this function must run inside a transaction alongside the
145
- * INSERT that created the new session. All callers satisfy this requirement:
146
- * - The `account_token_create` RPC handler runs under the dispatcher's
147
- * transaction path because its spec declares `side_effects: true`
148
- * - `POST /login`, `POST /bootstrap` and `POST /signup` manage their own
149
- * transactions and pass the transaction-scoped `deps` to
150
- * `create_capped_session`
152
+ * INSERT that created the new session. Its one caller is
153
+ * `create_capped_session`, which `POST /login`, `POST /bootstrap` and
154
+ * `POST /signup` each run in a transaction of their own.
151
155
  *
152
156
  * The transaction makes one creator's INSERT + enforce_limit pair atomic, but it
153
157
  * does **not** serialize concurrent creators. Under Read Committed, two
154
158
  * transactions can't see each other's uncommitted session row, so each computes
155
159
  * its `OFFSET` eviction against a stale count, each preserves its own row, and
156
- * both commit above `max_sessions`. A transaction is necessary here but not
157
- * sufficient. Closing this needs one serialization point per
158
- * `(account_id, credential_kind)` — a locked parent row or a transaction-scoped
159
- * advisory lock — taken before count/evict/insert.
160
+ * both commit above `max_sessions` — and their two `DELETE`s lock the same old
161
+ * rows in orders the plan picks, so they can deadlock. A transaction is
162
+ * necessary here but not sufficient: a creator for an existing account first
163
+ * takes its account row `FOR NO KEY UPDATE` (`query_account_lock_verified`),
164
+ * which makes the creators of one account take turns, each counting the rows
165
+ * the one before committed. A creator for an account its own transaction just
166
+ * made needs none — no other can reach the account.
160
167
  *
161
168
  * Ordering is `created_at DESC, id DESC`. The `id` leg is a **stability**
162
169
  * tie-breaker, not a recency one: `created_at` defaults to `NOW()`, the
@@ -165,11 +172,17 @@ export declare const query_session_list_for_account: (deps: QueryDeps, account_i
165
172
  * `OFFSET` may keep a different set on two evaluations of the same rows. `id` is
166
173
  * the blake3 token hash, so the tie-break is arbitrary but deterministic; it
167
174
  * makes the survivors reproducible, it does not make the row just inserted a
168
- * guaranteed survivor. Only the serialization point above does that.
175
+ * guaranteed survivor. Only the account lock above does that.
169
176
  *
170
177
  * Expired-but-unreaped rows count toward the cap (the predicate is `account_id`
171
178
  * alone). Matches the Rust twin `query_session_enforce_limit`.
172
179
  *
180
+ * The victims are locked in id order before the delete (`ORDER BY id FOR
181
+ * UPDATE`), as every multi-row delete of `auth_session` rows locks them: a
182
+ * revoke-all or the expiry sweep reaching the same rows waits on the eviction,
183
+ * or the eviction on it, instead of the two deadlocking in whatever orders
184
+ * their plans walk (`query_session_revoke_all_for_account`).
185
+ *
173
186
  * Deleting a session row does not end the connections it opened: a WebSocket
174
187
  * keeps the authority it resolved at upgrade and an SSE stream never rechecks.
175
188
  * The returned ids are the token hashes those connections are registered
@@ -215,8 +228,10 @@ export declare const query_session_list_all_active: (deps: QueryDeps, limit?: nu
215
228
  * a concurrent pass gets a disjoint set. Run it in a transaction, as the
216
229
  * caller above does, so the ids are in hand before the delete commits — on a
217
230
  * pool-level `Db` a connection lost on the response leaves the rows deleted
218
- * and their ids unread. Served by `idx_auth_session_expires`. Twin of the
219
- * Rust `query_session_cleanup_expired`.
231
+ * and their ids unread. Served by `idx_auth_session_expires`. The rows are
232
+ * locked in id order before the delete, as every multi-row delete of
233
+ * `auth_session` rows locks them (`query_session_revoke_all_for_account`). Twin
234
+ * of the Rust `query_session_cleanup_expired`.
220
235
  *
221
236
  * @param deps - query dependencies (transaction-scoped, see above)
222
237
  * @returns the deleted sessions' ids (their token hashes), empty when nothing had expired
@@ -258,6 +273,12 @@ export interface CappedSession {
258
273
  * `session` target, as `POST /login` does. A caller that mints for an account
259
274
  * created in the same transaction (signup, bootstrap) evicts nothing.
260
275
  *
276
+ * A caller minting for an existing account first takes its row lock in the
277
+ * same transaction (`query_account_lock_verified`) and refuses on `false`: a
278
+ * password change or a delete in flight is ordered against the mint, and the
279
+ * account's mints take turns, which the cap counts on
280
+ * (`query_session_enforce_limit`).
281
+ *
261
282
  * @returns the signed cookie value and the evicted sessions' token hashes
262
283
  * @mutates `auth_session` table - inserts the new session row (and evicts older rows when `max_sessions` is set)
263
284
  */
@@ -1 +1 @@
1
- {"version":3,"file":"session_queries.d.ts","sourceRoot":"","sources":["../../src/lib/auth/session_queries.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAA+B,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAEvF,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAGlE;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,QAA2B,CAAC;AAEjE;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,KAAG,SAElD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,QAAO,MAEzC,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,oBAAoB,GAChC,MAAM,SAAS,EACf,YAAY,MAAM,EAClB,YAAY,MAAM,EAClB,YAAY,IAAI,KACd,OAAO,CAAC,IAAI,CAMd,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,oBAAoB,2DAKoB,CAAC;AAQtD;;;GAGG;AACH,MAAM,WAAW,gBAAiB,SAAQ,WAAW;IACpD;;;;OAIG;IACH,aAAa,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,GACnC,MAAM,SAAS,EACf,YAAY,MAAM,KAChB,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAKtC,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;A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