@fuzdev/fuz_app 0.181.0 → 0.183.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 (267) hide show
  1. package/README.md +2 -0
  2. package/dist/actions/CLAUDE.md +28 -10
  3. package/dist/actions/action_rpc.d.ts +11 -1
  4. package/dist/actions/action_rpc.d.ts.map +1 -1
  5. package/dist/actions/action_rpc.js +3 -2
  6. package/dist/actions/action_types.d.ts +16 -1
  7. package/dist/actions/action_types.d.ts.map +1 -1
  8. package/dist/actions/action_types.js +1 -1
  9. package/dist/actions/notification_sender.d.ts +83 -0
  10. package/dist/actions/notification_sender.d.ts.map +1 -0
  11. package/dist/actions/notification_sender.js +85 -0
  12. package/dist/actions/perform_action.d.ts +9 -0
  13. package/dist/actions/perform_action.d.ts.map +1 -1
  14. package/dist/actions/perform_action.js +22 -13
  15. package/dist/actions/register_action_ws.d.ts +14 -0
  16. package/dist/actions/register_action_ws.d.ts.map +1 -1
  17. package/dist/actions/register_action_ws.js +3 -2
  18. package/dist/actions/ws_socket_session.d.ts +3 -0
  19. package/dist/actions/ws_socket_session.d.ts.map +1 -1
  20. package/dist/actions/ws_socket_session.js +2 -1
  21. package/dist/auth/CLAUDE.md +113 -70
  22. package/dist/auth/account_actions.d.ts.map +1 -1
  23. package/dist/auth/account_actions.js +13 -3
  24. package/dist/auth/account_queries.d.ts +158 -16
  25. package/dist/auth/account_queries.d.ts.map +1 -1
  26. package/dist/auth/account_queries.js +189 -18
  27. package/dist/auth/account_routes.d.ts +6 -4
  28. package/dist/auth/account_routes.d.ts.map +1 -1
  29. package/dist/auth/account_routes.js +98 -92
  30. package/dist/auth/admin_actions.d.ts.map +1 -1
  31. package/dist/auth/admin_actions.js +93 -45
  32. package/dist/auth/api_token_queries.d.ts +13 -5
  33. package/dist/auth/api_token_queries.d.ts.map +1 -1
  34. package/dist/auth/api_token_queries.js +24 -9
  35. package/dist/auth/audit_emitter.d.ts +75 -84
  36. package/dist/auth/audit_emitter.d.ts.map +1 -1
  37. package/dist/auth/audit_emitter.js +83 -71
  38. package/dist/auth/audit_log_schema.d.ts +10 -5
  39. package/dist/auth/audit_log_schema.d.ts.map +1 -1
  40. package/dist/auth/audit_log_schema.js +22 -6
  41. package/dist/auth/bearer_auth.d.ts.map +1 -1
  42. package/dist/auth/bearer_auth.js +3 -2
  43. package/dist/auth/bootstrap_account.d.ts +31 -13
  44. package/dist/auth/bootstrap_account.d.ts.map +1 -1
  45. package/dist/auth/bootstrap_account.js +34 -15
  46. package/dist/auth/bootstrap_routes.d.ts +1 -1
  47. package/dist/auth/bootstrap_routes.d.ts.map +1 -1
  48. package/dist/auth/bootstrap_routes.js +7 -14
  49. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  50. package/dist/auth/daemon_token_middleware.js +12 -1
  51. package/dist/auth/deps.d.ts +18 -5
  52. package/dist/auth/deps.d.ts.map +1 -1
  53. package/dist/auth/deps.js +3 -2
  54. package/dist/auth/request_context.d.ts +73 -73
  55. package/dist/auth/request_context.d.ts.map +1 -1
  56. package/dist/auth/request_context.js +78 -96
  57. package/dist/auth/resolved_auth.d.ts +16 -11
  58. package/dist/auth/resolved_auth.d.ts.map +1 -1
  59. package/dist/auth/resolved_auth.js +6 -6
  60. package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
  61. package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
  62. package/dist/auth/role_grant_offer_action_specs.js +2 -1
  63. package/dist/auth/role_grant_offer_actions.d.ts +32 -12
  64. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  65. package/dist/auth/role_grant_offer_actions.js +91 -116
  66. package/dist/auth/role_grant_offer_notifications.d.ts +87 -38
  67. package/dist/auth/role_grant_offer_notifications.d.ts.map +1 -1
  68. package/dist/auth/role_grant_offer_notifications.js +75 -22
  69. package/dist/auth/role_grant_offer_queries.d.ts +155 -18
  70. package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
  71. package/dist/auth/role_grant_offer_queries.js +262 -73
  72. package/dist/auth/role_grant_offer_schema.js +1 -1
  73. package/dist/auth/role_grant_queries.d.ts +43 -52
  74. package/dist/auth/role_grant_queries.d.ts.map +1 -1
  75. package/dist/auth/role_grant_queries.js +50 -74
  76. package/dist/auth/role_grant_sql.d.ts +21 -0
  77. package/dist/auth/role_grant_sql.d.ts.map +1 -1
  78. package/dist/auth/role_grant_sql.js +21 -0
  79. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  80. package/dist/auth/self_service_role_actions.js +17 -21
  81. package/dist/auth/session_queries.d.ts +40 -16
  82. package/dist/auth/session_queries.d.ts.map +1 -1
  83. package/dist/auth/session_queries.js +68 -29
  84. package/dist/auth/signup_routes.d.ts.map +1 -1
  85. package/dist/auth/signup_routes.js +90 -38
  86. package/dist/auth/standard_rpc_actions.d.ts +6 -19
  87. package/dist/auth/standard_rpc_actions.d.ts.map +1 -1
  88. package/dist/auth/standard_rpc_actions.js +5 -6
  89. package/dist/auth/token_scope.d.ts +1 -1
  90. package/dist/auth/token_scope.js +1 -1
  91. package/dist/db/sql_columns.d.ts +1 -1
  92. package/dist/db/sql_columns.js +1 -1
  93. package/dist/http/CLAUDE.md +22 -16
  94. package/dist/http/auth_shape.d.ts +1 -1
  95. package/dist/http/db_routes.d.ts +7 -22
  96. package/dist/http/db_routes.d.ts.map +1 -1
  97. package/dist/http/db_routes.js +72 -72
  98. package/dist/http/error_schemas.d.ts +9 -21
  99. package/dist/http/error_schemas.d.ts.map +1 -1
  100. package/dist/http/error_schemas.js +14 -24
  101. package/dist/http/pending_effects.d.ts +42 -4
  102. package/dist/http/pending_effects.d.ts.map +1 -1
  103. package/dist/http/pending_effects.js +34 -4
  104. package/dist/http/route_spec.d.ts +2 -2
  105. package/dist/server/app_backend.d.ts.map +1 -1
  106. package/dist/server/app_backend.js +3 -1
  107. package/dist/server/app_server.d.ts +69 -18
  108. package/dist/server/app_server.d.ts.map +1 -1
  109. package/dist/server/app_server.js +74 -10
  110. package/dist/server/serve_fact_route.d.ts +3 -3
  111. package/dist/server/serve_fact_route.js +12 -12
  112. package/dist/testing/CLAUDE.md +259 -790
  113. package/dist/testing/adversarial_404.d.ts +6 -1
  114. package/dist/testing/adversarial_404.d.ts.map +1 -1
  115. package/dist/testing/adversarial_404.js +38 -17
  116. package/dist/testing/app_server.d.ts +20 -140
  117. package/dist/testing/app_server.d.ts.map +1 -1
  118. package/dist/testing/app_server.js +48 -125
  119. package/dist/testing/audit_drift_guard.d.ts +11 -6
  120. package/dist/testing/audit_drift_guard.d.ts.map +1 -1
  121. package/dist/testing/audit_drift_guard.js +15 -8
  122. package/dist/testing/auth_apps.js +1 -1
  123. package/dist/testing/cross_backend/account_lifecycle.d.ts.map +1 -1
  124. package/dist/testing/cross_backend/account_lifecycle.js +43 -0
  125. package/dist/testing/cross_backend/audit_cross_helpers.d.ts +12 -9
  126. package/dist/testing/cross_backend/audit_cross_helpers.d.ts.map +1 -1
  127. package/dist/testing/cross_backend/audit_cross_helpers.js +13 -14
  128. package/dist/testing/cross_backend/capabilities.d.ts +9 -6
  129. package/dist/testing/cross_backend/capabilities.d.ts.map +1 -1
  130. package/dist/testing/cross_backend/capabilities.js +11 -7
  131. package/dist/testing/cross_backend/capability_mounts.d.ts +103 -0
  132. package/dist/testing/cross_backend/capability_mounts.d.ts.map +1 -0
  133. package/dist/testing/cross_backend/capability_mounts.js +423 -0
  134. package/dist/testing/cross_backend/cell_moderate.js +2 -2
  135. package/dist/testing/cross_backend/cell_relations.d.ts.map +1 -1
  136. package/dist/testing/cross_backend/cell_relations.js +12 -11
  137. package/dist/testing/cross_backend/conformance_principal.d.ts +54 -0
  138. package/dist/testing/cross_backend/conformance_principal.d.ts.map +1 -0
  139. package/dist/testing/cross_backend/conformance_principal.js +283 -0
  140. package/dist/testing/cross_backend/conformance_table.d.ts +2 -50
  141. package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
  142. package/dist/testing/cross_backend/conformance_table.js +2 -270
  143. package/dist/testing/cross_backend/credential_deadline.d.ts.map +1 -1
  144. package/dist/testing/cross_backend/credential_deadline.js +2 -0
  145. package/dist/testing/cross_backend/credential_wire.d.ts +1 -1
  146. package/dist/testing/cross_backend/credential_wire.d.ts.map +1 -1
  147. package/dist/testing/cross_backend/db_state_diff.d.ts +72 -0
  148. package/dist/testing/cross_backend/db_state_diff.d.ts.map +1 -0
  149. package/dist/testing/cross_backend/db_state_diff.js +444 -0
  150. package/dist/testing/cross_backend/declared_skips.d.ts +68 -0
  151. package/dist/testing/cross_backend/declared_skips.d.ts.map +1 -0
  152. package/dist/testing/cross_backend/declared_skips.js +132 -0
  153. package/dist/testing/cross_backend/default_spine_surface.d.ts +1 -26
  154. package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
  155. package/dist/testing/cross_backend/default_spine_surface.js +2 -6
  156. package/dist/testing/cross_backend/differential.d.ts +11 -2
  157. package/dist/testing/cross_backend/differential.d.ts.map +1 -1
  158. package/dist/testing/cross_backend/differential.js +35 -11
  159. package/dist/testing/cross_backend/differential_normalize.d.ts +12 -0
  160. package/dist/testing/cross_backend/differential_normalize.d.ts.map +1 -1
  161. package/dist/testing/cross_backend/differential_normalize.js +16 -0
  162. package/dist/testing/cross_backend/full_spine_mount.d.ts +3 -10
  163. package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
  164. package/dist/testing/cross_backend/full_spine_mount.js +4 -4
  165. package/dist/testing/cross_backend/in_process_setup.d.ts +22 -12
  166. package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
  167. package/dist/testing/cross_backend/in_process_setup.js +151 -134
  168. package/dist/testing/cross_backend/login_security.d.ts +12 -0
  169. package/dist/testing/cross_backend/login_security.d.ts.map +1 -1
  170. package/dist/testing/cross_backend/login_security.js +21 -4
  171. package/dist/testing/cross_backend/request_target_conformance_wire.d.ts +0 -2
  172. package/dist/testing/cross_backend/request_target_conformance_wire.d.ts.map +1 -1
  173. package/dist/testing/cross_backend/request_target_conformance_wire.js +3 -1
  174. package/dist/testing/cross_backend/revocation_drivers.d.ts +18 -5
  175. package/dist/testing/cross_backend/revocation_drivers.d.ts.map +1 -1
  176. package/dist/testing/cross_backend/revocation_drivers.js +18 -10
  177. package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts +1 -1
  178. package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts.map +1 -1
  179. package/dist/testing/cross_backend/role_grant_offer_lifecycle.d.ts.map +1 -1
  180. package/dist/testing/cross_backend/role_grant_offer_lifecycle.js +33 -1
  181. package/dist/testing/cross_backend/role_grant_offer_notification_ws.d.ts +5 -4
  182. package/dist/testing/cross_backend/role_grant_offer_notification_ws.d.ts.map +1 -1
  183. package/dist/testing/cross_backend/role_grant_offer_notification_ws.js +183 -30
  184. package/dist/testing/cross_backend/role_grant_participation.js +4 -4
  185. package/dist/testing/cross_backend/rpc_adversarial.js +1 -1
  186. package/dist/testing/cross_backend/session_cap.d.ts +2 -2
  187. package/dist/testing/cross_backend/session_cap.d.ts.map +1 -1
  188. package/dist/testing/cross_backend/session_cap.js +48 -16
  189. package/dist/testing/cross_backend/setup.d.ts +31 -1
  190. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  191. package/dist/testing/cross_backend/setup.js +32 -1
  192. package/dist/testing/cross_backend/spine_suites.d.ts +123 -12
  193. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  194. package/dist/testing/cross_backend/spine_suites.js +454 -57
  195. package/dist/testing/cross_backend/spine_surface_constants.d.ts +1 -1
  196. package/dist/testing/cross_backend/spine_surface_constants.d.ts.map +1 -1
  197. package/dist/testing/cross_backend/spine_surface_constants.js +1 -0
  198. package/dist/testing/cross_backend/sse_round_trip.d.ts +9 -1
  199. package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
  200. package/dist/testing/cross_backend/sse_round_trip.js +123 -27
  201. package/dist/testing/cross_backend/testing_backdoor.d.ts.map +1 -1
  202. package/dist/testing/cross_backend/testing_backdoor.js +4 -2
  203. package/dist/testing/cross_backend/testing_reset_actions.d.ts +59 -27
  204. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  205. package/dist/testing/cross_backend/testing_reset_actions.js +66 -42
  206. package/dist/testing/cross_backend/transport_composition.d.ts +1 -1
  207. package/dist/testing/cross_backend/transport_composition.d.ts.map +1 -1
  208. package/dist/testing/cross_backend/transport_composition.js +7 -3
  209. package/dist/testing/cross_backend/ws_round_trip.d.ts +5 -0
  210. package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
  211. package/dist/testing/cross_backend/ws_round_trip.js +54 -8
  212. package/dist/testing/data_exposure.d.ts.map +1 -1
  213. package/dist/testing/data_exposure.js +3 -0
  214. package/dist/testing/db.d.ts +0 -32
  215. package/dist/testing/db.d.ts.map +1 -1
  216. package/dist/testing/db.js +0 -86
  217. package/dist/testing/db_entities.d.ts +14 -0
  218. package/dist/testing/db_entities.d.ts.map +1 -1
  219. package/dist/testing/db_entities.js +17 -0
  220. package/dist/testing/db_invariant_queries.d.ts +259 -0
  221. package/dist/testing/db_invariant_queries.d.ts.map +1 -0
  222. package/dist/testing/db_invariant_queries.js +400 -0
  223. package/dist/testing/db_invariants.d.ts +106 -0
  224. package/dist/testing/db_invariants.d.ts.map +1 -0
  225. package/dist/testing/db_invariants.gen.sql.d.ts +11 -0
  226. package/dist/testing/db_invariants.gen.sql.d.ts.map +1 -0
  227. package/dist/testing/db_invariants.gen.sql.js +13 -0
  228. package/dist/testing/db_invariants.js +141 -0
  229. package/dist/testing/db_invariants.sql +152 -0
  230. package/dist/testing/describe_db.d.ts +36 -0
  231. package/dist/testing/describe_db.d.ts.map +1 -0
  232. package/dist/testing/describe_db.js +71 -0
  233. package/dist/testing/integration.d.ts +6 -9
  234. package/dist/testing/integration.d.ts.map +1 -1
  235. package/dist/testing/integration.js +9 -13
  236. package/dist/testing/integration_helpers.d.ts +1 -1
  237. package/dist/testing/integration_helpers.d.ts.map +1 -1
  238. package/dist/testing/middleware.d.ts +1 -1
  239. package/dist/testing/middleware.js +5 -5
  240. package/dist/testing/rate_limiting.d.ts +1 -1
  241. package/dist/testing/rate_limiting.d.ts.map +1 -1
  242. package/dist/testing/rate_limiting.js +3 -2
  243. package/dist/testing/resolved_auth.d.ts +3 -1
  244. package/dist/testing/resolved_auth.d.ts.map +1 -1
  245. package/dist/testing/resolved_auth.js +4 -4
  246. package/dist/testing/role_grant_helpers.d.ts +1 -1
  247. package/dist/testing/role_grant_helpers.d.ts.map +1 -1
  248. package/dist/testing/round_trip.d.ts.map +1 -1
  249. package/dist/testing/round_trip.js +3 -0
  250. package/dist/testing/rpc_attack_surface.d.ts +8 -6
  251. package/dist/testing/rpc_attack_surface.d.ts.map +1 -1
  252. package/dist/testing/rpc_attack_surface.js +69 -6
  253. package/dist/testing/rpc_round_trip.d.ts +1 -1
  254. package/dist/testing/rpc_round_trip.d.ts.map +1 -1
  255. package/dist/testing/rpc_round_trip.js +3 -0
  256. package/dist/testing/sse_round_trip.d.ts +1 -1
  257. package/dist/testing/sse_round_trip.d.ts.map +1 -1
  258. package/dist/testing/sse_round_trip.js +13 -4
  259. package/dist/testing/stubs.d.ts +4 -2
  260. package/dist/testing/stubs.d.ts.map +1 -1
  261. package/dist/testing/stubs.js +15 -7
  262. package/dist/testing/test_app.d.ts +166 -0
  263. package/dist/testing/test_app.d.ts.map +1 -0
  264. package/dist/testing/test_app.js +124 -0
  265. package/dist/testing/ws_round_trip.d.ts.map +1 -1
  266. package/dist/testing/ws_round_trip.js +8 -4
  267. package/package.json +1 -1
@@ -15,9 +15,9 @@ 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
17
  import { query_create_role_grant, query_role_grant_by_id } from './role_grant_queries.js';
18
- import { offer_pending_predicate, role_grant_active_sql } from './role_grant_sql.js';
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
- import { query_account_lock_live_many } from './account_queries.js';
20
+ import { query_account_lock_for_offer_accept, query_account_lock_for_revoke, query_account_lock_live_many } from './account_queries.js';
21
21
  import { is_builtin_role, ROLE_ADMIN, ROLE_KEEPER } from './role_schema.js';
22
22
  /**
23
23
  * Error thrown by offer-lifecycle queries when the offer is in a non-pending
@@ -114,12 +114,16 @@ export class RoleGrantOfferActorAccountMismatchError extends Error {
114
114
  }
115
115
  }
116
116
  /**
117
- * Error thrown by `query_role_grant_offer_create` when the recipient is not an
118
- * active account: `to_account_id` names no account, a soft-deleted one, or one
119
- * with no active actor. A deleted recipient and one that never existed are the
120
- * one refusal, so a caller learns nothing from it that a missing id wouldn't
121
- * tell; the handler answers it as `role_grant_assign` answers the same
122
- * recipient. Twin of the Rust `RoleGrantOfferError::RecipientNotActive`.
117
+ * Error thrown when the recipient is not an active account.
118
+ *
119
+ * By `query_role_grant_offer_create`: `to_account_id` names no account, a
120
+ * soft-deleted one, or one with no active actor. A deleted recipient and one
121
+ * that never existed are the one refusal, so a caller learns nothing from it
122
+ * that a missing id wouldn't tell; the handler answers it as
123
+ * `role_grant_assign` answers the same recipient. By `query_accept_offer`: the
124
+ * accepting account — the caller — was deleted or purged after its credential
125
+ * was read, and its handler answers the dead credential. Twin of the Rust
126
+ * `RoleGrantOfferError::RecipientNotActive`.
123
127
  */
124
128
  export class RoleGrantOfferRecipientNotActiveError extends Error {
125
129
  constructor() {
@@ -354,8 +358,11 @@ export const query_role_grant_offer_history_for_account = async (deps, account_i
354
358
  * caller's `role_grant_offer_expire` audit inserts — committed together, or
355
359
  * rolled back together so the next sweep retries. Concurrent sweeps claim
356
360
  * disjoint rows: a row another sweep has stamped but not committed blocks on
357
- * its row lock, then fails the re-checked `expire_audited_at IS NULL`.
358
- * `cleanup_expired_role_grant_offers` is the caller.
361
+ * its row lock, then fails the re-checked `expire_audited_at IS NULL`. The
362
+ * rows are locked in id order (`offer_rows_locked_in_id_order`), as every
363
+ * multi-row offer write takes them, so a sweep and a delete, purge, revoke or
364
+ * accept over the same expired offers never wait on each other in opposite
365
+ * orders. `cleanup_expired_role_grant_offers` is the caller.
359
366
  *
360
367
  * @mutates `role_grant_offer` rows - stamps `expire_audited_at` on each claimed row
361
368
  */
@@ -363,9 +370,7 @@ export const query_role_grant_offer_sweep_expired = async (deps) => {
363
370
  return deps.db.query(`WITH claimed AS (
364
371
  UPDATE role_grant_offer
365
372
  SET expire_audited_at = NOW()
366
- WHERE ${offer_pending_predicate('')}
367
- AND expires_at <= NOW()
368
- AND expire_audited_at IS NULL
373
+ WHERE ${offer_rows_locked_in_id_order('', `${offer_pending_predicate('')} AND expires_at <= NOW() AND expire_audited_at IS NULL`)}
369
374
  -- raw columns; the outer select formats them
370
375
  RETURNING ${columns_sql(ROLE_GRANT_OFFER_COLUMNS)}
371
376
  )
@@ -378,14 +383,14 @@ export const query_role_grant_offer_sweep_expired = async (deps) => {
378
383
  * actor of an active account — the authority a builtin-role offer needs at the
379
384
  * moment its grant is minted.
380
385
  *
381
- * A locking read: `FOR SHARE OF rg` takes a share lock on the grant row that
382
- * admits, so a concurrent revoke (an `UPDATE` of that row) is strictly ordered
383
- * against this accept — it either commits first, and the recheck of the locked
384
- * row sees `revoked_at` set and excludes it, or it waits for this transaction
385
- * to commit the grant it authorized. A plain `EXISTS` could read the grant as
386
- * active while a revoke commits in between. The actor and account tombstones
387
- * are read but not locked — ordering a delete racing an accept isn't worth
388
- * locking two more tables. Twin of the Rust `grantor_holds_global_admin`.
386
+ * An unlocked read, run after `query_account_lock_for_offer_accept` has taken
387
+ * the grantor's account row `FOR SHARE` in the accept's transaction: every
388
+ * write that ends a grantor's global `admin` takes that row first — a revoke of
389
+ * 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
391
+ * `query_account_lock_for_purge` — so one that committed first is
392
+ * seen here (each statement reads afresh), and one that came later waits for
393
+ * the accept to commit. Twin of the Rust `grantor_holds_global_admin`.
389
394
  */
390
395
  const query_grantor_holds_global_admin = async (deps, grantor_actor_id) => {
391
396
  const row = await deps.db.query_one(`SELECT 1 AS held FROM role_grant rg
@@ -397,8 +402,7 @@ const query_grantor_holds_global_admin = async (deps, grantor_actor_id) => {
397
402
  AND ${role_grant_active_sql('rg')}
398
403
  AND act.deleted_at IS NULL
399
404
  AND a.deleted_at IS NULL
400
- LIMIT 1
401
- FOR SHARE OF rg`, [grantor_actor_id, ROLE_ADMIN]);
405
+ LIMIT 1`, [grantor_actor_id, ROLE_ADMIN]);
402
406
  return row != null;
403
407
  };
404
408
  /**
@@ -414,9 +418,29 @@ const query_grantor_holds_global_admin = async (deps, grantor_actor_id) => {
414
418
  * creating a duplicate or throwing — while that grant is still held. A grant
415
419
  * revoked since the accept, or gone, makes the offer terminal.
416
420
  *
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
423
+ * (`query_account_lock_for_offer_accept`), then the offer and the pending
424
+ * siblings it supersedes in one id-ordered claim, then the grant insert. The
425
+ * 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).
438
+ *
417
439
  * Error map:
418
440
  * - `RoleGrantOfferNotFoundError` — offer does not exist, or belongs to a
419
441
  * different recipient (IDOR guard). The offer row is untouched.
442
+ * - `RoleGrantOfferRecipientNotActiveError` — the recipient's account was
443
+ * deleted or purged since the caller's credential was read.
420
444
  * - `RoleGrantOfferAlreadyTerminalError` — offer is declined, retracted, or
421
445
  * superseded; or accepted, and its grant revoked, gone, or bound to another
422
446
  * actor.
@@ -424,8 +448,8 @@ const query_grantor_holds_global_admin = async (deps, grantor_actor_id) => {
424
448
  * - `RoleGrantOfferActorMismatchError` — actor-targeted offer, different actor.
425
449
  * - `RoleGrantOfferBuiltinRefusedError` — a builtin-role offer that must not
426
450
  * mint: `keeper` at all, a builtin bound to a scope, or one whose grantor no
427
- * longer holds an active global `admin` (read under `FOR SHARE`). The offer
428
- * stays pending.
451
+ * longer holds an active global `admin` (read under the grantor's account
452
+ * lock). The offer stays pending.
429
453
  *
430
454
  * Sibling supersede is what closes the "accept a pre-revoke sibling offer
431
455
  * to bypass a revoke" path: once A is accepted, B/C/... can no longer be
@@ -436,6 +460,7 @@ const query_grantor_holds_global_admin = async (deps, grantor_actor_id) => {
436
460
  * @mutates `role_grant_offer` siblings - stamps `superseded_at` on every other pending offer for the tuple
437
461
  * @mutates `audit_log` table - emits `role_grant_offer_accept` + `role_grant_create` + one `role_grant_offer_supersede` per sibling
438
462
  * @throws RoleGrantOfferNotFoundError if the offer is missing or belongs to another recipient
463
+ * @throws RoleGrantOfferRecipientNotActiveError if the recipient's account is deleted or gone
439
464
  * @throws RoleGrantOfferAlreadyTerminalError if the offer is declined, retracted, or superseded, or accepted with its grant revoked, gone, or bound to another actor
440
465
  * @throws RoleGrantOfferExpiredError if the offer is pending but past `expires_at`
441
466
  * @throws RoleGrantOfferActorMismatchError if the offer is actor-targeted to a different actor
@@ -444,29 +469,57 @@ const query_grantor_holds_global_admin = async (deps, grantor_actor_id) => {
444
469
  */
445
470
  export const query_accept_offer = async (deps, input) => {
446
471
  const { offer_id, to_account_id, actor_id, ip } = input;
447
- // Claim the offer with a row-level lock. Subsequent concurrent callers
448
- // block on the lock until this transaction commits/rolls back; after commit
449
- // they see the new state (accepted or terminal) and branch idempotently.
450
- // We defer writing `accepted_at` until the role_grant row exists — the
451
- // `role_grant_offer_role_grant_iff_accepted` CHECK constraint demands both be set
452
- // (or neither) at row-visibility time.
472
+ // The grantor, role and scope are written once with the offer and never
473
+ // updated, so they are read without a lock: they name the rows to lock.
474
+ // Everything the claim checks is checked on the claimed row.
475
+ const head = await deps.db.query_one(`SELECT o.from_actor_id, o.role, o.scope_id, grantor.account_id AS from_account_id
476
+ FROM role_grant_offer o
477
+ JOIN actor grantor ON grantor.id = o.from_actor_id
478
+ WHERE o.id = $1 AND o.to_account_id = $2`, [offer_id, to_account_id]);
479
+ if (!head) {
480
+ throw new RoleGrantOfferNotFoundError(offer_id);
481
+ }
482
+ // An unscoped `admin` offer's grantor must still hold a global `admin` when
483
+ // the grant is minted.
484
+ const needs_grantor_admin = head.role === ROLE_ADMIN && head.scope_id === null;
485
+ if (!(await query_account_lock_for_offer_accept(deps, to_account_id, needs_grantor_admin ? head.from_account_id : null))) {
486
+ throw new RoleGrantOfferRecipientNotActiveError();
487
+ }
488
+ const grantor_holds_admin = needs_grantor_admin
489
+ ? await query_grantor_holds_global_admin(deps, head.from_actor_id)
490
+ : null;
491
+ // Claim the offer and every pending sibling it may supersede — one
492
+ // statement, in id order. A concurrent caller on any of these rows blocks
493
+ // until this transaction ends, then sees the new state. No sibling can be
494
+ // added meanwhile: an offer to the recipient takes its account row, which
495
+ // this transaction holds. We defer writing `accepted_at` until the
496
+ // role_grant row exists — the `role_grant_offer_role_grant_iff_accepted`
497
+ // CHECK constraint demands both be set (or neither) at row-visibility time.
453
498
  //
454
- // Trailing `expires_at > NOW() AS still_valid` rides on the FOR UPDATE row
455
- // so the expiry branch below compares the **raw** column on Postgres' clock
456
- // — the projected `expires_at` is floored to the second, so comparing it in
499
+ // Trailing `expires_at > NOW() AS still_valid` rides on the locked row so
500
+ // the expiry branch below compares the **raw** column on Postgres' clock —
501
+ // the projected `expires_at` is floored to the second, so comparing it in
457
502
  // JS would report expiry up to a second early. Twin of the Rust
458
503
  // `query_accept_offer`'s `o.expires_at > NOW() AS still_valid`.
459
- const locked_row = await deps.db.query_one(`SELECT ${columns_sql(ROLE_GRANT_OFFER_COLUMNS, role_grant_offer_expr(''))},
460
- role_grant_offer.expires_at > NOW() AS still_valid
461
- FROM role_grant_offer
462
- WHERE id = $1 AND to_account_id = $2
463
- FOR UPDATE`, [offer_id, to_account_id]);
504
+ const claimed = await deps.db.query(`SELECT ${qualify_columns(ROLE_GRANT_OFFER_COLUMNS, 'o', role_grant_offer_expr('o'))},
505
+ o.expires_at > NOW() AS still_valid,
506
+ grantor.account_id AS from_account_id
507
+ FROM role_grant_offer o
508
+ JOIN actor grantor ON grantor.id = o.from_actor_id
509
+ WHERE o.to_account_id = $2
510
+ AND (o.id = $1
511
+ OR (o.role = $3 AND o.scope_id IS NOT DISTINCT FROM $4
512
+ AND ${offer_pending_predicate('o')}))
513
+ ORDER BY o.id
514
+ FOR UPDATE OF o`, [offer_id, to_account_id, head.role, head.scope_id]);
515
+ const locked_row = claimed.find((row) => row.id === offer_id);
464
516
  if (!locked_row) {
465
517
  throw new RoleGrantOfferNotFoundError(offer_id);
466
518
  }
467
- // `still_valid` is the comparison result, not a column of the offer — split
468
- // it off so `locked` stays exactly `ROLE_GRANT_OFFER_COLUMNS`.
469
- const { still_valid, ...locked } = locked_row;
519
+ const sibling_ids = claimed.filter((row) => row.id !== offer_id).map((row) => row.id);
520
+ // `still_valid` and `from_account_id` are not columns of the offer — split
521
+ // them off so `locked` stays exactly `ROLE_GRANT_OFFER_COLUMNS`.
522
+ const { still_valid, from_account_id, ...locked } = locked_row;
470
523
  if (locked.accepted_at) {
471
524
  // Already accepted (a replay, or a race winner already committed);
472
525
  // return the pre-existing role_grant.
@@ -488,6 +541,7 @@ export const query_accept_offer = async (deps, input) => {
488
541
  return {
489
542
  role_grant,
490
543
  offer: locked,
544
+ from_account_id,
491
545
  created: false,
492
546
  superseded_offers: [],
493
547
  audit_events: []
@@ -498,7 +552,7 @@ export const query_accept_offer = async (deps, input) => {
498
552
  }
499
553
  // Expiry check AFTER the accepted-path: a validly-accepted offer past its
500
554
  // expires_at still returns the role_grant idempotently. Only pending offers
501
- // past expiry reach this branch. `still_valid` rode on the FOR UPDATE row
555
+ // past expiry reach this branch. `still_valid` rode on the claimed row
502
556
  // above, so the comparison stays on the raw column and Postgres' clock.
503
557
  if (!still_valid) {
504
558
  throw new RoleGrantOfferExpiredError(offer_id);
@@ -538,7 +592,9 @@ export const query_accept_offer = async (deps, input) => {
538
592
  // Builtin roles are global-only.
539
593
  refusal = 'scoped';
540
594
  }
541
- else if (!(await query_grantor_holds_global_admin(deps, locked.from_actor_id))) {
595
+ else if (grantor_holds_admin !== true) {
596
+ // read under the grantor's account lock before the claim, on the same
597
+ // immutable grantor the claimed row carries
542
598
  refusal = 'grantor_not_authorized';
543
599
  }
544
600
  if (refusal !== null) {
@@ -571,23 +627,22 @@ export const query_accept_offer = async (deps, input) => {
571
627
  WHERE id = $1
572
628
  RETURNING ${columns_sql(ROLE_GRANT_OFFER_COLUMNS, role_grant_offer_expr(''))}`, [locked.id, role_grant.id]);
573
629
  const offer = assert_row(offer_accepted, 'mark offer accepted');
574
- // Supersede sibling pending offers for the same (to_account, role, scope).
575
- // Forecloses the "accept this other sibling later to get the role back
576
- // after a revoke" path — any pending offer for this tuple at accept time
577
- // is obsoleted by the accept. CTE joins `actor` to surface each sibling's
578
- // grantor `account_id` for the caller's notification fan-out.
579
- const superseded = await deps.db.query(`WITH updated AS (
580
- UPDATE role_grant_offer
581
- SET superseded_at = NOW()
582
- WHERE to_account_id = $1
583
- AND role = $2
584
- AND scope_id IS NOT DISTINCT FROM $3
585
- AND id <> $4
586
- AND ${offer_pending_predicate('')}
587
- -- raw columns; the outer grantor select formats them
588
- RETURNING ${columns_sql(ROLE_GRANT_OFFER_COLUMNS)}
589
- )
590
- ${ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT}`, [to_account_id, offer.role, offer.scope_id, offer.id]);
630
+ // Supersede the sibling pending offers for the same (to_account, role,
631
+ // scope), the rows the claim locked. Forecloses the "accept this other
632
+ // sibling later to get the role back after a revoke" path — any pending
633
+ // offer for this tuple at accept time is obsoleted by the accept. CTE joins
634
+ // `actor` to surface each sibling's grantor `account_id` for the caller's
635
+ // notification fan-out.
636
+ const superseded = sibling_ids.length === 0
637
+ ? []
638
+ : await deps.db.query(`WITH updated AS (
639
+ UPDATE role_grant_offer
640
+ SET superseded_at = NOW()
641
+ WHERE id = ANY($1) AND ${offer_pending_predicate('')}
642
+ -- raw columns; the outer grantor select formats them
643
+ RETURNING ${columns_sql(ROLE_GRANT_OFFER_COLUMNS)}
644
+ )
645
+ ${ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT}`, [sibling_ids]);
591
646
  // Emit audit events in-transaction (atomic with the role_grant insert).
592
647
  // The `RETURNING` after the SET guarantees `offer.resulting_role_grant_id === role_grant.id`.
593
648
  // Accept binds the actor deterministically — populate both target
@@ -650,6 +705,7 @@ export const query_accept_offer = async (deps, input) => {
650
705
  return {
651
706
  role_grant,
652
707
  offer,
708
+ from_account_id,
653
709
  created: true,
654
710
  superseded_offers: superseded,
655
711
  audit_events: [offer_accept_event, role_grant_create_event, ...supersede_events]
@@ -661,12 +717,15 @@ export const query_accept_offer = async (deps, input) => {
661
717
  *
662
718
  * An offer carries no record of the authority it was made under, so one
663
719
  * outliving that authority would still confer its role on accept. Revoking a
664
- * grant (`role_grant_revoke`, the revokee's actor) and removing an account
665
- * (`account_delete` / `account_purge`, every actor of the account) end the
666
- * grantor's outgoing pending offers in the same transaction. Each row carries
720
+ * grant (`query_revoke_role_grant`, the revokee's actor) and removing an
721
+ * account (`account_delete` / `account_purge`, every actor of the account) end
722
+ * the grantor's outgoing pending offers in the same transaction. Each row carries
667
723
  * the grantor's `from_account_id`, as every supersede cascade's does; the
668
- * caller emits a `role_grant_offer_supersede` audit event per entry, and the
669
- * revoke caller also sends the supersede notification. Twin of the Rust
724
+ * caller emits a `role_grant_offer_supersede` audit event per entry and
725
+ * queues its notifications (`queue_supersede_notifications`). The rows are
726
+ * taken in id order; a caller that writes other offers too locks them all
727
+ * first (`query_role_grant_offer_lock_for_account_removal`,
728
+ * `query_role_grant_offer_lock_for_revoke`). Twin of the Rust
670
729
  * `query_role_grant_offer_supersede_outgoing`.
671
730
  *
672
731
  * @param deps - query dependencies
@@ -680,8 +739,7 @@ export const query_role_grant_offer_supersede_outgoing = async (deps, from_actor
680
739
  return deps.db.query(`WITH updated AS (
681
740
  UPDATE role_grant_offer
682
741
  SET superseded_at = NOW()
683
- WHERE from_actor_id = ANY($1)
684
- AND ${offer_pending_predicate('')}
742
+ WHERE ${offer_rows_locked_in_id_order('', `from_actor_id = ANY($1) AND ${offer_pending_predicate('')}`)}
685
743
  -- raw columns; the outer grantor select formats them
686
744
  RETURNING ${columns_sql(ROLE_GRANT_OFFER_COLUMNS)}
687
745
  )
@@ -689,7 +747,7 @@ export const query_role_grant_offer_supersede_outgoing = async (deps, from_actor
689
747
  };
690
748
  /**
691
749
  * Supersede every pending offer **made to** `to_account_id` — the
692
- * recipient-side cascade of an account delete.
750
+ * recipient-side cascade of an account delete or purge.
693
751
  *
694
752
  * An undelete restores the account, so an offer left pending through the
695
753
  * delete would come back with it and could then be accepted; superseding it
@@ -697,8 +755,11 @@ export const query_role_grant_offer_supersede_outgoing = async (deps, from_actor
697
755
  * `to_account_id` alone covers the actor-addressed offers too: an offer's
698
756
  * `to_actor_id` is bound to its `to_account_id` at create. Each row carries
699
757
  * the grantor's `from_account_id`, as every supersede cascade's does; the
700
- * caller emits a `role_grant_offer_supersede` audit event per entry. Twin of
701
- * the Rust `query_role_grant_offer_supersede_incoming`.
758
+ * caller emits a `role_grant_offer_supersede` audit event per entry and
759
+ * queues its notifications (`queue_supersede_notifications`). The rows are
760
+ * taken in id order, after the caller's
761
+ * `query_role_grant_offer_lock_for_account_removal`. Twin of the Rust
762
+ * `query_role_grant_offer_supersede_incoming`.
702
763
  *
703
764
  * @param deps - query dependencies
704
765
  * @param to_account_id - the recipient account whose pending offers end
@@ -709,10 +770,138 @@ export const query_role_grant_offer_supersede_incoming = async (deps, to_account
709
770
  return deps.db.query(`WITH updated AS (
710
771
  UPDATE role_grant_offer
711
772
  SET superseded_at = NOW()
712
- WHERE to_account_id = $1
713
- AND ${offer_pending_predicate('')}
773
+ WHERE ${offer_rows_locked_in_id_order('', `to_account_id = $1 AND ${offer_pending_predicate('')}`)}
714
774
  -- raw columns; the outer grantor select formats them
715
775
  RETURNING ${columns_sql(ROLE_GRANT_OFFER_COLUMNS)}
716
776
  )
717
777
  ${ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT}`, [to_account_id]);
718
778
  };
779
+ /**
780
+ * Lock every pending offer an account removal supersedes — the ones its actors
781
+ * made and the ones made to it — in one statement, in id order.
782
+ *
783
+ * A delete or a purge writes those offers in two statements, the outgoing
784
+ * then the incoming; taken that way, each could hold rows another writer of
785
+ * both sets (a revoke of the account's grant, the expiry sweep) waits on while
786
+ * waiting on rows that writer holds. Locked together first, in the order every
787
+ * multi-row offer write takes its rows, the two supersedes then write rows
788
+ * this transaction already holds. No offer to or from the account can be made
789
+ * meanwhile: an offer create takes both parties' account rows, which the
790
+ * removal has written or locked. Twin of the Rust
791
+ * `query_role_grant_offer_lock_for_account_removal`.
792
+ *
793
+ * @param deps - query dependencies (must be the removal's transaction)
794
+ * @param to_account_id - the removed account
795
+ * @param from_actor_ids - the removed account's actors
796
+ * @mutates `role_grant_offer` rows - row-locks every pending offer to the account or from its actors
797
+ */
798
+ export const query_role_grant_offer_lock_for_account_removal = async (deps, to_account_id, from_actor_ids) => {
799
+ await deps.db.query(`SELECT id FROM role_grant_offer
800
+ WHERE (to_account_id = $1 OR from_actor_id = ANY($2))
801
+ AND ${offer_pending_predicate('')}
802
+ ORDER BY id
803
+ FOR UPDATE`, [to_account_id, from_actor_ids]);
804
+ };
805
+ /**
806
+ * Lock every pending offer a revoke supersedes (`query_revoke_role_grant`) — the revokee
807
+ * account's incoming offers of the revoked grant's role and scope, and every
808
+ * offer the revokee's actor made — in one statement, in id order.
809
+ *
810
+ * Taken after the revokee's account row (`query_account_lock_for_revoke`),
811
+ * which keeps any new offer to or from the account out, and before the grant's
812
+ * update: an accept claims its offers before it inserts its grant (whose unique
813
+ * slot waits on a revoke's uncommitted update of the same grant), so a revoke
814
+ * updating the grant first and then reaching the offers the accept holds would
815
+ * close a cycle. Then the revoke's two supersedes write rows it already holds,
816
+ * in the order every multi-row offer write takes its rows. Twin of the Rust
817
+ * `query_role_grant_offer_lock_for_revoke`.
818
+ *
819
+ * @param deps - query dependencies (must be the revoke's transaction)
820
+ * @param actor_id - the revokee actor
821
+ * @param role - the revoked grant's role
822
+ * @param scope_id - the revoked grant's scope, `null` for global
823
+ * @mutates `role_grant_offer` rows - row-locks the pending offers the revoke supersedes
824
+ */
825
+ export const query_role_grant_offer_lock_for_revoke = async (deps, actor_id, role, scope_id) => {
826
+ await deps.db.query(`SELECT id FROM role_grant_offer
827
+ WHERE ((to_account_id = (SELECT account_id FROM actor WHERE id = $1)
828
+ AND role = $2 AND scope_id IS NOT DISTINCT FROM $3)
829
+ OR from_actor_id = $1)
830
+ AND ${offer_pending_predicate('')}
831
+ ORDER BY id
832
+ FOR UPDATE`, [actor_id, role, scope_id]);
833
+ };
834
+ /**
835
+ * Revoke a role_grant by id, constrained to a specific actor, and end the
836
+ * pending offers the grant stood behind — the one supersede rule every revoke
837
+ * follows, the admin `role_grant_revoke`, a self-service disable, and a
838
+ * consumer's direct call alike.
839
+ *
840
+ * Requires `actor_id` to prevent cross-account revocation (IDOR guard).
841
+ * Returns `null` if the role_grant is not found, already revoked, or belongs
842
+ * to a different actor.
843
+ *
844
+ * Two sets of offers end with the grant, in the same transaction:
845
+ *
846
+ * - the revokee account's pending offers of the grant's `(role, scope)` — an
847
+ * offer accepted after the revoke would hand the role straight back;
848
+ * - every pending offer the revokee's actor made, whatever its role — an
849
+ * offer records no authority, so one made under this grant would still
850
+ * confer on accept.
851
+ *
852
+ * Locks follow the class order (accounts, then offers, then grants): the
853
+ * revokee's account row (`query_account_lock_for_revoke`), which keeps any new
854
+ * offer to or from the account out and any accept by it waiting, then both
855
+ * sets of offers in one id-ordered statement
856
+ * (`query_role_grant_offer_lock_for_revoke`), then the grant's update — an
857
+ * accept claims its offers before it inserts its grant, and that insert waits
858
+ * on an uncommitted update of the same grant, so the grant first and the
859
+ * offers second would close a cycle. The two supersedes then write only rows
860
+ * the transaction holds. Twin of the Rust
861
+ * `fuz_auth::role_grant_offer_queries::query_revoke_role_grant`.
862
+ *
863
+ * @param deps - query dependencies (must be the revoke's transaction)
864
+ * @param role_grant_id - the role_grant to revoke
865
+ * @param actor_id - the actor that must own the role_grant
866
+ * @param revoked_by - the actor who revoked it (for audit trail)
867
+ * @param reason - optional free-form reason, stamped on `role_grant.revoked_reason` and surfaced to the revokee notification.
868
+ * @mutates `role_grant` row - sets `revoked_at`, `revoked_by`, and `revoked_reason`
869
+ * @mutates `role_grant_offer` rows - stamps `superseded_at` on every pending incoming sibling for the same `(account, role, scope)` and every pending offer from `actor_id`
870
+ */
871
+ export const query_revoke_role_grant = async (deps, role_grant_id, actor_id, revoked_by, reason) => {
872
+ await query_account_lock_for_revoke(deps, actor_id);
873
+ // the role and scope name the offers to lock; written once with the grant
874
+ 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]);
875
+ if (!target)
876
+ return null;
877
+ await query_role_grant_offer_lock_for_revoke(deps, actor_id, target.role, target.scope_id);
878
+ const rows = await deps.db.query(`UPDATE role_grant SET revoked_at = NOW(), revoked_by = $3, revoked_reason = $4
879
+ WHERE id = $1 AND actor_id = $2 AND revoked_at IS NULL
880
+ RETURNING id, role, scope_kind, scope_id`, [role_grant_id, actor_id, revoked_by ?? null, reason ?? null]);
881
+ const revoked = rows[0];
882
+ if (!revoked)
883
+ return null;
884
+ // CTE joins `actor` after the UPDATE so each superseded row carries the
885
+ // grantor's `account_id` — callers fan out `role_grant_offer_supersede`
886
+ // notifications to that account without a second round-trip. The match
887
+ // keys on `scope_id` only because the (scope_kind, scope_id) pair-CHECK
888
+ // makes scope_kind a function of scope_id; matching on both adds no
889
+ // new selectivity in v1.
890
+ const incoming = await deps.db.query(`WITH updated AS (
891
+ UPDATE role_grant_offer o
892
+ SET superseded_at = NOW()
893
+ WHERE ${offer_rows_locked_in_id_order('o', `to_account_id = (SELECT account_id FROM actor WHERE id = $1)
894
+ AND role = $2 AND scope_id IS NOT DISTINCT FROM $3 AND ${offer_pending_predicate('')}`)}
895
+ -- raw columns; the outer grantor select formats them
896
+ RETURNING ${qualify_columns(ROLE_GRANT_OFFER_COLUMNS, 'o')}
897
+ )
898
+ ${ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT}`, [actor_id, revoked.role, revoked.scope_id]);
899
+ const outgoing = await query_role_grant_offer_supersede_outgoing(deps, [actor_id]);
900
+ return {
901
+ id: revoked.id,
902
+ role: revoked.role,
903
+ scope_kind: revoked.scope_kind,
904
+ scope_id: revoked.scope_id,
905
+ superseded_offers: [...incoming, ...outgoing]
906
+ };
907
+ };
@@ -59,7 +59,7 @@ export const RoleGrantOfferJson = meta(object({
59
59
  description: 'ISO timestamp when the grantor retracted the offer.'
60
60
  }),
61
61
  superseded_at: meta(nullable(Timestamp), {
62
- description: 'ISO timestamp when this offer was obsoleted by a sibling accept or by revoke of the resulting role_grant.'
62
+ description: 'ISO timestamp when a cascade ended this pending offer: a sibling accept, a revoke of the recipient’s grant of its role and scope, an admin revoke of any grant of its grantor’s actor, the removal of either party’s account, or the teardown of its scope.'
63
63
  }),
64
64
  resulting_role_grant_id: meta(nullable(UuidSchema), {
65
65
  description: 'Role grant produced by accepting this offer. `null` until/unless accepted.'