@opengeni/db 0.36.1 → 3.1.0-canary.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 (274) hide show
  1. package/dist/attached-browser-devices.d.ts +7 -0
  2. package/dist/automations.d.ts +154 -0
  3. package/dist/canonical-human-identities.js +3 -3
  4. package/dist/capability-integrations.d.ts +13 -0
  5. package/dist/child-lifecycle-notices.d.ts +68 -0
  6. package/dist/{chunk-GYO7ED2Q.js → chunk-EULAN2NP.js} +2 -2
  7. package/dist/{chunk-VTPRIXKG.js → chunk-NJVZPWXY.js} +3 -3
  8. package/dist/{chunk-HSOWSYOX.js → chunk-NMUBV7Z5.js} +19 -11
  9. package/dist/chunk-NMUBV7Z5.js.map +1 -0
  10. package/dist/chunk-OF5OBB76.js +4457 -0
  11. package/dist/chunk-OF5OBB76.js.map +1 -0
  12. package/dist/{chunk-5KTIEDX7.js → chunk-PR2WMLOQ.js} +7714 -4443
  13. package/dist/chunk-PR2WMLOQ.js.map +1 -0
  14. package/dist/{chunk-Z7VYMNPZ.js → chunk-SCPUJS53.js} +41 -4
  15. package/dist/chunk-SCPUJS53.js.map +1 -0
  16. package/dist/{chunk-SK7YYHBI.js → chunk-SIBYBA2D.js} +46 -1
  17. package/dist/chunk-SIBYBA2D.js.map +1 -0
  18. package/dist/chunk-TIKF3KTI.js +383 -0
  19. package/dist/chunk-TIKF3KTI.js.map +1 -0
  20. package/dist/chunk-ZZ5LJEFX.js +516 -0
  21. package/dist/chunk-ZZ5LJEFX.js.map +1 -0
  22. package/dist/codemode-operations.d.ts +1 -6
  23. package/dist/company-brain-context-selection-schema.d.ts +705 -0
  24. package/dist/company-brain-context-selection.d.ts +94 -0
  25. package/dist/company-brain-governed-writes.d.ts +24 -0
  26. package/dist/company-brain.d.ts +55 -0
  27. package/dist/company-profile-agent-admin.d.ts +15 -0
  28. package/dist/company-profile-schema.d.ts +619 -2
  29. package/dist/company-profile.d.ts +9 -4
  30. package/dist/connection-authority.d.ts +95 -0
  31. package/dist/connection-scopes.d.ts +8 -0
  32. package/dist/connection-tenancy-backfill.d.ts +41 -0
  33. package/dist/connection-token-resolver.d.ts +30 -1
  34. package/dist/database.d.ts +19 -0
  35. package/dist/editable-artifact-durable-export.d.ts +10 -0
  36. package/dist/editable-artifact-durable-export.js +18 -3
  37. package/dist/editable-artifact-durable-export.js.map +1 -1
  38. package/dist/editable-artifacts.d.ts +2 -2
  39. package/dist/editable-artifacts.js +3 -3
  40. package/dist/governed-learning-activation-schema.d.ts +984 -0
  41. package/dist/governed-learning-activation.d.ts +98 -0
  42. package/dist/governed-learning-evaluator-schema.d.ts +633 -0
  43. package/dist/governed-learning-evaluator.d.ts +28 -0
  44. package/dist/index.d.ts +1633 -87
  45. package/dist/index.js +22004 -8109
  46. package/dist/index.js.map +1 -1
  47. package/dist/insights.d.ts +18 -0
  48. package/dist/integration-bindings.d.ts +1 -0
  49. package/dist/integration-facets.d.ts +30 -0
  50. package/dist/interaction-placement-loss.d.ts +22 -0
  51. package/dist/knowledge-source-sync-schema.d.ts +730 -0
  52. package/dist/knowledge-source-sync.d.ts +60 -0
  53. package/dist/managed-human-provisioning.d.ts +25 -0
  54. package/dist/memory-domain.d.ts +3 -3
  55. package/dist/migrate.d.ts +11 -1
  56. package/dist/migrate.js +1 -1
  57. package/dist/new-session-drafts.d.ts +43 -5
  58. package/dist/organization-membership-backfill.d.ts +174 -0
  59. package/dist/organization-membership-lifecycle.d.ts +162 -0
  60. package/dist/pr-review.d.ts +107 -0
  61. package/dist/preference-registry-schema.d.ts +2 -0
  62. package/dist/preference-registry.d.ts +2 -0
  63. package/dist/provision-roles.js +1 -1
  64. package/dist/runtime-posture.d.ts +23 -9
  65. package/dist/schema.d.ts +24925 -13697
  66. package/dist/schema.js +103 -1
  67. package/dist/scoped-knowledge-schema.d.ts +52 -1
  68. package/dist/session-background-commands.d.ts +125 -0
  69. package/dist/session-background-commands.js +34 -0
  70. package/dist/session-background-commands.js.map +1 -0
  71. package/dist/session-control.d.ts +156 -3
  72. package/dist/session-goal-pacing.d.ts +68 -0
  73. package/dist/session-queue-commands.d.ts +42 -5
  74. package/dist/session-realtime-ledger.d.ts +3 -0
  75. package/dist/session-tenancy.d.ts +71 -1
  76. package/dist/session-tenancy.js +25 -3
  77. package/dist/session-tool-call-settlement.d.ts +9 -4
  78. package/dist/slack-routing-personal-workspace.d.ts +23 -0
  79. package/dist/slack-routing.d.ts +153 -0
  80. package/dist/slack-task-policy-schema.d.ts +34 -0
  81. package/dist/slack-task-policy.d.ts +8 -0
  82. package/dist/slack-user-link-access.d.ts +1 -0
  83. package/dist/task-notes-schema.d.ts +594 -0
  84. package/dist/task-notes.d.ts +39 -0
  85. package/dist/turn-initiator.d.ts +2 -0
  86. package/dist/user-resource-authority.d.ts +109 -0
  87. package/dist/video-generation.js +3 -3
  88. package/dist/workspace-authority.d.ts +81 -0
  89. package/dist/workspace-instruction-policies-schema.d.ts +279 -0
  90. package/dist/workspace-instruction-policies.d.ts +62 -0
  91. package/dist/workspace-learning-policy.d.ts +1 -0
  92. package/dist/workspace-membership-permissions.d.ts +2 -0
  93. package/drizzle/0240_enrollment_connection_authority.sql +39 -0
  94. package/drizzle/0240_model_context_user_messages.sql +204 -0
  95. package/drizzle/0241_atomic_personal_resource_delegation.sql +1000 -0
  96. package/drizzle/0241_enrollment_agent_runtime.sql +70 -0
  97. package/drizzle/0243_google_drive_object_acl_authority.sql +554 -0
  98. package/drizzle/0244_slack_app_home_refresh_queue.sql +120 -0
  99. package/drizzle/0245_model_context_contribution_facts.sql +88 -0
  100. package/drizzle/0246_integration_personal_instance_authority.sql +70 -0
  101. package/drizzle/0247_terraform_stacks_provenance_repair.sql +428 -0
  102. package/drizzle/0248_terraform_stacks_component_resolution_fence.sql +83 -0
  103. package/drizzle/0249_personal_resource_delegation_authority_correction.sql +662 -0
  104. package/drizzle/0250_direct_retained_process_owner_liveness.sql +199 -0
  105. package/drizzle/0251_connected_machine_operation_policy.sql +45 -0
  106. package/drizzle/0252_scheduled_personal_resource_delegation.sql +1867 -0
  107. package/drizzle/0253_common_user_resource_authority_lifecycle.sql +1555 -0
  108. package/drizzle/0254_scoped_variable_set_authority.sql +1255 -0
  109. package/drizzle/0255_company_brain_governed_write_proposals.sql +524 -0
  110. package/drizzle/0256_connection_authority_delegation.sql +703 -0
  111. package/drizzle/0257_goal_revision_decisions_and_root_constraints.sql +436 -0
  112. package/drizzle/0258_three_scope_document_knowledge_authority.sql +1273 -0
  113. package/drizzle/0259_company_brain_context_selection_receipts.sql +869 -0
  114. package/drizzle/0260_task_note_knowledge_promotion.sql +820 -0
  115. package/drizzle/0261_preference_knowledge_proposal_actor_binding.sql +471 -0
  116. package/drizzle/0262_scoped_connected_machines_and_rigs.sql +1700 -0
  117. package/drizzle/0263_organization_membership_lifecycle.sql +3284 -0
  118. package/drizzle/0264_connection_authority_runtime_activation.sql +1084 -0
  119. package/drizzle/0266_company_brain_context_receipt_inspection.sql +156 -0
  120. package/drizzle/0268_governed_learning_decision_receipts.sql +825 -0
  121. package/drizzle/0269_governed_learning_activation_controller.sql +1874 -0
  122. package/drizzle/0270_governed_learning_history_inspection.sql +170 -0
  123. package/drizzle/0271_company_brain_retrieval_only_default.sql +524 -0
  124. package/drizzle/0272_human_confirmed_learning_activation.sql +591 -0
  125. package/drizzle/0273_scheduled_variable_set_materialization.sql +268 -0
  126. package/drizzle/0274_human_confirmed_knowledge_review.sql +310 -0
  127. package/drizzle/0275_scheduled_connection_authority.sql +5527 -0
  128. package/drizzle/0276_onboarding_proposal_initiating_human_guc.sql +198 -0
  129. package/drizzle/0277_workspace_writer_authority_attribution.sql +393 -0
  130. package/drizzle/0278_workspace_membership_removal_fencing.sql +1174 -0
  131. package/drizzle/0279_workspace_connection_use_lane.sql +343 -0
  132. package/drizzle/0280_connection_and_variable_set_audit_attribution.sql +773 -0
  133. package/drizzle/0281_viewer_holder_authority_claims.sql +52 -0
  134. package/drizzle/0282_variable_set_session_attach_attribution.sql +178 -0
  135. package/drizzle/0283_editable_spreadsheet_authored_state.sql +82 -0
  136. package/drizzle/0284_truthful_human_confirmed_review_reason.sql +930 -0
  137. package/drizzle/0285_organization_tenancy_inventory.sql +329 -0
  138. package/drizzle/0286_widen_task_note_expiry_ceiling.sql +391 -0
  139. package/drizzle/0287_open_suffix_pending_tool_calls.sql +18 -0
  140. package/drizzle/0288_attached_browser_reenrollment.sql +64 -0
  141. package/drizzle/0289_session_composer_policy_authority.sql +597 -0
  142. package/drizzle/0290_organization_membership_backfill.sql +268 -0
  143. package/drizzle/0291_resource_authority_classification_assertion.sql +371 -0
  144. package/drizzle/0292_truthful_tenancy_inventory_counters.sql +291 -0
  145. package/drizzle/0293_confirm_time_rule_rebaseline.sql +615 -0
  146. package/drizzle/0294_preference_activation_authority.sql +237 -0
  147. package/drizzle/0295_retire_legacy_standing_memory_mode.sql +476 -0
  148. package/drizzle/0296_force_rls_backfill_noop_repair.sql +215 -0
  149. package/drizzle/0297_session_ownership_classification_and_backfill.sql +752 -0
  150. package/drizzle/0298_organization_tenancy_parity.sql +609 -0
  151. package/drizzle/0299_organization_membership_lock_order.sql +291 -0
  152. package/drizzle/0300_tenancy_backfill_ledger.sql +459 -0
  153. package/drizzle/0301_session_snapshot_and_pin_visibility.sql +181 -0
  154. package/drizzle/0302_personal_workspace_session_ownership.sql +303 -0
  155. package/drizzle/0303_session_tenancy_product_activation.sql +919 -0
  156. package/drizzle/0304_personal_workspace_private_session_reads.sql +97 -0
  157. package/drizzle/0305_personal_resource_grant_management.sql +584 -0
  158. package/drizzle/0306_atomic_personal_resource_attachments.sql +1374 -0
  159. package/drizzle/0307_session_attention_state.sql +51 -0
  160. package/drizzle/0308_session_archives.sql +35 -0
  161. package/drizzle/0309_channel_project_pins.sql +6 -0
  162. package/drizzle/0310_channel_project_order.sql +27 -0
  163. package/drizzle/0311_company_scope_and_private_session_create.sql +656 -0
  164. package/drizzle/0312_quiescent_session_tree_deletion.sql +39 -0
  165. package/drizzle/0313_private_child_session_authority.sql +365 -0
  166. package/drizzle/0314_unregistered_organization_invitations.sql +606 -0
  167. package/drizzle/0315_personal_github_repository_selection.sql +624 -0
  168. package/drizzle/0316_human_confirmed_activation_resumed_generation.sql +869 -0
  169. package/drizzle/0317_session_goal_continuation_hold.sql +64 -0
  170. package/drizzle/0318_session_turn_startup_milestones.sql +115 -0
  171. package/drizzle/0319_event_triggered_automations.sql +254 -0
  172. package/drizzle/0320_pr_review_pack.sql +113 -0
  173. package/drizzle/0321_slack_bot_environment_display_name.sql +15 -0
  174. package/drizzle/0322_session_turns_unclaimed_prompt_trigger_index.sql +5 -0
  175. package/drizzle/0323_organization_private_session_enablement.sql +489 -0
  176. package/drizzle/0324_human_confirmed_company_profile_agent_admin.sql +856 -0
  177. package/drizzle/0325_child_lifecycle_notices.sql +44 -0
  178. package/drizzle/0326_interaction_operation_error_codes.sql +62 -0
  179. package/drizzle/0327_slack_first_task_hint.sql +29 -0
  180. package/drizzle/0328_session_background_commands.sql +590 -0
  181. package/drizzle/0329_slack_orchestration_delivery_events.sql +91 -0
  182. package/drizzle/0330_api_key_descriptions.sql +20 -0
  183. package/drizzle/0331_managed_organization_creation.sql +163 -0
  184. package/drizzle/0332_organization_shared_workspace_control_plane.sql +539 -0
  185. package/drizzle/0333_session_turn_prompt_routing.sql +45 -0
  186. package/drizzle/0334_connected_machine_workspace_root.sql +99 -0
  187. package/drizzle/0335_slack_workspace_routing.sql +460 -0
  188. package/drizzle/0336_atomic_session_fork_visibility.sql +667 -0
  189. package/drizzle/0337_slack_routed_action_handles.sql +189 -0
  190. package/drizzle/0338_atomic_connected_machine_attachments.sql +410 -0
  191. package/drizzle/0339_document_authority_reclassification.sql +1153 -0
  192. package/drizzle/0340_tenancy_backfill_activation_evidence.sql +1347 -0
  193. package/drizzle/0341_slack_routing_probe_organization_fence.sql +109 -0
  194. package/package.json +10 -6
  195. package/src/attached-browser-devices.ts +233 -5
  196. package/src/automations.ts +911 -0
  197. package/src/browser-sessions.ts +10 -0
  198. package/src/capability-integrations.ts +175 -1
  199. package/src/child-lifecycle-notices.ts +241 -0
  200. package/src/codemode-operations.ts +1 -35
  201. package/src/company-brain-context-selection-schema.ts +100 -0
  202. package/src/company-brain-context-selection.ts +359 -0
  203. package/src/company-brain-governed-writes.ts +1170 -0
  204. package/src/company-brain.ts +309 -0
  205. package/src/company-profile-agent-admin.ts +225 -0
  206. package/src/company-profile-schema.ts +121 -1
  207. package/src/company-profile.ts +21 -0
  208. package/src/computer-sessions.ts +10 -1
  209. package/src/connection-authority.ts +290 -0
  210. package/src/connection-scopes.ts +20 -0
  211. package/src/connection-tenancy-backfill.ts +81 -0
  212. package/src/connection-token-resolver.ts +263 -31
  213. package/src/database.ts +75 -2
  214. package/src/editable-artifact-durable-export.ts +27 -0
  215. package/src/editable-artifacts-schema.ts +10 -5
  216. package/src/editable-artifacts.ts +21 -11
  217. package/src/governed-learning-activation-schema.ts +121 -0
  218. package/src/governed-learning-activation.ts +588 -0
  219. package/src/governed-learning-evaluator-schema.ts +72 -0
  220. package/src/governed-learning-evaluator.ts +217 -0
  221. package/src/index.ts +12001 -1699
  222. package/src/insights.ts +107 -0
  223. package/src/integration-bindings.ts +88 -6
  224. package/src/integration-facets.ts +287 -84
  225. package/src/interaction-placement-loss.ts +234 -0
  226. package/src/knowledge-source-sync-schema.ts +83 -0
  227. package/src/knowledge-source-sync.ts +530 -0
  228. package/src/managed-human-provisioning.ts +116 -0
  229. package/src/memory-domain.ts +27 -32
  230. package/src/migrate.ts +77 -1
  231. package/src/new-session-drafts.ts +229 -35
  232. package/src/organization-membership-backfill.ts +735 -0
  233. package/src/organization-membership-lifecycle.ts +985 -0
  234. package/src/pr-review.ts +890 -0
  235. package/src/preference-registry.ts +31 -1
  236. package/src/provision-roles.ts +931 -24
  237. package/src/runtime-posture-cli.ts +2 -0
  238. package/src/runtime-posture.ts +1315 -52
  239. package/src/schema.ts +3336 -438
  240. package/src/scoped-knowledge-schema.ts +16 -8
  241. package/src/scoped-knowledge.ts +1 -1
  242. package/src/session-background-commands.ts +759 -0
  243. package/src/session-control.ts +640 -74
  244. package/src/session-goal-pacing.ts +126 -0
  245. package/src/session-queue-commands.ts +450 -98
  246. package/src/session-realtime-context.ts +19 -3
  247. package/src/session-realtime-ledger.ts +75 -12
  248. package/src/session-realtime-mirror.ts +1 -1
  249. package/src/session-tenancy.ts +403 -49
  250. package/src/session-tool-call-settlement.ts +38 -29
  251. package/src/slack-routing-personal-workspace.ts +67 -0
  252. package/src/slack-routing.ts +469 -0
  253. package/src/slack-task-policy-schema.ts +5 -0
  254. package/src/slack-task-policy.ts +18 -0
  255. package/src/slack-user-link-access.ts +17 -1
  256. package/src/task-notes-schema.ts +74 -0
  257. package/src/task-notes.ts +124 -2
  258. package/src/turn-initiator.ts +8 -0
  259. package/src/user-resource-authority.ts +377 -0
  260. package/src/workspace-authority.ts +160 -0
  261. package/src/workspace-instruction-policies-schema.ts +53 -1
  262. package/src/workspace-instruction-policies.ts +528 -77
  263. package/src/workspace-learning-policy.ts +21 -5
  264. package/src/workspace-membership-permissions.ts +9 -0
  265. package/dist/chunk-5KTIEDX7.js.map +0 -1
  266. package/dist/chunk-FPVAD4CO.js +0 -117
  267. package/dist/chunk-FPVAD4CO.js.map +0 -1
  268. package/dist/chunk-HSOWSYOX.js.map +0 -1
  269. package/dist/chunk-SK7YYHBI.js.map +0 -1
  270. package/dist/chunk-VKLB2ZGI.js +0 -2389
  271. package/dist/chunk-VKLB2ZGI.js.map +0 -1
  272. package/dist/chunk-Z7VYMNPZ.js.map +0 -1
  273. /package/dist/{chunk-GYO7ED2Q.js.map → chunk-EULAN2NP.js.map} +0 -0
  274. /package/dist/{chunk-VTPRIXKG.js.map → chunk-NJVZPWXY.js.map} +0 -0
@@ -0,0 +1,291 @@
1
+ -- deployment-mode: rolling
2
+ -- Migration 0292: remove the three untruthful `unclassified` counters from the
3
+ -- read-only organization tenancy inventory seam (0285).
4
+ --
5
+ -- WHAT WAS WRONG
6
+ --
7
+ -- 0285 reported an `unclassified` counter for Variable Sets, Rigs, and
8
+ -- Connected Machines defined as `authority_id IS NULL`. That predicate cannot
9
+ -- mean what its name claims. The authority SHAPE constraints REQUIRE a NULL
10
+ -- `authority_id` for every organization- and workspace-scoped row:
11
+ --
12
+ -- workspace_variable_sets_authority_shape_check (0230, widened 0254)
13
+ -- rigs_authority_shape_check (0230, widened 0262)
14
+ -- enrollments_authority_shape_check (0262)
15
+ --
16
+ -- (authority_scope IN ('organization','workspace')
17
+ -- AND authority_id IS NULL AND owner_organization_membership_id IS NULL)
18
+ -- OR (authority_scope = 'user'
19
+ -- AND authority_id IS NOT NULL
20
+ -- AND owner_organization_membership_id IS NOT NULL)
21
+ --
22
+ -- So `authority_id IS NULL` is structurally identical to `total - userScoped`.
23
+ -- Every CORRECTLY classified organization/workspace row was reported as
24
+ -- "unclassified". The number could never drain to zero, so it was useless as
25
+ -- the backfill gate it was documented to be. This is the same defect class the
26
+ -- 0285 review already caught and fixed for `documents`, where the truthful
27
+ -- predicate turned out to be `authority_kind = 'personal' AND
28
+ -- authority_id IS NULL` - a genuine post-migration INVARIANT VIOLATION, because
29
+ -- a correctly classified personal document is REQUIRED to carry an authority.
30
+ -- No such violation exists for these three families: their legacy rows satisfy
31
+ -- the (VALIDATED) shape check exactly like a new workspace-scoped row does.
32
+ --
33
+ -- WHY IT IS NOT REPLACED WITH A CORRECTED PREDICATE
34
+ --
35
+ -- `authority_scope` DEFAULTS to 'workspace' (0230 for variable sets/rigs, 0262
36
+ -- for enrollments), so an unmigrated legacy row and a deliberately
37
+ -- workspace-scoped row are literally identical in that column. Nothing else
38
+ -- distinguishes them either:
39
+ --
40
+ -- * workspace_variable_sets - `origin_workspace_id` (0230, no backfill) is
41
+ -- NULL for every pre-0254 row and non-NULL for every row created by
42
+ -- create_scoped_variable_set. That is a real fact, but it means "predates
43
+ -- the scoped lifecycle", NOT "lacks an explicit authority classification".
44
+ -- Backfill phase D classifies a reviewed legacy row explicitly AS
45
+ -- workspace-owned, which writes nothing - so a reviewed row still reads
46
+ -- NULL and the counter still never drains. Different fact, wrong name.
47
+ --
48
+ -- * rigs - `origin_workspace_id` is not even a legacy marker: createRig
49
+ -- (packages/db/src/index.ts) retains a live non-scoped branch that inserts
50
+ -- through Drizzle without it, so new rows keep arriving with a NULL origin
51
+ -- today.
52
+ --
53
+ -- * enrollments - 0262 added `origin_workspace_id` and backfilled it in the
54
+ -- same statement (`UPDATE enrollments SET origin_workspace_id =
55
+ -- workspace_id WHERE origin_workspace_id IS NULL`), while the ordinary
56
+ -- createEnrollment upsert still leaves it NULL. The polarity is INVERTED:
57
+ -- NULL now marks a POST-0262 ordinary row, not a legacy one.
58
+ --
59
+ -- The population "resources without an explicit authority classification" is
60
+ -- therefore UNREPRESENTABLE in the current schema for all three families. A
61
+ -- number whose name lies is worse than no number, so the key is removed rather
62
+ -- than renamed. Nothing is lost: `byScope` already reports every authority
63
+ -- distinction the schema can truthfully make, and any non-user-scoped total is
64
+ -- derivable from it. Reintroducing an `unclassified` counter requires FIRST
65
+ -- adding a durable classification-decision fact to these tables.
66
+ --
67
+ -- The seam stays read-only, integers-only, exact-organization scoped, and
68
+ -- executable only by the application role. `schemaVersion` moves 1 -> 2
69
+ -- because the reported shape changed. CREATE OR REPLACE preserves the
70
+ -- function's owner and its existing EXECUTE grant to opengeni_app; the 0285
71
+ -- capability table, predicate, and per-table read policies are untouched.
72
+
73
+ SET LOCAL lock_timeout = '5s';
74
+ SET LOCAL statement_timeout = '5min';
75
+
76
+ CREATE OR REPLACE FUNCTION inventory_organization_tenancy(
77
+ p_organization_id uuid
78
+ ) RETURNS jsonb
79
+ LANGUAGE plpgsql
80
+ SECURITY DEFINER
81
+ SET search_path FROM CURRENT
82
+ SET statement_timeout = '30s'
83
+ AS $$
84
+ DECLARE
85
+ result jsonb;
86
+ BEGIN
87
+ IF p_organization_id IS NULL THEN
88
+ RAISE EXCEPTION 'tenancy inventory requires an organization id'
89
+ USING ERRCODE = '22004';
90
+ END IF;
91
+ IF p_organization_id IS DISTINCT FROM nullif(
92
+ pg_catalog.current_setting('opengeni.account_id', true), ''
93
+ )::uuid
94
+ THEN
95
+ RAISE EXCEPTION 'tenancy inventory scope mismatch'
96
+ USING ERRCODE = '42501';
97
+ END IF;
98
+
99
+ INSERT INTO opengeni_private.organization_tenancy_inventory_capabilities (
100
+ backend_pid, transaction_id
101
+ ) VALUES (pg_catalog.pg_backend_pid(), pg_catalog.pg_current_xact_id())
102
+ ON CONFLICT DO NOTHING;
103
+
104
+ SELECT pg_catalog.jsonb_build_object(
105
+ 'schemaVersion', 2,
106
+ 'organizationId', p_organization_id,
107
+ 'workspaces', (
108
+ SELECT count(*)::int FROM workspaces w WHERE w.account_id = p_organization_id
109
+ ),
110
+ 'organizationMemberships', (
111
+ SELECT pg_catalog.jsonb_build_object(
112
+ 'byStatus', coalesce(
113
+ (SELECT pg_catalog.jsonb_object_agg(m.status, m.n)
114
+ FROM (
115
+ SELECT om.status, count(*)::int AS n
116
+ FROM organization_memberships om
117
+ WHERE om.account_id = p_organization_id
118
+ GROUP BY om.status
119
+ ) m),
120
+ '{}'::jsonb
121
+ ),
122
+ 'activeWithoutPersonalWorkspace', (
123
+ SELECT count(*)::int FROM organization_memberships om
124
+ WHERE om.account_id = p_organization_id
125
+ AND om.status = 'active'
126
+ AND om.personal_workspace_id IS NULL
127
+ )
128
+ )
129
+ ),
130
+ -- Humans with persisted workspace access in this organization but no
131
+ -- organization-membership anchor: nothing exists yet for a backfill to
132
+ -- attach canonical ownership to (they never re-authenticated post-0219).
133
+ 'workspaceMemberSubjectsWithoutMembershipAnchor', (
134
+ SELECT count(DISTINCT wm.subject_id)::int
135
+ FROM workspace_memberships wm
136
+ JOIN workspaces w ON w.id = wm.workspace_id
137
+ WHERE w.account_id = p_organization_id
138
+ AND wm.subject_id LIKE 'user:%'
139
+ AND NOT EXISTS (
140
+ SELECT 1 FROM organization_memberships om
141
+ WHERE om.account_id = p_organization_id
142
+ AND om.subject_id = wm.subject_id
143
+ )
144
+ ),
145
+ 'sessions', (
146
+ SELECT pg_catalog.jsonb_build_object(
147
+ 'total', count(*)::int,
148
+ 'ownerless', count(*) FILTER (
149
+ WHERE s.owner_organization_membership_id IS NULL
150
+ )::int,
151
+ 'userPrivate', count(*) FILTER (
152
+ WHERE s.visibility = 'user_private'
153
+ )::int,
154
+ 'advancedAuthorityEpoch', count(*) FILTER (
155
+ WHERE s.authority_epoch > 1
156
+ )::int
157
+ )
158
+ FROM sessions s WHERE s.account_id = p_organization_id
159
+ ),
160
+ -- Variable Sets, Rigs, and Connected Machines report ONLY the authority
161
+ -- lane breakdown. An `unclassified` counter is deliberately absent: the
162
+ -- shape checks REQUIRE a NULL authority_id for organization/workspace
163
+ -- rows, `authority_scope` DEFAULTS to 'workspace', and no column records
164
+ -- that a classification decision was ever made - see this migration's
165
+ -- header for the per-family analysis.
166
+ 'variableSets', (
167
+ SELECT pg_catalog.jsonb_build_object(
168
+ 'byScope', coalesce(
169
+ (SELECT pg_catalog.jsonb_object_agg(v.authority_scope, v.n)
170
+ FROM (
171
+ SELECT vs.authority_scope, count(*)::int AS n
172
+ FROM workspace_variable_sets vs
173
+ WHERE vs.account_id = p_organization_id
174
+ GROUP BY vs.authority_scope
175
+ ) v),
176
+ '{}'::jsonb
177
+ )
178
+ )
179
+ ),
180
+ 'rigs', (
181
+ SELECT pg_catalog.jsonb_build_object(
182
+ 'byScope', coalesce(
183
+ (SELECT pg_catalog.jsonb_object_agg(r.authority_scope, r.n)
184
+ FROM (
185
+ SELECT rr.authority_scope, count(*)::int AS n
186
+ FROM rigs rr WHERE rr.account_id = p_organization_id
187
+ GROUP BY rr.authority_scope
188
+ ) r),
189
+ '{}'::jsonb
190
+ )
191
+ )
192
+ ),
193
+ 'machines', (
194
+ SELECT pg_catalog.jsonb_build_object(
195
+ 'byScope', coalesce(
196
+ (SELECT pg_catalog.jsonb_object_agg(e.authority_scope, e.n)
197
+ FROM (
198
+ SELECT en.authority_scope, count(*)::int AS n
199
+ FROM enrollments en WHERE en.account_id = p_organization_id
200
+ GROUP BY en.authority_scope
201
+ ) e),
202
+ '{}'::jsonb
203
+ )
204
+ )
205
+ ),
206
+ 'connections', (
207
+ SELECT coalesce(
208
+ (SELECT pg_catalog.jsonb_object_agg(c.authority_scope, c.n)
209
+ FROM (
210
+ SELECT cn.authority_scope, count(*)::int AS n
211
+ FROM connections cn WHERE cn.account_id = p_organization_id
212
+ GROUP BY cn.authority_scope
213
+ ) c),
214
+ '{}'::jsonb
215
+ )
216
+ ),
217
+ -- Linked-input gate (documents-internal migration is owned elsewhere and
218
+ -- never written by this program; the count is the cutover gate input).
219
+ 'documents', (
220
+ SELECT pg_catalog.jsonb_build_object(
221
+ 'total', count(*)::int,
222
+ -- The gate population is a LEGACY personal Document with no common
223
+ -- authority - organization/workspace documents are REQUIRED to have
224
+ -- a NULL authority_id forever (documents_authority_chk, 0258) and
225
+ -- must never be counted as unmigrated.
226
+ 'legacyPersonalNullAuthority', count(*) FILTER (
227
+ WHERE d.authority_kind = 'personal' AND d.authority_id IS NULL
228
+ )::int
229
+ )
230
+ FROM documents d WHERE d.account_id = p_organization_id
231
+ ),
232
+ -- Linked-input gate (ownership repair is owned elsewhere; never backfilled
233
+ -- heuristically; the count is the cutover gate input).
234
+ 'codexCredentials', (
235
+ SELECT pg_catalog.jsonb_build_object(
236
+ 'total', count(*)::int,
237
+ 'unattributedConnector', count(*) FILTER (
238
+ WHERE cc.connected_by_subject_id IS NULL
239
+ )::int
240
+ )
241
+ FROM codex_subscription_credentials cc
242
+ WHERE cc.account_id = p_organization_id
243
+ ),
244
+ 'workspaceWriters', (
245
+ SELECT pg_catalog.jsonb_build_object(
246
+ 'admissions', (
247
+ SELECT pg_catalog.jsonb_build_object(
248
+ 'total', count(*)::int,
249
+ 'legacyUnattributed', count(*) FILTER (
250
+ WHERE a.initiator_kind = 'legacy_unattributed'
251
+ )::int
252
+ )
253
+ FROM sandbox_workspace_mutation_admissions a
254
+ WHERE a.account_id = p_organization_id
255
+ ),
256
+ 'retainedProcesses', (
257
+ SELECT pg_catalog.jsonb_build_object(
258
+ 'total', count(*)::int,
259
+ 'legacyUnattributed', count(*) FILTER (
260
+ WHERE p.initiator_kind = 'legacy_unattributed'
261
+ )::int
262
+ )
263
+ FROM sandbox_retained_processes p
264
+ WHERE p.account_id = p_organization_id
265
+ )
266
+ )
267
+ )
268
+ ) INTO result;
269
+
270
+ DELETE FROM opengeni_private.organization_tenancy_inventory_capabilities
271
+ WHERE backend_pid = pg_catalog.pg_backend_pid()
272
+ AND transaction_id = pg_catalog.pg_current_xact_id_if_assigned();
273
+ RETURN result;
274
+ EXCEPTION WHEN OTHERS THEN
275
+ DELETE FROM opengeni_private.organization_tenancy_inventory_capabilities
276
+ WHERE backend_pid = pg_catalog.pg_backend_pid()
277
+ AND transaction_id = pg_catalog.pg_current_xact_id_if_assigned();
278
+ RAISE;
279
+ END
280
+ $$;
281
+
282
+ -- CREATE OR REPLACE preserves the existing ACL, so this re-grant is a no-op on
283
+ -- an already-migrated database. It is retained only so a fresh install that
284
+ -- creates opengeni_app between 0285 and 0292 still converges.
285
+ DO $tenancy_inventory_0287_grant$
286
+ BEGIN
287
+ IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'opengeni_app') THEN
288
+ GRANT EXECUTE ON FUNCTION inventory_organization_tenancy(uuid) TO opengeni_app;
289
+ END IF;
290
+ END
291
+ $tenancy_inventory_0287_grant$;