@fuzdev/fuz_app 0.185.0 → 0.187.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 (131) hide show
  1. package/dist/actions/CLAUDE.md +14 -2
  2. package/dist/actions/perform_action.d.ts.map +1 -1
  3. package/dist/actions/perform_action.js +36 -5
  4. package/dist/actions/ws_socket_session.d.ts.map +1 -1
  5. package/dist/actions/ws_socket_session.js +3 -1
  6. package/dist/auth/CLAUDE.md +17 -6
  7. package/dist/auth/account_queries.d.ts +60 -44
  8. package/dist/auth/account_queries.d.ts.map +1 -1
  9. package/dist/auth/account_queries.js +64 -54
  10. package/dist/auth/account_routes.d.ts.map +1 -1
  11. package/dist/auth/account_routes.js +59 -25
  12. package/dist/auth/admin_actions.d.ts.map +1 -1
  13. package/dist/auth/admin_actions.js +26 -9
  14. package/dist/auth/audit_emitter.js +2 -2
  15. package/dist/auth/audit_log_queries.d.ts +11 -3
  16. package/dist/auth/audit_log_queries.d.ts.map +1 -1
  17. package/dist/auth/audit_log_queries.js +4 -4
  18. package/dist/auth/audit_log_schema.js +1 -1
  19. package/dist/auth/auth_ddl.d.ts.map +1 -1
  20. package/dist/auth/auth_ddl.js +4 -0
  21. package/dist/auth/cleanup.js +1 -1
  22. package/dist/auth/migrations.d.ts.map +1 -1
  23. package/dist/auth/migrations.js +35 -0
  24. package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
  25. package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
  26. package/dist/auth/role_grant_offer_action_specs.js +2 -1
  27. package/dist/auth/role_grant_offer_actions.d.ts +12 -0
  28. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  29. package/dist/auth/role_grant_offer_actions.js +48 -20
  30. package/dist/auth/role_grant_offer_queries.d.ts +28 -15
  31. package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
  32. package/dist/auth/role_grant_offer_queries.js +35 -16
  33. package/dist/auth/role_grant_queries.d.ts +118 -20
  34. package/dist/auth/role_grant_queries.d.ts.map +1 -1
  35. package/dist/auth/role_grant_queries.js +145 -24
  36. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  37. package/dist/auth/self_service_role_actions.js +15 -0
  38. package/dist/auth/session_queries.d.ts +24 -0
  39. package/dist/auth/session_queries.d.ts.map +1 -1
  40. package/dist/auth/session_queries.js +28 -0
  41. package/dist/db/CLAUDE.md +3 -3
  42. package/dist/db/cell_ddl.d.ts +8 -3
  43. package/dist/db/cell_ddl.d.ts.map +1 -1
  44. package/dist/db/cell_ddl.js +10 -5
  45. package/dist/db/migrate.d.ts +46 -22
  46. package/dist/db/migrate.d.ts.map +1 -1
  47. package/dist/db/migrate.js +103 -85
  48. package/dist/http/body_limit.d.ts +4 -4
  49. package/dist/http/body_limit.js +4 -4
  50. package/dist/server/app_backend.d.ts.map +1 -1
  51. package/dist/server/app_backend.js +1 -4
  52. package/dist/testing/CLAUDE.md +161 -27
  53. package/dist/testing/app_server.d.ts +5 -7
  54. package/dist/testing/app_server.d.ts.map +1 -1
  55. package/dist/testing/app_server.js +5 -7
  56. package/dist/testing/auth_apps.d.ts +3 -1
  57. package/dist/testing/auth_apps.d.ts.map +1 -1
  58. package/dist/testing/auth_apps.js +22 -5
  59. package/dist/testing/cross_backend/backend_both_live.d.ts +47 -0
  60. package/dist/testing/cross_backend/backend_both_live.d.ts.map +1 -0
  61. package/dist/testing/cross_backend/backend_both_live.js +566 -0
  62. package/dist/testing/cross_backend/backend_config.d.ts +8 -0
  63. package/dist/testing/cross_backend/backend_config.d.ts.map +1 -1
  64. package/dist/testing/cross_backend/backend_log.d.ts +71 -0
  65. package/dist/testing/cross_backend/backend_log.d.ts.map +1 -0
  66. package/dist/testing/cross_backend/backend_log.js +134 -0
  67. package/dist/testing/cross_backend/backend_swap.d.ts +41 -0
  68. package/dist/testing/cross_backend/backend_swap.d.ts.map +1 -0
  69. package/dist/testing/cross_backend/backend_swap.js +267 -0
  70. package/dist/testing/cross_backend/body_size_smuggling.js +3 -3
  71. package/dist/testing/cross_backend/bootstrap_backend.js +4 -4
  72. package/dist/testing/cross_backend/conformance_case.d.ts +11 -6
  73. package/dist/testing/cross_backend/conformance_case.d.ts.map +1 -1
  74. package/dist/testing/cross_backend/conformance_case.js +4 -0
  75. package/dist/testing/cross_backend/conformance_principal.d.ts +2 -2
  76. package/dist/testing/cross_backend/conformance_principal.d.ts.map +1 -1
  77. package/dist/testing/cross_backend/conformance_principal.js +27 -10
  78. package/dist/testing/cross_backend/default_backend_configs.d.ts +38 -5
  79. package/dist/testing/cross_backend/default_backend_configs.d.ts.map +1 -1
  80. package/dist/testing/cross_backend/default_backend_configs.js +50 -15
  81. package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
  82. package/dist/testing/cross_backend/in_process_setup.js +52 -30
  83. package/dist/testing/cross_backend/revocation_drivers.d.ts +14 -2
  84. package/dist/testing/cross_backend/revocation_drivers.d.ts.map +1 -1
  85. package/dist/testing/cross_backend/revocation_drivers.js +36 -8
  86. package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts +7 -0
  87. package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts.map +1 -1
  88. package/dist/testing/cross_backend/rust_spine_stub_backend_config.js +4 -2
  89. package/dist/testing/cross_backend/setup.d.ts +44 -43
  90. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  91. package/dist/testing/cross_backend/setup.js +64 -62
  92. package/dist/testing/cross_backend/shared_database_backends.d.ts +175 -0
  93. package/dist/testing/cross_backend/shared_database_backends.d.ts.map +1 -0
  94. package/dist/testing/cross_backend/shared_database_backends.js +298 -0
  95. package/dist/testing/cross_backend/spawn_backend.d.ts +7 -3
  96. package/dist/testing/cross_backend/spawn_backend.d.ts.map +1 -1
  97. package/dist/testing/cross_backend/spawn_backend.js +86 -7
  98. package/dist/testing/cross_backend/spine_suites.d.ts +1 -1
  99. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  100. package/dist/testing/cross_backend/spine_suites.js +5 -2
  101. package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
  102. package/dist/testing/cross_backend/sse_round_trip.js +4 -2
  103. package/dist/testing/cross_backend/testing_reset_actions.d.ts +7 -6
  104. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  105. package/dist/testing/cross_backend/testing_reset_actions.js +10 -8
  106. package/dist/testing/cross_backend/testing_server_bun.d.ts.map +1 -1
  107. package/dist/testing/cross_backend/testing_server_bun.js +7 -31
  108. package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
  109. package/dist/testing/cross_backend/ws_round_trip.js +4 -3
  110. package/dist/testing/data_exposure.d.ts +4 -1
  111. package/dist/testing/data_exposure.d.ts.map +1 -1
  112. package/dist/testing/data_exposure.js +93 -25
  113. package/dist/testing/db_invariants.d.ts +5 -5
  114. package/dist/testing/db_invariants.js +5 -5
  115. package/dist/testing/logged_errors.d.ts +61 -0
  116. package/dist/testing/logged_errors.d.ts.map +1 -0
  117. package/dist/testing/logged_errors.js +285 -0
  118. package/dist/testing/middleware_gate_probes.d.ts.map +1 -1
  119. package/dist/testing/middleware_gate_probes.js +15 -8
  120. package/dist/testing/rpc_attack_surface.d.ts.map +1 -1
  121. package/dist/testing/rpc_attack_surface.js +41 -16
  122. package/dist/testing/stubs.d.ts +5 -1
  123. package/dist/testing/stubs.d.ts.map +1 -1
  124. package/dist/testing/stubs.js +6 -3
  125. package/dist/testing/test_app.d.ts +40 -11
  126. package/dist/testing/test_app.d.ts.map +1 -1
  127. package/dist/testing/test_app.js +66 -9
  128. package/dist/testing/transports/fetch_transport.d.ts +10 -0
  129. package/dist/testing/transports/fetch_transport.d.ts.map +1 -1
  130. package/dist/testing/transports/fetch_transport.js +13 -0
  131. package/package.json +5 -3
@@ -15,6 +15,7 @@ import type { Uuid } from '../primitive_schemas.js';
15
15
  import type { QueryDeps } from '../db/query_deps.js';
16
16
  import type { RoleGrant } from './account_schema.js';
17
17
  import type { CreateRoleGrantOfferInput, RoleGrantOffer, SupersededOffer } from './role_grant_offer_schema.js';
18
+ import { type AuditLogQueryDeps } from './audit_log_queries.js';
18
19
  import type { AuditLogEvent } from './audit_log_schema.js';
19
20
  /**
20
21
  * Error thrown by offer-lifecycle queries when the offer is in a non-pending
@@ -164,6 +165,13 @@ export declare class RoleGrantOfferGrantorNotActiveError extends Error {
164
165
  * spans. The self-target check comes first, so an offer to one's own account
165
166
  * is refused as that whatever else it names.
166
167
  *
168
+ * A scoped offer takes the scope lock shared before anything else
169
+ * (`query_scope_lock_shared`), so a teardown of the scope
170
+ * (`query_role_grant_revoke_for_scope`) in flight is waited for, and one that
171
+ * starts after it waits for this transaction and then supersedes the offer. A
172
+ * caller that judged the scope live takes the lock before that read, holding
173
+ * no row; the spine's handler does.
174
+ *
167
175
  * @mutates `role_grant_offer` table - inserts a new offer or upserts the matching pending row
168
176
  * @throws RoleGrantOfferSelfTargetError if the offering actor belongs to `to_account_id`
169
177
  * @throws RoleGrantOfferRecipientNotActiveError if `to_account_id` is missing, soft-deleted, or has no active actor
@@ -299,23 +307,28 @@ export interface AcceptOfferResult {
299
307
  * creating a duplicate or throwing — while that grant is still held. A grant
300
308
  * revoked since the accept, or gone, makes the offer terminal.
301
309
  *
302
- * Lock order — accounts, then offers, then the grant: the recipient's account
303
- * row (and, for an unscoped `admin` offer, the grantor's) in id order
310
+ * Lock order — the scope, then accounts, then offers, then the grant: a
311
+ * scoped offer's scope lock shared (`query_scope_lock_shared`), the
312
+ * recipient's account row (and, for an unscoped `admin` offer, the grantor's)
313
+ * in id order
304
314
  * (`query_account_lock_for_offer_accept`), then the offer and the pending
305
315
  * siblings it supersedes in one id-ordered claim, then the grant insert. The
306
316
  * accounts first, as every removal of either account takes them first, so a
307
- * removal and an accept never meet at the offers, the actor, or a grant a
308
- * purge's cascade reaches; two accepts by one recipient take turns. The
309
- * offers in id order, as every transaction that writes several offer rows
310
- * locks them all first in one id-ordered statement (a delete, a purge, a
311
- * revoke, the expiry sweep). The grantor's authority is read unlocked after
312
- * the grantor's account lock — the invariant it rests on: every write that
313
- * ends a grantor's global `admin` takes the grantor's account row first (a
314
- * revoke of the grant through `query_account_lock_for_revoke`, whether or not
315
- * its last-admin guard ran; a delete through its write of the row; a purge
316
- * through `query_account_lock_admins_for_purge` and
317
- * `query_account_lock_for_purge`; the DB browser's delete, which takes none of
318
- * them, excepted).
317
+ * removal and an accept never meet at the offers or the actor; two accepts
318
+ * by one recipient take turns. The offers in id order, as every transaction
319
+ * that writes several offer rows locks them all first in one id-ordered
320
+ * statement (a delete, a purge, a revoke, the expiry sweep). The grantor's
321
+ * authority is read unlocked after the grantor's account lock — the invariant
322
+ * it rests on: every write that ends a grantor's global `admin` takes the
323
+ * grantor's account row first (a revoke of the grant through
324
+ * `query_account_lock_for_revoke`, whether or not its last-admin guard ran; a
325
+ * delete through its write of the row; a purge through its last-admin guard
326
+ * and `query_account_lock_for_purge`; the DB browser's delete, which takes
327
+ * none of them, excepted). The scope lock comes first because the grant insert
328
+ * takes it (`query_create_role_grant`): taken there, after the claim, it would
329
+ * wait on a teardown that waits on the claimed offer. Taken first, a teardown
330
+ * in flight is waited for and the claim then finds the offer superseded, and
331
+ * one that starts after waits for this accept and then revokes its grant.
319
332
  *
320
333
  * Error map:
321
334
  * - `RoleGrantOfferNotFoundError` — offer does not exist, or belongs to a
@@ -348,7 +361,7 @@ export interface AcceptOfferResult {
348
361
  * @throws RoleGrantOfferBuiltinRefusedError if a builtin-role offer must not mint (see `BuiltinOfferRefusal`)
349
362
  * @throws Error if the accepting `actor_id` does not belong to `to_account_id`, or invariant assertions fail
350
363
  */
351
- export declare const query_accept_offer: (deps: QueryDeps, input: AcceptOfferInput) => Promise<AcceptOfferResult>;
364
+ export declare const query_accept_offer: (deps: AuditLogQueryDeps, input: AcceptOfferInput) => Promise<AcceptOfferResult>;
352
365
  /**
353
366
  * Supersede every pending offer **made by** one of `from_actor_ids` — the
354
367
  * grantor-side cascade.
@@ -1 +1 @@
1
- {"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}
@@ -14,7 +14,7 @@
14
14
  import { assert_row } from '../db/assert_row.js';
15
15
  import { columns_sql, qualify_columns } from '../db/sql_columns.js';
16
16
  import { ROLE_GRANT_OFFER_COLUMNS, role_grant_offer_expr, 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';
17
- import { query_create_role_grant, query_role_grant_by_id } from './role_grant_queries.js';
17
+ import { query_create_role_grant, query_role_grant_by_id, query_scope_lock_shared } from './role_grant_queries.js';
18
18
  import { offer_pending_predicate, offer_rows_locked_in_id_order, role_grant_active_sql } from './role_grant_sql.js';
19
19
  import { query_audit_log } from './audit_log_queries.js';
20
20
  import { query_account_lock_for_offer_accept, query_account_lock_for_revoke, query_account_lock_live_many } from './account_queries.js';
@@ -178,6 +178,13 @@ export class RoleGrantOfferGrantorNotActiveError extends Error {
178
178
  * spans. The self-target check comes first, so an offer to one's own account
179
179
  * is refused as that whatever else it names.
180
180
  *
181
+ * A scoped offer takes the scope lock shared before anything else
182
+ * (`query_scope_lock_shared`), so a teardown of the scope
183
+ * (`query_role_grant_revoke_for_scope`) in flight is waited for, and one that
184
+ * starts after it waits for this transaction and then supersedes the offer. A
185
+ * caller that judged the scope live takes the lock before that read, holding
186
+ * no row; the spine's handler does.
187
+ *
181
188
  * @mutates `role_grant_offer` table - inserts a new offer or upserts the matching pending row
182
189
  * @throws RoleGrantOfferSelfTargetError if the offering actor belongs to `to_account_id`
183
190
  * @throws RoleGrantOfferRecipientNotActiveError if `to_account_id` is missing, soft-deleted, or has no active actor
@@ -185,6 +192,8 @@ export class RoleGrantOfferGrantorNotActiveError extends Error {
185
192
  * @throws RoleGrantOfferGrantorNotActiveError if the grantor's actor or account is missing or tombstoned
186
193
  */
187
194
  export const query_role_grant_offer_create = async (deps, input) => {
195
+ if (input.scope_id != null)
196
+ await query_scope_lock_shared(deps, input.scope_id);
188
197
  // Self-target check resolves the **grantor** actor's account and
189
198
  // compares against to_account_id. This is multi-actor-correct:
190
199
  // a single account may host many actors, and self-target means
@@ -387,7 +396,7 @@ export const query_role_grant_offer_sweep_expired = async (deps) => {
387
396
  * the grantor's account row `FOR SHARE` in the accept's transaction: every
388
397
  * write that ends a grantor's global `admin` takes that row first — a revoke of
389
398
  * the grant through `query_account_lock_for_revoke`, a delete through its write
390
- * of the row, a purge through `query_account_lock_admins_for_purge` and
399
+ * of the row, a purge through its last-admin guard and
391
400
  * `query_account_lock_for_purge` — so one that committed first is
392
401
  * seen here (each statement reads afresh), and one that came later waits for
393
402
  * the accept to commit. Twin of the Rust `grantor_holds_global_admin`.
@@ -418,23 +427,28 @@ const query_grantor_holds_global_admin = async (deps, grantor_actor_id) => {
418
427
  * creating a duplicate or throwing — while that grant is still held. A grant
419
428
  * revoked since the accept, or gone, makes the offer terminal.
420
429
  *
421
- * Lock order — accounts, then offers, then the grant: the recipient's account
422
- * row (and, for an unscoped `admin` offer, the grantor's) in id order
430
+ * Lock order — the scope, then accounts, then offers, then the grant: a
431
+ * scoped offer's scope lock shared (`query_scope_lock_shared`), the
432
+ * recipient's account row (and, for an unscoped `admin` offer, the grantor's)
433
+ * in id order
423
434
  * (`query_account_lock_for_offer_accept`), then the offer and the pending
424
435
  * siblings it supersedes in one id-ordered claim, then the grant insert. The
425
436
  * accounts first, as every removal of either account takes them first, so a
426
- * removal and an accept never meet at the offers, the actor, or a grant a
427
- * purge's cascade reaches; two accepts by one recipient take turns. The
428
- * offers in id order, as every transaction that writes several offer rows
429
- * locks them all first in one id-ordered statement (a delete, a purge, a
430
- * revoke, the expiry sweep). The grantor's authority is read unlocked after
431
- * the grantor's account lock — the invariant it rests on: every write that
432
- * ends a grantor's global `admin` takes the grantor's account row first (a
433
- * revoke of the grant through `query_account_lock_for_revoke`, whether or not
434
- * its last-admin guard ran; a delete through its write of the row; a purge
435
- * through `query_account_lock_admins_for_purge` and
436
- * `query_account_lock_for_purge`; the DB browser's delete, which takes none of
437
- * them, excepted).
437
+ * removal and an accept never meet at the offers or the actor; two accepts
438
+ * by one recipient take turns. The offers in id order, as every transaction
439
+ * that writes several offer rows locks them all first in one id-ordered
440
+ * statement (a delete, a purge, a revoke, the expiry sweep). The grantor's
441
+ * authority is read unlocked after the grantor's account lock — the invariant
442
+ * it rests on: every write that ends a grantor's global `admin` takes the
443
+ * grantor's account row first (a revoke of the grant through
444
+ * `query_account_lock_for_revoke`, whether or not its last-admin guard ran; a
445
+ * delete through its write of the row; a purge through its last-admin guard
446
+ * and `query_account_lock_for_purge`; the DB browser's delete, which takes
447
+ * none of them, excepted). The scope lock comes first because the grant insert
448
+ * takes it (`query_create_role_grant`): taken there, after the claim, it would
449
+ * wait on a teardown that waits on the claimed offer. Taken first, a teardown
450
+ * in flight is waited for and the claim then finds the offer superseded, and
451
+ * one that starts after waits for this accept and then revokes its grant.
438
452
  *
439
453
  * Error map:
440
454
  * - `RoleGrantOfferNotFoundError` — offer does not exist, or belongs to a
@@ -479,6 +493,11 @@ export const query_accept_offer = async (deps, input) => {
479
493
  if (!head) {
480
494
  throw new RoleGrantOfferNotFoundError(offer_id);
481
495
  }
496
+ // A scoped offer's scope lock, holding nothing yet — before the claim, since
497
+ // the grant insert takes it again, and a teardown holding it waits on the
498
+ // claimed offer.
499
+ if (head.scope_id !== null)
500
+ await query_scope_lock_shared(deps, head.scope_id);
482
501
  // An unscoped `admin` offer's grantor must still hold a global `admin` when
483
502
  // the grant is minted.
484
503
  const needs_grantor_admin = head.role === ROLE_ADMIN && head.scope_id === null;
@@ -19,6 +19,78 @@ import type { SupersededOffer } from './role_grant_offer_schema.js';
19
19
  * `role_grant` migration chain in `auth/auth_ddl.ts`.
20
20
  */
21
21
  export declare const ROLE_GRANT_COLUMNS: readonly ["id", "actor_id", "role", "scope_kind", "scope_id", "created_at", "expires_at", "revoked_at", "revoked_by", "revoked_reason", "granted_by", "source_offer_id"];
22
+ /**
23
+ * The first half of every scope lock's key (`scope_advisory_lock_key`): the
24
+ * ASCII of `fuzs`. Reserved for scope locks — no other lock in the spine takes it.
25
+ *
26
+ * Two-`int4` keys, `pg_advisory_xact_lock(int4, int4)`, sit a key space apart
27
+ * from the single `bigint` key the migration runner takes (`pg_locks.objsubid`
28
+ * 2 against 1), and this half differs from the purge lock's
29
+ * (`ACCOUNT_PURGE_ADVISORY_LOCK_KEY`, `fuz_`), so a scope lock meets neither.
30
+ * Twin of the Rust `fuz_auth::role_grant_offer_queries::SCOPE_ADVISORY_LOCK_CLASS`.
31
+ */
32
+ export declare const SCOPE_ADVISORY_LOCK_CLASS = 1718975091;
33
+ /**
34
+ * The advisory lock key of `scope_id`: `SCOPE_ADVISORY_LOCK_CLASS`, then the
35
+ * UUID's four big-endian 32-bit words folded by XOR into one signed `int4`.
36
+ *
37
+ * Every word counts — a UUIDv7 leads with its timestamp, so a prefix would put
38
+ * the scopes made in one millisecond on one key. Two scopes can still share a
39
+ * key; they then take turns, which costs a wait and nothing else. Twin of the
40
+ * Rust `fuz_auth::role_grant_offer_queries::scope_advisory_lock_key`, pinned on
41
+ * both sides by the same vectors.
42
+ *
43
+ * @param scope_id - the scope
44
+ * @returns the `(int4, int4)` pair `pg_advisory_xact_lock` takes
45
+ * @throws Error if `scope_id` is not a UUID's 32 hex digits
46
+ */
47
+ export declare const scope_advisory_lock_key: (scope_id: Uuid) => [number, number];
48
+ /**
49
+ * Take `scope_id`'s scope lock shared, until the transaction ends.
50
+ *
51
+ * The lock orders a scope's teardown against every write that brings a grant
52
+ * or an offer into the scope: `query_role_grant_revoke_for_scope` takes it
53
+ * exclusive as its first statement, and the scoped writes take it shared —
54
+ * `query_create_role_grant` and `query_role_grant_offer_create` first thing,
55
+ * `query_accept_offer` after its unlocked head read, before any row, and the
56
+ * `role_grant_offer_create` and `role_grant_assign` handlers before the
57
+ * consumer's authorizer runs. A write
58
+ * that reads its scope after the lock sees a teardown that committed first,
59
+ * and one holding the lock makes a teardown wait and then end what it wrote.
60
+ * Writers take turns with a teardown only, not with each other. Re-entrant:
61
+ * a second take in one transaction is free.
62
+ *
63
+ * Take it first, holding no row: the teardown holds it while it waits on offer
64
+ * and grant rows and on whatever its consumer's scope delete cascades into (an
65
+ * invite or a request at the scope), so a transaction that waits on it holding
66
+ * one of those deadlocks. The scope id may come from an unlocked read of a
67
+ * column that never changes, as the accept's head read does. Use it on a
68
+ * consumer path that reads its own scope row and then brings something into the
69
+ * scope (an invite accept, a join decision): the lock, then the read. A
70
+ * transaction never takes two scope locks here; one that must should take them
71
+ * in key order. Twin of the Rust
72
+ * `fuz_auth::role_grant_offer_queries::query_scope_lock_shared`.
73
+ *
74
+ * @param deps - query dependencies (must be the writing transaction)
75
+ * @param scope_id - the scope
76
+ */
77
+ export declare const query_scope_lock_shared: (deps: QueryDeps, scope_id: Uuid) => Promise<void>;
78
+ /**
79
+ * Take `scope_id`'s scope lock exclusive, until the transaction ends: every
80
+ * scoped write (`query_scope_lock_shared`) waits until then, and this waits for
81
+ * every one in flight.
82
+ *
83
+ * `query_role_grant_revoke_for_scope` takes it as its first statement. A
84
+ * consumer's teardown that must hold the lock before that — to read or lock
85
+ * its own scope row first — takes it here, holding no row, then calls the
86
+ * cascade (which takes it again, free) — never after taking it shared in the
87
+ * same transaction: two shared holders that both upgrade deadlock. Twin of the
88
+ * Rust `fuz_auth::role_grant_offer_queries::query_scope_lock_exclusive`.
89
+ *
90
+ * @param deps - query dependencies (must be the teardown's transaction)
91
+ * @param scope_id - the scope
92
+ */
93
+ export declare const query_scope_lock_exclusive: (deps: QueryDeps, scope_id: Uuid) => Promise<void>;
22
94
  /**
23
95
  * Grant a role_grant to an actor.
24
96
  * Idempotent — if an active (unrevoked and unexpired) role_grant already
@@ -40,6 +112,14 @@ export declare const ROLE_GRANT_COLUMNS: readonly ["id", "actor_id", "role", "sc
40
112
  * `role_grant_scope_kind_paired` CHECK; mismatched pairs raise at the DB
41
113
  * layer rather than producing silent rows.
42
114
  *
115
+ * A scoped grant takes the scope lock shared first (`query_scope_lock_shared`),
116
+ * so a teardown of the scope (`query_role_grant_revoke_for_scope`) in flight
117
+ * is waited for, and one that starts after it waits for this transaction and
118
+ * then revokes the grant. Run it inside the caller's transaction, holding no
119
+ * row unless the transaction already holds this scope's lock (the lock's
120
+ * rule; a re-take is free); a caller that judged the scope live takes the
121
+ * lock before that read.
122
+ *
43
123
  * @param deps - query dependencies
44
124
  * @param input - the role_grant fields
45
125
  * @returns the created or existing active role_grant
@@ -179,26 +259,33 @@ export declare const query_actor_has_active_global_role: (deps: QueryDeps, actor
179
259
  * removal.
180
260
  *
181
261
  * It does not conflict with a foreign key's `FOR KEY SHARE` — the lock an
182
- * insert or update naming a counted row takes (an accept's new grant naming its
183
- * grantor, a cell's `created_by`). Such a write only adds a reference: it never
262
+ * insert or update naming a counted row takes (a new grant to a counted actor,
263
+ * a cell's `created_by`). Such a write only adds a reference: it never
184
264
  * lowers the count, so the guard fails closed — what it counted is at most what
185
265
  * will stand, and a removal it allows leaves an admin. A purge that deletes a
186
266
  * row such a write names still takes `FOR UPDATE` and waits for it.
187
267
  *
188
- * A purge's cascade writes admin rows too — it sets null the `granted_by` of
189
- * every grant the purged account's actors granted, admins' grants among them,
190
- * in the order its plan walks them — so a purge takes every admin's account
191
- * row first, with its own, in one id-ordered statement
192
- * (`query_account_lock_admins_for_purge`), and runs this count after it only
193
- * when its target is an admin: another guard then waits at its first account
194
- * holding nothing.
268
+ * Both removals, a purge and a delete, run this count when their target is an
269
+ * admin, before any row of the target's (a purge after its advisory lock,
270
+ * `query_account_purge_advisory_lock`), and again once they hold the target's
271
+ * row (a purge's `query_account_lock_for_purge`, a delete's
272
+ * `query_account_lock_live`): whether the target is an admin is read
273
+ * unlocked, and an accept making it one can commit in between, with a revoke
274
+ * it let pass ending every other admin. The second count re-locks only rows
275
+ * the first holds, unless an admin was made in between — the target or
276
+ * another — whose rows it then takes out of id order. A deadlock error then
277
+ * rolls one of the two transactions back whole, removing nothing: the
278
+ * removal, or the other — which may be a removal that would have committed,
279
+ * and answers 500. A purge's cascade writes no other account's admin rows —
280
+ * the provenance stamps a grant names its granter by carry no foreign key —
281
+ * so another guard meets it only at these counts.
195
282
  * Twin of the Rust `query_count_active_accounts_with_global_role_locked`.
196
283
  *
197
284
  * The rows are locked in id order — account, then actor, then grant — as the
198
285
  * sort returns them: each account's row, its actors' and its grants' before
199
286
  * the next account's. `query_account_lock_live_many` and
200
287
  * `query_account_lock_for_offer_accept` take account rows in the same order,
201
- * so an offer or an accept between two admins and an admin's removal never
288
+ * so an offer, an assign or an accept between two admins and an admin's removal never
202
289
  * wait on each other's rows in opposite orders. Ids never change, so no
203
290
  * concurrent update can move a row the sort has placed.
204
291
  *
@@ -279,16 +366,26 @@ export interface RevokeForScopeResult {
279
366
  * orphan role_grants and offers. The cascade is **role-agnostic**: anything
280
367
  * attached to the destroyed scope is cleaned up.
281
368
  *
282
- * Both updates run as separate statements inside the caller's transaction,
283
- * the scope's pending offers locked in id order first, then the `role_grant`
284
- * revoke, then the supersede of the offers that lock returned — offers before
285
- * grants, as `query_revoke_role_grant` takes them. No account row keeps a new
286
- * offer at the scope out (the scope's offers span accounts), so the supersede
287
- * writes only the rows it locked: an offer made at the scope after the lock is
288
- * left pending rather than taken out of order. The two halves are
289
- * independent — orphan pending offers can exist at a scope with no active
290
- * role_grants, so the supersede half always runs even when no role_grant was
291
- * revoked.
369
+ * Its first statement takes the scope lock exclusive
370
+ * (`query_scope_lock_exclusive`): every write that brings a grant or an offer
371
+ * into the scope takes it shared (`query_scope_lock_shared` has the list), so
372
+ * one in flight commits first and is ended here, and one after waits for this
373
+ * transaction. A write whose consumer authorizer reads the scope reads it after
374
+ * its lock, so once this teardown has removed the scope row and committed, the
375
+ * write is refused. Call it before the transaction touches the scope row or
376
+ * any other row a writer's authorizer locks — a teardown holding such a row
377
+ * while it waits here on a writer that waits on that row deadlocks — or take
378
+ * `query_scope_lock_exclusive` first.
379
+ *
380
+ * Then the updates run as separate statements, the scope's pending offers
381
+ * locked in id order first, then the `role_grant` revoke, then the supersede
382
+ * of the offers that lock returned — offers before grants, as
383
+ * `query_revoke_role_grant` takes them. The supersede writes only the rows it
384
+ * locked; an offer written at the scope after that without the scope lock (a
385
+ * raw insert, which no query here makes) is left pending rather than taken
386
+ * out of order. The two halves are independent — orphan pending offers can
387
+ * exist at a scope with no active role_grants, so the supersede half always
388
+ * runs even when no role_grant was revoked.
292
389
  *
293
390
  * @param deps - query dependencies
294
391
  * @param scope_id - the scope whose role_grants and offers to terminate
@@ -297,6 +394,7 @@ export interface RevokeForScopeResult {
297
394
  * @returns the revoked role_grants (with `account_id` for fan-out) and superseded offers (with `from_account_id` for fan-out)
298
395
  * @mutates `role_grant` table - sets `revoked_at`/`revoked_by`/`revoked_reason` on every active row at `scope_id`
299
396
  * @mutates `role_grant_offer` table - stamps `superseded_at` on every pending row at `scope_id` its lock took
397
+ * @mutates the scope lock - holds `scope_id`'s advisory lock exclusive until the transaction ends
300
398
  */
301
399
  export declare const query_role_grant_revoke_for_scope: (deps: QueryDeps, scope_id: Uuid, revoked_by: Uuid | null, reason?: string | null) => Promise<RevokeForScopeResult>;
302
400
  //# 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,u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CAAC,oBAAoB,CAqD9B,CAAC"}
@@ -43,6 +43,96 @@ const role_grant_expr = iso8601_timestamp_expr(ROLE_GRANT_COLUMNS, [
43
43
  'expires_at',
44
44
  'revoked_at'
45
45
  ])('');
46
+ /**
47
+ * The first half of every scope lock's key (`scope_advisory_lock_key`): the
48
+ * ASCII of `fuzs`. Reserved for scope locks — no other lock in the spine takes it.
49
+ *
50
+ * Two-`int4` keys, `pg_advisory_xact_lock(int4, int4)`, sit a key space apart
51
+ * from the single `bigint` key the migration runner takes (`pg_locks.objsubid`
52
+ * 2 against 1), and this half differs from the purge lock's
53
+ * (`ACCOUNT_PURGE_ADVISORY_LOCK_KEY`, `fuz_`), so a scope lock meets neither.
54
+ * Twin of the Rust `fuz_auth::role_grant_offer_queries::SCOPE_ADVISORY_LOCK_CLASS`.
55
+ */
56
+ export const SCOPE_ADVISORY_LOCK_CLASS = 0x6675_7a73;
57
+ /**
58
+ * The advisory lock key of `scope_id`: `SCOPE_ADVISORY_LOCK_CLASS`, then the
59
+ * UUID's four big-endian 32-bit words folded by XOR into one signed `int4`.
60
+ *
61
+ * Every word counts — a UUIDv7 leads with its timestamp, so a prefix would put
62
+ * the scopes made in one millisecond on one key. Two scopes can still share a
63
+ * key; they then take turns, which costs a wait and nothing else. Twin of the
64
+ * Rust `fuz_auth::role_grant_offer_queries::scope_advisory_lock_key`, pinned on
65
+ * both sides by the same vectors.
66
+ *
67
+ * @param scope_id - the scope
68
+ * @returns the `(int4, int4)` pair `pg_advisory_xact_lock` takes
69
+ * @throws Error if `scope_id` is not a UUID's 32 hex digits
70
+ */
71
+ export const scope_advisory_lock_key = (scope_id) => {
72
+ const hex = scope_id.replaceAll('-', '');
73
+ if (!/^[0-9a-f]{32}$/i.test(hex)) {
74
+ throw new Error(`scope lock key: not a UUID: ${scope_id}`);
75
+ }
76
+ let folded = 0;
77
+ for (let i = 0; i < 32; i += 8) {
78
+ folded ^= Number.parseInt(hex.slice(i, i + 8), 16);
79
+ }
80
+ return [SCOPE_ADVISORY_LOCK_CLASS, folded];
81
+ };
82
+ /**
83
+ * Take `scope_id`'s scope lock shared, until the transaction ends.
84
+ *
85
+ * The lock orders a scope's teardown against every write that brings a grant
86
+ * or an offer into the scope: `query_role_grant_revoke_for_scope` takes it
87
+ * exclusive as its first statement, and the scoped writes take it shared —
88
+ * `query_create_role_grant` and `query_role_grant_offer_create` first thing,
89
+ * `query_accept_offer` after its unlocked head read, before any row, and the
90
+ * `role_grant_offer_create` and `role_grant_assign` handlers before the
91
+ * consumer's authorizer runs. A write
92
+ * that reads its scope after the lock sees a teardown that committed first,
93
+ * and one holding the lock makes a teardown wait and then end what it wrote.
94
+ * Writers take turns with a teardown only, not with each other. Re-entrant:
95
+ * a second take in one transaction is free.
96
+ *
97
+ * Take it first, holding no row: the teardown holds it while it waits on offer
98
+ * and grant rows and on whatever its consumer's scope delete cascades into (an
99
+ * invite or a request at the scope), so a transaction that waits on it holding
100
+ * one of those deadlocks. The scope id may come from an unlocked read of a
101
+ * column that never changes, as the accept's head read does. Use it on a
102
+ * consumer path that reads its own scope row and then brings something into the
103
+ * scope (an invite accept, a join decision): the lock, then the read. A
104
+ * transaction never takes two scope locks here; one that must should take them
105
+ * in key order. Twin of the Rust
106
+ * `fuz_auth::role_grant_offer_queries::query_scope_lock_shared`.
107
+ *
108
+ * @param deps - query dependencies (must be the writing transaction)
109
+ * @param scope_id - the scope
110
+ */
111
+ export const query_scope_lock_shared = async (deps, scope_id) => {
112
+ await deps.db.query('SELECT pg_advisory_xact_lock_shared($1, $2)', [
113
+ ...scope_advisory_lock_key(scope_id)
114
+ ]);
115
+ };
116
+ /**
117
+ * Take `scope_id`'s scope lock exclusive, until the transaction ends: every
118
+ * scoped write (`query_scope_lock_shared`) waits until then, and this waits for
119
+ * every one in flight.
120
+ *
121
+ * `query_role_grant_revoke_for_scope` takes it as its first statement. A
122
+ * consumer's teardown that must hold the lock before that — to read or lock
123
+ * its own scope row first — takes it here, holding no row, then calls the
124
+ * cascade (which takes it again, free) — never after taking it shared in the
125
+ * same transaction: two shared holders that both upgrade deadlock. Twin of the
126
+ * Rust `fuz_auth::role_grant_offer_queries::query_scope_lock_exclusive`.
127
+ *
128
+ * @param deps - query dependencies (must be the teardown's transaction)
129
+ * @param scope_id - the scope
130
+ */
131
+ export const query_scope_lock_exclusive = async (deps, scope_id) => {
132
+ await deps.db.query('SELECT pg_advisory_xact_lock($1, $2)', [
133
+ ...scope_advisory_lock_key(scope_id)
134
+ ]);
135
+ };
46
136
  /**
47
137
  * Grant a role_grant to an actor.
48
138
  * Idempotent — if an active (unrevoked and unexpired) role_grant already
@@ -64,12 +154,22 @@ const role_grant_expr = iso8601_timestamp_expr(ROLE_GRANT_COLUMNS, [
64
154
  * `role_grant_scope_kind_paired` CHECK; mismatched pairs raise at the DB
65
155
  * layer rather than producing silent rows.
66
156
  *
157
+ * A scoped grant takes the scope lock shared first (`query_scope_lock_shared`),
158
+ * so a teardown of the scope (`query_role_grant_revoke_for_scope`) in flight
159
+ * is waited for, and one that starts after it waits for this transaction and
160
+ * then revokes the grant. Run it inside the caller's transaction, holding no
161
+ * row unless the transaction already holds this scope's lock (the lock's
162
+ * rule; a re-take is free); a caller that judged the scope live takes the
163
+ * lock before that read.
164
+ *
67
165
  * @param deps - query dependencies
68
166
  * @param input - the role_grant fields
69
167
  * @returns the created or existing active role_grant
70
168
  * @mutates `role_grant` table - revokes an expired unrevoked row at `(actor_id, role, scope_kind, scope_id)`, then inserts a row when no active role_grant matches
71
169
  */
72
170
  export const query_create_role_grant = async (deps, input) => {
171
+ if (input.scope_id != null)
172
+ await query_scope_lock_shared(deps, input.scope_id);
73
173
  // an expired row still holds the partial unique slot (`revoked_at IS NULL`)
74
174
  await deps.db.query(`UPDATE role_grant SET revoked_at = NOW(), revoked_by = NULL, revoked_reason = 'expired'
75
175
  WHERE actor_id = $1
@@ -283,26 +383,33 @@ export const query_actor_has_active_global_role = async (deps, actor_id, role) =
283
383
  * removal.
284
384
  *
285
385
  * It does not conflict with a foreign key's `FOR KEY SHARE` — the lock an
286
- * insert or update naming a counted row takes (an accept's new grant naming its
287
- * grantor, a cell's `created_by`). Such a write only adds a reference: it never
386
+ * insert or update naming a counted row takes (a new grant to a counted actor,
387
+ * a cell's `created_by`). Such a write only adds a reference: it never
288
388
  * lowers the count, so the guard fails closed — what it counted is at most what
289
389
  * will stand, and a removal it allows leaves an admin. A purge that deletes a
290
390
  * row such a write names still takes `FOR UPDATE` and waits for it.
291
391
  *
292
- * A purge's cascade writes admin rows too — it sets null the `granted_by` of
293
- * every grant the purged account's actors granted, admins' grants among them,
294
- * in the order its plan walks them — so a purge takes every admin's account
295
- * row first, with its own, in one id-ordered statement
296
- * (`query_account_lock_admins_for_purge`), and runs this count after it only
297
- * when its target is an admin: another guard then waits at its first account
298
- * holding nothing.
392
+ * Both removals, a purge and a delete, run this count when their target is an
393
+ * admin, before any row of the target's (a purge after its advisory lock,
394
+ * `query_account_purge_advisory_lock`), and again once they hold the target's
395
+ * row (a purge's `query_account_lock_for_purge`, a delete's
396
+ * `query_account_lock_live`): whether the target is an admin is read
397
+ * unlocked, and an accept making it one can commit in between, with a revoke
398
+ * it let pass ending every other admin. The second count re-locks only rows
399
+ * the first holds, unless an admin was made in between — the target or
400
+ * another — whose rows it then takes out of id order. A deadlock error then
401
+ * rolls one of the two transactions back whole, removing nothing: the
402
+ * removal, or the other — which may be a removal that would have committed,
403
+ * and answers 500. A purge's cascade writes no other account's admin rows —
404
+ * the provenance stamps a grant names its granter by carry no foreign key —
405
+ * so another guard meets it only at these counts.
299
406
  * Twin of the Rust `query_count_active_accounts_with_global_role_locked`.
300
407
  *
301
408
  * The rows are locked in id order — account, then actor, then grant — as the
302
409
  * sort returns them: each account's row, its actors' and its grants' before
303
410
  * the next account's. `query_account_lock_live_many` and
304
411
  * `query_account_lock_for_offer_accept` take account rows in the same order,
305
- * so an offer or an accept between two admins and an admin's removal never
412
+ * so an offer, an assign or an accept between two admins and an admin's removal never
306
413
  * wait on each other's rows in opposite orders. Ids never change, so no
307
414
  * concurrent update can move a row the sort has placed.
308
415
  *
@@ -384,16 +491,26 @@ export const query_role_grant_find_account_id_for_role = async (deps, role) => {
384
491
  * orphan role_grants and offers. The cascade is **role-agnostic**: anything
385
492
  * attached to the destroyed scope is cleaned up.
386
493
  *
387
- * Both updates run as separate statements inside the caller's transaction,
388
- * the scope's pending offers locked in id order first, then the `role_grant`
389
- * revoke, then the supersede of the offers that lock returned — offers before
390
- * grants, as `query_revoke_role_grant` takes them. No account row keeps a new
391
- * offer at the scope out (the scope's offers span accounts), so the supersede
392
- * writes only the rows it locked: an offer made at the scope after the lock is
393
- * left pending rather than taken out of order. The two halves are
394
- * independent — orphan pending offers can exist at a scope with no active
395
- * role_grants, so the supersede half always runs even when no role_grant was
396
- * revoked.
494
+ * Its first statement takes the scope lock exclusive
495
+ * (`query_scope_lock_exclusive`): every write that brings a grant or an offer
496
+ * into the scope takes it shared (`query_scope_lock_shared` has the list), so
497
+ * one in flight commits first and is ended here, and one after waits for this
498
+ * transaction. A write whose consumer authorizer reads the scope reads it after
499
+ * its lock, so once this teardown has removed the scope row and committed, the
500
+ * write is refused. Call it before the transaction touches the scope row or
501
+ * any other row a writer's authorizer locks — a teardown holding such a row
502
+ * while it waits here on a writer that waits on that row deadlocks — or take
503
+ * `query_scope_lock_exclusive` first.
504
+ *
505
+ * Then the updates run as separate statements, the scope's pending offers
506
+ * locked in id order first, then the `role_grant` revoke, then the supersede
507
+ * of the offers that lock returned — offers before grants, as
508
+ * `query_revoke_role_grant` takes them. The supersede writes only the rows it
509
+ * locked; an offer written at the scope after that without the scope lock (a
510
+ * raw insert, which no query here makes) is left pending rather than taken
511
+ * out of order. The two halves are independent — orphan pending offers can
512
+ * exist at a scope with no active role_grants, so the supersede half always
513
+ * runs even when no role_grant was revoked.
397
514
  *
398
515
  * @param deps - query dependencies
399
516
  * @param scope_id - the scope whose role_grants and offers to terminate
@@ -402,16 +519,20 @@ export const query_role_grant_find_account_id_for_role = async (deps, role) => {
402
519
  * @returns the revoked role_grants (with `account_id` for fan-out) and superseded offers (with `from_account_id` for fan-out)
403
520
  * @mutates `role_grant` table - sets `revoked_at`/`revoked_by`/`revoked_reason` on every active row at `scope_id`
404
521
  * @mutates `role_grant_offer` table - stamps `superseded_at` on every pending row at `scope_id` its lock took
522
+ * @mutates the scope lock - holds `scope_id`'s advisory lock exclusive until the transaction ends
405
523
  */
406
524
  export const query_role_grant_revoke_for_scope = async (deps, scope_id, revoked_by, reason) => {
525
+ // The scope lock first, holding nothing: every scoped writer holds it
526
+ // shared until its commit, and none waits here holding a row we lock next.
527
+ await query_scope_lock_exclusive(deps, scope_id);
528
+ // The scope's pending offers first, in id order — before the grants, for
529
+ // the reason `query_revoke_role_grant` locks its offers before its grant.
530
+ const at_scope = `scope_id = $1 AND ${offer_pending_predicate('')}`;
531
+ 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);
407
532
  // Revoke every active role_grant at the scope. CTE returns `actor_id` directly
408
533
  // from the role_grant row (drives `target_actor_id` audit envelopes); a join
409
534
  // against `actor` resolves `account_id` for `target_account_id`
410
535
  // + WS/SSE socket-close fan-out, all in one round-trip.
411
- // The scope's pending offers first, in id order — before the grants, for
412
- // the reason `query_revoke_role_grant` locks its offers before its grant
413
- const at_scope = `scope_id = $1 AND ${offer_pending_predicate('')}`;
414
- 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);
415
536
  const revoked = await deps.db.query(`WITH updated AS (
416
537
  UPDATE role_grant
417
538
  SET revoked_at = NOW(), revoked_by = $2, revoked_reason = $3