@opengeni/db 1.0.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 (265) 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-LTG3S6ZY.js → chunk-EULAN2NP.js} +2 -2
  7. package/dist/{chunk-CUPM3FGB.js → chunk-NJVZPWXY.js} +3 -3
  8. package/dist/{chunk-4SR4HKKA.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-6ZYKCRFO.js → chunk-PR2WMLOQ.js} +7580 -4417
  13. package/dist/chunk-PR2WMLOQ.js.map +1 -0
  14. package/dist/{chunk-ZWREYEWA.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.js +2 -2
  36. package/dist/editable-artifacts.d.ts +2 -2
  37. package/dist/editable-artifacts.js +3 -3
  38. package/dist/governed-learning-activation-schema.d.ts +984 -0
  39. package/dist/governed-learning-activation.d.ts +98 -0
  40. package/dist/governed-learning-evaluator-schema.d.ts +633 -0
  41. package/dist/governed-learning-evaluator.d.ts +28 -0
  42. package/dist/index.d.ts +1595 -81
  43. package/dist/index.js +21571 -8126
  44. package/dist/index.js.map +1 -1
  45. package/dist/insights.d.ts +18 -0
  46. package/dist/integration-bindings.d.ts +1 -0
  47. package/dist/integration-facets.d.ts +30 -0
  48. package/dist/interaction-placement-loss.d.ts +22 -0
  49. package/dist/managed-human-provisioning.d.ts +25 -0
  50. package/dist/memory-domain.d.ts +3 -3
  51. package/dist/migrate.d.ts +11 -1
  52. package/dist/migrate.js +1 -1
  53. package/dist/new-session-drafts.d.ts +43 -5
  54. package/dist/organization-membership-backfill.d.ts +174 -0
  55. package/dist/organization-membership-lifecycle.d.ts +162 -0
  56. package/dist/pr-review.d.ts +107 -0
  57. package/dist/preference-registry-schema.d.ts +2 -0
  58. package/dist/preference-registry.d.ts +2 -0
  59. package/dist/provision-roles.js +1 -1
  60. package/dist/runtime-posture.d.ts +22 -8
  61. package/dist/schema.d.ts +24914 -13703
  62. package/dist/schema.js +99 -1
  63. package/dist/scoped-knowledge-schema.d.ts +52 -1
  64. package/dist/session-background-commands.d.ts +125 -0
  65. package/dist/session-background-commands.js +34 -0
  66. package/dist/session-background-commands.js.map +1 -0
  67. package/dist/session-control.d.ts +156 -3
  68. package/dist/session-goal-pacing.d.ts +68 -0
  69. package/dist/session-queue-commands.d.ts +41 -4
  70. package/dist/session-realtime-ledger.d.ts +2 -0
  71. package/dist/session-tenancy.d.ts +71 -1
  72. package/dist/session-tenancy.js +25 -3
  73. package/dist/session-tool-call-settlement.d.ts +9 -4
  74. package/dist/slack-routing-personal-workspace.d.ts +23 -0
  75. package/dist/slack-routing.d.ts +153 -0
  76. package/dist/slack-task-policy-schema.d.ts +34 -0
  77. package/dist/slack-task-policy.d.ts +8 -0
  78. package/dist/slack-user-link-access.d.ts +1 -0
  79. package/dist/task-notes-schema.d.ts +594 -0
  80. package/dist/task-notes.d.ts +39 -0
  81. package/dist/turn-initiator.d.ts +2 -0
  82. package/dist/user-resource-authority.d.ts +109 -0
  83. package/dist/video-generation.js +3 -3
  84. package/dist/workspace-authority.d.ts +81 -0
  85. package/dist/workspace-instruction-policies-schema.d.ts +279 -0
  86. package/dist/workspace-instruction-policies.d.ts +62 -0
  87. package/dist/workspace-learning-policy.d.ts +1 -0
  88. package/dist/workspace-membership-permissions.d.ts +2 -0
  89. package/drizzle/0240_enrollment_connection_authority.sql +39 -0
  90. package/drizzle/0241_atomic_personal_resource_delegation.sql +1000 -0
  91. package/drizzle/0241_enrollment_agent_runtime.sql +70 -0
  92. package/drizzle/0244_slack_app_home_refresh_queue.sql +120 -0
  93. package/drizzle/0245_model_context_contribution_facts.sql +88 -0
  94. package/drizzle/0246_integration_personal_instance_authority.sql +70 -0
  95. package/drizzle/0247_terraform_stacks_provenance_repair.sql +428 -0
  96. package/drizzle/0248_terraform_stacks_component_resolution_fence.sql +83 -0
  97. package/drizzle/0249_personal_resource_delegation_authority_correction.sql +662 -0
  98. package/drizzle/0250_direct_retained_process_owner_liveness.sql +199 -0
  99. package/drizzle/0251_connected_machine_operation_policy.sql +45 -0
  100. package/drizzle/0252_scheduled_personal_resource_delegation.sql +1867 -0
  101. package/drizzle/0253_common_user_resource_authority_lifecycle.sql +1555 -0
  102. package/drizzle/0254_scoped_variable_set_authority.sql +1255 -0
  103. package/drizzle/0255_company_brain_governed_write_proposals.sql +524 -0
  104. package/drizzle/0256_connection_authority_delegation.sql +703 -0
  105. package/drizzle/0257_goal_revision_decisions_and_root_constraints.sql +436 -0
  106. package/drizzle/0258_three_scope_document_knowledge_authority.sql +1273 -0
  107. package/drizzle/0259_company_brain_context_selection_receipts.sql +869 -0
  108. package/drizzle/0260_task_note_knowledge_promotion.sql +820 -0
  109. package/drizzle/0261_preference_knowledge_proposal_actor_binding.sql +471 -0
  110. package/drizzle/0262_scoped_connected_machines_and_rigs.sql +1700 -0
  111. package/drizzle/0263_organization_membership_lifecycle.sql +3284 -0
  112. package/drizzle/0264_connection_authority_runtime_activation.sql +1084 -0
  113. package/drizzle/0266_company_brain_context_receipt_inspection.sql +156 -0
  114. package/drizzle/0268_governed_learning_decision_receipts.sql +825 -0
  115. package/drizzle/0269_governed_learning_activation_controller.sql +1874 -0
  116. package/drizzle/0270_governed_learning_history_inspection.sql +170 -0
  117. package/drizzle/0271_company_brain_retrieval_only_default.sql +524 -0
  118. package/drizzle/0272_human_confirmed_learning_activation.sql +591 -0
  119. package/drizzle/0273_scheduled_variable_set_materialization.sql +268 -0
  120. package/drizzle/0274_human_confirmed_knowledge_review.sql +310 -0
  121. package/drizzle/0275_scheduled_connection_authority.sql +5527 -0
  122. package/drizzle/0276_onboarding_proposal_initiating_human_guc.sql +198 -0
  123. package/drizzle/0277_workspace_writer_authority_attribution.sql +393 -0
  124. package/drizzle/0278_workspace_membership_removal_fencing.sql +1174 -0
  125. package/drizzle/0279_workspace_connection_use_lane.sql +343 -0
  126. package/drizzle/0280_connection_and_variable_set_audit_attribution.sql +773 -0
  127. package/drizzle/0281_viewer_holder_authority_claims.sql +52 -0
  128. package/drizzle/0282_variable_set_session_attach_attribution.sql +178 -0
  129. package/drizzle/0283_editable_spreadsheet_authored_state.sql +82 -0
  130. package/drizzle/0284_truthful_human_confirmed_review_reason.sql +930 -0
  131. package/drizzle/0285_organization_tenancy_inventory.sql +329 -0
  132. package/drizzle/0286_widen_task_note_expiry_ceiling.sql +391 -0
  133. package/drizzle/0287_open_suffix_pending_tool_calls.sql +18 -0
  134. package/drizzle/0288_attached_browser_reenrollment.sql +64 -0
  135. package/drizzle/0289_session_composer_policy_authority.sql +597 -0
  136. package/drizzle/0290_organization_membership_backfill.sql +268 -0
  137. package/drizzle/0291_resource_authority_classification_assertion.sql +371 -0
  138. package/drizzle/0292_truthful_tenancy_inventory_counters.sql +291 -0
  139. package/drizzle/0293_confirm_time_rule_rebaseline.sql +615 -0
  140. package/drizzle/0294_preference_activation_authority.sql +237 -0
  141. package/drizzle/0295_retire_legacy_standing_memory_mode.sql +476 -0
  142. package/drizzle/0296_force_rls_backfill_noop_repair.sql +215 -0
  143. package/drizzle/0297_session_ownership_classification_and_backfill.sql +752 -0
  144. package/drizzle/0298_organization_tenancy_parity.sql +609 -0
  145. package/drizzle/0299_organization_membership_lock_order.sql +291 -0
  146. package/drizzle/0300_tenancy_backfill_ledger.sql +459 -0
  147. package/drizzle/0301_session_snapshot_and_pin_visibility.sql +181 -0
  148. package/drizzle/0302_personal_workspace_session_ownership.sql +303 -0
  149. package/drizzle/0303_session_tenancy_product_activation.sql +919 -0
  150. package/drizzle/0304_personal_workspace_private_session_reads.sql +97 -0
  151. package/drizzle/0305_personal_resource_grant_management.sql +584 -0
  152. package/drizzle/0306_atomic_personal_resource_attachments.sql +1374 -0
  153. package/drizzle/0307_session_attention_state.sql +51 -0
  154. package/drizzle/0308_session_archives.sql +35 -0
  155. package/drizzle/0309_channel_project_pins.sql +6 -0
  156. package/drizzle/0310_channel_project_order.sql +27 -0
  157. package/drizzle/0311_company_scope_and_private_session_create.sql +656 -0
  158. package/drizzle/0312_quiescent_session_tree_deletion.sql +39 -0
  159. package/drizzle/0313_private_child_session_authority.sql +365 -0
  160. package/drizzle/0314_unregistered_organization_invitations.sql +606 -0
  161. package/drizzle/0315_personal_github_repository_selection.sql +624 -0
  162. package/drizzle/0316_human_confirmed_activation_resumed_generation.sql +869 -0
  163. package/drizzle/0317_session_goal_continuation_hold.sql +64 -0
  164. package/drizzle/0318_session_turn_startup_milestones.sql +115 -0
  165. package/drizzle/0319_event_triggered_automations.sql +254 -0
  166. package/drizzle/0320_pr_review_pack.sql +113 -0
  167. package/drizzle/0321_slack_bot_environment_display_name.sql +15 -0
  168. package/drizzle/0322_session_turns_unclaimed_prompt_trigger_index.sql +5 -0
  169. package/drizzle/0323_organization_private_session_enablement.sql +489 -0
  170. package/drizzle/0324_human_confirmed_company_profile_agent_admin.sql +856 -0
  171. package/drizzle/0325_child_lifecycle_notices.sql +44 -0
  172. package/drizzle/0326_interaction_operation_error_codes.sql +62 -0
  173. package/drizzle/0327_slack_first_task_hint.sql +29 -0
  174. package/drizzle/0328_session_background_commands.sql +590 -0
  175. package/drizzle/0329_slack_orchestration_delivery_events.sql +91 -0
  176. package/drizzle/0330_api_key_descriptions.sql +20 -0
  177. package/drizzle/0331_managed_organization_creation.sql +163 -0
  178. package/drizzle/0332_organization_shared_workspace_control_plane.sql +539 -0
  179. package/drizzle/0333_session_turn_prompt_routing.sql +45 -0
  180. package/drizzle/0334_connected_machine_workspace_root.sql +99 -0
  181. package/drizzle/0335_slack_workspace_routing.sql +460 -0
  182. package/drizzle/0336_atomic_session_fork_visibility.sql +667 -0
  183. package/drizzle/0337_slack_routed_action_handles.sql +189 -0
  184. package/drizzle/0338_atomic_connected_machine_attachments.sql +410 -0
  185. package/drizzle/0339_document_authority_reclassification.sql +1153 -0
  186. package/drizzle/0340_tenancy_backfill_activation_evidence.sql +1347 -0
  187. package/drizzle/0341_slack_routing_probe_organization_fence.sql +109 -0
  188. package/package.json +10 -6
  189. package/src/attached-browser-devices.ts +233 -5
  190. package/src/automations.ts +911 -0
  191. package/src/browser-sessions.ts +10 -0
  192. package/src/capability-integrations.ts +175 -1
  193. package/src/child-lifecycle-notices.ts +241 -0
  194. package/src/codemode-operations.ts +1 -35
  195. package/src/company-brain-context-selection-schema.ts +100 -0
  196. package/src/company-brain-context-selection.ts +359 -0
  197. package/src/company-brain-governed-writes.ts +1170 -0
  198. package/src/company-brain.ts +309 -0
  199. package/src/company-profile-agent-admin.ts +225 -0
  200. package/src/company-profile-schema.ts +121 -1
  201. package/src/company-profile.ts +21 -0
  202. package/src/computer-sessions.ts +10 -1
  203. package/src/connection-authority.ts +290 -0
  204. package/src/connection-scopes.ts +20 -0
  205. package/src/connection-tenancy-backfill.ts +81 -0
  206. package/src/connection-token-resolver.ts +263 -31
  207. package/src/database.ts +75 -2
  208. package/src/editable-artifacts-schema.ts +10 -5
  209. package/src/editable-artifacts.ts +21 -11
  210. package/src/governed-learning-activation-schema.ts +121 -0
  211. package/src/governed-learning-activation.ts +588 -0
  212. package/src/governed-learning-evaluator-schema.ts +72 -0
  213. package/src/governed-learning-evaluator.ts +217 -0
  214. package/src/index.ts +11651 -1548
  215. package/src/insights.ts +107 -0
  216. package/src/integration-bindings.ts +88 -6
  217. package/src/integration-facets.ts +287 -84
  218. package/src/interaction-placement-loss.ts +234 -0
  219. package/src/managed-human-provisioning.ts +116 -0
  220. package/src/memory-domain.ts +27 -32
  221. package/src/migrate.ts +77 -1
  222. package/src/new-session-drafts.ts +229 -35
  223. package/src/organization-membership-backfill.ts +735 -0
  224. package/src/organization-membership-lifecycle.ts +985 -0
  225. package/src/pr-review.ts +890 -0
  226. package/src/preference-registry.ts +31 -1
  227. package/src/provision-roles.ts +877 -6
  228. package/src/runtime-posture-cli.ts +2 -0
  229. package/src/runtime-posture.ts +1291 -76
  230. package/src/schema.ts +3303 -428
  231. package/src/scoped-knowledge-schema.ts +16 -8
  232. package/src/scoped-knowledge.ts +1 -1
  233. package/src/session-background-commands.ts +759 -0
  234. package/src/session-control.ts +640 -74
  235. package/src/session-goal-pacing.ts +126 -0
  236. package/src/session-queue-commands.ts +437 -84
  237. package/src/session-realtime-context.ts +3 -3
  238. package/src/session-realtime-ledger.ts +22 -11
  239. package/src/session-realtime-mirror.ts +1 -1
  240. package/src/session-tenancy.ts +403 -49
  241. package/src/session-tool-call-settlement.ts +38 -29
  242. package/src/slack-routing-personal-workspace.ts +67 -0
  243. package/src/slack-routing.ts +469 -0
  244. package/src/slack-task-policy-schema.ts +5 -0
  245. package/src/slack-task-policy.ts +18 -0
  246. package/src/slack-user-link-access.ts +17 -1
  247. package/src/task-notes-schema.ts +74 -0
  248. package/src/task-notes.ts +124 -2
  249. package/src/turn-initiator.ts +8 -0
  250. package/src/user-resource-authority.ts +377 -0
  251. package/src/workspace-authority.ts +160 -0
  252. package/src/workspace-instruction-policies-schema.ts +53 -1
  253. package/src/workspace-instruction-policies.ts +528 -77
  254. package/src/workspace-learning-policy.ts +21 -5
  255. package/src/workspace-membership-permissions.ts +9 -0
  256. package/dist/chunk-4SR4HKKA.js.map +0 -1
  257. package/dist/chunk-6ZYKCRFO.js.map +0 -1
  258. package/dist/chunk-NXAFLCQX.js +0 -2468
  259. package/dist/chunk-NXAFLCQX.js.map +0 -1
  260. package/dist/chunk-SK7YYHBI.js.map +0 -1
  261. package/dist/chunk-WD5ROSLL.js +0 -117
  262. package/dist/chunk-WD5ROSLL.js.map +0 -1
  263. package/dist/chunk-ZWREYEWA.js.map +0 -1
  264. /package/dist/{chunk-LTG3S6ZY.js.map → chunk-EULAN2NP.js.map} +0 -0
  265. /package/dist/{chunk-CUPM3FGB.js.map → chunk-NJVZPWXY.js.map} +0 -0
@@ -0,0 +1,524 @@
1
+ -- deployment-mode: rolling
2
+
3
+ -- Allocated after the scoped Variable Set authority migration to preserve the
4
+ -- repository's forward-only migration filename ledger.
5
+
6
+ -- The historical proposal table keeps its onboarding name, but the canonical
7
+ -- instruction revision already supports knowledge_proposal provenance. Broaden
8
+ -- only its validator so an exact immutable scoped-Knowledge change proposal can
9
+ -- materialize one inactive draft without gaining activation authority.
10
+ CREATE OR REPLACE FUNCTION workspace_instruction_policy_validate_onboarding_proposal()
11
+ RETURNS trigger
12
+ LANGUAGE plpgsql
13
+ AS $$
14
+ BEGIN
15
+ IF NEW."baseline_revision_id" IS NULL THEN
16
+ IF EXISTS (
17
+ SELECT 1
18
+ FROM "workspace_instruction_policy_heads" head
19
+ WHERE head."account_id" = NEW."account_id"
20
+ AND head."workspace_id" = NEW."workspace_id"
21
+ AND head."kind" = NEW."kind"
22
+ AND head."scope" = NEW."scope"
23
+ AND head."role_key" IS NOT DISTINCT FROM NEW."role_key"
24
+ ) THEN
25
+ RAISE EXCEPTION 'instruction-policy proposal must capture the exact active head baseline'
26
+ USING ERRCODE = '23514';
27
+ END IF;
28
+ ELSIF NOT EXISTS (
29
+ SELECT 1
30
+ FROM "workspace_instruction_policy_heads" head
31
+ WHERE head."account_id" = NEW."account_id"
32
+ AND head."workspace_id" = NEW."workspace_id"
33
+ AND head."kind" = NEW."kind"
34
+ AND head."scope" = NEW."scope"
35
+ AND head."role_key" IS NOT DISTINCT FROM NEW."role_key"
36
+ AND head."revision_id" = NEW."baseline_revision_id"
37
+ AND head."revision" = NEW."baseline_revision"
38
+ AND head."content_hash" = NEW."baseline_content_hash"
39
+ AND head."activation_version" = NEW."baseline_activation_version"
40
+ AND head."activated_at" = NEW."baseline_activated_at"
41
+ ) THEN
42
+ RAISE EXCEPTION 'instruction-policy proposal must capture the exact active head baseline'
43
+ USING ERRCODE = '23514';
44
+ END IF;
45
+
46
+ IF NEW."baseline_revision_id" IS NOT NULL AND NOT EXISTS (
47
+ SELECT 1
48
+ FROM "workspace_instruction_policy_revisions" revision
49
+ WHERE revision."id" = NEW."baseline_revision_id"
50
+ AND revision."account_id" = NEW."account_id"
51
+ AND revision."workspace_id" = NEW."workspace_id"
52
+ AND revision."kind" = NEW."kind"
53
+ AND revision."scope" = NEW."scope"
54
+ AND revision."role_key" IS NOT DISTINCT FROM NEW."role_key"
55
+ AND revision."revision" = NEW."baseline_revision"
56
+ AND revision."content_hash" = NEW."baseline_content_hash"
57
+ ) THEN
58
+ RAISE EXCEPTION 'instruction-policy proposal has an invalid baseline revision'
59
+ USING ERRCODE = '23514';
60
+ END IF;
61
+
62
+ IF NOT EXISTS (
63
+ SELECT 1
64
+ FROM "workspace_instruction_policy_revisions" revision
65
+ WHERE revision."id" = NEW."draft_revision_id"
66
+ AND revision."operation_id" = NEW."operation_id"
67
+ AND revision."request_fingerprint" = NEW."request_fingerprint"
68
+ AND revision."account_id" = NEW."account_id"
69
+ AND revision."workspace_id" = NEW."workspace_id"
70
+ AND revision."kind" = NEW."kind"
71
+ AND revision."scope" = NEW."scope"
72
+ AND revision."role_key" IS NOT DISTINCT FROM NEW."role_key"
73
+ AND revision."revision" = NEW."draft_revision"
74
+ AND revision."content_hash" = NEW."draft_content_hash"
75
+ AND revision."supersedes_revision_id" IS NOT DISTINCT FROM NEW."baseline_revision_id"
76
+ AND revision."created_by_subject_id" = NEW."created_by_subject_id"
77
+ AND (
78
+ (
79
+ revision."provenance_source" = 'onboarding'
80
+ AND revision."provenance_source_id" = NEW."id"::text
81
+ ) OR (
82
+ revision."provenance_source" = 'knowledge_proposal'
83
+ AND revision."provenance_source_id" = NEW."source_id"
84
+ AND NEW."source_version" = NEW."draft_content_hash"
85
+ AND EXISTS (
86
+ SELECT 1
87
+ FROM "knowledge_change_proposals" proposal
88
+ WHERE proposal."id"::text = NEW."source_id"
89
+ AND proposal."account_id" = NEW."account_id"
90
+ AND proposal."scope_kind" = 'workspace'
91
+ AND proposal."scope_workspace_id" = NEW."workspace_id"
92
+ AND proposal."scope_subject_id" IS NULL
93
+ AND proposal."target_kind" = 'instruction_policy'
94
+ AND proposal."target_scope" = 'workspace'
95
+ AND proposal."target_key" = concat(
96
+ NEW."kind", ':', NEW."scope", ':', coalesce(NEW."role_key", 'global')
97
+ )
98
+ AND proposal."content_hash" = NEW."source_version"
99
+ AND proposal."actor_kind" = 'service'
100
+ AND proposal."actor_subject_id" = NEW."created_by_subject_id"
101
+ AND proposal."initiating_human_subject_id"
102
+ = current_setting('opengeni.subject_id', true)
103
+ AND proposal."claim_id" IS NOT NULL
104
+ AND proposal."evidence_id" IS NOT NULL
105
+ AND proposal."status" = 'proposed'
106
+ )
107
+ )
108
+ )
109
+ ) THEN
110
+ RAISE EXCEPTION 'instruction-policy proposal must identify its exact inactive draft'
111
+ USING ERRCODE = '23514';
112
+ END IF;
113
+
114
+ IF EXISTS (
115
+ SELECT 1
116
+ FROM "workspace_instruction_policy_heads" head
117
+ WHERE head."account_id" = NEW."account_id"
118
+ AND head."workspace_id" = NEW."workspace_id"
119
+ AND head."revision_id" = NEW."draft_revision_id"
120
+ ) OR EXISTS (
121
+ SELECT 1
122
+ FROM "workspace_instruction_policy_activation_events" event
123
+ WHERE event."account_id" = NEW."account_id"
124
+ AND event."workspace_id" = NEW."workspace_id"
125
+ AND event."new_revision_id" = NEW."draft_revision_id"
126
+ ) THEN
127
+ RAISE EXCEPTION 'instruction-policy proposal must identify a never-activated inactive draft'
128
+ USING ERRCODE = '23514';
129
+ END IF;
130
+ RETURN NEW;
131
+ END;
132
+ $$;
133
+
134
+ CREATE TABLE "company_brain_preference_proposal_receipts" (
135
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
136
+ "account_id" uuid NOT NULL REFERENCES "managed_accounts"("id") ON DELETE RESTRICT,
137
+ "workspace_id" uuid NOT NULL REFERENCES "workspaces"("id") ON DELETE RESTRICT,
138
+ "operation_id" uuid NOT NULL,
139
+ "input_hash" text NOT NULL,
140
+ "knowledge_proposal_id" uuid NOT NULL REFERENCES "knowledge_change_proposals"("id")
141
+ ON DELETE RESTRICT,
142
+ "preference_id" uuid NOT NULL REFERENCES "preference_registry_preferences"("id")
143
+ ON DELETE RESTRICT,
144
+ "revision_id" uuid NOT NULL REFERENCES "preference_registry_revisions"("id")
145
+ ON DELETE RESTRICT,
146
+ "creation_event_id" uuid NOT NULL REFERENCES "preference_registry_events"("id")
147
+ ON DELETE RESTRICT,
148
+ "session_id" uuid NOT NULL REFERENCES "sessions"("id") ON DELETE RESTRICT,
149
+ "turn_id" uuid NOT NULL REFERENCES "session_turns"("id") ON DELETE RESTRICT,
150
+ "attempt_id" uuid NOT NULL REFERENCES "session_turn_attempts"("id") ON DELETE RESTRICT,
151
+ "execution_generation" integer NOT NULL,
152
+ "actor_subject_id" text NOT NULL,
153
+ "initiating_human_subject_id" text NOT NULL,
154
+ "created_at" timestamptz NOT NULL DEFAULT now(),
155
+ CONSTRAINT "company_brain_preference_proposal_receipts_operation_uq"
156
+ UNIQUE ("workspace_id", "operation_id"),
157
+ CONSTRAINT "company_brain_preference_proposal_receipts_knowledge_uq"
158
+ UNIQUE ("account_id", "knowledge_proposal_id"),
159
+ CONSTRAINT "company_brain_preference_proposal_receipts_hash_chk"
160
+ CHECK ("input_hash" ~ '^[0-9a-f]{64}$'),
161
+ CONSTRAINT "company_brain_preference_proposal_receipts_attempt_chk" CHECK (
162
+ "execution_generation" > 0
163
+ AND length(btrim("actor_subject_id")) BETWEEN 1 AND 1024
164
+ AND length(btrim("initiating_human_subject_id")) BETWEEN 1 AND 1024
165
+ )
166
+ );
167
+
168
+ ALTER TABLE "company_brain_preference_proposal_receipts" ENABLE ROW LEVEL SECURITY;
169
+ ALTER TABLE "company_brain_preference_proposal_receipts" FORCE ROW LEVEL SECURITY;
170
+ CREATE POLICY workspace_isolation ON "company_brain_preference_proposal_receipts"
171
+ USING (opengeni_private.workspace_rls_visible("account_id", "workspace_id"))
172
+ WITH CHECK (opengeni_private.workspace_rls_visible("account_id", "workspace_id"));
173
+ CREATE POLICY session_visibility_isolation ON "company_brain_preference_proposal_receipts"
174
+ AS RESTRICTIVE
175
+ FOR ALL
176
+ USING (session_reference_visible("account_id", "workspace_id", "session_id"))
177
+ WITH CHECK (session_reference_visible("account_id", "workspace_id", "session_id"));
178
+ CREATE TRIGGER company_brain_preference_proposal_receipts_immutable
179
+ BEFORE UPDATE OR DELETE ON "company_brain_preference_proposal_receipts"
180
+ FOR EACH ROW EXECUTE FUNCTION preference_registry_reject_history_mutation();
181
+
182
+ -- Preference proposals created by an agent attempt are not human governance
183
+ -- mutations. This function records a service actor, revalidates the exact live
184
+ -- attempt and causal human, and creates only an inactive workspace proposal.
185
+ CREATE OR REPLACE FUNCTION preference_registry_create_knowledge_proposal_for_attempt(
186
+ p_account_id uuid,
187
+ p_workspace_id uuid,
188
+ p_session_id uuid,
189
+ p_turn_id uuid,
190
+ p_attempt_id uuid,
191
+ p_execution_generation integer,
192
+ p_operation_id uuid,
193
+ p_input_hash text,
194
+ p_knowledge_proposal_id uuid,
195
+ p_stable_key text,
196
+ p_title text,
197
+ p_description text,
198
+ p_content text,
199
+ p_precedence_rank integer,
200
+ p_conflict_strategy text,
201
+ p_conflicts_with jsonb,
202
+ p_expires_at timestamptz,
203
+ p_reason text
204
+ ) RETURNS TABLE (preference_id uuid, revision_id uuid)
205
+ LANGUAGE plpgsql
206
+ SECURITY DEFINER
207
+ SET search_path FROM CURRENT
208
+ AS $$
209
+ DECLARE
210
+ turn_row record;
211
+ proposal_row knowledge_change_proposals%ROWTYPE;
212
+ preference_row preference_registry_preferences%ROWTYPE;
213
+ revision_row preference_registry_revisions%ROWTYPE;
214
+ event_row preference_registry_events%ROWTYPE;
215
+ receipt_row company_brain_preference_proposal_receipts%ROWTYPE;
216
+ actor_subject_id text;
217
+ expected_proposal_content jsonb;
218
+ normalized_conflicts jsonb;
219
+ BEGIN
220
+ IF NULLIF(current_setting('opengeni.account_id', true), '')::uuid
221
+ IS DISTINCT FROM p_account_id
222
+ OR NULLIF(current_setting('opengeni.workspace_id', true), '')::uuid
223
+ IS DISTINCT FROM p_workspace_id
224
+ THEN
225
+ RAISE EXCEPTION 'preference Knowledge proposal requires exact tenant context'
226
+ USING ERRCODE = '42501';
227
+ END IF;
228
+ IF p_execution_generation IS NULL OR p_execution_generation <= 0
229
+ OR p_operation_id IS NULL
230
+ OR p_input_hash !~ '^[0-9a-f]{64}$'
231
+ OR p_stable_key IS NULL
232
+ OR length(p_stable_key) NOT BETWEEN 1 AND 96
233
+ OR p_stable_key <> lower(btrim(p_stable_key))
234
+ OR p_stable_key !~ '^[a-z0-9](?:[a-z0-9._-]*[a-z0-9])?$'
235
+ OR p_stable_key ~ '--'
236
+ OR p_title IS NULL OR length(btrim(p_title)) NOT BETWEEN 1 AND 120
237
+ OR p_description IS NULL OR length(btrim(p_description)) NOT BETWEEN 1 AND 240
238
+ OR p_content IS NULL OR length(btrim(p_content)) = 0 OR length(p_content) > 262144
239
+ OR p_precedence_rank IS NULL OR p_precedence_rank NOT BETWEEN -1000 AND 1000
240
+ OR p_conflict_strategy IS NULL
241
+ OR p_conflict_strategy NOT IN ('override', 'merge', 'reject', 'inform')
242
+ OR p_conflicts_with IS NULL
243
+ OR jsonb_typeof(p_conflicts_with) IS DISTINCT FROM 'array'
244
+ OR jsonb_array_length(p_conflicts_with) > 32
245
+ OR p_reason IS NULL OR length(btrim(p_reason)) NOT BETWEEN 1 AND 4096
246
+ THEN
247
+ RAISE EXCEPTION 'preference Knowledge proposal input is invalid'
248
+ USING ERRCODE = '22023';
249
+ END IF;
250
+ IF EXISTS (
251
+ SELECT 1
252
+ FROM jsonb_array_elements(p_conflicts_with) item
253
+ WHERE jsonb_typeof(item) <> 'string'
254
+ ) OR EXISTS (
255
+ SELECT 1
256
+ FROM jsonb_array_elements_text(p_conflicts_with) item(value)
257
+ WHERE length(value) NOT BETWEEN 1 AND 96
258
+ OR value <> lower(btrim(value))
259
+ OR value !~ '^[a-z0-9](?:[a-z0-9._-]*[a-z0-9])?$'
260
+ OR value ~ '--'
261
+ ) THEN
262
+ RAISE EXCEPTION 'preference Knowledge proposal conflicts are invalid'
263
+ USING ERRCODE = '22023';
264
+ END IF;
265
+ SELECT COALESCE(jsonb_agg(value ORDER BY value), '[]'::jsonb)
266
+ INTO normalized_conflicts
267
+ FROM (
268
+ SELECT DISTINCT value
269
+ FROM jsonb_array_elements_text(p_conflicts_with) item(value)
270
+ ) normalized;
271
+ IF normalized_conflicts IS DISTINCT FROM p_conflicts_with THEN
272
+ RAISE EXCEPTION 'preference Knowledge proposal conflicts must be unique and sorted'
273
+ USING ERRCODE = '22023';
274
+ END IF;
275
+
276
+ PERFORM 1
277
+ FROM workspaces workspace
278
+ WHERE workspace.id = p_workspace_id AND workspace.account_id = p_account_id
279
+ FOR KEY SHARE;
280
+ IF NOT FOUND THEN
281
+ RAISE EXCEPTION 'preference Knowledge proposal workspace is unavailable'
282
+ USING ERRCODE = '42501';
283
+ END IF;
284
+ PERFORM 1
285
+ FROM sessions session
286
+ WHERE session.id = p_session_id
287
+ AND session.account_id = p_account_id
288
+ AND session.workspace_id = p_workspace_id
289
+ AND session.active_turn_id = p_turn_id
290
+ FOR SHARE;
291
+ IF NOT FOUND THEN
292
+ RAISE EXCEPTION 'preference Knowledge proposal session is unavailable'
293
+ USING ERRCODE = '42501';
294
+ END IF;
295
+ SELECT turn.id, turn.account_id, turn.workspace_id, turn.session_id,
296
+ turn.active_attempt_id, turn.execution_generation,
297
+ coalesce(
298
+ turn.initiating_human_subject_id,
299
+ case when turn.initiator_kind = 'subject' then turn.initiator_subject_id end
300
+ ) AS initiating_human_subject_id
301
+ INTO turn_row
302
+ FROM session_turns turn
303
+ WHERE turn.id = p_turn_id
304
+ AND turn.account_id = p_account_id
305
+ AND turn.workspace_id = p_workspace_id
306
+ AND turn.session_id = p_session_id
307
+ AND turn.active_attempt_id = p_attempt_id
308
+ AND turn.execution_generation = p_execution_generation
309
+ AND turn.status IN ('running', 'requires_action', 'recovering', 'waiting_capacity')
310
+ FOR SHARE;
311
+ IF NOT FOUND
312
+ OR length(btrim(coalesce(turn_row.initiating_human_subject_id, ''))) NOT BETWEEN 1 AND 1024
313
+ OR current_setting('opengeni.subject_id', true)
314
+ IS DISTINCT FROM turn_row.initiating_human_subject_id
315
+ THEN
316
+ RAISE EXCEPTION 'preference Knowledge proposal requires the immutable initiating human'
317
+ USING ERRCODE = '42501';
318
+ END IF;
319
+ PERFORM 1
320
+ FROM session_turn_attempts attempt
321
+ WHERE attempt.id = p_attempt_id
322
+ AND attempt.account_id = p_account_id
323
+ AND attempt.workspace_id = p_workspace_id
324
+ AND attempt.session_id = p_session_id
325
+ AND attempt.turn_id = p_turn_id
326
+ AND attempt.execution_generation = p_execution_generation
327
+ AND attempt.state IN ('claimed', 'running')
328
+ AND NOT EXISTS (
329
+ SELECT 1
330
+ FROM session_attempt_interruptions interruption
331
+ WHERE interruption.workspace_id = attempt.workspace_id
332
+ AND interruption.attempt_id = attempt.id
333
+ AND interruption.state IN ('pending', 'delivered', 'acknowledged')
334
+ )
335
+ FOR SHARE;
336
+ IF NOT FOUND THEN
337
+ RAISE EXCEPTION 'preference Knowledge proposal requires the exact current attempt'
338
+ USING ERRCODE = '42501';
339
+ END IF;
340
+
341
+ actor_subject_id := 'service:company-brain-governed-write:' || p_input_hash;
342
+ expected_proposal_content := jsonb_build_object(
343
+ 'stableKey', p_stable_key,
344
+ 'title', p_title,
345
+ 'description', p_description,
346
+ 'content', p_content,
347
+ 'precedenceRank', p_precedence_rank,
348
+ 'conflictStrategy', p_conflict_strategy,
349
+ 'conflictsWith', p_conflicts_with,
350
+ 'expiresAt', CASE WHEN p_expires_at IS NULL THEN NULL ELSE
351
+ to_char(p_expires_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') END
352
+ );
353
+ SELECT * INTO proposal_row
354
+ FROM knowledge_change_proposals proposal
355
+ WHERE proposal.id = p_knowledge_proposal_id
356
+ AND proposal.account_id = p_account_id
357
+ AND proposal.scope_kind = 'workspace'
358
+ AND proposal.scope_workspace_id = p_workspace_id
359
+ AND proposal.scope_subject_id IS NULL
360
+ AND proposal.target_kind = 'preference'
361
+ AND proposal.target_scope = 'workspace'
362
+ AND proposal.target_key = p_stable_key
363
+ AND proposal.status = 'proposed'
364
+ AND proposal.actor_kind = 'service'
365
+ AND proposal.actor_subject_id = actor_subject_id
366
+ AND proposal.initiating_human_subject_id = turn_row.initiating_human_subject_id
367
+ FOR SHARE;
368
+ IF NOT FOUND OR proposal_row.content::jsonb IS DISTINCT FROM expected_proposal_content
369
+ OR NOT EXISTS (
370
+ SELECT 1
371
+ FROM knowledge_claims claim
372
+ JOIN knowledge_claim_evidence evidence
373
+ ON evidence.account_id = claim.account_id
374
+ AND evidence.claim_id = claim.id
375
+ WHERE claim.id = proposal_row.claim_id
376
+ AND claim.account_id = p_account_id
377
+ AND claim.scope_kind = 'workspace'
378
+ AND claim.scope_workspace_id = p_workspace_id
379
+ AND claim.scope_subject_id IS NULL
380
+ AND evidence.id = proposal_row.evidence_id
381
+ AND evidence.scope_kind = 'workspace'
382
+ AND evidence.scope_workspace_id = p_workspace_id
383
+ AND evidence.scope_subject_id IS NULL
384
+ AND evidence.polarity = 'supports'
385
+ AND EXISTS (
386
+ SELECT 1
387
+ FROM knowledge_claim_reviews review
388
+ WHERE review.account_id = p_account_id
389
+ AND review.claim_id = claim.id
390
+ AND review.scope_kind = 'workspace'
391
+ AND review.scope_workspace_id = p_workspace_id
392
+ AND review.scope_subject_id IS NULL
393
+ AND review.state = 'proposed'
394
+ AND review.reason = p_reason
395
+ AND review.actor_kind = 'service'
396
+ AND review.actor_subject_id = actor_subject_id
397
+ AND review.initiating_human_subject_id = turn_row.initiating_human_subject_id
398
+ )
399
+ )
400
+ THEN
401
+ RAISE EXCEPTION 'preference Knowledge proposal provenance is inexact'
402
+ USING ERRCODE = '23514';
403
+ END IF;
404
+
405
+ -- Replay resolves only the immutable workspace-local adapter receipt. It is
406
+ -- unaffected by later human activation, rejection, deactivation,
407
+ -- supersession, or scope change on the destination preference.
408
+ SELECT * INTO receipt_row
409
+ FROM company_brain_preference_proposal_receipts receipt
410
+ WHERE receipt.workspace_id = p_workspace_id
411
+ AND receipt.operation_id = p_operation_id
412
+ FOR SHARE;
413
+ IF FOUND THEN
414
+ IF receipt_row.account_id <> p_account_id
415
+ OR receipt_row.input_hash <> p_input_hash
416
+ OR receipt_row.knowledge_proposal_id <> p_knowledge_proposal_id
417
+ OR receipt_row.session_id <> p_session_id
418
+ OR receipt_row.turn_id <> p_turn_id
419
+ OR receipt_row.attempt_id <> p_attempt_id
420
+ OR receipt_row.execution_generation <> p_execution_generation
421
+ OR receipt_row.actor_subject_id <> actor_subject_id
422
+ OR receipt_row.initiating_human_subject_id <> turn_row.initiating_human_subject_id
423
+ THEN
424
+ RAISE EXCEPTION 'preference proposal operation conflicts with immutable receipt'
425
+ USING ERRCODE = '23505';
426
+ END IF;
427
+ preference_id := receipt_row.preference_id;
428
+ revision_id := receipt_row.revision_id;
429
+ RETURN NEXT;
430
+ RETURN;
431
+ END IF;
432
+
433
+ IF EXISTS (
434
+ SELECT 1
435
+ FROM company_brain_preference_proposal_receipts receipt
436
+ WHERE receipt.account_id = p_account_id
437
+ AND receipt.knowledge_proposal_id = p_knowledge_proposal_id
438
+ ) THEN
439
+ RAISE EXCEPTION 'Knowledge proposal is already bound to another preference receipt'
440
+ USING ERRCODE = '23505';
441
+ END IF;
442
+
443
+ PERFORM pg_advisory_xact_lock(hashtextextended(
444
+ concat('company-brain-preference:', p_account_id, ':', p_workspace_id, ':', p_stable_key),
445
+ 0::bigint
446
+ ));
447
+ SELECT * INTO preference_row
448
+ FROM preference_registry_preferences preference
449
+ WHERE preference.account_id = p_account_id
450
+ AND preference.scope = 'workspace'
451
+ AND preference.scope_workspace_id = p_workspace_id
452
+ AND preference.scope_subject_id IS NULL
453
+ AND preference.stable_key = p_stable_key
454
+ FOR UPDATE;
455
+ IF FOUND THEN
456
+ RAISE EXCEPTION 'workspace preference key is bound to another proposal'
457
+ USING ERRCODE = '23505';
458
+ END IF;
459
+
460
+ INSERT INTO preference_registry_preferences (
461
+ account_id, stable_key, scope, scope_workspace_id, scope_subject_id,
462
+ created_by_subject_id
463
+ ) VALUES (
464
+ p_account_id, p_stable_key, 'workspace', p_workspace_id, NULL,
465
+ actor_subject_id
466
+ ) RETURNING * INTO preference_row;
467
+ INSERT INTO preference_registry_revisions (
468
+ account_id, preference_id, title, description, content, content_hash,
469
+ precedence_rank, conflict_strategy, conflicts_with, provenance_source,
470
+ provenance_source_id, trust, expires_at, corrects_revision_id,
471
+ created_by_subject_id
472
+ ) VALUES (
473
+ p_account_id, preference_row.id, p_title, p_description, p_content,
474
+ encode(sha256(convert_to(p_content, 'UTF8')), 'hex'), p_precedence_rank,
475
+ p_conflict_strategy, p_conflicts_with, 'knowledge_proposal',
476
+ p_knowledge_proposal_id::text, 'untrusted_proposal', p_expires_at, NULL,
477
+ actor_subject_id
478
+ ) RETURNING * INTO revision_row;
479
+ INSERT INTO preference_registry_events (
480
+ account_id, preference_id, type, version, old_revision_id, new_revision_id,
481
+ old_scope, old_workspace_id, old_subject_id, new_scope, new_workspace_id,
482
+ new_subject_id, related_preference_id, actor_subject_id, reason
483
+ ) VALUES (
484
+ p_account_id, preference_row.id, 'proposal_created', 1, NULL, revision_row.id,
485
+ NULL, NULL, NULL, 'workspace', p_workspace_id, NULL, NULL,
486
+ actor_subject_id, p_reason
487
+ ) RETURNING * INTO event_row;
488
+ INSERT INTO company_brain_preference_proposal_receipts (
489
+ account_id, workspace_id, operation_id, input_hash, knowledge_proposal_id,
490
+ preference_id, revision_id, creation_event_id, session_id, turn_id,
491
+ attempt_id, execution_generation, actor_subject_id,
492
+ initiating_human_subject_id
493
+ ) VALUES (
494
+ p_account_id, p_workspace_id, p_operation_id, p_input_hash,
495
+ p_knowledge_proposal_id, preference_row.id, revision_row.id, event_row.id,
496
+ p_session_id, p_turn_id, p_attempt_id, p_execution_generation,
497
+ actor_subject_id, turn_row.initiating_human_subject_id
498
+ );
499
+ preference_id := preference_row.id;
500
+ revision_id := revision_row.id;
501
+ RETURN NEXT;
502
+ END;
503
+ $$;
504
+
505
+ REVOKE ALL ON FUNCTION preference_registry_create_knowledge_proposal_for_attempt(
506
+ uuid, uuid, uuid, uuid, uuid, integer, uuid, text, uuid, text, text, text, text,
507
+ integer, text, jsonb, timestamptz, text
508
+ ) FROM PUBLIC;
509
+
510
+ REVOKE ALL PRIVILEGES ON TABLE "company_brain_preference_proposal_receipts"
511
+ FROM PUBLIC;
512
+
513
+ DO $grant_preference_knowledge_proposal$
514
+ BEGIN
515
+ IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'opengeni_app') THEN
516
+ REVOKE ALL PRIVILEGES ON TABLE "company_brain_preference_proposal_receipts"
517
+ FROM opengeni_app;
518
+ GRANT EXECUTE ON FUNCTION preference_registry_create_knowledge_proposal_for_attempt(
519
+ uuid, uuid, uuid, uuid, uuid, integer, uuid, text, uuid, text, text, text, text,
520
+ integer, text, jsonb, timestamptz, text
521
+ ) TO opengeni_app;
522
+ END IF;
523
+ END;
524
+ $grant_preference_knowledge_proposal$;