@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,471 @@
1
+ -- deployment-mode: rolling
2
+ -- Migration 0261 follows the Task-note authority introduced at 0260.
3
+ -- Repair the existing Knowledge-backed Ways adapters without changing their
4
+ -- signatures, privileges, authority, or durable receipt contracts. Migration
5
+ -- 0255 used an output-column-conflicting PL/pgSQL local name in preference SQL
6
+ -- predicates and projected instruction targets in a shape rejected by the
7
+ -- canonical Knowledge table constraint.
8
+ -- The replacement SECURITY DEFINER function is pinned after creation to a
9
+ -- target-schema-safe path with pg_temp explicitly last, so caller temporary
10
+ -- relations cannot shadow any durable authority relation.
11
+
12
+ SET LOCAL lock_timeout = '5s';
13
+ SET LOCAL statement_timeout = '10min';
14
+
15
+ CREATE OR REPLACE FUNCTION workspace_instruction_policy_validate_onboarding_proposal()
16
+ RETURNS trigger
17
+ LANGUAGE plpgsql
18
+ AS $$
19
+ BEGIN
20
+ IF NEW."baseline_revision_id" IS NULL THEN
21
+ IF EXISTS (
22
+ SELECT 1
23
+ FROM "workspace_instruction_policy_heads" head
24
+ WHERE head."account_id" = NEW."account_id"
25
+ AND head."workspace_id" = NEW."workspace_id"
26
+ AND head."kind" = NEW."kind"
27
+ AND head."scope" = NEW."scope"
28
+ AND head."role_key" IS NOT DISTINCT FROM NEW."role_key"
29
+ ) THEN
30
+ RAISE EXCEPTION 'instruction-policy proposal must capture the exact active head baseline'
31
+ USING ERRCODE = '23514';
32
+ END IF;
33
+ ELSIF NOT EXISTS (
34
+ SELECT 1
35
+ FROM "workspace_instruction_policy_heads" head
36
+ WHERE head."account_id" = NEW."account_id"
37
+ AND head."workspace_id" = NEW."workspace_id"
38
+ AND head."kind" = NEW."kind"
39
+ AND head."scope" = NEW."scope"
40
+ AND head."role_key" IS NOT DISTINCT FROM NEW."role_key"
41
+ AND head."revision_id" = NEW."baseline_revision_id"
42
+ AND head."revision" = NEW."baseline_revision"
43
+ AND head."content_hash" = NEW."baseline_content_hash"
44
+ AND head."activation_version" = NEW."baseline_activation_version"
45
+ AND head."activated_at" = NEW."baseline_activated_at"
46
+ ) THEN
47
+ RAISE EXCEPTION 'instruction-policy proposal must capture the exact active head baseline'
48
+ USING ERRCODE = '23514';
49
+ END IF;
50
+
51
+ IF NEW."baseline_revision_id" IS NOT NULL AND NOT EXISTS (
52
+ SELECT 1
53
+ FROM "workspace_instruction_policy_revisions" revision
54
+ WHERE revision."id" = NEW."baseline_revision_id"
55
+ AND revision."account_id" = NEW."account_id"
56
+ AND revision."workspace_id" = NEW."workspace_id"
57
+ AND revision."kind" = NEW."kind"
58
+ AND revision."scope" = NEW."scope"
59
+ AND revision."role_key" IS NOT DISTINCT FROM NEW."role_key"
60
+ AND revision."revision" = NEW."baseline_revision"
61
+ AND revision."content_hash" = NEW."baseline_content_hash"
62
+ ) THEN
63
+ RAISE EXCEPTION 'instruction-policy proposal has an invalid baseline revision'
64
+ USING ERRCODE = '23514';
65
+ END IF;
66
+
67
+ IF NOT EXISTS (
68
+ SELECT 1
69
+ FROM "workspace_instruction_policy_revisions" revision
70
+ WHERE revision."id" = NEW."draft_revision_id"
71
+ AND revision."operation_id" = NEW."operation_id"
72
+ AND revision."request_fingerprint" = NEW."request_fingerprint"
73
+ AND revision."account_id" = NEW."account_id"
74
+ AND revision."workspace_id" = NEW."workspace_id"
75
+ AND revision."kind" = NEW."kind"
76
+ AND revision."scope" = NEW."scope"
77
+ AND revision."role_key" IS NOT DISTINCT FROM NEW."role_key"
78
+ AND revision."revision" = NEW."draft_revision"
79
+ AND revision."content_hash" = NEW."draft_content_hash"
80
+ AND revision."supersedes_revision_id" IS NOT DISTINCT FROM NEW."baseline_revision_id"
81
+ AND revision."created_by_subject_id" = NEW."created_by_subject_id"
82
+ AND (
83
+ (
84
+ revision."provenance_source" = 'onboarding'
85
+ AND revision."provenance_source_id" = NEW."id"::text
86
+ ) OR (
87
+ revision."provenance_source" = 'knowledge_proposal'
88
+ AND revision."provenance_source_id" = NEW."source_id"
89
+ AND NEW."source_version" = NEW."draft_content_hash"
90
+ AND EXISTS (
91
+ SELECT 1
92
+ FROM "knowledge_change_proposals" proposal
93
+ WHERE proposal."id"::text = NEW."source_id"
94
+ AND proposal."account_id" = NEW."account_id"
95
+ AND proposal."scope_kind" = 'workspace'
96
+ AND proposal."scope_workspace_id" = NEW."workspace_id"
97
+ AND proposal."scope_subject_id" IS NULL
98
+ AND proposal."target_kind" = 'instruction_policy'
99
+ AND proposal."target_scope" = NEW."scope"
100
+ AND proposal."target_key" IS NOT DISTINCT FROM CASE
101
+ WHEN NEW."scope" = 'role' THEN NEW."role_key"
102
+ ELSE NULL
103
+ END
104
+ AND proposal."content_hash" = NEW."source_version"
105
+ AND proposal."actor_kind" = 'service'
106
+ AND proposal."actor_subject_id" = NEW."created_by_subject_id"
107
+ AND proposal."initiating_human_subject_id"
108
+ = current_setting('opengeni.subject_id', true)
109
+ AND proposal."claim_id" IS NOT NULL
110
+ AND proposal."evidence_id" IS NOT NULL
111
+ AND proposal."status" = 'proposed'
112
+ )
113
+ )
114
+ )
115
+ ) THEN
116
+ RAISE EXCEPTION 'instruction-policy proposal must identify its exact inactive draft'
117
+ USING ERRCODE = '23514';
118
+ END IF;
119
+
120
+ IF EXISTS (
121
+ SELECT 1
122
+ FROM "workspace_instruction_policy_heads" head
123
+ WHERE head."account_id" = NEW."account_id"
124
+ AND head."workspace_id" = NEW."workspace_id"
125
+ AND head."revision_id" = NEW."draft_revision_id"
126
+ ) OR EXISTS (
127
+ SELECT 1
128
+ FROM "workspace_instruction_policy_activation_events" event
129
+ WHERE event."account_id" = NEW."account_id"
130
+ AND event."workspace_id" = NEW."workspace_id"
131
+ AND event."new_revision_id" = NEW."draft_revision_id"
132
+ ) THEN
133
+ RAISE EXCEPTION 'instruction-policy proposal must identify a never-activated inactive draft'
134
+ USING ERRCODE = '23514';
135
+ END IF;
136
+ RETURN NEW;
137
+ END;
138
+ $$;
139
+
140
+ CREATE OR REPLACE FUNCTION preference_registry_create_knowledge_proposal_for_attempt(
141
+ p_account_id uuid,
142
+ p_workspace_id uuid,
143
+ p_session_id uuid,
144
+ p_turn_id uuid,
145
+ p_attempt_id uuid,
146
+ p_execution_generation integer,
147
+ p_operation_id uuid,
148
+ p_input_hash text,
149
+ p_knowledge_proposal_id uuid,
150
+ p_stable_key text,
151
+ p_title text,
152
+ p_description text,
153
+ p_content text,
154
+ p_precedence_rank integer,
155
+ p_conflict_strategy text,
156
+ p_conflicts_with jsonb,
157
+ p_expires_at timestamptz,
158
+ p_reason text
159
+ ) RETURNS TABLE (preference_id uuid, revision_id uuid)
160
+ LANGUAGE plpgsql
161
+ SECURITY DEFINER
162
+ SET search_path FROM CURRENT
163
+ AS $$
164
+ DECLARE
165
+ turn_row record;
166
+ proposal_row knowledge_change_proposals%ROWTYPE;
167
+ preference_row preference_registry_preferences%ROWTYPE;
168
+ revision_row preference_registry_revisions%ROWTYPE;
169
+ event_row preference_registry_events%ROWTYPE;
170
+ receipt_row company_brain_preference_proposal_receipts%ROWTYPE;
171
+ v_actor_subject_id text;
172
+ expected_proposal_content jsonb;
173
+ normalized_conflicts jsonb;
174
+ BEGIN
175
+ IF NULLIF(current_setting('opengeni.account_id', true), '')::uuid
176
+ IS DISTINCT FROM p_account_id
177
+ OR NULLIF(current_setting('opengeni.workspace_id', true), '')::uuid
178
+ IS DISTINCT FROM p_workspace_id
179
+ THEN
180
+ RAISE EXCEPTION 'preference Knowledge proposal requires exact tenant context'
181
+ USING ERRCODE = '42501';
182
+ END IF;
183
+ IF p_execution_generation IS NULL OR p_execution_generation <= 0
184
+ OR p_operation_id IS NULL
185
+ OR p_input_hash !~ '^[0-9a-f]{64}$'
186
+ OR p_stable_key IS NULL
187
+ OR length(p_stable_key) NOT BETWEEN 1 AND 96
188
+ OR p_stable_key <> lower(btrim(p_stable_key))
189
+ OR p_stable_key !~ '^[a-z0-9](?:[a-z0-9._-]*[a-z0-9])?$'
190
+ OR p_stable_key ~ '--'
191
+ OR p_title IS NULL OR length(btrim(p_title)) NOT BETWEEN 1 AND 120
192
+ OR p_description IS NULL OR length(btrim(p_description)) NOT BETWEEN 1 AND 240
193
+ OR p_content IS NULL OR length(btrim(p_content)) = 0 OR length(p_content) > 262144
194
+ OR p_precedence_rank IS NULL OR p_precedence_rank NOT BETWEEN -1000 AND 1000
195
+ OR p_conflict_strategy IS NULL
196
+ OR p_conflict_strategy NOT IN ('override', 'merge', 'reject', 'inform')
197
+ OR p_conflicts_with IS NULL
198
+ OR jsonb_typeof(p_conflicts_with) IS DISTINCT FROM 'array'
199
+ OR jsonb_array_length(p_conflicts_with) > 32
200
+ OR p_reason IS NULL OR length(btrim(p_reason)) NOT BETWEEN 1 AND 4096
201
+ THEN
202
+ RAISE EXCEPTION 'preference Knowledge proposal input is invalid'
203
+ USING ERRCODE = '22023';
204
+ END IF;
205
+ IF EXISTS (
206
+ SELECT 1
207
+ FROM jsonb_array_elements(p_conflicts_with) item
208
+ WHERE jsonb_typeof(item) <> 'string'
209
+ ) OR EXISTS (
210
+ SELECT 1
211
+ FROM jsonb_array_elements_text(p_conflicts_with) item(value)
212
+ WHERE length(value) NOT BETWEEN 1 AND 96
213
+ OR value <> lower(btrim(value))
214
+ OR value !~ '^[a-z0-9](?:[a-z0-9._-]*[a-z0-9])?$'
215
+ OR value ~ '--'
216
+ ) THEN
217
+ RAISE EXCEPTION 'preference Knowledge proposal conflicts are invalid'
218
+ USING ERRCODE = '22023';
219
+ END IF;
220
+ SELECT COALESCE(jsonb_agg(value ORDER BY value), '[]'::jsonb)
221
+ INTO normalized_conflicts
222
+ FROM (
223
+ SELECT DISTINCT value
224
+ FROM jsonb_array_elements_text(p_conflicts_with) item(value)
225
+ ) normalized;
226
+ IF normalized_conflicts IS DISTINCT FROM p_conflicts_with THEN
227
+ RAISE EXCEPTION 'preference Knowledge proposal conflicts must be unique and sorted'
228
+ USING ERRCODE = '22023';
229
+ END IF;
230
+
231
+ PERFORM 1
232
+ FROM workspaces workspace
233
+ WHERE workspace.id = p_workspace_id AND workspace.account_id = p_account_id
234
+ FOR KEY SHARE;
235
+ IF NOT FOUND THEN
236
+ RAISE EXCEPTION 'preference Knowledge proposal workspace is unavailable'
237
+ USING ERRCODE = '42501';
238
+ END IF;
239
+ PERFORM 1
240
+ FROM sessions session
241
+ WHERE session.id = p_session_id
242
+ AND session.account_id = p_account_id
243
+ AND session.workspace_id = p_workspace_id
244
+ AND session.active_turn_id = p_turn_id
245
+ FOR SHARE;
246
+ IF NOT FOUND THEN
247
+ RAISE EXCEPTION 'preference Knowledge proposal session is unavailable'
248
+ USING ERRCODE = '42501';
249
+ END IF;
250
+ SELECT turn.id, turn.account_id, turn.workspace_id, turn.session_id,
251
+ turn.active_attempt_id, turn.execution_generation,
252
+ coalesce(
253
+ turn.initiating_human_subject_id,
254
+ case when turn.initiator_kind = 'subject' then turn.initiator_subject_id end
255
+ ) AS initiating_human_subject_id
256
+ INTO turn_row
257
+ FROM session_turns turn
258
+ WHERE turn.id = p_turn_id
259
+ AND turn.account_id = p_account_id
260
+ AND turn.workspace_id = p_workspace_id
261
+ AND turn.session_id = p_session_id
262
+ AND turn.active_attempt_id = p_attempt_id
263
+ AND turn.execution_generation = p_execution_generation
264
+ AND turn.status IN ('running', 'requires_action', 'recovering', 'waiting_capacity')
265
+ FOR SHARE;
266
+ IF NOT FOUND
267
+ OR length(btrim(coalesce(turn_row.initiating_human_subject_id, ''))) NOT BETWEEN 1 AND 1024
268
+ OR current_setting('opengeni.subject_id', true)
269
+ IS DISTINCT FROM turn_row.initiating_human_subject_id
270
+ THEN
271
+ RAISE EXCEPTION 'preference Knowledge proposal requires the immutable initiating human'
272
+ USING ERRCODE = '42501';
273
+ END IF;
274
+ PERFORM 1
275
+ FROM session_turn_attempts attempt
276
+ WHERE attempt.id = p_attempt_id
277
+ AND attempt.account_id = p_account_id
278
+ AND attempt.workspace_id = p_workspace_id
279
+ AND attempt.session_id = p_session_id
280
+ AND attempt.turn_id = p_turn_id
281
+ AND attempt.execution_generation = p_execution_generation
282
+ AND attempt.state IN ('claimed', 'running')
283
+ AND NOT EXISTS (
284
+ SELECT 1
285
+ FROM session_attempt_interruptions interruption
286
+ WHERE interruption.workspace_id = attempt.workspace_id
287
+ AND interruption.attempt_id = attempt.id
288
+ AND interruption.state IN ('pending', 'delivered', 'acknowledged')
289
+ )
290
+ FOR SHARE;
291
+ IF NOT FOUND THEN
292
+ RAISE EXCEPTION 'preference Knowledge proposal requires the exact current attempt'
293
+ USING ERRCODE = '42501';
294
+ END IF;
295
+
296
+ v_actor_subject_id := 'service:company-brain-governed-write:' || p_input_hash;
297
+ expected_proposal_content := jsonb_build_object(
298
+ 'stableKey', p_stable_key,
299
+ 'title', p_title,
300
+ 'description', p_description,
301
+ 'content', p_content,
302
+ 'precedenceRank', p_precedence_rank,
303
+ 'conflictStrategy', p_conflict_strategy,
304
+ 'conflictsWith', p_conflicts_with,
305
+ 'expiresAt', CASE WHEN p_expires_at IS NULL THEN NULL ELSE
306
+ to_char(p_expires_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') END
307
+ );
308
+ SELECT * INTO proposal_row
309
+ FROM knowledge_change_proposals proposal
310
+ WHERE proposal.id = p_knowledge_proposal_id
311
+ AND proposal.account_id = p_account_id
312
+ AND proposal.scope_kind = 'workspace'
313
+ AND proposal.scope_workspace_id = p_workspace_id
314
+ AND proposal.scope_subject_id IS NULL
315
+ AND proposal.target_kind = 'preference'
316
+ AND proposal.target_scope = 'workspace'
317
+ AND proposal.target_key = p_stable_key
318
+ AND proposal.status = 'proposed'
319
+ AND proposal.actor_kind = 'service'
320
+ AND proposal.actor_subject_id = v_actor_subject_id
321
+ AND proposal.initiating_human_subject_id = turn_row.initiating_human_subject_id
322
+ FOR SHARE;
323
+ IF NOT FOUND OR proposal_row.content::jsonb IS DISTINCT FROM expected_proposal_content
324
+ OR NOT EXISTS (
325
+ SELECT 1
326
+ FROM knowledge_claims claim
327
+ JOIN knowledge_claim_evidence evidence
328
+ ON evidence.account_id = claim.account_id
329
+ AND evidence.claim_id = claim.id
330
+ WHERE claim.id = proposal_row.claim_id
331
+ AND claim.account_id = p_account_id
332
+ AND claim.scope_kind = 'workspace'
333
+ AND claim.scope_workspace_id = p_workspace_id
334
+ AND claim.scope_subject_id IS NULL
335
+ AND evidence.id = proposal_row.evidence_id
336
+ AND evidence.scope_kind = 'workspace'
337
+ AND evidence.scope_workspace_id = p_workspace_id
338
+ AND evidence.scope_subject_id IS NULL
339
+ AND evidence.polarity = 'supports'
340
+ AND EXISTS (
341
+ SELECT 1
342
+ FROM knowledge_claim_reviews review
343
+ WHERE review.account_id = p_account_id
344
+ AND review.claim_id = claim.id
345
+ AND review.scope_kind = 'workspace'
346
+ AND review.scope_workspace_id = p_workspace_id
347
+ AND review.scope_subject_id IS NULL
348
+ AND review.state = 'proposed'
349
+ AND review.reason = p_reason
350
+ AND review.actor_kind = 'service'
351
+ AND review.actor_subject_id = v_actor_subject_id
352
+ AND review.initiating_human_subject_id = turn_row.initiating_human_subject_id
353
+ )
354
+ )
355
+ THEN
356
+ RAISE EXCEPTION 'preference Knowledge proposal provenance is inexact'
357
+ USING ERRCODE = '23514';
358
+ END IF;
359
+
360
+ -- Replay resolves only the immutable workspace-local adapter receipt. It is
361
+ -- unaffected by later human activation, rejection, deactivation,
362
+ -- supersession, or scope change on the destination preference.
363
+ SELECT * INTO receipt_row
364
+ FROM company_brain_preference_proposal_receipts receipt
365
+ WHERE receipt.workspace_id = p_workspace_id
366
+ AND receipt.operation_id = p_operation_id
367
+ FOR SHARE;
368
+ IF FOUND THEN
369
+ IF receipt_row.account_id <> p_account_id
370
+ OR receipt_row.input_hash <> p_input_hash
371
+ OR receipt_row.knowledge_proposal_id <> p_knowledge_proposal_id
372
+ OR receipt_row.session_id <> p_session_id
373
+ OR receipt_row.turn_id <> p_turn_id
374
+ OR receipt_row.attempt_id <> p_attempt_id
375
+ OR receipt_row.execution_generation <> p_execution_generation
376
+ OR receipt_row.actor_subject_id <> v_actor_subject_id
377
+ OR receipt_row.initiating_human_subject_id <> turn_row.initiating_human_subject_id
378
+ THEN
379
+ RAISE EXCEPTION 'preference proposal operation conflicts with immutable receipt'
380
+ USING ERRCODE = '23505';
381
+ END IF;
382
+ preference_id := receipt_row.preference_id;
383
+ revision_id := receipt_row.revision_id;
384
+ RETURN NEXT;
385
+ RETURN;
386
+ END IF;
387
+
388
+ IF EXISTS (
389
+ SELECT 1
390
+ FROM company_brain_preference_proposal_receipts receipt
391
+ WHERE receipt.account_id = p_account_id
392
+ AND receipt.knowledge_proposal_id = p_knowledge_proposal_id
393
+ ) THEN
394
+ RAISE EXCEPTION 'Knowledge proposal is already bound to another preference receipt'
395
+ USING ERRCODE = '23505';
396
+ END IF;
397
+
398
+ PERFORM pg_advisory_xact_lock(hashtextextended(
399
+ concat('company-brain-preference:', p_account_id, ':', p_workspace_id, ':', p_stable_key),
400
+ 0::bigint
401
+ ));
402
+ SELECT * INTO preference_row
403
+ FROM preference_registry_preferences preference
404
+ WHERE preference.account_id = p_account_id
405
+ AND preference.scope = 'workspace'
406
+ AND preference.scope_workspace_id = p_workspace_id
407
+ AND preference.scope_subject_id IS NULL
408
+ AND preference.stable_key = p_stable_key
409
+ FOR UPDATE;
410
+ IF FOUND THEN
411
+ RAISE EXCEPTION 'workspace preference key is bound to another proposal'
412
+ USING ERRCODE = '23505';
413
+ END IF;
414
+
415
+ INSERT INTO preference_registry_preferences (
416
+ account_id, stable_key, scope, scope_workspace_id, scope_subject_id,
417
+ created_by_subject_id
418
+ ) VALUES (
419
+ p_account_id, p_stable_key, 'workspace', p_workspace_id, NULL,
420
+ v_actor_subject_id
421
+ ) RETURNING * INTO preference_row;
422
+ INSERT INTO preference_registry_revisions (
423
+ account_id, preference_id, title, description, content, content_hash,
424
+ precedence_rank, conflict_strategy, conflicts_with, provenance_source,
425
+ provenance_source_id, trust, expires_at, corrects_revision_id,
426
+ created_by_subject_id
427
+ ) VALUES (
428
+ p_account_id, preference_row.id, p_title, p_description, p_content,
429
+ encode(sha256(convert_to(p_content, 'UTF8')), 'hex'), p_precedence_rank,
430
+ p_conflict_strategy, p_conflicts_with, 'knowledge_proposal',
431
+ p_knowledge_proposal_id::text, 'untrusted_proposal', p_expires_at, NULL,
432
+ v_actor_subject_id
433
+ ) RETURNING * INTO revision_row;
434
+ INSERT INTO preference_registry_events (
435
+ account_id, preference_id, type, version, old_revision_id, new_revision_id,
436
+ old_scope, old_workspace_id, old_subject_id, new_scope, new_workspace_id,
437
+ new_subject_id, related_preference_id, actor_subject_id, reason
438
+ ) VALUES (
439
+ p_account_id, preference_row.id, 'proposal_created', 1, NULL, revision_row.id,
440
+ NULL, NULL, NULL, 'workspace', p_workspace_id, NULL, NULL,
441
+ v_actor_subject_id, p_reason
442
+ ) RETURNING * INTO event_row;
443
+ INSERT INTO company_brain_preference_proposal_receipts (
444
+ account_id, workspace_id, operation_id, input_hash, knowledge_proposal_id,
445
+ preference_id, revision_id, creation_event_id, session_id, turn_id,
446
+ attempt_id, execution_generation, actor_subject_id,
447
+ initiating_human_subject_id
448
+ ) VALUES (
449
+ p_account_id, p_workspace_id, p_operation_id, p_input_hash,
450
+ p_knowledge_proposal_id, preference_row.id, revision_row.id, event_row.id,
451
+ p_session_id, p_turn_id, p_attempt_id, p_execution_generation,
452
+ v_actor_subject_id, turn_row.initiating_human_subject_id
453
+ );
454
+ preference_id := preference_row.id;
455
+ revision_id := revision_row.id;
456
+ RETURN NEXT;
457
+ END;
458
+ $$;
459
+
460
+ DO $preference_knowledge_proposal_search_path$
461
+ DECLARE
462
+ data_schema text := current_schema();
463
+ BEGIN
464
+ EXECUTE format(
465
+ 'ALTER FUNCTION %I.preference_registry_create_knowledge_proposal_for_attempt('
466
+ || 'uuid,uuid,uuid,uuid,uuid,integer,uuid,text,uuid,text,text,text,text,integer,text,jsonb,timestamptz,text) '
467
+ || 'SET search_path = pg_catalog, %I, pg_temp',
468
+ data_schema, data_schema
469
+ );
470
+ END;
471
+ $preference_knowledge_proposal_search_path$;