@fuzdev/fuz_app 0.181.0 → 0.182.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/dist/actions/CLAUDE.md +24 -6
  2. package/dist/actions/action_rpc.d.ts +11 -1
  3. package/dist/actions/action_rpc.d.ts.map +1 -1
  4. package/dist/actions/action_rpc.js +3 -2
  5. package/dist/actions/action_types.d.ts +16 -1
  6. package/dist/actions/action_types.d.ts.map +1 -1
  7. package/dist/actions/action_types.js +1 -1
  8. package/dist/actions/notification_sender.d.ts +83 -0
  9. package/dist/actions/notification_sender.d.ts.map +1 -0
  10. package/dist/actions/notification_sender.js +85 -0
  11. package/dist/actions/perform_action.d.ts +9 -0
  12. package/dist/actions/perform_action.d.ts.map +1 -1
  13. package/dist/actions/perform_action.js +22 -13
  14. package/dist/actions/register_action_ws.d.ts +14 -0
  15. package/dist/actions/register_action_ws.d.ts.map +1 -1
  16. package/dist/actions/register_action_ws.js +3 -2
  17. package/dist/actions/ws_socket_session.d.ts +3 -0
  18. package/dist/actions/ws_socket_session.d.ts.map +1 -1
  19. package/dist/actions/ws_socket_session.js +2 -1
  20. package/dist/auth/CLAUDE.md +60 -41
  21. package/dist/auth/account_actions.d.ts.map +1 -1
  22. package/dist/auth/account_actions.js +13 -3
  23. package/dist/auth/account_queries.d.ts +158 -16
  24. package/dist/auth/account_queries.d.ts.map +1 -1
  25. package/dist/auth/account_queries.js +189 -18
  26. package/dist/auth/account_routes.d.ts +1 -1
  27. package/dist/auth/account_routes.d.ts.map +1 -1
  28. package/dist/auth/account_routes.js +27 -26
  29. package/dist/auth/admin_actions.d.ts.map +1 -1
  30. package/dist/auth/admin_actions.js +71 -28
  31. package/dist/auth/api_token_queries.d.ts +13 -5
  32. package/dist/auth/api_token_queries.d.ts.map +1 -1
  33. package/dist/auth/api_token_queries.js +24 -9
  34. package/dist/auth/bearer_auth.d.ts.map +1 -1
  35. package/dist/auth/bearer_auth.js +3 -2
  36. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  37. package/dist/auth/daemon_token_middleware.js +12 -1
  38. package/dist/auth/deps.d.ts +10 -0
  39. package/dist/auth/deps.d.ts.map +1 -1
  40. package/dist/auth/request_context.d.ts +73 -73
  41. package/dist/auth/request_context.d.ts.map +1 -1
  42. package/dist/auth/request_context.js +78 -96
  43. package/dist/auth/resolved_auth.d.ts +10 -5
  44. package/dist/auth/resolved_auth.d.ts.map +1 -1
  45. package/dist/auth/resolved_auth.js +2 -2
  46. package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
  47. package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
  48. package/dist/auth/role_grant_offer_action_specs.js +2 -1
  49. package/dist/auth/role_grant_offer_actions.d.ts +7 -11
  50. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  51. package/dist/auth/role_grant_offer_actions.js +61 -93
  52. package/dist/auth/role_grant_offer_notifications.d.ts +87 -38
  53. package/dist/auth/role_grant_offer_notifications.d.ts.map +1 -1
  54. package/dist/auth/role_grant_offer_notifications.js +75 -22
  55. package/dist/auth/role_grant_offer_queries.d.ts +96 -15
  56. package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
  57. package/dist/auth/role_grant_offer_queries.js +185 -70
  58. package/dist/auth/role_grant_offer_schema.js +1 -1
  59. package/dist/auth/role_grant_queries.d.ts +52 -12
  60. package/dist/auth/role_grant_queries.d.ts.map +1 -1
  61. package/dist/auth/role_grant_queries.js +75 -24
  62. package/dist/auth/role_grant_sql.d.ts +21 -0
  63. package/dist/auth/role_grant_sql.d.ts.map +1 -1
  64. package/dist/auth/role_grant_sql.js +21 -0
  65. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  66. package/dist/auth/self_service_role_actions.js +9 -2
  67. package/dist/auth/session_queries.d.ts +34 -13
  68. package/dist/auth/session_queries.d.ts.map +1 -1
  69. package/dist/auth/session_queries.js +48 -18
  70. package/dist/auth/standard_rpc_actions.d.ts +6 -19
  71. package/dist/auth/standard_rpc_actions.d.ts.map +1 -1
  72. package/dist/auth/standard_rpc_actions.js +5 -6
  73. package/dist/auth/token_scope.d.ts +1 -1
  74. package/dist/auth/token_scope.js +1 -1
  75. package/dist/http/CLAUDE.md +7 -6
  76. package/dist/http/auth_shape.d.ts +1 -1
  77. package/dist/http/error_schemas.d.ts +9 -21
  78. package/dist/http/error_schemas.d.ts.map +1 -1
  79. package/dist/http/error_schemas.js +14 -24
  80. package/dist/http/route_spec.d.ts +2 -2
  81. package/dist/server/app_backend.d.ts.map +1 -1
  82. package/dist/server/app_backend.js +3 -1
  83. package/dist/server/app_server.d.ts +26 -16
  84. package/dist/server/app_server.d.ts.map +1 -1
  85. package/dist/server/app_server.js +17 -7
  86. package/dist/server/serve_fact_route.d.ts +3 -3
  87. package/dist/server/serve_fact_route.js +12 -12
  88. package/dist/testing/CLAUDE.md +5 -5
  89. package/dist/testing/app_server.d.ts.map +1 -1
  90. package/dist/testing/app_server.js +3 -1
  91. package/dist/testing/auth_apps.js +1 -1
  92. package/dist/testing/cross_backend/account_lifecycle.d.ts.map +1 -1
  93. package/dist/testing/cross_backend/account_lifecycle.js +43 -0
  94. package/dist/testing/cross_backend/default_spine_surface.d.ts +1 -26
  95. package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
  96. package/dist/testing/cross_backend/default_spine_surface.js +2 -6
  97. package/dist/testing/cross_backend/full_spine_mount.d.ts +3 -10
  98. package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
  99. package/dist/testing/cross_backend/full_spine_mount.js +4 -4
  100. package/dist/testing/cross_backend/role_grant_offer_notification_ws.d.ts +5 -4
  101. package/dist/testing/cross_backend/role_grant_offer_notification_ws.d.ts.map +1 -1
  102. package/dist/testing/cross_backend/role_grant_offer_notification_ws.js +183 -30
  103. package/dist/testing/cross_backend/session_cap.d.ts.map +1 -1
  104. package/dist/testing/cross_backend/session_cap.js +26 -5
  105. package/dist/testing/middleware.d.ts +1 -1
  106. package/dist/testing/middleware.js +5 -5
  107. package/dist/testing/resolved_auth.d.ts +3 -1
  108. package/dist/testing/resolved_auth.d.ts.map +1 -1
  109. package/dist/testing/resolved_auth.js +4 -4
  110. package/dist/testing/stubs.d.ts.map +1 -1
  111. package/dist/testing/stubs.js +5 -2
  112. package/dist/testing/ws_round_trip.d.ts.map +1 -1
  113. package/dist/testing/ws_round_trip.js +8 -4
  114. package/package.json +1 -1
@@ -44,13 +44,12 @@
44
44
  * transaction, a failure row on the pool). `audit.notify` (SSE/WS broadcast)
45
45
  * fires post-commit in both paths.
46
46
  *
47
- * WS notifications fan out post-commit via `emit_after_commit` when a
48
- * `notification_sender` is wired: offer lifecycle transitions notify the
47
+ * WS notifications go through the handler context's sender
48
+ * (`ctx.notification_sender`), queued post-commit (`queue_offer_notification`,
49
+ * `queue_supersede_notifications`): offer lifecycle transitions notify the
49
50
  * counterparty, `role_grant_revoke` notifies the revokee (`role_grant_revoked`)
50
- * plus each superseded
51
- * pending offer's grantor. Under DEV each notification's params are checked
52
- * against its spec's schema before it is sent, a mismatch logged
53
- * (`create_validated_notification_sender`). `role_grant_revoke` of a global
51
+ * plus both parties of each superseded pending offer, and an accept each
52
+ * superseded sibling's grantor. `role_grant_revoke` of a global
54
53
  * grant also closes the revokee's connections gated on the role through
55
54
  * `deps.connection_closer`, after the commit.
56
55
  *
@@ -63,11 +62,11 @@ import { queue_connection_close } from '../actions/connection_closer.js';
63
62
  import { builtin_role_specs, is_builtin_role, ROLE_ADMIN, ROLE_KEEPER, role_has_grant_path } from './role_schema.js';
64
63
  import { GRANT_PATH_ADMIN } from './grant_path_schema.js';
65
64
  import { ROLE_GRANT_OFFER_DEFAULT_TTL_MS, to_role_grant_offer_json } from './role_grant_offer_schema.js';
66
- import { query_role_grant_offer_create, query_role_grant_offer_decline, query_role_grant_offer_retract, query_role_grant_offer_list, query_role_grant_offer_history_for_account, query_accept_offer, RoleGrantOfferActorAccountMismatchError, RoleGrantOfferActorMismatchError, RoleGrantOfferGrantorNotActiveError, RoleGrantOfferRecipientNotActiveError, RoleGrantOfferAlreadyTerminalError, RoleGrantOfferBuiltinRefusedError, RoleGrantOfferExpiredError, RoleGrantOfferNotFoundError, RoleGrantOfferSelfTargetError, query_role_grant_offer_supersede_outgoing } from './role_grant_offer_queries.js';
67
- import { query_account_has_active_global_role, query_account_has_global_role, query_count_active_accounts_with_global_role_for_update, query_create_role_grant, query_role_grant_find_active_role_for_actor, query_revoke_role_grant } from './role_grant_queries.js';
68
- import { query_actor_by_id, query_active_actors_by_account } from './account_queries.js';
65
+ import { query_role_grant_offer_create, query_role_grant_offer_decline, query_role_grant_offer_retract, query_role_grant_offer_list, query_role_grant_offer_history_for_account, query_accept_offer, RoleGrantOfferActorAccountMismatchError, RoleGrantOfferActorMismatchError, RoleGrantOfferGrantorNotActiveError, RoleGrantOfferRecipientNotActiveError, RoleGrantOfferAlreadyTerminalError, RoleGrantOfferBuiltinRefusedError, RoleGrantOfferExpiredError, RoleGrantOfferNotFoundError, RoleGrantOfferSelfTargetError, query_role_grant_offer_lock_for_revoke, query_role_grant_offer_supersede_outgoing } from './role_grant_offer_queries.js';
66
+ import { query_account_has_active_global_role, query_account_has_global_role, query_count_active_accounts_with_global_role_locked, query_create_role_grant, query_role_grant_find_active_role_for_actor, query_revoke_role_grant } from './role_grant_queries.js';
67
+ import { query_account_lock_for_revoke, query_active_actors_by_account } from './account_queries.js';
69
68
  import { has_scoped_role } from './request_context.js';
70
- import { build_role_grant_offer_accepted_notification, build_role_grant_offer_declined_notification, build_role_grant_offer_received_notification, build_role_grant_offer_retracted_notification, build_role_grant_offer_supersede_notification, build_role_grant_revoked_notification, create_validated_notification_sender } from './role_grant_offer_notifications.js';
69
+ import { build_role_grant_offer_accepted_notification, build_role_grant_offer_declined_notification, build_role_grant_offer_received_notification, build_role_grant_offer_retracted_notification, build_role_grant_revoked_notification, queue_offer_notification, queue_supersede_notifications } from './role_grant_offer_notifications.js';
71
70
  import { ERROR_AUTHENTICATION_REQUIRED, ERROR_INSUFFICIENT_PERMISSIONS, ERROR_ROLE_GRANT_NOT_FOUND, ERROR_ROLE_NOT_WEB_GRANTABLE } from '../http/error_schemas.js';
72
71
  import { ERROR_CANNOT_REVOKE_KEEPER_ADMIN, ERROR_CANNOT_REVOKE_LAST_ADMIN, ERROR_ROLE_GRANT_BUILTIN_SCOPED, ERROR_ROLE_GRANT_OFFER_ACTOR_ACCOUNT_MISMATCH, ERROR_ROLE_GRANT_OFFER_ACTOR_MISMATCH, ERROR_ROLE_GRANT_OFFER_EXPIRED, ERROR_ROLE_GRANT_OFFER_NOT_AUTHORIZED, ERROR_ROLE_GRANT_OFFER_NOT_FOUND, ERROR_ROLE_GRANT_OFFER_ROLE_NOT_GRANTABLE, ERROR_ROLE_GRANT_OFFER_SELF_TARGET, ERROR_ROLE_GRANT_OFFER_TERMINAL, role_grant_offer_create_action_spec, role_grant_offer_accept_action_spec, role_grant_offer_decline_action_spec, role_grant_offer_retract_action_spec, role_grant_offer_list_action_spec, role_grant_offer_history_action_spec, role_grant_revoke_action_spec, role_grant_assign_action_spec } from './role_grant_offer_action_specs.js';
73
72
  // -- Helpers ----------------------------------------------------------------
@@ -109,17 +108,12 @@ const default_authorize = async (auth, _input, _deps, _ctx) => {
109
108
  * Create the role-grant-offer RPC actions: the offer-lifecycle methods plus
110
109
  * `role_grant_revoke` and the immediate `role_grant_assign`.
111
110
  *
112
- * @param deps - `RevokingActionFactoryDeps` (`log`, `audit`, and the `connection_closer` a revoked global grant closes the revokee's role-gated connections through) plus optional `notification_sender` for WS fan-out — when absent, WS fan-out is silently skipped (DB-only side effects still happen). Consumers wiring `ConnectionRegistry` assign its instance directly (the registry's `send_to_account` signature accepts the broader `JsonrpcMessageFromServerToClient`, which is contravariantly compatible)
111
+ * @param deps - `RevokingActionFactoryDeps`: `log`, `audit`, and the `connection_closer` a revoked global grant closes the revokee's role-gated connections through. WS notifications go through the handler context's `notification_sender`, not a dep
113
112
  * @param options - role registry, default TTL, authorization override
114
113
  * @returns the `RpcAction` array to spread into a `create_rpc_endpoint` call
115
114
  */
116
115
  export const create_role_grant_offer_actions = (deps, options = {}) => {
117
116
  const { log, audit, connection_closer } = deps;
118
- // under DEV each notification sent is checked against its spec's params
119
- // schema, a mismatch logged, as DEV output validation does
120
- const notification_sender = deps.notification_sender
121
- ? create_validated_notification_sender(deps.notification_sender, log)
122
- : null;
123
117
  const role_specs = options.roles?.role_specs ?? builtin_role_specs;
124
118
  const default_ttl_ms = options.default_ttl_ms ?? ROLE_GRANT_OFFER_DEFAULT_TTL_MS;
125
119
  const authorize = options.authorize ?? default_authorize;
@@ -299,11 +293,7 @@ export const create_role_grant_offer_actions = (deps, options = {}) => {
299
293
  }
300
294
  });
301
295
  const offer_json = to_role_grant_offer_json(offer);
302
- if (notification_sender) {
303
- emit_after_commit(ctx, () => {
304
- notification_sender.send_to_account(offer.to_account_id, build_role_grant_offer_received_notification({ offer: offer_json }));
305
- });
306
- }
296
+ queue_offer_notification(ctx, offer.to_account_id, build_role_grant_offer_received_notification({ offer: offer_json }));
307
297
  return { offer: offer_json };
308
298
  };
309
299
  const accept_handler = async (input, ctx) => {
@@ -335,47 +325,36 @@ export const create_role_grant_offer_actions = (deps, options = {}) => {
335
325
  if (err instanceof RoleGrantOfferBuiltinRefusedError) {
336
326
  throw await refuse_builtin_accept(ctx, auth, err);
337
327
  }
328
+ // The caller's own account was deleted while the request was in flight:
329
+ // refused as the dead credential it now is, as offer-create refuses a
330
+ // grantor deleted in flight, with no audit row from an account that is gone.
331
+ if (err instanceof RoleGrantOfferRecipientNotActiveError) {
332
+ throw jsonrpc_errors.unauthenticated('unauthenticated', {
333
+ reason: ERROR_AUTHENTICATION_REQUIRED
334
+ });
335
+ }
338
336
  throw err;
339
337
  }
340
- // Look up the grantor's account_id inside the transaction so the
341
- // post-commit notification has a valid target. One cheap SELECT by
342
- // PK — the alternative (widening `query_accept_offer` again) would
343
- // bleed transport concerns into the query layer. A replay
344
- // (`created: false`) notifies nothing, so it skips the lookup.
345
- const grantor_actor = notification_sender && result.created
346
- ? await query_actor_by_id(ctx, result.offer.from_actor_id)
347
- : null;
348
- const grantor_account_id = grantor_actor?.account_id ?? null;
349
338
  const offer_json = to_role_grant_offer_json(result.offer);
350
- const supersede_payloads = result.superseded_offers.map((sib) => ({
351
- offer: to_role_grant_offer_json(sib),
352
- from_account_id: sib.from_account_id
353
- }));
354
339
  // Audit events are written in-transaction by query_accept_offer; wire
355
340
  // them through `audit.notify` post-commit so SSE/WS broadcasts fire.
356
- // WS notifications ride the same deferred post-commit thunk so the
357
- // grantor sees "accepted" and each superseded grantor sees "supersede"
358
- // only once the accept has durably committed — and never if it rolls
359
- // back (the dispatch site discards `post_commit_effects` on rollback).
360
- // A replay (or a race loser) wrote nothing — the accept that minted the
361
- // grant already audited it and notified the grantor — so it queues
362
- // nothing; repeating the notification would let any replay send them
363
- // without bound.
341
+ // The WS notifications are queued behind them on the same post-commit
342
+ // queue, so the grantor sees "accepted" and each superseded grantor sees
343
+ // "supersede" only once the accept has durably committed — and never if
344
+ // it rolls back (the dispatch site discards `post_commit_effects` on
345
+ // rollback). A replay (or a race loser) wrote nothing — the accept that
346
+ // minted the grant already audited it and notified the grantor — so it
347
+ // queues nothing; repeating the notification would let any replay send
348
+ // them without bound. Only the siblings' grantors hear the supersede: their
349
+ // recipient is the caller, whose answer lists `superseded_offer_ids`.
364
350
  if (result.created) {
365
351
  emit_after_commit(ctx, () => {
366
352
  fan_out_audit_events(result.audit_events, audit);
367
- if (notification_sender && grantor_account_id) {
368
- notification_sender.send_to_account(grantor_account_id, build_role_grant_offer_accepted_notification({ offer: offer_json }));
369
- }
370
- if (notification_sender) {
371
- for (const sib of supersede_payloads) {
372
- notification_sender.send_to_account(sib.from_account_id, build_role_grant_offer_supersede_notification({
373
- offer: sib.offer,
374
- reason: 'sibling_accepted',
375
- cause_id: result.offer.id
376
- }));
377
- }
378
- }
353
+ });
354
+ queue_offer_notification(ctx, result.from_account_id, build_role_grant_offer_accepted_notification({ offer: offer_json }));
355
+ queue_supersede_notifications(ctx, result.superseded_offers, 'sibling_accepted', {
356
+ grantor: { kind: 'id', id: result.offer.id },
357
+ recipient: null
379
358
  });
380
359
  }
381
360
  return {
@@ -416,15 +395,10 @@ export const create_role_grant_offer_actions = (deps, options = {}) => {
416
395
  }
417
396
  });
418
397
  const offer_json = to_role_grant_offer_json(declined);
419
- if (notification_sender) {
420
- // Grantor's account_id rides on `declined.from_account_id` from
421
- // the decline RETURNING — no second SELECT needed. The decline
422
- // reason rides along on `offer.decline_reason` — the DB set it
423
- // in the RETURNING above.
424
- emit_after_commit(ctx, () => {
425
- notification_sender.send_to_account(declined.from_account_id, build_role_grant_offer_declined_notification({ offer: offer_json }));
426
- });
427
- }
398
+ // Grantor's account_id rides on `declined.from_account_id` from the
399
+ // decline RETURNING — no second SELECT needed. The decline reason rides
400
+ // along on `offer.decline_reason` — the DB set it in the RETURNING above.
401
+ queue_offer_notification(ctx, declined.from_account_id, build_role_grant_offer_declined_notification({ offer: offer_json }));
428
402
  // the terminal offer, as accept answers its own
429
403
  return { offer: offer_json };
430
404
  };
@@ -458,11 +432,7 @@ export const create_role_grant_offer_actions = (deps, options = {}) => {
458
432
  }
459
433
  });
460
434
  const offer_json = to_role_grant_offer_json(retracted);
461
- if (notification_sender) {
462
- emit_after_commit(ctx, () => {
463
- notification_sender.send_to_account(retracted.to_account_id, build_role_grant_offer_retracted_notification({ offer: offer_json }));
464
- });
465
- }
435
+ queue_offer_notification(ctx, retracted.to_account_id, build_role_grant_offer_retracted_notification({ offer: offer_json }));
466
436
  // the terminal offer, as accept answers its own
467
437
  return { offer: offer_json };
468
438
  };
@@ -535,13 +505,20 @@ export const create_role_grant_offer_actions = (deps, options = {}) => {
535
505
  });
536
506
  }
537
507
  if ((await query_account_has_active_global_role(ctx, target_account_id, ROLE_ADMIN)) &&
538
- (await query_count_active_accounts_with_global_role_for_update(ctx, ROLE_ADMIN, input.role_grant_id)) === 0) {
508
+ (await query_count_active_accounts_with_global_role_locked(ctx, ROLE_ADMIN, input.role_grant_id)) === 0) {
539
509
  await emit_revoke_failure();
540
510
  throw jsonrpc_errors.forbidden("cannot revoke the last admin's admin grant", {
541
511
  reason: ERROR_CANNOT_REVOKE_LAST_ADMIN
542
512
  });
543
513
  }
544
514
  }
515
+ // The revokee's account row, then every offer the revoke supersedes — the
516
+ // revokee's incoming siblings and its outgoing offers — in one statement in
517
+ // id order, before the grant is touched (`query_account_lock_for_revoke`
518
+ // and `query_role_grant_offer_lock_for_revoke` have why). After the guard,
519
+ // which holds an admin revokee's row already.
520
+ await query_account_lock_for_revoke(ctx, input.actor_id);
521
+ await query_role_grant_offer_lock_for_revoke(ctx, input.actor_id, role_grant_row.role, role_grant_row.scope_id);
545
522
  const result = await query_revoke_role_grant(ctx, input.role_grant_id, input.actor_id, auth.actor.id, input.reason ?? null);
546
523
  if (!result) {
547
524
  // Raced with another revoker or the role_grant was revoked between
@@ -594,30 +571,21 @@ export const create_role_grant_offer_actions = (deps, options = {}) => {
594
571
  }
595
572
  });
596
573
  }
597
- if (notification_sender) {
598
- const superseded = result.superseded_offers.map((o) => ({
599
- offer: to_role_grant_offer_json(o),
600
- from_account_id: o.from_account_id
601
- }));
602
- const cause_id = result.id;
603
- const reason = input.reason ?? null;
604
- emit_after_commit(ctx, () => {
605
- notification_sender.send_to_account(target_account_id, build_role_grant_revoked_notification({
606
- role_grant_id: result.id,
607
- role: result.role,
608
- scope_kind: result.scope_kind,
609
- scope_id: result.scope_id,
610
- reason
611
- }));
612
- for (const sib of superseded) {
613
- notification_sender.send_to_account(sib.from_account_id, build_role_grant_offer_supersede_notification({
614
- offer: sib.offer,
615
- reason: 'role_grant_revoked',
616
- cause_id
617
- }));
618
- }
619
- });
620
- }
574
+ queue_offer_notification(ctx, target_account_id, build_role_grant_revoked_notification({
575
+ role_grant_id: result.id,
576
+ role: result.role,
577
+ scope_kind: result.scope_kind,
578
+ scope_id: result.scope_id,
579
+ reason: input.reason ?? null
580
+ }));
581
+ // Both parties of each superseded offer: the revokee is the recipient of
582
+ // its incoming siblings and the grantor of its outgoing offers, and the
583
+ // other party of each is told as well.
584
+ const revoked_grant = { kind: 'id', id: result.id };
585
+ queue_supersede_notifications(ctx, result.superseded_offers, 'role_grant_revoked', {
586
+ grantor: revoked_grant,
587
+ recipient: revoked_grant
588
+ });
621
589
  return { ok: true, revoked: true };
622
590
  };
623
591
  // The immediate admin-only conferral path — the consent-free sibling of
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Role grant offer WebSocket notification specs, builders, and the narrow
3
- * `NotificationSender` interface that decouples offer/revoke send sites
4
- * from `ConnectionRegistry`.
2
+ * Role grant offer WebSocket notification specs, builders, and the helpers
3
+ * the handlers queue them with (`queue_offer_notification`,
4
+ * `queue_supersede_notifications`).
5
5
  *
6
6
  * These `RemoteNotificationActionSpec`s cover the consentful-role-grants
7
7
  * lifecycle events the server pushes to affected accounts:
@@ -10,9 +10,9 @@
10
10
  * - `role_grant_offer_retracted` → recipient's sockets when a grantor retracts
11
11
  * - `role_grant_offer_accepted` → grantor's sockets when the recipient accepts
12
12
  * - `role_grant_offer_declined` → grantor's sockets when the recipient declines
13
- * - `role_grant_offer_supersede` → grantor's sockets when a sibling accept,
14
- * a revoke of the resulting role_grant, or destruction of the parent scope
15
- * row obsoletes their pending offer
13
+ * - `role_grant_offer_supersede` → each live party to a pending offer a
14
+ * cascade ended — a sibling accept, a revoke, an account removal — but the
15
+ * one the cascade's own answer or socket close already covers
16
16
  * - `role_grant_revoked` → revokee's sockets when one of their active
17
17
  * role_grants is revoked by the `role_grant_revoke` action (companion to the
18
18
  * `role_grant_revoke` audit event)
@@ -29,10 +29,9 @@
29
29
  * action set of an RPC list that carries the role_grant_offer actions, so the
30
30
  * surface lists them on the WS endpoint as `remote_notification` methods. An
31
31
  * inbound frame for one is still answered `method_not_found`, as no handler is
32
- * registered. Under DEV,
33
- * `create_role_grant_offer_actions` checks each params object it sends
34
- * against its spec's `input` (`create_validated_notification_sender`);
35
- * production sends unchecked.
32
+ * registered. Under DEV, `perform_action` hands every action handler a sender
33
+ * wrapped in `create_validated_notification_sender`, so each params object
34
+ * sent is checked against its spec's `input`; production sends unchecked.
36
35
  *
37
36
  * @module
38
37
  */
@@ -42,23 +41,9 @@ import { type Uuid } from '../primitive_schemas.js';
42
41
  import type { Action } from '../actions/action_types.js';
43
42
  import type { EventSpec } from '../realtime/sse.js';
44
43
  import type { JsonrpcNotification } from '../http/jsonrpc.js';
45
- /**
46
- * Narrow structural capability for sending a JSON-RPC notification to every
47
- * socket bound to an account.
48
- *
49
- * `ConnectionRegistry` satisfies this interface — its
50
- * `send_to_account(account_id, message)` signature accepts the broader
51
- * `JsonrpcMessageFromServerToClient` type, which is contravariantly
52
- * compatible with `JsonrpcNotification` here. The interface stays local so
53
- * handlers don't couple to the concrete registry, and tests can inject a
54
- * capturing stub with no WS machinery.
55
- *
56
- * Returns the number of sockets the notification was sent to — callers
57
- * typically ignore it (used by telemetry / tests).
58
- */
59
- export interface NotificationSender {
60
- send_to_account: (account_id: Uuid, message: JsonrpcNotification) => number;
61
- }
44
+ import { type EmitAfterCommitContext } from '../http/pending_effects.js';
45
+ import type { NotificationSender } from '../actions/notification_sender.js';
46
+ import { type SupersededOffer } from './role_grant_offer_schema.js';
62
47
  export declare const ROLE_GRANT_OFFER_RECEIVED_NOTIFICATION_METHOD = "role_grant_offer_received";
63
48
  export declare const ROLE_GRANT_OFFER_RETRACTED_NOTIFICATION_METHOD = "role_grant_offer_retracted";
64
49
  export declare const ROLE_GRANT_OFFER_ACCEPTED_NOTIFICATION_METHOD = "role_grant_offer_accepted";
@@ -158,14 +143,17 @@ export declare const RoleGrantOfferDeclinedParams: import("@fuzdev/fuz_schema/sc
158
143
  }>;
159
144
  export type RoleGrantOfferDeclinedParams = Infer<typeof RoleGrantOfferDeclinedParams>;
160
145
  /**
161
- * Params for `role_grant_offer_supersede`. Fires to the grantor's sockets when
162
- * their pending offer is obsoleted — either by a sibling accept
163
- * (`reason: 'sibling_accepted'`), by revoke of the resulting role_grant or of
164
- * one of the grantor's own (`reason: 'role_grant_revoked'`), or by deletion of
165
- * the parent scope row
166
- * the offer was bound to (`reason: 'scope_destroyed'`). `cause_id` points
167
- * at the accepted offer id, the revoked role_grant id, or the destroyed scope
168
- * row id respectively.
146
+ * Params for `role_grant_offer_supersede`, sent to a party of a pending offer
147
+ * a cascade ended: a sibling accept (`reason: 'sibling_accepted'`), a revoke of
148
+ * the recipient's grant or of any grant of the grantor's actor
149
+ * (`reason: 'role_grant_revoked'`), or the removal of either party's account
150
+ * (`reason: 'account_deleted'`). `cause_id` points at the accepted offer, the
151
+ * revoked role_grant, or the removed account respectively — the `cause_id` of
152
+ * the offer's `role_grant_offer_supersede` audit row — except on a recipient's
153
+ * frame of its grantor's account removal, where it is the offer's grantor
154
+ * actor (`from_actor_id`), an id the recipient already holds. The audit
155
+ * metadata's reason set is wider: a consumer's scope teardown audits
156
+ * `scope_destroyed`, which no notification carries.
169
157
  */
170
158
  export declare const RoleGrantOfferSupersedeParams: import("@fuzdev/fuz_schema/schema.ts").ObjectSchema<{
171
159
  offer: import("@fuzdev/fuz_schema/schema.ts").ObjectSchema<{
@@ -186,7 +174,7 @@ export declare const RoleGrantOfferSupersedeParams: import("@fuzdev/fuz_schema/s
186
174
  superseded_at: import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>>;
187
175
  resulting_role_grant_id: import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").BrandSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>, "Uuid">>;
188
176
  }>;
189
- reason: import("@fuzdev/fuz_schema/schema.ts").OneOfSchema<readonly ["sibling_accepted", "role_grant_revoked", "scope_destroyed"]>;
177
+ reason: import("@fuzdev/fuz_schema/schema.ts").OneOfSchema<readonly ["sibling_accepted", "role_grant_revoked", "account_deleted"]>;
190
178
  cause_id: import("@fuzdev/fuz_schema/schema.ts").BrandSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>, "Uuid">;
191
179
  }>;
192
180
  export type RoleGrantOfferSupersedeParams = Infer<typeof RoleGrantOfferSupersedeParams>;
@@ -351,7 +339,7 @@ export declare const role_grant_offer_supersede_notification_spec: {
351
339
  superseded_at: import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>>;
352
340
  resulting_role_grant_id: import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").BrandSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>, "Uuid">>;
353
341
  }>;
354
- reason: import("@fuzdev/fuz_schema/schema.ts").OneOfSchema<readonly ["sibling_accepted", "role_grant_revoked", "scope_destroyed"]>;
342
+ reason: import("@fuzdev/fuz_schema/schema.ts").OneOfSchema<readonly ["sibling_accepted", "role_grant_revoked", "account_deleted"]>;
355
343
  cause_id: import("@fuzdev/fuz_schema/schema.ts").BrandSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>, "Uuid">;
356
344
  }>;
357
345
  output: import("@fuzdev/fuz_schema/schema.ts").VoidSchema;
@@ -398,7 +386,8 @@ export declare const role_grant_offer_notification_specs: Array<EventSpec>;
398
386
  * a mismatch logged as an error the way DEV output validation logs one; the
399
387
  * notification is sent either way, unchanged. A method that is no
400
388
  * role_grant_offer notification passes unchecked. In production the sender is
401
- * returned as it is.
389
+ * returned as it is. `perform_action` wraps the sender of every handler
390
+ * context, so every send site is checked whichever factory it sits in.
402
391
  *
403
392
  * @param sender - the sender to wrap
404
393
  * @param log - where a mismatch is logged
@@ -411,4 +400,64 @@ export declare const build_role_grant_offer_accepted_notification: (params: Role
411
400
  export declare const build_role_grant_offer_declined_notification: (params: RoleGrantOfferDeclinedParams) => JsonrpcNotification;
412
401
  export declare const build_role_grant_offer_supersede_notification: (params: RoleGrantOfferSupersedeParams) => JsonrpcNotification;
413
402
  export declare const build_role_grant_revoked_notification: (params: RoleGrantRevokedParams) => JsonrpcNotification;
403
+ /**
404
+ * What the queue helpers read of a handler context; `ActionContext` satisfies
405
+ * it.
406
+ */
407
+ export interface NotificationQueueContext extends EmitAfterCommitContext {
408
+ notification_sender: NotificationSender;
409
+ }
410
+ /**
411
+ * Queue `message` to every socket bound to `account_id`, sent once the
412
+ * handler's transaction commits and dropped when it rolls back, so a
413
+ * notification never announces state that never landed.
414
+ *
415
+ * Twin of the Rust spine's `queue_offer_notification`.
416
+ *
417
+ * @param ctx - the handler context, whose sender the send goes through
418
+ * @param account_id - the recipient account
419
+ * @param message - the notification, from one of the builders above
420
+ */
421
+ export declare const queue_offer_notification: (ctx: NotificationQueueContext, account_id: Uuid, message: JsonrpcNotification) => void;
422
+ /**
423
+ * The `cause_id` a party of a superseded offer is sent: an id the cascade
424
+ * names (`id` — the accepted offer, the revoked role_grant, the removed
425
+ * account), or the offer's own grantor actor (`from_actor`, an id its
426
+ * recipient already holds). Twin of the Rust `SupersedeCause`.
427
+ */
428
+ export type SupersedeCause = {
429
+ kind: 'id';
430
+ id: Uuid;
431
+ } | {
432
+ kind: 'from_actor';
433
+ };
434
+ /**
435
+ * Which parties of a superseded offer a cascade tells, and the `cause_id`
436
+ * each is sent; `null` leaves that party out. A party the cascade's own answer
437
+ * covers (the accepting recipient) or whose account it removes (its
438
+ * connections close) is left out by the call site, which knows which side
439
+ * that party is on. A recipient told of its grantor's account removal is sent
440
+ * the grantor's actor, never the account id, which no recipient sees
441
+ * otherwise. Twin of the Rust `SupersedeAudience`.
442
+ */
443
+ export interface SupersedeAudience {
444
+ grantor: SupersedeCause | null;
445
+ recipient: SupersedeCause | null;
446
+ }
447
+ /**
448
+ * Queue a `role_grant_offer_supersede` notification to the parties of each
449
+ * superseded offer `audience` names, each with its own `cause_id` — the
450
+ * grantor (`from_account_id`, off the cascade's `RETURNING`) and the recipient
451
+ * (`to_account_id`). Sent in the cascade's row order, which is the order of
452
+ * its audit rows, each offer's parties together and its grantor ahead of its
453
+ * recipient — one fixed order, so both spines send a cascade's frames alike.
454
+ *
455
+ * Twin of the Rust spine's `queue_supersede_notifications`.
456
+ *
457
+ * @param ctx - the handler context, whose sender the sends go through
458
+ * @param superseded - the offers the cascade superseded
459
+ * @param reason - why they were superseded
460
+ * @param audience - which party of each offer is told, and the `cause_id` it is sent
461
+ */
462
+ export declare const queue_supersede_notifications: (ctx: NotificationQueueContext, superseded: ReadonlyArray<SupersededOffer>, reason: RoleGrantOfferSupersedeParams["reason"], audience: SupersedeAudience) => void;
414
463
  //# sourceMappingURL=role_grant_offer_notifications.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"role_grant_offer_notifications.d.ts","sourceRoot":"","sources":["../../src/lib/auth/role_grant_offer_notifications.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAGH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAKN,KAAK,KAAK,EAEV,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAsC,KAAK,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAExF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAEzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAO9D;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,kBAAkB;IAClC,eAAe,EAAE,CAAC,UAAU,EAAE,IAAI,EAAE,OAAO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AC;AAEzC,eAAO,MAAM,4CAA4C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAUjB,CAAC;AAEzC,eAAO,MAAM,2CAA2C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAUhB,CAAC;AAEzC,eAAO,MAAM,2CAA2C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAUhB,CAAC;AAEzC,eAAO,MAAM,4CAA4C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAWjB,CAAC;AAEzC,eAAO,MAAM,oCAAoC;;;;;;;;;;;;;;;;CAUT,CAAC;AAczC;;;;;;GAMG;AACH,eAAO,MAAM,qCAAqC,EAAE,aAAa,CAAC,MAAM,CACH,CAAC;AAEtE;;;;;;;GAOG;AACH,eAAO,MAAM,mCAAmC,EAAE,KAAK,CAAC,SAAS,CACwB,CAAC;AAM1F;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,oCAAoC,GAChD,QAAQ,kBAAkB,EAC1B,KAAK,MAAM,KACT,kBAcF,CAAC;AAIF,eAAO,MAAM,4CAA4C,GACxD,QAAQ,4BAA4B,KAClC,mBACgF,CAAC;AAEpF,eAAO,MAAM,6CAA6C,GACzD,QAAQ,6BAA6B,KACnC,mBACiF,CAAC;AAErF,eAAO,MAAM,4CAA4C,GACxD,QAAQ,4BAA4B,KAClC,mBACgF,CAAC;AAEpF,eAAO,MAAM,4CAA4C,GACxD,QAAQ,4BAA4B,KAClC,mBACgF,CAAC;AAEpF,eAAO,MAAM,6CAA6C,GACzD,QAAQ,6BAA6B,KACnC,mBACiF,CAAC;AAErF,eAAO,MAAM,qCAAqC,GACjD,QAAQ,sBAAsB,KAC5B,mBACyE,CAAC"}
1
+ {"version":3,"file":"role_grant_offer_notifications.d.ts","sourceRoot":"","sources":["../../src/lib/auth/role_grant_offer_notifications.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAGH,OAAO,KAAK,EAAE,MAAM,EAAE,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AO,MAAM,4CAA4C,GACxD,QAAQ,4BAA4B,KAClC,mBACgF,CAAC;AAEpF,eAAO,MAAM,4CAA4C,GACxD,QAAQ,4BAA4B,KAClC,mBACgF,CAAC;AAEpF,eAAO,MAAM,6CAA6C,GACzD,QAAQ,6BAA6B,KACnC,mBACiF,CAAC;AAErF,eAAO,MAAM,qCAAqC,GACjD,QAAQ,sBAAsB,KAC5B,mBACyE,CAAC;AAI7E;;;GAGG;AACH,MAAM,WAAW,wBAAyB,SAAQ,sBAAsB;IACvE,mBAAmB,EAAE,kBAAkB,CAAC;CACxC;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,wBAAwB,GACpC,KAAK,wBAAwB,EAC7B,YAAY,IAAI,EAChB,SAAS,mBAAmB,KAC1B,IAKF,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,cAAc,GAAG;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,YAAY,CAAA;CAAE,CAAC;AAE/E;;;;;;;;GAQG;AACH,MAAM,WAAW,iBAAiB;IACjC,OAAO,EAAE,cAAc,GAAG,IAAI,CAAC;IAC/B,SAAS,EAAE,cAAc,GAAG,IAAI,CAAC;CACjC;AAKD;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,6BAA6B,GACzC,KAAK,wBAAwB,EAC7B,YAAY,aAAa,CAAC,eAAe,CAAC,EAC1C,QAAQ,6BAA6B,CAAC,QAAQ,CAAC,EAC/C,UAAU,iBAAiB,KACzB,IAkBF,CAAC"}
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Role grant offer WebSocket notification specs, builders, and the narrow
3
- * `NotificationSender` interface that decouples offer/revoke send sites
4
- * from `ConnectionRegistry`.
2
+ * Role grant offer WebSocket notification specs, builders, and the helpers
3
+ * the handlers queue them with (`queue_offer_notification`,
4
+ * `queue_supersede_notifications`).
5
5
  *
6
6
  * These `RemoteNotificationActionSpec`s cover the consentful-role-grants
7
7
  * lifecycle events the server pushes to affected accounts:
@@ -10,9 +10,9 @@
10
10
  * - `role_grant_offer_retracted` → recipient's sockets when a grantor retracts
11
11
  * - `role_grant_offer_accepted` → grantor's sockets when the recipient accepts
12
12
  * - `role_grant_offer_declined` → grantor's sockets when the recipient declines
13
- * - `role_grant_offer_supersede` → grantor's sockets when a sibling accept,
14
- * a revoke of the resulting role_grant, or destruction of the parent scope
15
- * row obsoletes their pending offer
13
+ * - `role_grant_offer_supersede` → each live party to a pending offer a
14
+ * cascade ended — a sibling accept, a revoke, an account removal — but the
15
+ * one the cascade's own answer or socket close already covers
16
16
  * - `role_grant_revoked` → revokee's sockets when one of their active
17
17
  * role_grants is revoked by the `role_grant_revoke` action (companion to the
18
18
  * `role_grant_revoke` audit event)
@@ -29,10 +29,9 @@
29
29
  * action set of an RPC list that carries the role_grant_offer actions, so the
30
30
  * surface lists them on the WS endpoint as `remote_notification` methods. An
31
31
  * inbound frame for one is still answered `method_not_found`, as no handler is
32
- * registered. Under DEV,
33
- * `create_role_grant_offer_actions` checks each params object it sends
34
- * against its spec's `input` (`create_validated_notification_sender`);
35
- * production sends unchecked.
32
+ * registered. Under DEV, `perform_action` hands every action handler a sender
33
+ * wrapped in `create_validated_notification_sender`, so each params object
34
+ * sent is checked against its spec's `input`; production sends unchecked.
36
35
  *
37
36
  * @module
38
37
  */
@@ -41,9 +40,10 @@ import { nullable, object, one_of, void as fuz_void, parse } from '@fuzdev/fuz_s
41
40
  import { Uuid as UuidSchema, bounded_string } from '../primitive_schemas.js';
42
41
  import { create_action_event_spec } from '../actions/action_bridge.js';
43
42
  import { create_jsonrpc_notification } from '../http/jsonrpc_helpers.js';
43
+ import { emit_after_commit } from '../http/pending_effects.js';
44
44
  import { RoleName } from './role_schema.js';
45
45
  import { ScopeKindName } from './scope_kind_schema.js';
46
- import { RoleGrantOfferJson } from './role_grant_offer_schema.js';
46
+ import { RoleGrantOfferJson, to_role_grant_offer_json } from './role_grant_offer_schema.js';
47
47
  import { ROLE_GRANT_REVOKED_REASON_LENGTH_MAX } from './account_schema.js';
48
48
  // -- Method constants -------------------------------------------------------
49
49
  export const ROLE_GRANT_OFFER_RECEIVED_NOTIFICATION_METHOD = 'role_grant_offer_received';
@@ -74,18 +74,21 @@ export const RoleGrantOfferDeclinedParams = object({
74
74
  offer: RoleGrantOfferJson
75
75
  });
76
76
  /**
77
- * Params for `role_grant_offer_supersede`. Fires to the grantor's sockets when
78
- * their pending offer is obsoleted — either by a sibling accept
79
- * (`reason: 'sibling_accepted'`), by revoke of the resulting role_grant or of
80
- * one of the grantor's own (`reason: 'role_grant_revoked'`), or by deletion of
81
- * the parent scope row
82
- * the offer was bound to (`reason: 'scope_destroyed'`). `cause_id` points
83
- * at the accepted offer id, the revoked role_grant id, or the destroyed scope
84
- * row id respectively.
77
+ * Params for `role_grant_offer_supersede`, sent to a party of a pending offer
78
+ * a cascade ended: a sibling accept (`reason: 'sibling_accepted'`), a revoke of
79
+ * the recipient's grant or of any grant of the grantor's actor
80
+ * (`reason: 'role_grant_revoked'`), or the removal of either party's account
81
+ * (`reason: 'account_deleted'`). `cause_id` points at the accepted offer, the
82
+ * revoked role_grant, or the removed account respectively — the `cause_id` of
83
+ * the offer's `role_grant_offer_supersede` audit row — except on a recipient's
84
+ * frame of its grantor's account removal, where it is the offer's grantor
85
+ * actor (`from_actor_id`), an id the recipient already holds. The audit
86
+ * metadata's reason set is wider: a consumer's scope teardown audits
87
+ * `scope_destroyed`, which no notification carries.
85
88
  */
86
89
  export const RoleGrantOfferSupersedeParams = object({
87
90
  offer: RoleGrantOfferJson,
88
- reason: one_of(['sibling_accepted', 'role_grant_revoked', 'scope_destroyed']),
91
+ reason: one_of(['sibling_accepted', 'role_grant_revoked', 'account_deleted']),
89
92
  cause_id: UuidSchema
90
93
  });
91
94
  /**
@@ -157,7 +160,7 @@ export const role_grant_offer_supersede_notification_spec = {
157
160
  input: RoleGrantOfferSupersedeParams,
158
161
  output: fuz_void,
159
162
  async: true,
160
- description: 'A grantor’s pending role_grant offer was obsoleted by a sibling accept, by revoke of the resulting role_grant, or by destruction of the parent scope row.'
163
+ description: 'A pending role_grant offer this account was a party to, as its grantor or its recipient, was ended by a sibling accept, a role_grant revoke, or the removal of the other party’s account.'
161
164
  };
162
165
  export const role_grant_revoked_notification_spec = {
163
166
  method: ROLE_GRANT_REVOKED_NOTIFICATION_METHOD,
@@ -204,7 +207,8 @@ const notification_spec_by_method = new Map(role_grant_offer_notification_action
204
207
  * a mismatch logged as an error the way DEV output validation logs one; the
205
208
  * notification is sent either way, unchanged. A method that is no
206
209
  * role_grant_offer notification passes unchecked. In production the sender is
207
- * returned as it is.
210
+ * returned as it is. `perform_action` wraps the sender of every handler
211
+ * context, so every send site is checked whichever factory it sits in.
208
212
  *
209
213
  * @param sender - the sender to wrap
210
214
  * @param log - where a mismatch is logged
@@ -233,3 +237,52 @@ export const build_role_grant_offer_accepted_notification = (params) => create_j
233
237
  export const build_role_grant_offer_declined_notification = (params) => create_jsonrpc_notification(ROLE_GRANT_OFFER_DECLINED_NOTIFICATION_METHOD, params);
234
238
  export const build_role_grant_offer_supersede_notification = (params) => create_jsonrpc_notification(ROLE_GRANT_OFFER_SUPERSEDE_NOTIFICATION_METHOD, params);
235
239
  export const build_role_grant_revoked_notification = (params) => create_jsonrpc_notification(ROLE_GRANT_REVOKED_NOTIFICATION_METHOD, params);
240
+ /**
241
+ * Queue `message` to every socket bound to `account_id`, sent once the
242
+ * handler's transaction commits and dropped when it rolls back, so a
243
+ * notification never announces state that never landed.
244
+ *
245
+ * Twin of the Rust spine's `queue_offer_notification`.
246
+ *
247
+ * @param ctx - the handler context, whose sender the send goes through
248
+ * @param account_id - the recipient account
249
+ * @param message - the notification, from one of the builders above
250
+ */
251
+ export const queue_offer_notification = (ctx, account_id, message) => {
252
+ const { notification_sender } = ctx;
253
+ emit_after_commit(ctx, () => {
254
+ notification_sender.send_to_account(account_id, message);
255
+ });
256
+ };
257
+ const to_supersede_cause_id = (cause, offer) => cause.kind === 'id' ? cause.id : offer.from_actor_id;
258
+ /**
259
+ * Queue a `role_grant_offer_supersede` notification to the parties of each
260
+ * superseded offer `audience` names, each with its own `cause_id` — the
261
+ * grantor (`from_account_id`, off the cascade's `RETURNING`) and the recipient
262
+ * (`to_account_id`). Sent in the cascade's row order, which is the order of
263
+ * its audit rows, each offer's parties together and its grantor ahead of its
264
+ * recipient — one fixed order, so both spines send a cascade's frames alike.
265
+ *
266
+ * Twin of the Rust spine's `queue_supersede_notifications`.
267
+ *
268
+ * @param ctx - the handler context, whose sender the sends go through
269
+ * @param superseded - the offers the cascade superseded
270
+ * @param reason - why they were superseded
271
+ * @param audience - which party of each offer is told, and the `cause_id` it is sent
272
+ */
273
+ export const queue_supersede_notifications = (ctx, superseded, reason, audience) => {
274
+ for (const offer of superseded) {
275
+ for (const [party, cause] of [
276
+ [offer.from_account_id, audience.grantor],
277
+ [offer.to_account_id, audience.recipient]
278
+ ]) {
279
+ if (!cause)
280
+ continue;
281
+ queue_offer_notification(ctx, party, build_role_grant_offer_supersede_notification({
282
+ offer: to_role_grant_offer_json(offer),
283
+ reason,
284
+ cause_id: to_supersede_cause_id(cause, offer)
285
+ }));
286
+ }
287
+ }
288
+ };