@fuzdev/fuz_app 0.149.0 → 0.151.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 (650) hide show
  1. package/README.md +1 -1
  2. package/dist/actions/CLAUDE.md +327 -300
  3. package/dist/actions/action_bridge.d.ts +5 -79
  4. package/dist/actions/action_bridge.d.ts.map +1 -1
  5. package/dist/actions/action_bridge.js +4 -74
  6. package/dist/actions/action_codegen.d.ts +8 -166
  7. package/dist/actions/action_codegen.d.ts.map +1 -1
  8. package/dist/actions/action_codegen.js +19 -95
  9. package/dist/actions/action_dispatcher.d.ts +10 -0
  10. package/dist/actions/action_dispatcher.d.ts.map +1 -1
  11. package/dist/actions/action_dispatcher.js +9 -6
  12. package/dist/actions/action_event.d.ts +1 -2
  13. package/dist/actions/action_event.d.ts.map +1 -1
  14. package/dist/actions/action_event.js +7 -13
  15. package/dist/actions/action_event_helpers.d.ts +1 -40
  16. package/dist/actions/action_event_helpers.d.ts.map +1 -1
  17. package/dist/actions/action_event_helpers.js +1 -25
  18. package/dist/actions/action_event_types.d.ts +1 -1
  19. package/dist/actions/action_event_types.d.ts.map +1 -1
  20. package/dist/actions/action_rpc.d.ts +10 -215
  21. package/dist/actions/action_rpc.d.ts.map +1 -1
  22. package/dist/actions/action_rpc.js +56 -111
  23. package/dist/actions/action_spec.d.ts +2 -2
  24. package/dist/actions/action_spec.js +2 -2
  25. package/dist/actions/action_types.d.ts +229 -8
  26. package/dist/actions/action_types.d.ts.map +1 -1
  27. package/dist/actions/action_types.js +56 -5
  28. package/dist/actions/broadcast_api.d.ts +22 -17
  29. package/dist/actions/broadcast_api.d.ts.map +1 -1
  30. package/dist/actions/broadcast_api.js +16 -27
  31. package/dist/actions/cancel.d.ts +4 -13
  32. package/dist/actions/cancel.d.ts.map +1 -1
  33. package/dist/actions/cancel.js +5 -17
  34. package/dist/actions/compile_action_registry.d.ts +1 -2
  35. package/dist/actions/compile_action_registry.d.ts.map +1 -1
  36. package/dist/actions/connection_closer.d.ts +120 -50
  37. package/dist/actions/connection_closer.d.ts.map +1 -1
  38. package/dist/actions/connection_closer.js +139 -31
  39. package/dist/actions/{transports_ws_backend.d.ts → connection_registry.d.ts} +120 -132
  40. package/dist/actions/connection_registry.d.ts.map +1 -0
  41. package/dist/actions/{transports_ws_backend.js → connection_registry.js} +149 -152
  42. package/dist/actions/frontend_rpc_client.d.ts +52 -2
  43. package/dist/actions/frontend_rpc_client.d.ts.map +1 -1
  44. package/dist/actions/frontend_rpc_client.js +42 -4
  45. package/dist/actions/peer_ping.d.ts +7 -9
  46. package/dist/actions/peer_ping.d.ts.map +1 -1
  47. package/dist/actions/peer_ping.js +6 -8
  48. package/dist/actions/peer_request.d.ts +6 -5
  49. package/dist/actions/peer_request.d.ts.map +1 -1
  50. package/dist/actions/peer_request.js +6 -5
  51. package/dist/actions/perform_action.d.ts +36 -28
  52. package/dist/actions/perform_action.d.ts.map +1 -1
  53. package/dist/actions/perform_action.js +60 -31
  54. package/dist/actions/protocol.d.ts +14 -0
  55. package/dist/actions/protocol.d.ts.map +1 -1
  56. package/dist/actions/protocol.js +18 -0
  57. package/dist/actions/register_action_ws.d.ts +137 -292
  58. package/dist/actions/register_action_ws.d.ts.map +1 -1
  59. package/dist/actions/register_action_ws.js +155 -903
  60. package/dist/actions/socket.svelte.d.ts +9 -0
  61. package/dist/actions/socket.svelte.d.ts.map +1 -1
  62. package/dist/actions/socket.svelte.js +16 -5
  63. package/dist/actions/transports.d.ts +88 -34
  64. package/dist/actions/transports.d.ts.map +1 -1
  65. package/dist/actions/transports.js +76 -76
  66. package/dist/actions/transports_http.d.ts +48 -12
  67. package/dist/actions/transports_http.d.ts.map +1 -1
  68. package/dist/actions/transports_http.js +75 -44
  69. package/dist/actions/transports_ws.d.ts +29 -0
  70. package/dist/actions/transports_ws.d.ts.map +1 -1
  71. package/dist/actions/transports_ws.js +34 -8
  72. package/dist/actions/ws_endpoint_spec.d.ts +53 -76
  73. package/dist/actions/ws_endpoint_spec.d.ts.map +1 -1
  74. package/dist/actions/ws_endpoint_spec.js +27 -10
  75. package/dist/actions/ws_outbound_writer.d.ts +3 -3
  76. package/dist/actions/ws_outbound_writer.js +3 -3
  77. package/dist/actions/ws_socket_session.d.ts +321 -0
  78. package/dist/actions/ws_socket_session.d.ts.map +1 -0
  79. package/dist/actions/ws_socket_session.js +783 -0
  80. package/dist/auth/CLAUDE.md +132 -68
  81. package/dist/auth/account_action_specs.d.ts +2 -0
  82. package/dist/auth/account_action_specs.d.ts.map +1 -1
  83. package/dist/auth/account_actions.d.ts +1 -1
  84. package/dist/auth/account_actions.d.ts.map +1 -1
  85. package/dist/auth/account_actions.js +9 -3
  86. package/dist/auth/account_queries.d.ts +10 -0
  87. package/dist/auth/account_queries.d.ts.map +1 -1
  88. package/dist/auth/account_queries.js +27 -17
  89. package/dist/auth/account_routes.d.ts +2 -1
  90. package/dist/auth/account_routes.d.ts.map +1 -1
  91. package/dist/auth/account_routes.js +14 -17
  92. package/dist/auth/account_schema.d.ts +5 -5
  93. package/dist/auth/account_schema.d.ts.map +1 -1
  94. package/dist/auth/account_schema.js +5 -2
  95. package/dist/auth/actor_lookup_actions.d.ts +1 -1
  96. package/dist/auth/actor_lookup_actions.d.ts.map +1 -1
  97. package/dist/auth/actor_lookup_actions.js +1 -1
  98. package/dist/auth/actor_lookup_queries.d.ts +3 -2
  99. package/dist/auth/actor_lookup_queries.d.ts.map +1 -1
  100. package/dist/auth/actor_lookup_queries.js +6 -3
  101. package/dist/auth/actor_search_action_specs.d.ts.map +1 -1
  102. package/dist/auth/actor_search_action_specs.js +2 -1
  103. package/dist/auth/actor_search_actions.d.ts +1 -1
  104. package/dist/auth/actor_search_actions.d.ts.map +1 -1
  105. package/dist/auth/actor_search_actions.js +1 -1
  106. package/dist/auth/actor_search_queries.d.ts +10 -7
  107. package/dist/auth/actor_search_queries.d.ts.map +1 -1
  108. package/dist/auth/actor_search_queries.js +18 -11
  109. package/dist/auth/admin_action_specs.d.ts +4 -4
  110. package/dist/auth/admin_action_specs.d.ts.map +1 -1
  111. package/dist/auth/admin_action_specs.js +7 -5
  112. package/dist/auth/admin_actions.d.ts +1 -1
  113. package/dist/auth/admin_actions.d.ts.map +1 -1
  114. package/dist/auth/admin_actions.js +47 -48
  115. package/dist/auth/all_cell_actions.d.ts +14 -3
  116. package/dist/auth/all_cell_actions.d.ts.map +1 -1
  117. package/dist/auth/all_cell_actions.js +2 -1
  118. package/dist/auth/api_token_queries.d.ts +1 -0
  119. package/dist/auth/api_token_queries.d.ts.map +1 -1
  120. package/dist/auth/api_token_queries.js +1 -0
  121. package/dist/auth/audit_emitter.d.ts +12 -6
  122. package/dist/auth/audit_emitter.d.ts.map +1 -1
  123. package/dist/auth/audit_emitter.js +11 -5
  124. package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
  125. package/dist/auth/audit_log_route_schema.js +3 -2
  126. package/dist/auth/audit_log_routes.d.ts +20 -11
  127. package/dist/auth/audit_log_routes.d.ts.map +1 -1
  128. package/dist/auth/audit_log_routes.js +49 -29
  129. package/dist/auth/audit_log_schema.d.ts +14 -21
  130. package/dist/auth/audit_log_schema.d.ts.map +1 -1
  131. package/dist/auth/audit_log_schema.js +19 -23
  132. package/dist/auth/bearer_auth.d.ts +7 -4
  133. package/dist/auth/bearer_auth.d.ts.map +1 -1
  134. package/dist/auth/bearer_auth.js +21 -15
  135. package/dist/auth/bootstrap_account.d.ts +25 -1
  136. package/dist/auth/bootstrap_account.d.ts.map +1 -1
  137. package/dist/auth/bootstrap_account.js +37 -4
  138. package/dist/auth/bootstrap_routes.d.ts +3 -2
  139. package/dist/auth/bootstrap_routes.d.ts.map +1 -1
  140. package/dist/auth/bootstrap_routes.js +5 -4
  141. package/dist/auth/cell_action_helpers.d.ts +89 -0
  142. package/dist/auth/cell_action_helpers.d.ts.map +1 -0
  143. package/dist/auth/cell_action_helpers.js +113 -0
  144. package/dist/auth/cell_action_specs.d.ts +215 -15
  145. package/dist/auth/cell_action_specs.d.ts.map +1 -1
  146. package/dist/auth/cell_action_specs.js +103 -16
  147. package/dist/auth/cell_actions.d.ts +58 -27
  148. package/dist/auth/cell_actions.d.ts.map +1 -1
  149. package/dist/auth/cell_actions.js +206 -161
  150. package/dist/auth/cell_audit_actions.d.ts +1 -1
  151. package/dist/auth/cell_audit_actions.d.ts.map +1 -1
  152. package/dist/auth/cell_audit_actions.js +3 -12
  153. package/dist/auth/cell_audit_events.d.ts +1 -1
  154. package/dist/auth/cell_audit_events.d.ts.map +1 -1
  155. package/dist/auth/cell_audit_events.js +3 -2
  156. package/dist/auth/cell_audit_metadata.d.ts +12 -1
  157. package/dist/auth/cell_audit_metadata.d.ts.map +1 -1
  158. package/dist/auth/cell_audit_metadata.js +11 -1
  159. package/dist/auth/cell_field_actions.d.ts +1 -1
  160. package/dist/auth/cell_field_actions.d.ts.map +1 -1
  161. package/dist/auth/cell_field_actions.js +16 -66
  162. package/dist/auth/cell_grant_action_specs.d.ts +9 -2
  163. package/dist/auth/cell_grant_action_specs.d.ts.map +1 -1
  164. package/dist/auth/cell_grant_action_specs.js +10 -3
  165. package/dist/auth/cell_grant_actions.d.ts +105 -11
  166. package/dist/auth/cell_grant_actions.d.ts.map +1 -1
  167. package/dist/auth/cell_grant_actions.js +81 -56
  168. package/dist/auth/cell_grant_audit_metadata.d.ts +6 -3
  169. package/dist/auth/cell_grant_audit_metadata.d.ts.map +1 -1
  170. package/dist/auth/cell_grant_audit_metadata.js +7 -4
  171. package/dist/auth/cell_item_actions.d.ts +1 -1
  172. package/dist/auth/cell_item_actions.d.ts.map +1 -1
  173. package/dist/auth/cell_item_actions.js +21 -82
  174. package/dist/auth/cleanup.d.ts +1 -1
  175. package/dist/auth/cleanup.d.ts.map +1 -1
  176. package/dist/auth/cleanup.js +6 -3
  177. package/dist/auth/credential_deadline.d.ts +3 -3
  178. package/dist/auth/credential_deadline.js +3 -3
  179. package/dist/auth/credential_type_schema.d.ts +8 -7
  180. package/dist/auth/credential_type_schema.d.ts.map +1 -1
  181. package/dist/auth/credential_type_schema.js +8 -7
  182. package/dist/auth/daemon_token_middleware.d.ts +12 -6
  183. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  184. package/dist/auth/daemon_token_middleware.js +28 -17
  185. package/dist/auth/deps.d.ts +4 -4
  186. package/dist/auth/fail_floor.d.ts +55 -0
  187. package/dist/auth/fail_floor.d.ts.map +1 -0
  188. package/dist/auth/fail_floor.js +63 -0
  189. package/dist/auth/invite_queries.d.ts +3 -15
  190. package/dist/auth/invite_queries.d.ts.map +1 -1
  191. package/dist/auth/invite_queries.js +3 -21
  192. package/dist/auth/middleware.d.ts +12 -7
  193. package/dist/auth/middleware.d.ts.map +1 -1
  194. package/dist/auth/middleware.js +42 -29
  195. package/dist/auth/migrations.d.ts +5 -33
  196. package/dist/auth/migrations.d.ts.map +1 -1
  197. package/dist/auth/migrations.js +9 -16
  198. package/dist/auth/request_context.d.ts +101 -71
  199. package/dist/auth/request_context.d.ts.map +1 -1
  200. package/dist/auth/request_context.js +137 -140
  201. package/dist/auth/resolved_auth.d.ts +122 -36
  202. package/dist/auth/resolved_auth.d.ts.map +1 -1
  203. package/dist/auth/resolved_auth.js +89 -38
  204. package/dist/auth/role_grant_offer_action_specs.d.ts +117 -16
  205. package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
  206. package/dist/auth/role_grant_offer_action_specs.js +51 -16
  207. package/dist/auth/role_grant_offer_actions.d.ts +22 -11
  208. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  209. package/dist/auth/role_grant_offer_actions.js +72 -19
  210. package/dist/auth/role_grant_offer_notifications.d.ts +6 -5
  211. package/dist/auth/role_grant_offer_notifications.d.ts.map +1 -1
  212. package/dist/auth/role_grant_offer_notifications.js +4 -3
  213. package/dist/auth/role_grant_offer_queries.d.ts +20 -5
  214. package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
  215. package/dist/auth/role_grant_offer_queries.js +40 -37
  216. package/dist/auth/role_grant_queries.d.ts +44 -76
  217. package/dist/auth/role_grant_queries.d.ts.map +1 -1
  218. package/dist/auth/role_grant_queries.js +73 -118
  219. package/dist/auth/role_grant_sql.d.ts +42 -0
  220. package/dist/auth/role_grant_sql.d.ts.map +1 -0
  221. package/dist/auth/role_grant_sql.js +54 -0
  222. package/dist/auth/role_schema.d.ts +14 -8
  223. package/dist/auth/role_schema.d.ts.map +1 -1
  224. package/dist/auth/role_schema.js +6 -3
  225. package/dist/auth/scope_kind_schema.d.ts +1 -1
  226. package/dist/auth/scope_kind_schema.js +1 -1
  227. package/dist/auth/self_service_role_actions.d.ts +1 -1
  228. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  229. package/dist/auth/self_service_role_actions.js +21 -1
  230. package/dist/auth/session_queries.d.ts +2 -1
  231. package/dist/auth/session_queries.d.ts.map +1 -1
  232. package/dist/auth/session_queries.js +2 -1
  233. package/dist/auth/signup_routes.d.ts +2 -1
  234. package/dist/auth/signup_routes.d.ts.map +1 -1
  235. package/dist/auth/signup_routes.js +14 -13
  236. package/dist/auth/standard_rpc_actions.d.ts +1 -1
  237. package/dist/auth/standard_rpc_actions.d.ts.map +1 -1
  238. package/dist/auth/token_scope.d.ts +19 -46
  239. package/dist/auth/token_scope.d.ts.map +1 -1
  240. package/dist/auth/token_scope.js +23 -41
  241. package/dist/db/CLAUDE.md +18 -6
  242. package/dist/db/cell_ddl.d.ts +5 -134
  243. package/dist/db/cell_ddl.d.ts.map +1 -1
  244. package/dist/db/cell_ddl.js +28 -17
  245. package/dist/db/cell_field_queries.d.ts +0 -13
  246. package/dist/db/cell_field_queries.d.ts.map +1 -1
  247. package/dist/db/cell_field_queries.js +0 -16
  248. package/dist/db/cell_grant_queries.d.ts +10 -5
  249. package/dist/db/cell_grant_queries.d.ts.map +1 -1
  250. package/dist/db/cell_grant_queries.js +13 -7
  251. package/dist/db/cell_item_queries.d.ts +0 -7
  252. package/dist/db/cell_item_queries.d.ts.map +1 -1
  253. package/dist/db/cell_item_queries.js +0 -10
  254. package/dist/db/cell_queries.d.ts +65 -55
  255. package/dist/db/cell_queries.d.ts.map +1 -1
  256. package/dist/db/cell_queries.js +85 -62
  257. package/dist/db/cell_view_sql.d.ts +1 -6
  258. package/dist/db/cell_view_sql.d.ts.map +1 -1
  259. package/dist/db/cell_view_sql.js +1 -2
  260. package/dist/db/fact_ddl.d.ts +5 -30
  261. package/dist/db/fact_ddl.d.ts.map +1 -1
  262. package/dist/db/fact_ddl.js +14 -13
  263. package/dist/db/fact_store.d.ts +11 -0
  264. package/dist/db/fact_store.d.ts.map +1 -1
  265. package/dist/db/fact_store.js +4 -1
  266. package/dist/db/migrate.d.ts +44 -6
  267. package/dist/db/migrate.d.ts.map +1 -1
  268. package/dist/db/migrate.js +63 -4
  269. package/dist/dev/setup.d.ts +18 -14
  270. package/dist/dev/setup.d.ts.map +1 -1
  271. package/dist/dev/setup.js +42 -39
  272. package/dist/env/dotenv.d.ts +2 -2
  273. package/dist/env/dotenv.js +2 -2
  274. package/dist/env/update_env_variable.d.ts +33 -21
  275. package/dist/env/update_env_variable.d.ts.map +1 -1
  276. package/dist/env/update_env_variable.js +54 -35
  277. package/dist/hono_context.d.ts +15 -90
  278. package/dist/hono_context.d.ts.map +1 -1
  279. package/dist/hono_context.js +9 -56
  280. package/dist/http/CLAUDE.md +35 -20
  281. package/dist/http/auth_shape.d.ts +1 -8
  282. package/dist/http/auth_shape.d.ts.map +1 -1
  283. package/dist/http/auth_shape.js +6 -12
  284. package/dist/http/body_limit.d.ts +3 -1
  285. package/dist/http/body_limit.d.ts.map +1 -1
  286. package/dist/http/body_limit.js +6 -1
  287. package/dist/http/common_routes.d.ts.map +1 -1
  288. package/dist/http/common_routes.js +3 -2
  289. package/dist/http/db_routes.d.ts +3 -3
  290. package/dist/http/db_routes.js +3 -3
  291. package/dist/http/error_schemas.d.ts +1 -2
  292. package/dist/http/error_schemas.d.ts.map +1 -1
  293. package/dist/http/error_schemas.js +1 -2
  294. package/dist/http/health_schema.d.ts +16 -0
  295. package/dist/http/health_schema.d.ts.map +1 -0
  296. package/dist/http/health_schema.js +12 -0
  297. package/dist/http/jsonrpc.d.ts +9 -0
  298. package/dist/http/jsonrpc.d.ts.map +1 -1
  299. package/dist/http/jsonrpc.js +17 -0
  300. package/dist/http/jsonrpc_errors.d.ts +22 -2
  301. package/dist/http/jsonrpc_errors.d.ts.map +1 -1
  302. package/dist/http/jsonrpc_errors.js +70 -23
  303. package/dist/http/jsonrpc_helpers.d.ts +60 -17
  304. package/dist/http/jsonrpc_helpers.d.ts.map +1 -1
  305. package/dist/http/jsonrpc_helpers.js +67 -25
  306. package/dist/http/origin.d.ts +30 -1
  307. package/dist/http/origin.d.ts.map +1 -1
  308. package/dist/http/origin.js +46 -9
  309. package/dist/http/origin_wildcard.d.ts +97 -0
  310. package/dist/http/origin_wildcard.d.ts.map +1 -0
  311. package/dist/http/origin_wildcard.js +142 -0
  312. package/dist/http/pending_effects.d.ts +28 -0
  313. package/dist/http/pending_effects.d.ts.map +1 -1
  314. package/dist/http/pending_effects.js +32 -1
  315. package/dist/http/proxy.d.ts +4 -2
  316. package/dist/http/proxy.d.ts.map +1 -1
  317. package/dist/http/proxy.js +7 -2
  318. package/dist/http/route_spec.d.ts +4 -3
  319. package/dist/http/route_spec.d.ts.map +1 -1
  320. package/dist/http/route_spec.js +54 -45
  321. package/dist/http/surface.d.ts +4 -2
  322. package/dist/http/surface.d.ts.map +1 -1
  323. package/dist/http/surface.js +7 -4
  324. package/dist/http/surface_query.d.ts +9 -50
  325. package/dist/http/surface_query.d.ts.map +1 -1
  326. package/dist/http/surface_query.js +48 -94
  327. package/dist/realtime/audit_log_sse.d.ts +180 -0
  328. package/dist/realtime/audit_log_sse.d.ts.map +1 -0
  329. package/dist/realtime/audit_log_sse.js +154 -0
  330. package/dist/realtime/sse.d.ts +21 -6
  331. package/dist/realtime/sse.d.ts.map +1 -1
  332. package/dist/realtime/sse.js +33 -10
  333. package/dist/realtime/sse_constants.d.ts +29 -2
  334. package/dist/realtime/sse_constants.d.ts.map +1 -1
  335. package/dist/realtime/sse_constants.js +35 -2
  336. package/dist/realtime/{subscriber_registry.d.ts → sse_registry.d.ts} +12 -12
  337. package/dist/realtime/sse_registry.d.ts.map +1 -0
  338. package/dist/realtime/{subscriber_registry.js → sse_registry.js} +4 -4
  339. package/dist/schema_meta.d.ts +52 -1
  340. package/dist/schema_meta.d.ts.map +1 -1
  341. package/dist/schema_meta.js +123 -6
  342. package/dist/sensitivity.d.ts +9 -3
  343. package/dist/sensitivity.d.ts.map +1 -1
  344. package/dist/sensitivity.js +3 -2
  345. package/dist/server/app_backend.d.ts +8 -5
  346. package/dist/server/app_backend.d.ts.map +1 -1
  347. package/dist/server/app_backend.js +6 -12
  348. package/dist/server/app_server.d.ts +73 -54
  349. package/dist/server/app_server.d.ts.map +1 -1
  350. package/dist/server/app_server.js +115 -78
  351. package/dist/server/app_server_context.d.ts +1 -1
  352. package/dist/server/app_server_context.d.ts.map +1 -1
  353. package/dist/server/env.d.ts +24 -20
  354. package/dist/server/env.d.ts.map +1 -1
  355. package/dist/server/env.js +24 -34
  356. package/dist/server/serve_fact_route.d.ts +12 -16
  357. package/dist/server/serve_fact_route.d.ts.map +1 -1
  358. package/dist/server/serve_fact_route.js +59 -34
  359. package/dist/server/static.d.ts.map +1 -1
  360. package/dist/server/static.js +2 -2
  361. package/dist/testing/CLAUDE.md +213 -179
  362. package/dist/testing/admin_integration.d.ts.map +1 -1
  363. package/dist/testing/admin_integration.js +10 -19
  364. package/dist/testing/adversarial_headers.d.ts.map +1 -1
  365. package/dist/testing/adversarial_headers.js +8 -0
  366. package/dist/testing/app_server.d.ts +9 -75
  367. package/dist/testing/app_server.d.ts.map +1 -1
  368. package/dist/testing/app_server.js +31 -112
  369. package/dist/testing/attack_surface.d.ts.map +1 -1
  370. package/dist/testing/attack_surface.js +3 -4
  371. package/dist/testing/audit_completeness.d.ts.map +1 -1
  372. package/dist/testing/audit_completeness.js +4 -5
  373. package/dist/testing/auth_apps.d.ts +3 -3
  374. package/dist/testing/auth_apps.d.ts.map +1 -1
  375. package/dist/testing/auth_apps.js +9 -6
  376. package/dist/testing/connection_closer_helpers.d.ts +8 -16
  377. package/dist/testing/connection_closer_helpers.d.ts.map +1 -1
  378. package/dist/testing/connection_closer_helpers.js +7 -20
  379. package/dist/testing/cross_backend/action_manifest.d.ts +15 -4
  380. package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
  381. package/dist/testing/cross_backend/action_manifest.js +13 -3
  382. package/dist/testing/cross_backend/action_manifest_parity.d.ts +15 -7
  383. package/dist/testing/cross_backend/action_manifest_parity.d.ts.map +1 -1
  384. package/dist/testing/cross_backend/action_manifest_parity.js +8 -1
  385. package/dist/testing/cross_backend/actor_lookup.d.ts.map +1 -1
  386. package/dist/testing/cross_backend/actor_lookup.js +27 -0
  387. package/dist/testing/cross_backend/body_size.d.ts.map +1 -1
  388. package/dist/testing/cross_backend/body_size.js +25 -22
  389. package/dist/testing/cross_backend/build_test_backend_paths.d.ts +22 -3
  390. package/dist/testing/cross_backend/build_test_backend_paths.d.ts.map +1 -1
  391. package/dist/testing/cross_backend/build_test_backend_paths.js +34 -11
  392. package/dist/testing/cross_backend/capabilities.d.ts +15 -8
  393. package/dist/testing/cross_backend/capabilities.d.ts.map +1 -1
  394. package/dist/testing/cross_backend/cell_gated_create.d.ts.map +1 -1
  395. package/dist/testing/cross_backend/cell_gated_create.js +109 -13
  396. package/dist/testing/cross_backend/cell_grant_role.js +2 -2
  397. package/dist/testing/cross_backend/cell_grant_steward.d.ts +4 -0
  398. package/dist/testing/cross_backend/cell_grant_steward.d.ts.map +1 -0
  399. package/dist/testing/cross_backend/cell_grant_steward.js +120 -0
  400. package/dist/testing/cross_backend/cell_moderate.d.ts +7 -1
  401. package/dist/testing/cross_backend/cell_moderate.d.ts.map +1 -1
  402. package/dist/testing/cross_backend/cell_moderate.js +68 -4
  403. package/dist/testing/cross_backend/cell_ref_admission.d.ts.map +1 -1
  404. package/dist/testing/cross_backend/cell_ref_admission.js +18 -9
  405. package/dist/testing/cross_backend/cell_relations.d.ts.map +1 -1
  406. package/dist/testing/cross_backend/cell_relations.js +21 -0
  407. package/dist/testing/cross_backend/conformance_table.d.ts +6 -1
  408. package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
  409. package/dist/testing/cross_backend/conformance_table.js +12 -10
  410. package/dist/testing/cross_backend/default_backend_configs.d.ts +18 -4
  411. package/dist/testing/cross_backend/default_backend_configs.d.ts.map +1 -1
  412. package/dist/testing/cross_backend/default_backend_configs.js +29 -9
  413. package/dist/testing/cross_backend/default_spine_surface.d.ts +7 -7
  414. package/dist/testing/cross_backend/default_spine_surface.js +2 -2
  415. package/dist/testing/cross_backend/differential.d.ts +7 -0
  416. package/dist/testing/cross_backend/differential.d.ts.map +1 -1
  417. package/dist/testing/cross_backend/differential.js +17 -0
  418. package/dist/testing/cross_backend/expected_schema.json +1 -0
  419. package/dist/testing/cross_backend/fact_serving.d.ts.map +1 -1
  420. package/dist/testing/cross_backend/fact_serving.js +48 -0
  421. package/dist/testing/cross_backend/full_spine_mount.d.ts +3 -4
  422. package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
  423. package/dist/testing/cross_backend/full_spine_mount.js +13 -5
  424. package/dist/testing/cross_backend/origin_scope.d.ts.map +1 -1
  425. package/dist/testing/cross_backend/origin_scope.js +5 -13
  426. package/dist/testing/cross_backend/peer_ping_ws.d.ts.map +1 -1
  427. package/dist/testing/cross_backend/peer_ping_ws.js +29 -4
  428. package/dist/testing/cross_backend/postgres_database.d.ts +32 -0
  429. package/dist/testing/cross_backend/postgres_database.d.ts.map +1 -1
  430. package/dist/testing/cross_backend/postgres_database.js +56 -1
  431. package/dist/testing/cross_backend/query_shape.d.ts.map +1 -1
  432. package/dist/testing/cross_backend/query_shape.js +18 -1
  433. package/dist/testing/cross_backend/role_grant_offer_lifecycle.d.ts.map +1 -1
  434. package/dist/testing/cross_backend/role_grant_offer_lifecycle.js +96 -2
  435. package/dist/testing/cross_backend/rpc_success_fixtures.js +2 -2
  436. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts +1 -1
  437. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
  438. package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts +17 -5
  439. package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts.map +1 -1
  440. package/dist/testing/cross_backend/rust_spine_stub_backend_config.js +5 -4
  441. package/dist/testing/cross_backend/session_cap.d.ts +8 -0
  442. package/dist/testing/cross_backend/session_cap.d.ts.map +1 -1
  443. package/dist/testing/cross_backend/session_cap.js +66 -2
  444. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  445. package/dist/testing/cross_backend/setup.js +5 -4
  446. package/dist/testing/cross_backend/spine_suites.d.ts +4 -1
  447. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  448. package/dist/testing/cross_backend/spine_suites.js +21 -0
  449. package/dist/testing/cross_backend/spine_surface_constants.d.ts +22 -7
  450. package/dist/testing/cross_backend/spine_surface_constants.d.ts.map +1 -1
  451. package/dist/testing/cross_backend/spine_surface_constants.js +22 -7
  452. package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
  453. package/dist/testing/cross_backend/sse_round_trip.js +4 -3
  454. package/dist/testing/cross_backend/standard.d.ts +6 -12
  455. package/dist/testing/cross_backend/standard.d.ts.map +1 -1
  456. package/dist/testing/cross_backend/test_cell_gated_create_authorize.d.ts +18 -19
  457. package/dist/testing/cross_backend/test_cell_gated_create_authorize.d.ts.map +1 -1
  458. package/dist/testing/cross_backend/test_cell_gated_create_authorize.js +66 -42
  459. package/dist/testing/cross_backend/test_cell_grant_steward_revoke_authorize.d.ts +8 -0
  460. package/dist/testing/cross_backend/test_cell_grant_steward_revoke_authorize.d.ts.map +1 -0
  461. package/dist/testing/cross_backend/test_cell_grant_steward_revoke_authorize.js +22 -0
  462. package/dist/testing/cross_backend/testing_backdoor.d.ts.map +1 -1
  463. package/dist/testing/cross_backend/testing_backdoor.js +4 -6
  464. package/dist/testing/cross_backend/testing_origin_check.d.ts +1 -1
  465. package/dist/testing/cross_backend/testing_origin_check.d.ts.map +1 -1
  466. package/dist/testing/cross_backend/testing_origin_check.js +1 -1
  467. package/dist/testing/cross_backend/testing_reset_actions.d.ts +10 -5
  468. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  469. package/dist/testing/cross_backend/testing_reset_actions.js +7 -6
  470. package/dist/testing/cross_backend/testing_server_bun.d.ts.map +1 -1
  471. package/dist/testing/cross_backend/testing_server_bun.js +5 -4
  472. package/dist/testing/cross_backend/testing_server_core.d.ts +6 -7
  473. package/dist/testing/cross_backend/testing_server_core.d.ts.map +1 -1
  474. package/dist/testing/cross_backend/testing_server_core.js +1 -1
  475. package/dist/testing/cross_backend/testing_server_node.d.ts.map +1 -1
  476. package/dist/testing/cross_backend/testing_server_node.js +13 -2
  477. package/dist/testing/cross_backend/token_lifetime.d.ts.map +1 -1
  478. package/dist/testing/cross_backend/token_lifetime.js +68 -0
  479. package/dist/testing/cross_backend/transport_composition.d.ts +1 -1
  480. package/dist/testing/cross_backend/transport_composition.d.ts.map +1 -1
  481. package/dist/testing/cross_backend/transport_composition.js +48 -6
  482. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts +16 -2
  483. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts.map +1 -1
  484. package/dist/testing/cross_backend/ts_spine_backend_config.js +11 -8
  485. package/dist/testing/cross_backend/ws_action_rate_limit.d.ts.map +1 -1
  486. package/dist/testing/cross_backend/ws_action_rate_limit.js +30 -12
  487. package/dist/testing/cross_backend/ws_connection_cap.d.ts.map +1 -1
  488. package/dist/testing/cross_backend/ws_connection_cap.js +5 -7
  489. package/dist/testing/cross_backend/ws_endpoint.d.ts +2 -3
  490. package/dist/testing/cross_backend/ws_endpoint.d.ts.map +1 -1
  491. package/dist/testing/cross_backend/ws_envelope.d.ts +17 -0
  492. package/dist/testing/cross_backend/ws_envelope.d.ts.map +1 -0
  493. package/dist/testing/cross_backend/ws_envelope.js +172 -0
  494. package/dist/testing/cross_backend/ws_inbound_dispatch.d.ts +16 -0
  495. package/dist/testing/cross_backend/ws_inbound_dispatch.d.ts.map +1 -1
  496. package/dist/testing/cross_backend/ws_inbound_dispatch.js +8 -4
  497. package/dist/testing/cross_backend/ws_outbound_overflow.d.ts.map +1 -1
  498. package/dist/testing/cross_backend/ws_outbound_overflow.js +5 -8
  499. package/dist/testing/cross_backend/ws_role_gated.js +2 -2
  500. package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
  501. package/dist/testing/cross_backend/ws_round_trip.js +6 -4
  502. package/dist/{cli/daemon.d.ts → testing/daemon_info.d.ts} +15 -47
  503. package/dist/testing/daemon_info.d.ts.map +1 -0
  504. package/dist/testing/daemon_info.js +108 -0
  505. package/dist/testing/daemon_token_rotation.js +1 -1
  506. package/dist/testing/data_exposure.d.ts.map +1 -1
  507. package/dist/testing/data_exposure.js +3 -4
  508. package/dist/testing/db.js +2 -2
  509. package/dist/testing/integration.d.ts.map +1 -1
  510. package/dist/testing/integration.js +35 -65
  511. package/dist/testing/integration_helpers.d.ts +0 -15
  512. package/dist/testing/integration_helpers.d.ts.map +1 -1
  513. package/dist/testing/integration_helpers.js +2 -42
  514. package/dist/testing/middleware.d.ts +20 -12
  515. package/dist/testing/middleware.d.ts.map +1 -1
  516. package/dist/testing/middleware.js +49 -40
  517. package/dist/testing/rate_limiting.d.ts.map +1 -1
  518. package/dist/testing/rate_limiting.js +13 -22
  519. package/dist/testing/resolved_auth.d.ts +30 -0
  520. package/dist/testing/resolved_auth.d.ts.map +1 -0
  521. package/dist/testing/resolved_auth.js +41 -0
  522. package/dist/testing/rpc_envelope_cases.d.ts.map +1 -1
  523. package/dist/testing/rpc_envelope_cases.js +3 -0
  524. package/dist/testing/rpc_helpers.d.ts +1 -1
  525. package/dist/testing/rpc_helpers.d.ts.map +1 -1
  526. package/dist/testing/rpc_helpers.js +2 -1
  527. package/dist/testing/sse_round_trip.d.ts +5 -4
  528. package/dist/testing/sse_round_trip.d.ts.map +1 -1
  529. package/dist/testing/sse_round_trip.js +2 -2
  530. package/dist/testing/stubs.d.ts +2 -2
  531. package/dist/testing/stubs.d.ts.map +1 -1
  532. package/dist/testing/stubs.js +24 -21
  533. package/dist/testing/test_origin.d.ts +25 -0
  534. package/dist/testing/test_origin.d.ts.map +1 -0
  535. package/dist/testing/test_origin.js +28 -0
  536. package/dist/testing/transports/raw_http.d.ts +54 -0
  537. package/dist/testing/transports/raw_http.d.ts.map +1 -1
  538. package/dist/testing/transports/raw_http.js +58 -0
  539. package/dist/testing/transports/ws_raw_client.d.ts +8 -0
  540. package/dist/testing/transports/ws_raw_client.d.ts.map +1 -1
  541. package/dist/testing/transports/ws_raw_client.js +13 -0
  542. package/dist/testing/transports/ws_transport.d.ts +15 -2
  543. package/dist/testing/transports/ws_transport.d.ts.map +1 -1
  544. package/dist/testing/transports/ws_transport.js +4 -2
  545. package/dist/testing/ws_round_trip.d.ts +27 -25
  546. package/dist/testing/ws_round_trip.d.ts.map +1 -1
  547. package/dist/testing/ws_round_trip.js +34 -26
  548. package/dist/ui/AccountSessions.svelte +48 -19
  549. package/dist/ui/AccountSessions.svelte.d.ts +2 -17
  550. package/dist/ui/AccountSessions.svelte.d.ts.map +1 -1
  551. package/dist/ui/AdminAccounts.svelte +6 -1
  552. package/dist/ui/AdminAuditLog.svelte +61 -22
  553. package/dist/ui/AdminAuditLog.svelte.d.ts.map +1 -1
  554. package/dist/ui/AdminInvites.svelte +1 -1
  555. package/dist/ui/AdminOverview.svelte +76 -37
  556. package/dist/ui/AdminOverview.svelte.d.ts.map +1 -1
  557. package/dist/ui/AdminRoleGrantHistory.svelte +6 -1
  558. package/dist/ui/AdminSessions.svelte +1 -1
  559. package/dist/ui/AppShell.svelte +3 -2
  560. package/dist/ui/AppShell.svelte.d.ts +3 -2
  561. package/dist/ui/AppShell.svelte.d.ts.map +1 -1
  562. package/dist/ui/CLAUDE.md +121 -35
  563. package/dist/ui/Datatable.svelte +33 -18
  564. package/dist/ui/Datatable.svelte.d.ts +2 -14
  565. package/dist/ui/Datatable.svelte.d.ts.map +1 -1
  566. package/dist/ui/OpenSignupToggle.svelte +10 -1
  567. package/dist/ui/OpenSignupToggle.svelte.d.ts.map +1 -1
  568. package/dist/ui/PopoverButton.svelte +13 -8
  569. package/dist/ui/PopoverButton.svelte.d.ts.map +1 -1
  570. package/dist/ui/RoleGrantOfferForm.svelte +3 -2
  571. package/dist/ui/RoleGrantOfferForm.svelte.d.ts +3 -2
  572. package/dist/ui/RoleGrantOfferForm.svelte.d.ts.map +1 -1
  573. package/dist/ui/RoleGrantOfferHistory.svelte +3 -3
  574. package/dist/ui/RoleGrantOfferInbox.svelte +19 -15
  575. package/dist/ui/RoleGrantOfferInbox.svelte.d.ts.map +1 -1
  576. package/dist/ui/SurfaceExplorer.svelte +66 -27
  577. package/dist/ui/SurfaceExplorer.svelte.d.ts.map +1 -1
  578. package/dist/ui/account_sessions_state.svelte.d.ts +7 -14
  579. package/dist/ui/account_sessions_state.svelte.d.ts.map +1 -1
  580. package/dist/ui/admin_accounts_state.svelte.d.ts +14 -9
  581. package/dist/ui/admin_accounts_state.svelte.d.ts.map +1 -1
  582. package/dist/ui/admin_accounts_state.svelte.js +6 -4
  583. package/dist/ui/admin_invites_state.svelte.d.ts +2 -2
  584. package/dist/ui/admin_invites_state.svelte.d.ts.map +1 -1
  585. package/dist/ui/admin_rpc_adapters.d.ts +9 -9
  586. package/dist/ui/admin_rpc_adapters.d.ts.map +1 -1
  587. package/dist/ui/admin_rpc_adapters.js +9 -9
  588. package/dist/ui/app_settings_state.svelte.d.ts +2 -2
  589. package/dist/ui/app_settings_state.svelte.d.ts.map +1 -1
  590. package/dist/ui/audit_log_state.svelte.d.ts +70 -13
  591. package/dist/ui/audit_log_state.svelte.d.ts.map +1 -1
  592. package/dist/ui/audit_log_state.svelte.js +239 -47
  593. package/dist/ui/backend_probe.svelte.d.ts +93 -0
  594. package/dist/ui/backend_probe.svelte.d.ts.map +1 -0
  595. package/dist/ui/backend_probe.svelte.js +140 -0
  596. package/dist/ui/format_scope.d.ts +7 -7
  597. package/dist/ui/format_scope.d.ts.map +1 -1
  598. package/dist/ui/format_scope.js +4 -4
  599. package/dist/ui/keyed_async_slot.svelte.d.ts +2 -2
  600. package/dist/ui/keyed_async_slot.svelte.js +2 -2
  601. package/dist/ui/popover.svelte.d.ts +18 -19
  602. package/dist/ui/popover.svelte.d.ts.map +1 -1
  603. package/dist/ui/popover.svelte.js +103 -16
  604. package/dist/ui/role_grant_offers_state.svelte.d.ts +41 -46
  605. package/dist/ui/role_grant_offers_state.svelte.d.ts.map +1 -1
  606. package/dist/ui/role_grant_offers_state.svelte.js +37 -19
  607. package/dist/ui/ui_format.d.ts +17 -0
  608. package/dist/ui/ui_format.d.ts.map +1 -1
  609. package/dist/ui/ui_format.js +20 -0
  610. package/package.json +2 -1
  611. package/dist/actions/register_ws_endpoint.d.ts +0 -79
  612. package/dist/actions/register_ws_endpoint.d.ts.map +0 -1
  613. package/dist/actions/register_ws_endpoint.js +0 -109
  614. package/dist/actions/transports_ws_auth_guard.d.ts +0 -94
  615. package/dist/actions/transports_ws_auth_guard.d.ts.map +0 -1
  616. package/dist/actions/transports_ws_auth_guard.js +0 -138
  617. package/dist/actions/transports_ws_backend.d.ts.map +0 -1
  618. package/dist/cli/args.d.ts +0 -55
  619. package/dist/cli/args.d.ts.map +0 -1
  620. package/dist/cli/args.js +0 -100
  621. package/dist/cli/config.d.ts +0 -47
  622. package/dist/cli/config.d.ts.map +0 -1
  623. package/dist/cli/config.js +0 -77
  624. package/dist/cli/daemon.d.ts.map +0 -1
  625. package/dist/cli/daemon.js +0 -181
  626. package/dist/cli/help.d.ts +0 -124
  627. package/dist/cli/help.d.ts.map +0 -1
  628. package/dist/cli/help.js +0 -215
  629. package/dist/cli/logger.d.ts +0 -46
  630. package/dist/cli/logger.d.ts.map +0 -1
  631. package/dist/cli/logger.js +0 -48
  632. package/dist/cli/util.d.ts +0 -36
  633. package/dist/cli/util.d.ts.map +0 -1
  634. package/dist/cli/util.js +0 -50
  635. package/dist/realtime/sse_auth_guard.d.ts +0 -197
  636. package/dist/realtime/sse_auth_guard.d.ts.map +0 -1
  637. package/dist/realtime/sse_auth_guard.js +0 -238
  638. package/dist/realtime/subscriber_registry.d.ts.map +0 -1
  639. package/dist/testing/bootstrap_success.d.ts +0 -29
  640. package/dist/testing/bootstrap_success.d.ts.map +0 -1
  641. package/dist/testing/bootstrap_success.js +0 -144
  642. package/dist/testing/mock_fs.d.ts +0 -20
  643. package/dist/testing/mock_fs.d.ts.map +0 -1
  644. package/dist/testing/mock_fs.js +0 -28
  645. package/dist/testing/standard.d.ts +0 -122
  646. package/dist/testing/standard.d.ts.map +0 -1
  647. package/dist/testing/standard.js +0 -73
  648. package/dist/testing/testing_rate_limiter.d.ts +0 -65
  649. package/dist/testing/testing_rate_limiter.d.ts.map +0 -1
  650. package/dist/testing/testing_rate_limiter.js +0 -80
@@ -21,8 +21,14 @@ import type { SupersededOffer } from './role_grant_offer_schema.js';
21
21
  export declare const ROLE_GRANT_COLUMNS: readonly ["id", "actor_id", "role", "scope_kind", "scope_id", "created_at", "expires_at", "revoked_at", "revoked_by", "revoked_reason", "granted_by", "source_offer_id"];
22
22
  /**
23
23
  * Grant a role_grant to an actor.
24
- * Idempotent — if an active role_grant already exists for this actor, role, and
25
- * scope, returns the existing role_grant instead of creating a duplicate.
24
+ * Idempotent — if an active (unrevoked and unexpired) role_grant already
25
+ * exists for this actor, role, and scope, returns it instead of creating a
26
+ * duplicate. An expired but unrevoked row in the slot is first revoked
27
+ * (`revoked_reason = 'expired'`, `revoked_by` null — nobody chose to revoke
28
+ * it) so the fresh grant can take its place; returning the expired row would
29
+ * report a grant that confers nothing. The revoke and the insert are two
30
+ * statements, atomic when the caller runs them in a transaction (the action
31
+ * and offer-accept paths do).
26
32
  *
27
33
  * The `ON CONFLICT` target and the fallback `SELECT` both collapse `NULL`
28
34
  * scopes via the same sentinel + index-side `'GLOBAL'` token used by the
@@ -37,7 +43,7 @@ export declare const ROLE_GRANT_COLUMNS: readonly ["id", "actor_id", "role", "sc
37
43
  * @param deps - query dependencies
38
44
  * @param input - the role_grant fields
39
45
  * @returns the created or existing active role_grant
40
- * @mutates `role_grant` table - inserts a row when no active role_grant matches `(actor_id, role, scope_kind, scope_id)`
46
+ * @mutates `role_grant` table - revokes an expired unrevoked row at `(actor_id, role, scope_kind, scope_id)`, then inserts a row when no active role_grant matches
41
47
  */
42
48
  export declare const query_create_role_grant: (deps: QueryDeps, input: CreateRoleGrantInput) => Promise<RoleGrant>;
43
49
  /**
@@ -50,11 +56,12 @@ export declare const query_create_role_grant: (deps: QueryDeps, input: CreateRol
50
56
  */
51
57
  export declare const query_role_grant_by_id: (deps: QueryDeps, id: Uuid) => Promise<RoleGrant | undefined>;
52
58
  /**
53
- * Look up the role of an active role_grant (constrained to a specific
54
- * actor) plus the actor's `account_id`.
59
+ * Look up the role and scope of an active role_grant (constrained to a
60
+ * specific actor) plus the actor's `account_id`.
55
61
  *
56
62
  * Used by admin routes to inspect the role_grant's role before acting
57
- * (e.g., enforcing the admin-grant-path gate on revoke). The actor constraint
63
+ * (e.g., enforcing the admin-grant-path gate and the global-`admin` guards on
64
+ * revoke). The actor constraint
58
65
  * mirrors `query_revoke_role_grant` so IDOR protection is consistent:
59
66
  * a caller can only see role_grants belonging to the target actor.
60
67
  *
@@ -71,10 +78,11 @@ export declare const query_role_grant_by_id: (deps: QueryDeps, id: Uuid) => Prom
71
78
  * @param deps - query dependencies
72
79
  * @param role_grant_id - the role_grant id to look up
73
80
  * @param actor_id - the actor that must own the role_grant
74
- * @returns `{role, account_id}` on a match, or `null`
81
+ * @returns `{role, scope_id, account_id}` on a match, or `null`
75
82
  */
76
83
  export declare const query_role_grant_find_active_role_for_actor: (deps: QueryDeps, role_grant_id: string, actor_id: string) => Promise<{
77
84
  role: string;
85
+ scope_id: Uuid | null;
78
86
  account_id: Uuid;
79
87
  } | null>;
80
88
  /** Result of `query_revoke_role_grant` — the revoked role_grant plus any pending offers superseded by the revoke. */
@@ -120,23 +128,11 @@ export declare const query_revoke_role_grant: (deps: QueryDeps, role_grant_id: U
120
128
  * Find all active (non-revoked, non-expired) role_grants for an actor.
121
129
  */
122
130
  export declare const query_role_grant_find_active_for_actor: (deps: QueryDeps, actor_id: string) => Promise<Array<RoleGrant>>;
123
- /**
124
- * Check if an actor has an active role_grant for a given role.
125
- *
126
- * The `scope_id` parameter selects between global and scoped checks:
127
- * - Omitted or `null` — matches a global role_grant (`scope_id IS NULL`).
128
- * Pre-scope callers keep their existing semantics.
129
- * - A scope uuid — matches a role_grant bound to that exact scope.
130
- *
131
- * The `IS NOT DISTINCT FROM` comparison handles the NULL case uniformly.
132
- */
133
- export declare const query_role_grant_has_role: (deps: QueryDeps, actor_id: string, role: string, scope_id?: string | null) => Promise<boolean>;
134
131
  /**
135
132
  * Account-grain check: does any actor on `account_id` hold an active
136
133
  * global role_grant for `role`?
137
134
  *
138
- * Symmetric with `query_role_grant_has_role` but keyed on the account
139
- * instead of a single actor — for surfaces with `auth: actor: 'none'`
135
+ * Keyed on the account rather than a single actor — for surfaces with `auth: actor: 'none'`
140
136
  * that don't load `auth.role_grants` and can't use the in-memory
141
137
  * `has_scoped_role` predicate. Joins `role_grant` → `actor`; matches
142
138
  * only global role_grants (`scope_id IS NULL`) since the use case is
@@ -163,7 +159,7 @@ export declare const query_account_has_global_role: (deps: QueryDeps, account_id
163
159
  * Like `query_account_has_global_role`, but only counts the grant when the
164
160
  * **account and the granting actor are both active** (`deleted_at IS NULL`).
165
161
  * Used by the last-admin branch of the removability guard: a soft-deleted admin
166
- * can't log in and is excluded from `query_count_active_accounts_with_global_role`,
162
+ * can't log in and is excluded from `query_count_active_accounts_with_global_role_for_update`,
167
163
  * so the guard must use the same active predicate when testing whether the
168
164
  * *target* is an admin — otherwise removing an already-tombstoned admin is
169
165
  * falsely blocked as `cannot_delete_last_admin`. Soft-delete does **not** revoke
@@ -183,24 +179,40 @@ export declare const query_account_has_active_global_role: (deps: QueryDeps, acc
183
179
  /**
184
180
  * Count **active** accounts (`deleted_at IS NULL`) holding an active global
185
181
  * role_grant for `role`, counting only grants on **active actors**
186
- * (`act.deleted_at IS NULL`). Used by the last-admin guard on `account_delete` /
187
- * `account_purge`: soft-delete revokes neither a tombstoned account's nor a
182
+ * (`act.deleted_at IS NULL`) — and lock what it counted. Used by the last-admin
183
+ * guards on `account_delete` / `account_purge` and on `role_grant_revoke` of an
184
+ * `admin` grant: soft-delete revokes neither a tombstoned account's nor a
188
185
  * tombstoned actor's admin grant, so an unfiltered count would include unusable
189
186
  * admins — this joins `account` *and* filters the actor, so an account whose
190
187
  * only admin grant sits on a tombstoned actor no longer counts (otherwise the
191
- * guard could fail-open and let the last usable admin be deleted).
188
+ * guard could fail-open and let the last usable admin be removed).
189
+ *
190
+ * **Locks** every counted `role_grant`, `actor`, and `account` row
191
+ * (`FOR UPDATE`) until the caller's transaction ends, so two guards racing to
192
+ * remove the last two admins serialize: the second waits for the first to
193
+ * commit, re-reads the rows it changed, and counts one. Run it inside the
194
+ * transaction that does the removal. Twin of the Rust
195
+ * `query_count_active_accounts_with_global_role_for_update`.
196
+ *
197
+ * `exclude_role_grant_id` leaves one grant out of the count, so a revoke asks
198
+ * whether any active admin account would remain without that grant — an
199
+ * account holding the grant on two actors keeps counting through the other.
200
+ * The excluded grant is still locked, so two revokes of each other's grant
201
+ * serialize rather than deadlock.
192
202
  *
193
203
  * @param deps - query dependencies
194
204
  * @param role - the role to count (e.g. `ROLE_ADMIN`)
205
+ * @param exclude_role_grant_id - a grant to leave out of the count (the one being revoked)
195
206
  * @returns the number of distinct active accounts with an active global `role` grant
207
+ * @mutates `role_grant` / `actor` / `account` rows - row-locks the counted rows for the transaction
196
208
  */
197
- export declare const query_count_active_accounts_with_global_role: (deps: QueryDeps, role: string) => Promise<number>;
209
+ export declare const query_count_active_accounts_with_global_role_for_update: (deps: QueryDeps, role: string, exclude_role_grant_id?: Uuid | null) => Promise<number>;
198
210
  /**
199
- * List all role_grants for an actor (including revoked/expired).
200
- */
201
- export declare const query_role_grant_list_for_actor: (deps: QueryDeps, actor_id: string) => Promise<Array<RoleGrant>>;
202
- /**
203
- * Find the account ID of an account that holds an active role_grant for a given role.
211
+ * Find the account ID of an account that holds an active **global** role_grant
212
+ * for a given role.
213
+ *
214
+ * Global-only (`scope_id IS NULL`), like every builtin read: a scoped row of
215
+ * the role confers nothing, so it must not resolve the keeper identity.
204
216
  *
205
217
  * Joins role_grant → actor → account, counting only an **active account + active
206
218
  * actor** (`deleted_at IS NULL` on both). This *resolves* an identity (the
@@ -257,8 +269,8 @@ export interface RevokeForScopeResult {
257
269
  * orphan role_grants and offers. The cascade is **role-agnostic**: anything
258
270
  * attached to the destroyed scope is cleaned up.
259
271
  *
260
- * Both updates run as separate statements inside the caller's transaction
261
- * (mirrors `query_role_grant_revoke_role`'s shape). The two halves are
272
+ * Both updates run as separate statements inside the caller's transaction,
273
+ * the `role_grant` revoke first. The two halves are
262
274
  * independent — orphan pending offers can exist at a scope with no active
263
275
  * role_grants, so the supersede half always runs even when no role_grant was
264
276
  * revoked.
@@ -272,48 +284,4 @@ export interface RevokeForScopeResult {
272
284
  * @mutates `role_grant_offer` table - stamps `superseded_at` on every pending row at `scope_id`
273
285
  */
274
286
  export declare const query_role_grant_revoke_for_scope: (deps: QueryDeps, scope_id: Uuid, revoked_by: Uuid | null, reason?: string | null) => Promise<RevokeForScopeResult>;
275
- /** Result of `query_role_grant_revoke_role` — every role_grant revoked plus the pending offers superseded by the bulk revoke. */
276
- export interface RevokeRoleResult {
277
- /**
278
- * One entry per role_grant revoked by this call. Carries the revokee's
279
- * `account_id` so callers can fan out a `role_grant_revoke` notification per
280
- * scope-instance. Empty array means nothing was active for `(actor, role)`.
281
- */
282
- revoked: Array<{
283
- role_grant_id: string;
284
- role: string;
285
- scope_kind: string | null;
286
- scope_id: string | null;
287
- account_id: string;
288
- }>;
289
- /**
290
- * Pending offers for the actor's account+role (all scopes) superseded by
291
- * the bulk revoke. Each entry carries its grantor's `from_account_id` so
292
- * callers can fan out `role_grant_offer_supersede` notifications without a
293
- * second round-trip.
294
- */
295
- superseded_offers: Array<SupersededOffer>;
296
- }
297
- /**
298
- * Revoke every active role_grant an actor holds for a given role.
299
- *
300
- * With scoped role_grants a single actor+role tuple can hold several active
301
- * role_grants (one per scope), so this revokes all of them. Pass
302
- * `query_revoke_role_grant(role_grant_id, ...)` when a single scoped role_grant
303
- * is the target.
304
- *
305
- * Also supersedes pending offers for the actor's account across every
306
- * scope of this role (the actor can no longer hold the role, so any
307
- * pending offer of the same role is a bypass vector).
308
- *
309
- * @param deps - query dependencies
310
- * @param actor_id - the actor whose role_grants to revoke
311
- * @param role - the role to revoke
312
- * @param revoked_by - the actor who revoked it (for audit trail)
313
- * @param reason - optional free-form reason, stamped on `role_grant.revoked_reason`.
314
- * @returns the list of revoked role_grants (empty if none were active) and superseded pending offers
315
- * @mutates `role_grant` table - sets `revoked_at`/`revoked_by`/`revoked_reason` on every active row for `(actor, role)`
316
- * @mutates `role_grant_offer` table - stamps `superseded_at` on every matching pending offer
317
- */
318
- export declare const query_role_grant_revoke_role: (deps: QueryDeps, actor_id: string, role: string, revoked_by: string | null, reason?: string | null) => Promise<RevokeRoleResult>;
319
287
  //# sourceMappingURL=role_grant_queries.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"role_grant_queries.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/auth/role_grant_queries.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAEpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,SAAS,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAS3E,OAA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,OAAO,CAYjB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,oCAAoC,GAChD,MAAM,SAAS,EACf,YAAY,MAAM,EAClB,MAAM,MAAM,KACV,OAAO,CAAC,OAAO,CAgBjB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,eAAO,MAAM,uDAAuD,GACnE,MAAM,SAAS,EACf,MAAM,MAAM,EACZ,wBAAuB,IAAI,GAAG,IAAW,KACvC,OAAO,CAAC,MAAM,CAuBhB,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,yCAAyC,GACrD,MAAM,SAAS,EACf,MAAM,MAAM,KACV,OAAO,CAAC,MAAM,GAAG,IAAI,CAgBvB,CAAC;AAEF,8IAA8I;AAC9I,MAAM,WAAW,oBAAoB;IACpC;;;;;;;;OAQG;IACH,OAAO,EAAE,KAAK,CAAC;QACd,aAAa,EAAE,IAAI,CAAC;QACpB,IAAI,EAAE,MAAM,CAAC;QACb,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;QAC1B,QAAQ,EAAE,IAAI,CAAC;QACf,QAAQ,EAAE,IAAI,CAAC;QACf,UAAU,EAAE,IAAI,CAAC;KACjB,CAAC,CAAC;IACH;;;;;;;;;;OAUG;IACH,iBAAiB,EAAE,KAAK,CAAC,eAAe,CAAC,CAAC;CAC1C;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,iCAAiC,GAC7C,MAAM,SAAS,EACf,UAAU,IAAI,EACd,YAAY,IAAI,GAAG,IAAI,EACvB,SAAS,MAAM,GAAG,IAAI,KACpB,OAAO,CAAC,oBAAoB,CA0C9B,CAAC"}
@@ -9,6 +9,7 @@
9
9
  import { assert_row } from '../db/assert_row.js';
10
10
  import { columns_sql, iso8601_timestamp_expr, qualify_columns } from '../db/sql_columns.js';
11
11
  import { ROLE_GRANT_OFFER_COLUMNS, ROLE_GRANT_OFFER_SCOPE_KIND_GLOBAL_TOKEN, ROLE_GRANT_OFFER_SCOPE_SENTINEL_UUID, ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT } from './role_grant_offer_ddl.js';
12
+ import { offer_pending_predicate, role_grant_active_sql } from './role_grant_sql.js';
12
13
  /**
13
14
  * The full `role_grant` column set, named explicitly so a row read fails loud
14
15
  * on schema drift (see `ACCOUNT_COLUMNS` in `auth/account_queries.ts` for the
@@ -44,8 +45,14 @@ const role_grant_expr = iso8601_timestamp_expr(ROLE_GRANT_COLUMNS, [
44
45
  ])('');
45
46
  /**
46
47
  * Grant a role_grant to an actor.
47
- * Idempotent — if an active role_grant already exists for this actor, role, and
48
- * scope, returns the existing role_grant instead of creating a duplicate.
48
+ * Idempotent — if an active (unrevoked and unexpired) role_grant already
49
+ * exists for this actor, role, and scope, returns it instead of creating a
50
+ * duplicate. An expired but unrevoked row in the slot is first revoked
51
+ * (`revoked_reason = 'expired'`, `revoked_by` null — nobody chose to revoke
52
+ * it) so the fresh grant can take its place; returning the expired row would
53
+ * report a grant that confers nothing. The revoke and the insert are two
54
+ * statements, atomic when the caller runs them in a transaction (the action
55
+ * and offer-accept paths do).
49
56
  *
50
57
  * The `ON CONFLICT` target and the fallback `SELECT` both collapse `NULL`
51
58
  * scopes via the same sentinel + index-side `'GLOBAL'` token used by the
@@ -60,9 +67,17 @@ const role_grant_expr = iso8601_timestamp_expr(ROLE_GRANT_COLUMNS, [
60
67
  * @param deps - query dependencies
61
68
  * @param input - the role_grant fields
62
69
  * @returns the created or existing active role_grant
63
- * @mutates `role_grant` table - inserts a row when no active role_grant matches `(actor_id, role, scope_kind, scope_id)`
70
+ * @mutates `role_grant` table - revokes an expired unrevoked row at `(actor_id, role, scope_kind, scope_id)`, then inserts a row when no active role_grant matches
64
71
  */
65
72
  export const query_create_role_grant = async (deps, input) => {
73
+ // an expired row still holds the partial unique slot (`revoked_at IS NULL`)
74
+ await deps.db.query(`UPDATE role_grant SET revoked_at = NOW(), revoked_by = NULL, revoked_reason = 'expired'
75
+ WHERE actor_id = $1
76
+ AND role = $2
77
+ AND scope_kind IS NOT DISTINCT FROM $3
78
+ AND scope_id IS NOT DISTINCT FROM $4
79
+ AND revoked_at IS NULL
80
+ AND expires_at <= NOW()`, [input.actor_id, input.role, input.scope_kind ?? null, input.scope_id ?? null]);
66
81
  const inserted = await deps.db.query_one(`INSERT INTO role_grant (actor_id, role, scope_kind, scope_id, expires_at, granted_by, source_offer_id)
67
82
  VALUES ($1, $2, $3, $4, $5, $6, $7)
68
83
  ON CONFLICT (
@@ -103,11 +118,12 @@ export const query_create_role_grant = async (deps, input) => {
103
118
  */
104
119
  export const query_role_grant_by_id = async (deps, id) => deps.db.query_one(`SELECT ${columns_sql(ROLE_GRANT_COLUMNS, role_grant_expr)} FROM role_grant WHERE id = $1`, [id]);
105
120
  /**
106
- * Look up the role of an active role_grant (constrained to a specific
107
- * actor) plus the actor's `account_id`.
121
+ * Look up the role and scope of an active role_grant (constrained to a
122
+ * specific actor) plus the actor's `account_id`.
108
123
  *
109
124
  * Used by admin routes to inspect the role_grant's role before acting
110
- * (e.g., enforcing the admin-grant-path gate on revoke). The actor constraint
125
+ * (e.g., enforcing the admin-grant-path gate and the global-`admin` guards on
126
+ * revoke). The actor constraint
111
127
  * mirrors `query_revoke_role_grant` so IDOR protection is consistent:
112
128
  * a caller can only see role_grants belonging to the target actor.
113
129
  *
@@ -124,10 +140,10 @@ export const query_role_grant_by_id = async (deps, id) => deps.db.query_one(`SEL
124
140
  * @param deps - query dependencies
125
141
  * @param role_grant_id - the role_grant id to look up
126
142
  * @param actor_id - the actor that must own the role_grant
127
- * @returns `{role, account_id}` on a match, or `null`
143
+ * @returns `{role, scope_id, account_id}` on a match, or `null`
128
144
  */
129
145
  export const query_role_grant_find_active_role_for_actor = async (deps, role_grant_id, actor_id) => {
130
- const row = await deps.db.query_one(`SELECT role_grant.role, actor.account_id
146
+ const row = await deps.db.query_one(`SELECT role_grant.role, role_grant.scope_id, actor.account_id
131
147
  FROM role_grant
132
148
  JOIN actor ON actor.id = role_grant.actor_id
133
149
  WHERE role_grant.id = $1 AND role_grant.actor_id = $2 AND role_grant.revoked_at IS NULL`, [role_grant_id, actor_id]);
@@ -175,10 +191,7 @@ export const query_revoke_role_grant = async (deps, role_grant_id, actor_id, rev
175
191
  AND o.to_account_id = a.account_id
176
192
  AND o.role = $2
177
193
  AND o.scope_id IS NOT DISTINCT FROM $3
178
- AND o.accepted_at IS NULL
179
- AND o.declined_at IS NULL
180
- AND o.retracted_at IS NULL
181
- AND o.superseded_at IS NULL
194
+ AND ${offer_pending_predicate('o')}
182
195
  -- raw columns; the outer grantor select formats them
183
196
  RETURNING ${qualify_columns(ROLE_GRANT_OFFER_COLUMNS, 'o')}
184
197
  )
@@ -197,37 +210,14 @@ export const query_revoke_role_grant = async (deps, role_grant_id, actor_id, rev
197
210
  export const query_role_grant_find_active_for_actor = async (deps, actor_id) => {
198
211
  return deps.db.query(`SELECT ${columns_sql(ROLE_GRANT_COLUMNS, role_grant_expr)} FROM role_grant
199
212
  WHERE actor_id = $1
200
- AND revoked_at IS NULL
201
- AND (expires_at IS NULL OR expires_at > NOW())
213
+ AND ${role_grant_active_sql('')}
202
214
  ORDER BY role_grant.created_at`, [actor_id]);
203
215
  };
204
- /**
205
- * Check if an actor has an active role_grant for a given role.
206
- *
207
- * The `scope_id` parameter selects between global and scoped checks:
208
- * - Omitted or `null` — matches a global role_grant (`scope_id IS NULL`).
209
- * Pre-scope callers keep their existing semantics.
210
- * - A scope uuid — matches a role_grant bound to that exact scope.
211
- *
212
- * The `IS NOT DISTINCT FROM` comparison handles the NULL case uniformly.
213
- */
214
- export const query_role_grant_has_role = async (deps, actor_id, role, scope_id) => {
215
- const row = await deps.db.query_one(`SELECT EXISTS(
216
- SELECT 1 FROM role_grant
217
- WHERE actor_id = $1
218
- AND role = $2
219
- AND scope_id IS NOT DISTINCT FROM $3
220
- AND revoked_at IS NULL
221
- AND (expires_at IS NULL OR expires_at > NOW())
222
- ) AS exists`, [actor_id, role, scope_id ?? null]);
223
- return row?.exists ?? false;
224
- };
225
216
  /**
226
217
  * Account-grain check: does any actor on `account_id` hold an active
227
218
  * global role_grant for `role`?
228
219
  *
229
- * Symmetric with `query_role_grant_has_role` but keyed on the account
230
- * instead of a single actor — for surfaces with `auth: actor: 'none'`
220
+ * Keyed on the account rather than a single actor — for surfaces with `auth: actor: 'none'`
231
221
  * that don't load `auth.role_grants` and can't use the in-memory
232
222
  * `has_scoped_role` predicate. Joins `role_grant` → `actor`; matches
233
223
  * only global role_grants (`scope_id IS NULL`) since the use case is
@@ -255,8 +245,7 @@ export const query_account_has_global_role = async (deps, account_id, role) => {
255
245
  WHERE actor_id IN (SELECT id FROM actor WHERE account_id = $1)
256
246
  AND role = $2
257
247
  AND scope_id IS NULL
258
- AND revoked_at IS NULL
259
- AND (expires_at IS NULL OR expires_at > NOW())
248
+ AND ${role_grant_active_sql('')}
260
249
  ) AS exists`, [account_id, role]);
261
250
  return row?.exists ?? false;
262
251
  };
@@ -264,7 +253,7 @@ export const query_account_has_global_role = async (deps, account_id, role) => {
264
253
  * Like `query_account_has_global_role`, but only counts the grant when the
265
254
  * **account and the granting actor are both active** (`deleted_at IS NULL`).
266
255
  * Used by the last-admin branch of the removability guard: a soft-deleted admin
267
- * can't log in and is excluded from `query_count_active_accounts_with_global_role`,
256
+ * can't log in and is excluded from `query_count_active_accounts_with_global_role_for_update`,
268
257
  * so the guard must use the same active predicate when testing whether the
269
258
  * *target* is an admin — otherwise removing an already-tombstoned admin is
270
259
  * falsely blocked as `cannot_delete_last_admin`. Soft-delete does **not** revoke
@@ -290,27 +279,46 @@ export const query_account_has_active_global_role = async (deps, account_id, rol
290
279
  AND act.deleted_at IS NULL
291
280
  AND rg.role = $2
292
281
  AND rg.scope_id IS NULL
293
- AND rg.revoked_at IS NULL
294
- AND (rg.expires_at IS NULL OR rg.expires_at > NOW())
282
+ AND ${role_grant_active_sql('rg')}
295
283
  ) AS exists`, [account_id, role]);
296
284
  return row?.exists ?? false;
297
285
  };
298
286
  /**
299
287
  * Count **active** accounts (`deleted_at IS NULL`) holding an active global
300
288
  * role_grant for `role`, counting only grants on **active actors**
301
- * (`act.deleted_at IS NULL`). Used by the last-admin guard on `account_delete` /
302
- * `account_purge`: soft-delete revokes neither a tombstoned account's nor a
289
+ * (`act.deleted_at IS NULL`) — and lock what it counted. Used by the last-admin
290
+ * guards on `account_delete` / `account_purge` and on `role_grant_revoke` of an
291
+ * `admin` grant: soft-delete revokes neither a tombstoned account's nor a
303
292
  * tombstoned actor's admin grant, so an unfiltered count would include unusable
304
293
  * admins — this joins `account` *and* filters the actor, so an account whose
305
294
  * only admin grant sits on a tombstoned actor no longer counts (otherwise the
306
- * guard could fail-open and let the last usable admin be deleted).
295
+ * guard could fail-open and let the last usable admin be removed).
296
+ *
297
+ * **Locks** every counted `role_grant`, `actor`, and `account` row
298
+ * (`FOR UPDATE`) until the caller's transaction ends, so two guards racing to
299
+ * remove the last two admins serialize: the second waits for the first to
300
+ * commit, re-reads the rows it changed, and counts one. Run it inside the
301
+ * transaction that does the removal. Twin of the Rust
302
+ * `query_count_active_accounts_with_global_role_for_update`.
303
+ *
304
+ * `exclude_role_grant_id` leaves one grant out of the count, so a revoke asks
305
+ * whether any active admin account would remain without that grant — an
306
+ * account holding the grant on two actors keeps counting through the other.
307
+ * The excluded grant is still locked, so two revokes of each other's grant
308
+ * serialize rather than deadlock.
307
309
  *
308
310
  * @param deps - query dependencies
309
311
  * @param role - the role to count (e.g. `ROLE_ADMIN`)
312
+ * @param exclude_role_grant_id - a grant to leave out of the count (the one being revoked)
310
313
  * @returns the number of distinct active accounts with an active global `role` grant
314
+ * @mutates `role_grant` / `actor` / `account` rows - row-locks the counted rows for the transaction
311
315
  */
312
- export const query_count_active_accounts_with_global_role = async (deps, role) => {
313
- const row = await deps.db.query_one(`SELECT COUNT(DISTINCT a.id) AS count
316
+ export const query_count_active_accounts_with_global_role_for_update = async (deps, role, exclude_role_grant_id = null) => {
317
+ // `FOR UPDATE` refuses an aggregate, so the rows come back and are counted
318
+ // here. The excluded grant is dropped in code, not SQL: filtering it in the
319
+ // WHERE would leave it unlocked, and two cross-revokes (each locking only the
320
+ // other's grant, then updating it) would deadlock instead of serializing
321
+ const rows = await deps.db.query(`SELECT a.id AS account_id, rg.id AS role_grant_id
314
322
  FROM account a
315
323
  JOIN actor act ON act.account_id = a.id
316
324
  JOIN role_grant rg ON rg.actor_id = act.id
@@ -318,18 +326,21 @@ export const query_count_active_accounts_with_global_role = async (deps, role) =
318
326
  AND act.deleted_at IS NULL
319
327
  AND rg.role = $1
320
328
  AND rg.scope_id IS NULL
321
- AND rg.revoked_at IS NULL
322
- AND (rg.expires_at IS NULL OR rg.expires_at > NOW())`, [role]);
323
- return Number(row?.count ?? 0);
324
- };
325
- /**
326
- * List all role_grants for an actor (including revoked/expired).
327
- */
328
- export const query_role_grant_list_for_actor = async (deps, actor_id) => {
329
- return deps.db.query(`SELECT ${columns_sql(ROLE_GRANT_COLUMNS, role_grant_expr)} FROM role_grant WHERE actor_id = $1 ORDER BY role_grant.created_at DESC`, [actor_id]);
329
+ AND ${role_grant_active_sql('rg')}
330
+ FOR UPDATE`, [role]);
331
+ const account_ids = new Set();
332
+ for (const row of rows) {
333
+ if (row.role_grant_id !== exclude_role_grant_id)
334
+ account_ids.add(row.account_id);
335
+ }
336
+ return account_ids.size;
330
337
  };
331
338
  /**
332
- * Find the account ID of an account that holds an active role_grant for a given role.
339
+ * Find the account ID of an account that holds an active **global** role_grant
340
+ * for a given role.
341
+ *
342
+ * Global-only (`scope_id IS NULL`), like every builtin read: a scoped row of
343
+ * the role confers nothing, so it must not resolve the keeper identity.
333
344
  *
334
345
  * Joins role_grant → actor → account, counting only an **active account + active
335
346
  * actor** (`deleted_at IS NULL` on both). This *resolves* an identity (the
@@ -349,10 +360,10 @@ export const query_role_grant_find_account_id_for_role = async (deps, role) => {
349
360
  JOIN actor act ON act.id = p.actor_id
350
361
  JOIN account a ON a.id = act.account_id
351
362
  WHERE p.role = $1
363
+ AND p.scope_id IS NULL
352
364
  AND a.deleted_at IS NULL
353
365
  AND act.deleted_at IS NULL
354
- AND p.revoked_at IS NULL
355
- AND (p.expires_at IS NULL OR p.expires_at > NOW())
366
+ AND ${role_grant_active_sql('p')}
356
367
  ORDER BY a.id ASC
357
368
  LIMIT 1`, [role]);
358
369
  return row?.account_id ?? null;
@@ -367,8 +378,8 @@ export const query_role_grant_find_account_id_for_role = async (deps, role) => {
367
378
  * orphan role_grants and offers. The cascade is **role-agnostic**: anything
368
379
  * attached to the destroyed scope is cleaned up.
369
380
  *
370
- * Both updates run as separate statements inside the caller's transaction
371
- * (mirrors `query_role_grant_revoke_role`'s shape). The two halves are
381
+ * Both updates run as separate statements inside the caller's transaction,
382
+ * the `role_grant` revoke first. The two halves are
372
383
  * independent — orphan pending offers can exist at a scope with no active
373
384
  * role_grants, so the supersede half always runs even when no role_grant was
374
385
  * revoked.
@@ -404,66 +415,10 @@ export const query_role_grant_revoke_for_scope = async (deps, scope_id, revoked_
404
415
  UPDATE role_grant_offer o
405
416
  SET superseded_at = NOW()
406
417
  WHERE o.scope_id = $1
407
- AND o.accepted_at IS NULL
408
- AND o.declined_at IS NULL
409
- AND o.retracted_at IS NULL
410
- AND o.superseded_at IS NULL
418
+ AND ${offer_pending_predicate('o')}
411
419
  -- raw columns; the outer grantor select formats them
412
420
  RETURNING ${qualify_columns(ROLE_GRANT_OFFER_COLUMNS, 'o')}
413
421
  )
414
422
  ${ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT}`, [scope_id]);
415
423
  return { revoked, superseded_offers };
416
424
  };
417
- /**
418
- * Revoke every active role_grant an actor holds for a given role.
419
- *
420
- * With scoped role_grants a single actor+role tuple can hold several active
421
- * role_grants (one per scope), so this revokes all of them. Pass
422
- * `query_revoke_role_grant(role_grant_id, ...)` when a single scoped role_grant
423
- * is the target.
424
- *
425
- * Also supersedes pending offers for the actor's account across every
426
- * scope of this role (the actor can no longer hold the role, so any
427
- * pending offer of the same role is a bypass vector).
428
- *
429
- * @param deps - query dependencies
430
- * @param actor_id - the actor whose role_grants to revoke
431
- * @param role - the role to revoke
432
- * @param revoked_by - the actor who revoked it (for audit trail)
433
- * @param reason - optional free-form reason, stamped on `role_grant.revoked_reason`.
434
- * @returns the list of revoked role_grants (empty if none were active) and superseded pending offers
435
- * @mutates `role_grant` table - sets `revoked_at`/`revoked_by`/`revoked_reason` on every active row for `(actor, role)`
436
- * @mutates `role_grant_offer` table - stamps `superseded_at` on every matching pending offer
437
- */
438
- export const query_role_grant_revoke_role = async (deps, actor_id, role, revoked_by, reason) => {
439
- // CTE pulls the revokee's `account_id` via a join on `actor` so callers
440
- // can address the revokee without an extra round-trip.
441
- const revoked = await deps.db.query(`WITH updated AS (
442
- UPDATE role_grant
443
- SET revoked_at = NOW(), revoked_by = $3, revoked_reason = $4
444
- WHERE actor_id = $1 AND role = $2 AND revoked_at IS NULL
445
- RETURNING id, role, scope_kind, scope_id, actor_id
446
- )
447
- SELECT u.id AS role_grant_id, u.role, u.scope_kind, u.scope_id, a.account_id
448
- FROM updated u
449
- JOIN actor a ON a.id = u.actor_id`, [actor_id, role, revoked_by ?? null, reason ?? null]);
450
- if (revoked.length === 0) {
451
- return { revoked: [], superseded_offers: [] };
452
- }
453
- const superseded_offers = await deps.db.query(`WITH updated AS (
454
- UPDATE role_grant_offer o
455
- SET superseded_at = NOW()
456
- FROM actor a
457
- WHERE a.id = $1
458
- AND o.to_account_id = a.account_id
459
- AND o.role = $2
460
- AND o.accepted_at IS NULL
461
- AND o.declined_at IS NULL
462
- AND o.retracted_at IS NULL
463
- AND o.superseded_at IS NULL
464
- -- raw columns; the outer grantor select formats them
465
- RETURNING ${qualify_columns(ROLE_GRANT_OFFER_COLUMNS, 'o')}
466
- )
467
- ${ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT}`, [actor_id, role]);
468
- return { revoked, superseded_offers };
469
- };
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The liveness predicates of `role_grant` and `role_grant_offer` — one
3
+ * spelling each, so a new terminal state or liveness condition lands in one
4
+ * place instead of at every query that filters on it.
5
+ *
6
+ * Twins of the Rust spine's `role_grant_active_sql` and
7
+ * `offer_pending_predicate` (with its `OFFER_TERMINAL_COLUMNS`). The frozen
8
+ * partial-index DDL (`auth/role_grant_offer_ddl.ts`, `auth/migrations.ts`)
9
+ * keeps its literal predicate text, since a migration body never changes.
10
+ *
11
+ * @module
12
+ */
13
+ /**
14
+ * The SQL predicate for an **active** role grant: not revoked and not
15
+ * expired.
16
+ *
17
+ * The grant row's own liveness only. Whether its holder is live — the actor
18
+ * and its account not soft-deleted, which doesn't revoke grants — is a join
19
+ * the caller adds where the read needs it.
20
+ *
21
+ * @param alias - the table alias qualifying the columns, or `''` for unqualified columns
22
+ * @returns the predicate, with no surrounding parentheses (it is a top-level `AND` chain)
23
+ */
24
+ export declare const role_grant_active_sql: (alias: string) => string;
25
+ /**
26
+ * The `role_grant_offer` timestamp columns that each end an offer. An offer is
27
+ * pending while every one is null.
28
+ */
29
+ export declare const OFFER_TERMINAL_COLUMNS: readonly ["accepted_at", "declined_at", "retracted_at", "superseded_at"];
30
+ /**
31
+ * The still-pending predicate for a role grant offer — every
32
+ * `OFFER_TERMINAL_COLUMNS` entry `IS NULL`, `AND`-joined.
33
+ *
34
+ * Expiry is not part of it: an expired offer that no sweep has terminated is
35
+ * still pending here, and each read that must refuse one adds its own
36
+ * `expires_at` condition.
37
+ *
38
+ * @param alias - the table alias qualifying the columns, or `''` for unqualified columns
39
+ * @returns the predicate, with no surrounding parentheses
40
+ */
41
+ export declare const offer_pending_predicate: (alias: string) => string;
42
+ //# sourceMappingURL=role_grant_sql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"role_grant_sql.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/auth/role_grant_sql.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAQH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,qBAAqB,GAAI,OAAO,MAAM,KAAG,MAIrD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,0EAKzB,CAAC;AAEX;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,GAAI,OAAO,MAAM,KAAG,MACkC,CAAC"}
@@ -0,0 +1,54 @@
1
+ /**
2
+ * The liveness predicates of `role_grant` and `role_grant_offer` — one
3
+ * spelling each, so a new terminal state or liveness condition lands in one
4
+ * place instead of at every query that filters on it.
5
+ *
6
+ * Twins of the Rust spine's `role_grant_active_sql` and
7
+ * `offer_pending_predicate` (with its `OFFER_TERMINAL_COLUMNS`). The frozen
8
+ * partial-index DDL (`auth/role_grant_offer_ddl.ts`, `auth/migrations.ts`)
9
+ * keeps its literal predicate text, since a migration body never changes.
10
+ *
11
+ * @module
12
+ */
13
+ /**
14
+ * Prefix `column` with `alias`, or leave it bare when `alias` is empty.
15
+ */
16
+ const qualify = (alias, column) => alias === '' ? column : `${alias}.${column}`;
17
+ /**
18
+ * The SQL predicate for an **active** role grant: not revoked and not
19
+ * expired.
20
+ *
21
+ * The grant row's own liveness only. Whether its holder is live — the actor
22
+ * and its account not soft-deleted, which doesn't revoke grants — is a join
23
+ * the caller adds where the read needs it.
24
+ *
25
+ * @param alias - the table alias qualifying the columns, or `''` for unqualified columns
26
+ * @returns the predicate, with no surrounding parentheses (it is a top-level `AND` chain)
27
+ */
28
+ export const role_grant_active_sql = (alias) => {
29
+ const revoked_at = qualify(alias, 'revoked_at');
30
+ const expires_at = qualify(alias, 'expires_at');
31
+ return `${revoked_at} IS NULL AND (${expires_at} IS NULL OR ${expires_at} > NOW())`;
32
+ };
33
+ /**
34
+ * The `role_grant_offer` timestamp columns that each end an offer. An offer is
35
+ * pending while every one is null.
36
+ */
37
+ export const OFFER_TERMINAL_COLUMNS = [
38
+ 'accepted_at',
39
+ 'declined_at',
40
+ 'retracted_at',
41
+ 'superseded_at'
42
+ ];
43
+ /**
44
+ * The still-pending predicate for a role grant offer — every
45
+ * `OFFER_TERMINAL_COLUMNS` entry `IS NULL`, `AND`-joined.
46
+ *
47
+ * Expiry is not part of it: an expired offer that no sweep has terminated is
48
+ * still pending here, and each read that must refuse one adds its own
49
+ * `expires_at` condition.
50
+ *
51
+ * @param alias - the table alias qualifying the columns, or `''` for unqualified columns
52
+ * @returns the predicate, with no surrounding parentheses
53
+ */
54
+ export const offer_pending_predicate = (alias) => OFFER_TERMINAL_COLUMNS.map((column) => `${qualify(alias, column)} IS NULL`).join(' AND ');