@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.
- package/dist/attached-browser-devices.d.ts +7 -0
- package/dist/automations.d.ts +154 -0
- package/dist/canonical-human-identities.js +3 -3
- package/dist/capability-integrations.d.ts +13 -0
- package/dist/child-lifecycle-notices.d.ts +68 -0
- package/dist/{chunk-GYO7ED2Q.js → chunk-EULAN2NP.js} +2 -2
- package/dist/{chunk-VTPRIXKG.js → chunk-NJVZPWXY.js} +3 -3
- package/dist/{chunk-HSOWSYOX.js → chunk-NMUBV7Z5.js} +19 -11
- package/dist/chunk-NMUBV7Z5.js.map +1 -0
- package/dist/chunk-OF5OBB76.js +4457 -0
- package/dist/chunk-OF5OBB76.js.map +1 -0
- package/dist/{chunk-5KTIEDX7.js → chunk-PR2WMLOQ.js} +7714 -4443
- package/dist/chunk-PR2WMLOQ.js.map +1 -0
- package/dist/{chunk-Z7VYMNPZ.js → chunk-SCPUJS53.js} +41 -4
- package/dist/chunk-SCPUJS53.js.map +1 -0
- package/dist/{chunk-SK7YYHBI.js → chunk-SIBYBA2D.js} +46 -1
- package/dist/chunk-SIBYBA2D.js.map +1 -0
- package/dist/chunk-TIKF3KTI.js +383 -0
- package/dist/chunk-TIKF3KTI.js.map +1 -0
- package/dist/chunk-ZZ5LJEFX.js +516 -0
- package/dist/chunk-ZZ5LJEFX.js.map +1 -0
- package/dist/codemode-operations.d.ts +1 -6
- package/dist/company-brain-context-selection-schema.d.ts +705 -0
- package/dist/company-brain-context-selection.d.ts +94 -0
- package/dist/company-brain-governed-writes.d.ts +24 -0
- package/dist/company-brain.d.ts +55 -0
- package/dist/company-profile-agent-admin.d.ts +15 -0
- package/dist/company-profile-schema.d.ts +619 -2
- package/dist/company-profile.d.ts +9 -4
- package/dist/connection-authority.d.ts +95 -0
- package/dist/connection-scopes.d.ts +8 -0
- package/dist/connection-tenancy-backfill.d.ts +41 -0
- package/dist/connection-token-resolver.d.ts +30 -1
- package/dist/database.d.ts +19 -0
- package/dist/editable-artifact-durable-export.d.ts +10 -0
- package/dist/editable-artifact-durable-export.js +18 -3
- package/dist/editable-artifact-durable-export.js.map +1 -1
- package/dist/editable-artifacts.d.ts +2 -2
- package/dist/editable-artifacts.js +3 -3
- package/dist/governed-learning-activation-schema.d.ts +984 -0
- package/dist/governed-learning-activation.d.ts +98 -0
- package/dist/governed-learning-evaluator-schema.d.ts +633 -0
- package/dist/governed-learning-evaluator.d.ts +28 -0
- package/dist/index.d.ts +1633 -87
- package/dist/index.js +22004 -8109
- package/dist/index.js.map +1 -1
- package/dist/insights.d.ts +18 -0
- package/dist/integration-bindings.d.ts +1 -0
- package/dist/integration-facets.d.ts +30 -0
- package/dist/interaction-placement-loss.d.ts +22 -0
- package/dist/knowledge-source-sync-schema.d.ts +730 -0
- package/dist/knowledge-source-sync.d.ts +60 -0
- package/dist/managed-human-provisioning.d.ts +25 -0
- package/dist/memory-domain.d.ts +3 -3
- package/dist/migrate.d.ts +11 -1
- package/dist/migrate.js +1 -1
- package/dist/new-session-drafts.d.ts +43 -5
- package/dist/organization-membership-backfill.d.ts +174 -0
- package/dist/organization-membership-lifecycle.d.ts +162 -0
- package/dist/pr-review.d.ts +107 -0
- package/dist/preference-registry-schema.d.ts +2 -0
- package/dist/preference-registry.d.ts +2 -0
- package/dist/provision-roles.js +1 -1
- package/dist/runtime-posture.d.ts +23 -9
- package/dist/schema.d.ts +24925 -13697
- package/dist/schema.js +103 -1
- package/dist/scoped-knowledge-schema.d.ts +52 -1
- package/dist/session-background-commands.d.ts +125 -0
- package/dist/session-background-commands.js +34 -0
- package/dist/session-background-commands.js.map +1 -0
- package/dist/session-control.d.ts +156 -3
- package/dist/session-goal-pacing.d.ts +68 -0
- package/dist/session-queue-commands.d.ts +42 -5
- package/dist/session-realtime-ledger.d.ts +3 -0
- package/dist/session-tenancy.d.ts +71 -1
- package/dist/session-tenancy.js +25 -3
- package/dist/session-tool-call-settlement.d.ts +9 -4
- package/dist/slack-routing-personal-workspace.d.ts +23 -0
- package/dist/slack-routing.d.ts +153 -0
- package/dist/slack-task-policy-schema.d.ts +34 -0
- package/dist/slack-task-policy.d.ts +8 -0
- package/dist/slack-user-link-access.d.ts +1 -0
- package/dist/task-notes-schema.d.ts +594 -0
- package/dist/task-notes.d.ts +39 -0
- package/dist/turn-initiator.d.ts +2 -0
- package/dist/user-resource-authority.d.ts +109 -0
- package/dist/video-generation.js +3 -3
- package/dist/workspace-authority.d.ts +81 -0
- package/dist/workspace-instruction-policies-schema.d.ts +279 -0
- package/dist/workspace-instruction-policies.d.ts +62 -0
- package/dist/workspace-learning-policy.d.ts +1 -0
- package/dist/workspace-membership-permissions.d.ts +2 -0
- package/drizzle/0240_enrollment_connection_authority.sql +39 -0
- package/drizzle/0240_model_context_user_messages.sql +204 -0
- package/drizzle/0241_atomic_personal_resource_delegation.sql +1000 -0
- package/drizzle/0241_enrollment_agent_runtime.sql +70 -0
- package/drizzle/0243_google_drive_object_acl_authority.sql +554 -0
- package/drizzle/0244_slack_app_home_refresh_queue.sql +120 -0
- package/drizzle/0245_model_context_contribution_facts.sql +88 -0
- package/drizzle/0246_integration_personal_instance_authority.sql +70 -0
- package/drizzle/0247_terraform_stacks_provenance_repair.sql +428 -0
- package/drizzle/0248_terraform_stacks_component_resolution_fence.sql +83 -0
- package/drizzle/0249_personal_resource_delegation_authority_correction.sql +662 -0
- package/drizzle/0250_direct_retained_process_owner_liveness.sql +199 -0
- package/drizzle/0251_connected_machine_operation_policy.sql +45 -0
- package/drizzle/0252_scheduled_personal_resource_delegation.sql +1867 -0
- package/drizzle/0253_common_user_resource_authority_lifecycle.sql +1555 -0
- package/drizzle/0254_scoped_variable_set_authority.sql +1255 -0
- package/drizzle/0255_company_brain_governed_write_proposals.sql +524 -0
- package/drizzle/0256_connection_authority_delegation.sql +703 -0
- package/drizzle/0257_goal_revision_decisions_and_root_constraints.sql +436 -0
- package/drizzle/0258_three_scope_document_knowledge_authority.sql +1273 -0
- package/drizzle/0259_company_brain_context_selection_receipts.sql +869 -0
- package/drizzle/0260_task_note_knowledge_promotion.sql +820 -0
- package/drizzle/0261_preference_knowledge_proposal_actor_binding.sql +471 -0
- package/drizzle/0262_scoped_connected_machines_and_rigs.sql +1700 -0
- package/drizzle/0263_organization_membership_lifecycle.sql +3284 -0
- package/drizzle/0264_connection_authority_runtime_activation.sql +1084 -0
- package/drizzle/0266_company_brain_context_receipt_inspection.sql +156 -0
- package/drizzle/0268_governed_learning_decision_receipts.sql +825 -0
- package/drizzle/0269_governed_learning_activation_controller.sql +1874 -0
- package/drizzle/0270_governed_learning_history_inspection.sql +170 -0
- package/drizzle/0271_company_brain_retrieval_only_default.sql +524 -0
- package/drizzle/0272_human_confirmed_learning_activation.sql +591 -0
- package/drizzle/0273_scheduled_variable_set_materialization.sql +268 -0
- package/drizzle/0274_human_confirmed_knowledge_review.sql +310 -0
- package/drizzle/0275_scheduled_connection_authority.sql +5527 -0
- package/drizzle/0276_onboarding_proposal_initiating_human_guc.sql +198 -0
- package/drizzle/0277_workspace_writer_authority_attribution.sql +393 -0
- package/drizzle/0278_workspace_membership_removal_fencing.sql +1174 -0
- package/drizzle/0279_workspace_connection_use_lane.sql +343 -0
- package/drizzle/0280_connection_and_variable_set_audit_attribution.sql +773 -0
- package/drizzle/0281_viewer_holder_authority_claims.sql +52 -0
- package/drizzle/0282_variable_set_session_attach_attribution.sql +178 -0
- package/drizzle/0283_editable_spreadsheet_authored_state.sql +82 -0
- package/drizzle/0284_truthful_human_confirmed_review_reason.sql +930 -0
- package/drizzle/0285_organization_tenancy_inventory.sql +329 -0
- package/drizzle/0286_widen_task_note_expiry_ceiling.sql +391 -0
- package/drizzle/0287_open_suffix_pending_tool_calls.sql +18 -0
- package/drizzle/0288_attached_browser_reenrollment.sql +64 -0
- package/drizzle/0289_session_composer_policy_authority.sql +597 -0
- package/drizzle/0290_organization_membership_backfill.sql +268 -0
- package/drizzle/0291_resource_authority_classification_assertion.sql +371 -0
- package/drizzle/0292_truthful_tenancy_inventory_counters.sql +291 -0
- package/drizzle/0293_confirm_time_rule_rebaseline.sql +615 -0
- package/drizzle/0294_preference_activation_authority.sql +237 -0
- package/drizzle/0295_retire_legacy_standing_memory_mode.sql +476 -0
- package/drizzle/0296_force_rls_backfill_noop_repair.sql +215 -0
- package/drizzle/0297_session_ownership_classification_and_backfill.sql +752 -0
- package/drizzle/0298_organization_tenancy_parity.sql +609 -0
- package/drizzle/0299_organization_membership_lock_order.sql +291 -0
- package/drizzle/0300_tenancy_backfill_ledger.sql +459 -0
- package/drizzle/0301_session_snapshot_and_pin_visibility.sql +181 -0
- package/drizzle/0302_personal_workspace_session_ownership.sql +303 -0
- package/drizzle/0303_session_tenancy_product_activation.sql +919 -0
- package/drizzle/0304_personal_workspace_private_session_reads.sql +97 -0
- package/drizzle/0305_personal_resource_grant_management.sql +584 -0
- package/drizzle/0306_atomic_personal_resource_attachments.sql +1374 -0
- package/drizzle/0307_session_attention_state.sql +51 -0
- package/drizzle/0308_session_archives.sql +35 -0
- package/drizzle/0309_channel_project_pins.sql +6 -0
- package/drizzle/0310_channel_project_order.sql +27 -0
- package/drizzle/0311_company_scope_and_private_session_create.sql +656 -0
- package/drizzle/0312_quiescent_session_tree_deletion.sql +39 -0
- package/drizzle/0313_private_child_session_authority.sql +365 -0
- package/drizzle/0314_unregistered_organization_invitations.sql +606 -0
- package/drizzle/0315_personal_github_repository_selection.sql +624 -0
- package/drizzle/0316_human_confirmed_activation_resumed_generation.sql +869 -0
- package/drizzle/0317_session_goal_continuation_hold.sql +64 -0
- package/drizzle/0318_session_turn_startup_milestones.sql +115 -0
- package/drizzle/0319_event_triggered_automations.sql +254 -0
- package/drizzle/0320_pr_review_pack.sql +113 -0
- package/drizzle/0321_slack_bot_environment_display_name.sql +15 -0
- package/drizzle/0322_session_turns_unclaimed_prompt_trigger_index.sql +5 -0
- package/drizzle/0323_organization_private_session_enablement.sql +489 -0
- package/drizzle/0324_human_confirmed_company_profile_agent_admin.sql +856 -0
- package/drizzle/0325_child_lifecycle_notices.sql +44 -0
- package/drizzle/0326_interaction_operation_error_codes.sql +62 -0
- package/drizzle/0327_slack_first_task_hint.sql +29 -0
- package/drizzle/0328_session_background_commands.sql +590 -0
- package/drizzle/0329_slack_orchestration_delivery_events.sql +91 -0
- package/drizzle/0330_api_key_descriptions.sql +20 -0
- package/drizzle/0331_managed_organization_creation.sql +163 -0
- package/drizzle/0332_organization_shared_workspace_control_plane.sql +539 -0
- package/drizzle/0333_session_turn_prompt_routing.sql +45 -0
- package/drizzle/0334_connected_machine_workspace_root.sql +99 -0
- package/drizzle/0335_slack_workspace_routing.sql +460 -0
- package/drizzle/0336_atomic_session_fork_visibility.sql +667 -0
- package/drizzle/0337_slack_routed_action_handles.sql +189 -0
- package/drizzle/0338_atomic_connected_machine_attachments.sql +410 -0
- package/drizzle/0339_document_authority_reclassification.sql +1153 -0
- package/drizzle/0340_tenancy_backfill_activation_evidence.sql +1347 -0
- package/drizzle/0341_slack_routing_probe_organization_fence.sql +109 -0
- package/package.json +10 -6
- package/src/attached-browser-devices.ts +233 -5
- package/src/automations.ts +911 -0
- package/src/browser-sessions.ts +10 -0
- package/src/capability-integrations.ts +175 -1
- package/src/child-lifecycle-notices.ts +241 -0
- package/src/codemode-operations.ts +1 -35
- package/src/company-brain-context-selection-schema.ts +100 -0
- package/src/company-brain-context-selection.ts +359 -0
- package/src/company-brain-governed-writes.ts +1170 -0
- package/src/company-brain.ts +309 -0
- package/src/company-profile-agent-admin.ts +225 -0
- package/src/company-profile-schema.ts +121 -1
- package/src/company-profile.ts +21 -0
- package/src/computer-sessions.ts +10 -1
- package/src/connection-authority.ts +290 -0
- package/src/connection-scopes.ts +20 -0
- package/src/connection-tenancy-backfill.ts +81 -0
- package/src/connection-token-resolver.ts +263 -31
- package/src/database.ts +75 -2
- package/src/editable-artifact-durable-export.ts +27 -0
- package/src/editable-artifacts-schema.ts +10 -5
- package/src/editable-artifacts.ts +21 -11
- package/src/governed-learning-activation-schema.ts +121 -0
- package/src/governed-learning-activation.ts +588 -0
- package/src/governed-learning-evaluator-schema.ts +72 -0
- package/src/governed-learning-evaluator.ts +217 -0
- package/src/index.ts +12001 -1699
- package/src/insights.ts +107 -0
- package/src/integration-bindings.ts +88 -6
- package/src/integration-facets.ts +287 -84
- package/src/interaction-placement-loss.ts +234 -0
- package/src/knowledge-source-sync-schema.ts +83 -0
- package/src/knowledge-source-sync.ts +530 -0
- package/src/managed-human-provisioning.ts +116 -0
- package/src/memory-domain.ts +27 -32
- package/src/migrate.ts +77 -1
- package/src/new-session-drafts.ts +229 -35
- package/src/organization-membership-backfill.ts +735 -0
- package/src/organization-membership-lifecycle.ts +985 -0
- package/src/pr-review.ts +890 -0
- package/src/preference-registry.ts +31 -1
- package/src/provision-roles.ts +931 -24
- package/src/runtime-posture-cli.ts +2 -0
- package/src/runtime-posture.ts +1315 -52
- package/src/schema.ts +3336 -438
- package/src/scoped-knowledge-schema.ts +16 -8
- package/src/scoped-knowledge.ts +1 -1
- package/src/session-background-commands.ts +759 -0
- package/src/session-control.ts +640 -74
- package/src/session-goal-pacing.ts +126 -0
- package/src/session-queue-commands.ts +450 -98
- package/src/session-realtime-context.ts +19 -3
- package/src/session-realtime-ledger.ts +75 -12
- package/src/session-realtime-mirror.ts +1 -1
- package/src/session-tenancy.ts +403 -49
- package/src/session-tool-call-settlement.ts +38 -29
- package/src/slack-routing-personal-workspace.ts +67 -0
- package/src/slack-routing.ts +469 -0
- package/src/slack-task-policy-schema.ts +5 -0
- package/src/slack-task-policy.ts +18 -0
- package/src/slack-user-link-access.ts +17 -1
- package/src/task-notes-schema.ts +74 -0
- package/src/task-notes.ts +124 -2
- package/src/turn-initiator.ts +8 -0
- package/src/user-resource-authority.ts +377 -0
- package/src/workspace-authority.ts +160 -0
- package/src/workspace-instruction-policies-schema.ts +53 -1
- package/src/workspace-instruction-policies.ts +528 -77
- package/src/workspace-learning-policy.ts +21 -5
- package/src/workspace-membership-permissions.ts +9 -0
- package/dist/chunk-5KTIEDX7.js.map +0 -1
- package/dist/chunk-FPVAD4CO.js +0 -117
- package/dist/chunk-FPVAD4CO.js.map +0 -1
- package/dist/chunk-HSOWSYOX.js.map +0 -1
- package/dist/chunk-SK7YYHBI.js.map +0 -1
- package/dist/chunk-VKLB2ZGI.js +0 -2389
- package/dist/chunk-VKLB2ZGI.js.map +0 -1
- package/dist/chunk-Z7VYMNPZ.js.map +0 -1
- /package/dist/{chunk-GYO7ED2Q.js.map → chunk-EULAN2NP.js.map} +0 -0
- /package/dist/{chunk-VTPRIXKG.js.map → chunk-NJVZPWXY.js.map} +0 -0
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
-- deployment-mode: rolling
|
|
2
|
+
SET lock_timeout = '5s';
|
|
3
|
+
SET statement_timeout = '60s';
|
|
4
|
+
|
|
5
|
+
-- Human-confirmed governed-learning activation. A `remember` proposal that the
|
|
6
|
+
-- workspace learning policy would not activate automatically may still be
|
|
7
|
+
-- activated once the exact initiating human answered the bound structured
|
|
8
|
+
-- human-input question (`remember:<proposalId>` = `save`) on the same
|
|
9
|
+
-- session/turn generation. The activation is recorded on the same immutable
|
|
10
|
+
-- receipt ledger with an explicit authority kind and the human-input request
|
|
11
|
+
-- id, so the human answer that authorized it stays provable.
|
|
12
|
+
|
|
13
|
+
ALTER TABLE "governed_learning_activation_receipts"
|
|
14
|
+
ADD COLUMN "authority_kind" text NOT NULL DEFAULT 'automatic',
|
|
15
|
+
ADD COLUMN "human_input_request_id" uuid;
|
|
16
|
+
ALTER TABLE "governed_learning_activation_receipts"
|
|
17
|
+
ADD CONSTRAINT "governed_learning_activation_receipts_authority_kind_check"
|
|
18
|
+
CHECK ("authority_kind" IN ('automatic', 'human_confirmed')),
|
|
19
|
+
ADD CONSTRAINT "governed_learning_activation_receipts_human_input_check"
|
|
20
|
+
CHECK (("authority_kind" = 'human_confirmed') = ("human_input_request_id" IS NOT NULL));
|
|
21
|
+
|
|
22
|
+
CREATE OR REPLACE FUNCTION activate_human_confirmed_learning_decision(
|
|
23
|
+
p_account_id uuid,
|
|
24
|
+
p_workspace_id uuid,
|
|
25
|
+
p_operation_id uuid,
|
|
26
|
+
p_decision_receipt_id uuid,
|
|
27
|
+
p_human_input_request_id uuid
|
|
28
|
+
) RETURNS SETOF governed_learning_activation_receipts
|
|
29
|
+
LANGUAGE plpgsql SECURITY DEFINER AS $$
|
|
30
|
+
DECLARE
|
|
31
|
+
caller_subject_id text := nullif(current_setting('opengeni.subject_id', true), '');
|
|
32
|
+
decision_row governed_learning_decision_receipts%ROWTYPE;
|
|
33
|
+
result_row governed_learning_activation_receipts%ROWTYPE;
|
|
34
|
+
proposal_row knowledge_change_proposals%ROWTYPE;
|
|
35
|
+
claim_row knowledge_claims%ROWTYPE;
|
|
36
|
+
evidence_row knowledge_claim_evidence%ROWTYPE;
|
|
37
|
+
review_row knowledge_claim_reviews%ROWTYPE;
|
|
38
|
+
approval_row knowledge_claim_reviews%ROWTYPE;
|
|
39
|
+
policy_head workspace_learning_policy_heads%ROWTYPE;
|
|
40
|
+
policy_revision workspace_learning_policy_revisions%ROWTYPE;
|
|
41
|
+
policy_snapshot workspace_learning_policy_snapshots%ROWTYPE;
|
|
42
|
+
task_note_row task_notes%ROWTYPE;
|
|
43
|
+
document_authority record;
|
|
44
|
+
instruction_proposal workspace_instruction_policy_onboarding_proposals%ROWTYPE;
|
|
45
|
+
instruction_event workspace_instruction_policy_activation_events%ROWTYPE;
|
|
46
|
+
preference_receipt company_brain_preference_proposal_receipts%ROWTYPE;
|
|
47
|
+
preference_row preference_registry_preferences%ROWTYPE;
|
|
48
|
+
preference_revision preference_registry_revisions%ROWTYPE;
|
|
49
|
+
preference_event preference_registry_events%ROWTYPE;
|
|
50
|
+
current_instruction_head workspace_instruction_policy_heads%ROWTYPE;
|
|
51
|
+
current_instruction_activation_version bigint;
|
|
52
|
+
input_hash_value text;
|
|
53
|
+
service_actor text;
|
|
54
|
+
current_authority_hash text;
|
|
55
|
+
current_mode text;
|
|
56
|
+
conflict_count integer;
|
|
57
|
+
destination_value text;
|
|
58
|
+
destination_proposal_id_value uuid;
|
|
59
|
+
destination_revision_id_value uuid;
|
|
60
|
+
destination_old_revision_id_value uuid;
|
|
61
|
+
destination_old_content_hash_value text;
|
|
62
|
+
destination_old_version_value bigint;
|
|
63
|
+
destination_new_content_hash_value text;
|
|
64
|
+
destination_new_version_value bigint;
|
|
65
|
+
destination_event_id_value uuid;
|
|
66
|
+
effective_at_value timestamptz;
|
|
67
|
+
review_operation_id uuid;
|
|
68
|
+
destination_operation_id uuid;
|
|
69
|
+
human_input_row session_human_input_requests%ROWTYPE;
|
|
70
|
+
human_question_id text;
|
|
71
|
+
human_question_lane text;
|
|
72
|
+
human_question_prompt text;
|
|
73
|
+
human_question_help text;
|
|
74
|
+
human_question_options jsonb := jsonb_build_array(
|
|
75
|
+
jsonb_build_object('id', 'save', 'label', 'Save'),
|
|
76
|
+
jsonb_build_object('id', 'skip', 'label', 'Don''t save')
|
|
77
|
+
);
|
|
78
|
+
BEGIN
|
|
79
|
+
IF p_account_id IS NULL OR p_workspace_id IS NULL OR p_operation_id IS NULL
|
|
80
|
+
OR p_decision_receipt_id IS NULL OR p_human_input_request_id IS NULL
|
|
81
|
+
OR caller_subject_id IS NULL
|
|
82
|
+
OR length(btrim(caller_subject_id)) NOT BETWEEN 1 AND 1024
|
|
83
|
+
OR p_account_id IS DISTINCT FROM opengeni_private.current_account_id()
|
|
84
|
+
OR p_workspace_id IS DISTINCT FROM opengeni_private.current_workspace_id()
|
|
85
|
+
THEN
|
|
86
|
+
RAISE EXCEPTION 'governed-learning activation requires exact tenant authority'
|
|
87
|
+
USING ERRCODE = '42501';
|
|
88
|
+
END IF;
|
|
89
|
+
input_hash_value := encode(sha256(convert_to(jsonb_build_array(
|
|
90
|
+
'governed-learning-human-confirmed-activation', 1, p_account_id, p_workspace_id,
|
|
91
|
+
p_operation_id, p_decision_receipt_id, p_human_input_request_id
|
|
92
|
+
)::text, 'UTF8')), 'hex');
|
|
93
|
+
SELECT * INTO result_row FROM governed_learning_activation_receipts receipt
|
|
94
|
+
WHERE receipt.workspace_id = p_workspace_id AND receipt.operation_id = p_operation_id
|
|
95
|
+
FOR SHARE;
|
|
96
|
+
IF FOUND THEN
|
|
97
|
+
IF result_row.account_id <> p_account_id OR result_row.input_hash <> input_hash_value
|
|
98
|
+
OR result_row.decision_receipt_id <> p_decision_receipt_id
|
|
99
|
+
OR result_row.initiating_human_subject_id <> caller_subject_id
|
|
100
|
+
OR result_row.authority_kind <> 'human_confirmed'
|
|
101
|
+
OR result_row.human_input_request_id IS DISTINCT FROM p_human_input_request_id
|
|
102
|
+
THEN
|
|
103
|
+
RAISE EXCEPTION 'governed-learning activation operation conflicts with immutable receipt'
|
|
104
|
+
USING ERRCODE = '23505';
|
|
105
|
+
END IF;
|
|
106
|
+
RETURN NEXT result_row;
|
|
107
|
+
RETURN;
|
|
108
|
+
END IF;
|
|
109
|
+
|
|
110
|
+
-- Canonical shared prefix. The workspace UPDATE lock also gives all three
|
|
111
|
+
-- destination adapters one order, avoiding claim/head inversion.
|
|
112
|
+
PERFORM 1 FROM workspaces workspace
|
|
113
|
+
WHERE workspace.account_id = p_account_id AND workspace.id = p_workspace_id
|
|
114
|
+
FOR UPDATE;
|
|
115
|
+
IF NOT FOUND THEN
|
|
116
|
+
RAISE EXCEPTION 'governed-learning workspace is unavailable' USING ERRCODE = '42501';
|
|
117
|
+
END IF;
|
|
118
|
+
SELECT * INTO result_row FROM governed_learning_activation_receipts receipt
|
|
119
|
+
WHERE receipt.workspace_id = p_workspace_id AND receipt.operation_id = p_operation_id
|
|
120
|
+
FOR SHARE;
|
|
121
|
+
IF FOUND THEN
|
|
122
|
+
IF result_row.account_id <> p_account_id OR result_row.input_hash <> input_hash_value
|
|
123
|
+
OR result_row.decision_receipt_id <> p_decision_receipt_id
|
|
124
|
+
OR result_row.initiating_human_subject_id <> caller_subject_id
|
|
125
|
+
OR result_row.authority_kind <> 'human_confirmed'
|
|
126
|
+
OR result_row.human_input_request_id IS DISTINCT FROM p_human_input_request_id
|
|
127
|
+
THEN
|
|
128
|
+
RAISE EXCEPTION 'governed-learning activation operation conflicts with immutable receipt'
|
|
129
|
+
USING ERRCODE = '23505';
|
|
130
|
+
END IF;
|
|
131
|
+
RETURN NEXT result_row;
|
|
132
|
+
RETURN;
|
|
133
|
+
END IF;
|
|
134
|
+
SELECT * INTO decision_row FROM governed_learning_decision_receipts receipt
|
|
135
|
+
WHERE receipt.id = p_decision_receipt_id AND receipt.account_id = p_account_id
|
|
136
|
+
AND receipt.workspace_id = p_workspace_id
|
|
137
|
+
AND receipt.initiating_human_subject_id = caller_subject_id
|
|
138
|
+
AND receipt.outcome IN ('suggest', 'automatic', 'confidence')
|
|
139
|
+
FOR UPDATE;
|
|
140
|
+
IF NOT FOUND OR NOT session_reference_visible(
|
|
141
|
+
decision_row.account_id, decision_row.workspace_id, decision_row.session_id
|
|
142
|
+
) THEN
|
|
143
|
+
RAISE EXCEPTION 'confirmable evaluator receipt is unavailable'
|
|
144
|
+
USING ERRCODE = '42501';
|
|
145
|
+
END IF;
|
|
146
|
+
IF EXISTS (
|
|
147
|
+
SELECT 1 FROM governed_learning_activation_receipts receipt
|
|
148
|
+
WHERE receipt.decision_receipt_id = p_decision_receipt_id
|
|
149
|
+
) THEN
|
|
150
|
+
RAISE EXCEPTION 'automatic evaluator receipt was consumed by another operation'
|
|
151
|
+
USING ERRCODE = '23505';
|
|
152
|
+
END IF;
|
|
153
|
+
PERFORM 1 FROM sessions session
|
|
154
|
+
WHERE session.account_id = p_account_id AND session.workspace_id = p_workspace_id
|
|
155
|
+
AND session.id = decision_row.session_id AND session.active_turn_id = decision_row.turn_id
|
|
156
|
+
AND session_reference_visible(session.account_id, session.workspace_id, session.id)
|
|
157
|
+
FOR SHARE;
|
|
158
|
+
IF NOT FOUND THEN
|
|
159
|
+
RAISE EXCEPTION 'governed-learning activation session is unavailable' USING ERRCODE = '42501';
|
|
160
|
+
END IF;
|
|
161
|
+
-- The decision receipt was minted before the human-input pause, so the
|
|
162
|
+
-- turn's active attempt is normally a later attempt of the same logical
|
|
163
|
+
-- turn and execution generation. Require that live attempt instead of the
|
|
164
|
+
-- exact minting attempt; the human answer is bound to the same generation.
|
|
165
|
+
PERFORM 1 FROM session_turns turn
|
|
166
|
+
WHERE turn.account_id = p_account_id AND turn.workspace_id = p_workspace_id
|
|
167
|
+
AND turn.session_id = decision_row.session_id AND turn.id = decision_row.turn_id
|
|
168
|
+
AND turn.execution_generation = decision_row.execution_generation
|
|
169
|
+
AND turn.status IN ('running', 'requires_action', 'recovering', 'waiting_capacity')
|
|
170
|
+
AND coalesce(turn.initiating_human_subject_id,
|
|
171
|
+
CASE WHEN turn.initiator_kind = 'subject' THEN turn.initiator_subject_id END
|
|
172
|
+
) = caller_subject_id
|
|
173
|
+
FOR SHARE;
|
|
174
|
+
IF NOT FOUND THEN
|
|
175
|
+
RAISE EXCEPTION 'governed-learning activation turn is unavailable' USING ERRCODE = '42501';
|
|
176
|
+
END IF;
|
|
177
|
+
PERFORM 1 FROM session_turns turn
|
|
178
|
+
JOIN session_turn_attempts attempt
|
|
179
|
+
ON attempt.account_id = turn.account_id AND attempt.workspace_id = turn.workspace_id
|
|
180
|
+
AND attempt.session_id = turn.session_id AND attempt.turn_id = turn.id
|
|
181
|
+
AND attempt.id = turn.active_attempt_id
|
|
182
|
+
WHERE turn.account_id = p_account_id AND turn.workspace_id = p_workspace_id
|
|
183
|
+
AND turn.session_id = decision_row.session_id AND turn.id = decision_row.turn_id
|
|
184
|
+
AND attempt.execution_generation = decision_row.execution_generation
|
|
185
|
+
AND attempt.state IN ('claimed', 'running')
|
|
186
|
+
AND NOT EXISTS (
|
|
187
|
+
SELECT 1 FROM session_attempt_interruptions interruption
|
|
188
|
+
WHERE interruption.workspace_id = p_workspace_id
|
|
189
|
+
AND interruption.attempt_id = attempt.id
|
|
190
|
+
AND interruption.state IN ('pending', 'delivered', 'acknowledged')
|
|
191
|
+
)
|
|
192
|
+
FOR SHARE OF attempt;
|
|
193
|
+
IF NOT FOUND THEN
|
|
194
|
+
RAISE EXCEPTION 'governed-learning activation attempt is unavailable' USING ERRCODE = '42501';
|
|
195
|
+
END IF;
|
|
196
|
+
|
|
197
|
+
SELECT * INTO policy_snapshot FROM workspace_learning_policy_snapshots snapshot
|
|
198
|
+
WHERE snapshot.id = decision_row.policy_snapshot_id
|
|
199
|
+
AND snapshot.account_id = p_account_id AND snapshot.workspace_id = p_workspace_id
|
|
200
|
+
AND snapshot.session_id = decision_row.session_id
|
|
201
|
+
AND snapshot.turn_id = decision_row.turn_id
|
|
202
|
+
AND snapshot.attempt_id = decision_row.attempt_id
|
|
203
|
+
AND snapshot.execution_generation = decision_row.execution_generation
|
|
204
|
+
AND snapshot.snapshot_hash = decision_row.policy_snapshot_hash
|
|
205
|
+
FOR SHARE;
|
|
206
|
+
SELECT * INTO policy_head FROM workspace_learning_policy_heads head
|
|
207
|
+
WHERE head.account_id = p_account_id AND head.workspace_id = p_workspace_id
|
|
208
|
+
FOR SHARE;
|
|
209
|
+
SELECT * INTO policy_revision FROM workspace_learning_policy_revisions revision
|
|
210
|
+
WHERE revision.account_id = p_account_id AND revision.workspace_id = p_workspace_id
|
|
211
|
+
AND revision.id = policy_head.revision_id
|
|
212
|
+
FOR SHARE;
|
|
213
|
+
IF policy_snapshot.id IS NULL
|
|
214
|
+
OR policy_head.revision_id IS DISTINCT FROM decision_row.policy_revision_id
|
|
215
|
+
OR policy_head.policy_hash IS DISTINCT FROM policy_snapshot.policy_hash
|
|
216
|
+
OR policy_head.activation_version IS DISTINCT FROM decision_row.policy_activation_version
|
|
217
|
+
OR policy_revision.policy_hash IS DISTINCT FROM policy_snapshot.policy_hash
|
|
218
|
+
THEN
|
|
219
|
+
RAISE EXCEPTION 'governed-learning policy authority changed after evaluation'
|
|
220
|
+
USING ERRCODE = '40001';
|
|
221
|
+
END IF;
|
|
222
|
+
current_mode := coalesce((
|
|
223
|
+
SELECT override.value->>'mode'
|
|
224
|
+
FROM jsonb_array_elements(policy_revision.source_overrides) override(value)
|
|
225
|
+
WHERE override.value->>'kind' = decision_row.source_kind
|
|
226
|
+
AND override.value->>'id' = decision_row.source_id::text LIMIT 1
|
|
227
|
+
), policy_revision.workspace_mode);
|
|
228
|
+
IF current_mode = 'off' THEN
|
|
229
|
+
RAISE EXCEPTION 'governed-learning source is off' USING ERRCODE = '42501';
|
|
230
|
+
END IF;
|
|
231
|
+
|
|
232
|
+
SELECT * INTO proposal_row FROM knowledge_change_proposals proposal
|
|
233
|
+
WHERE proposal.id = decision_row.proposal_id AND proposal.account_id = p_account_id
|
|
234
|
+
AND proposal.scope_kind = 'workspace' AND proposal.scope_workspace_id = p_workspace_id
|
|
235
|
+
AND proposal.scope_subject_id IS NULL AND proposal.status = 'proposed'
|
|
236
|
+
AND proposal.input_hash = decision_row.proposal_input_hash
|
|
237
|
+
AND proposal.content_hash = decision_row.proposal_content_hash
|
|
238
|
+
AND proposal.claim_id = decision_row.claim_id
|
|
239
|
+
AND proposal.evidence_id = decision_row.evidence_id
|
|
240
|
+
AND proposal.initiating_human_subject_id = caller_subject_id
|
|
241
|
+
FOR SHARE;
|
|
242
|
+
SELECT * INTO claim_row FROM knowledge_claims claim
|
|
243
|
+
WHERE claim.id = decision_row.claim_id AND claim.account_id = p_account_id
|
|
244
|
+
AND claim.scope_kind = 'workspace' AND claim.scope_workspace_id = p_workspace_id
|
|
245
|
+
AND claim.scope_subject_id IS NULL AND claim.input_hash = decision_row.claim_input_hash
|
|
246
|
+
AND claim.initiating_human_subject_id = caller_subject_id
|
|
247
|
+
AND claim.confidence_bps >= decision_row.confidence_floor_bps
|
|
248
|
+
AND claim.effective_at <= transaction_timestamp()
|
|
249
|
+
AND (claim.expires_at IS NULL OR claim.expires_at > transaction_timestamp())
|
|
250
|
+
FOR UPDATE;
|
|
251
|
+
SELECT * INTO evidence_row FROM knowledge_claim_evidence evidence
|
|
252
|
+
WHERE evidence.id = decision_row.evidence_id AND evidence.account_id = p_account_id
|
|
253
|
+
AND evidence.scope_kind = 'workspace' AND evidence.scope_workspace_id = p_workspace_id
|
|
254
|
+
AND evidence.scope_subject_id IS NULL AND evidence.claim_id = decision_row.claim_id
|
|
255
|
+
AND evidence.polarity = 'supports' AND evidence.input_hash = decision_row.evidence_input_hash
|
|
256
|
+
AND evidence.content_hash = decision_row.evidence_content_hash
|
|
257
|
+
AND evidence.initiating_human_subject_id = caller_subject_id
|
|
258
|
+
FOR SHARE;
|
|
259
|
+
IF proposal_row.id IS NULL OR claim_row.id IS NULL OR evidence_row.id IS NULL THEN
|
|
260
|
+
RAISE EXCEPTION 'governed-learning proposal lineage changed after evaluation'
|
|
261
|
+
USING ERRCODE = '42501';
|
|
262
|
+
END IF;
|
|
263
|
+
SELECT * INTO review_row FROM knowledge_claim_reviews review
|
|
264
|
+
WHERE review.account_id = p_account_id AND review.claim_id = decision_row.claim_id
|
|
265
|
+
ORDER BY review.review_revision DESC LIMIT 1 FOR SHARE;
|
|
266
|
+
IF review_row.id IS NULL OR review_row.state <> 'proposed'
|
|
267
|
+
OR review_row.review_revision <> decision_row.review_revision
|
|
268
|
+
THEN
|
|
269
|
+
RAISE EXCEPTION 'governed-learning review was superseded after evaluation'
|
|
270
|
+
USING ERRCODE = '40001';
|
|
271
|
+
END IF;
|
|
272
|
+
SELECT least(1000, count(*))::integer INTO conflict_count FROM (
|
|
273
|
+
SELECT relation.id FROM knowledge_claim_relations relation
|
|
274
|
+
WHERE relation.account_id = p_account_id AND relation.scope_kind = 'workspace'
|
|
275
|
+
AND relation.scope_workspace_id = p_workspace_id AND relation.scope_subject_id IS NULL
|
|
276
|
+
AND relation.relation_type = 'conflicts_with'
|
|
277
|
+
AND (relation.from_claim_id = decision_row.claim_id
|
|
278
|
+
OR relation.to_claim_id = decision_row.claim_id)
|
|
279
|
+
UNION ALL
|
|
280
|
+
SELECT contradiction.id FROM knowledge_claim_evidence contradiction
|
|
281
|
+
WHERE contradiction.account_id = p_account_id AND contradiction.scope_kind = 'workspace'
|
|
282
|
+
AND contradiction.scope_workspace_id = p_workspace_id
|
|
283
|
+
AND contradiction.scope_subject_id IS NULL
|
|
284
|
+
AND contradiction.claim_id = decision_row.claim_id
|
|
285
|
+
AND contradiction.polarity = 'contradicts'
|
|
286
|
+
) conflicts;
|
|
287
|
+
IF conflict_count <> 0 THEN
|
|
288
|
+
RAISE EXCEPTION 'governed-learning evidence now conflicts' USING ERRCODE = '23514';
|
|
289
|
+
END IF;
|
|
290
|
+
|
|
291
|
+
IF evidence_row.task_note_id IS NOT NULL THEN
|
|
292
|
+
SELECT * INTO task_note_row FROM task_notes note
|
|
293
|
+
WHERE note.id = evidence_row.task_note_id AND note.account_id = p_account_id
|
|
294
|
+
AND note.workspace_id = p_workspace_id
|
|
295
|
+
AND note.root_session_id = evidence_row.task_note_root_session_id
|
|
296
|
+
AND note.version = evidence_row.task_note_version
|
|
297
|
+
AND note.text_hash = evidence_row.content_hash
|
|
298
|
+
AND note.status = 'active' AND note.expires_at > transaction_timestamp()
|
|
299
|
+
AND session_reference_visible(note.account_id, note.workspace_id, note.root_session_id)
|
|
300
|
+
FOR SHARE;
|
|
301
|
+
IF NOT FOUND THEN
|
|
302
|
+
RAISE EXCEPTION 'governed-learning Task-note authority was revoked' USING ERRCODE = '42501';
|
|
303
|
+
END IF;
|
|
304
|
+
current_authority_hash := encode(sha256(convert_to(jsonb_build_array(
|
|
305
|
+
'task-note-authority', 1, evidence_row.task_note_id,
|
|
306
|
+
evidence_row.task_note_root_session_id, evidence_row.task_note_version,
|
|
307
|
+
evidence_row.content_hash, task_note_row.status, task_note_row.expires_at
|
|
308
|
+
)::text, 'UTF8')), 'hex');
|
|
309
|
+
ELSE
|
|
310
|
+
SELECT provider.id AS provider_id, provider.lifecycle_state AS provider_state,
|
|
311
|
+
provider.lifecycle_generation AS provider_generation, source.id AS source_id,
|
|
312
|
+
source.lifecycle_state AS source_state, source.lifecycle_generation AS source_generation,
|
|
313
|
+
source.current_acl_generation, object.id AS object_id,
|
|
314
|
+
object.lifecycle_state AS object_state, object.lifecycle_generation AS object_generation,
|
|
315
|
+
object.version_generation AS object_version_generation, object.current_version_id,
|
|
316
|
+
version.id AS version_id, version.version_generation, version.content_sha256,
|
|
317
|
+
version.acl_version_id, version.acl_generation, version.document_id,
|
|
318
|
+
evidence_acl.acl_hash AS evidence_acl_hash,
|
|
319
|
+
evidence_acl.agent_access AS evidence_agent_access,
|
|
320
|
+
current_acl.id AS current_acl_id, current_acl.acl_hash AS current_acl_hash,
|
|
321
|
+
current_acl.agent_access AS current_agent_access
|
|
322
|
+
INTO document_authority
|
|
323
|
+
FROM knowledge_document_versions version
|
|
324
|
+
JOIN knowledge_source_objects object ON object.account_id = version.account_id
|
|
325
|
+
AND object.id = version.object_id AND object.scope_key = version.scope_key
|
|
326
|
+
JOIN knowledge_sources source ON source.account_id = version.account_id
|
|
327
|
+
AND source.id = version.source_id AND source.scope_key = version.scope_key
|
|
328
|
+
JOIN knowledge_providers provider ON provider.account_id = source.account_id
|
|
329
|
+
AND provider.id = source.provider_id AND provider.scope_key = source.scope_key
|
|
330
|
+
JOIN knowledge_source_acl_versions evidence_acl ON evidence_acl.account_id = version.account_id
|
|
331
|
+
AND evidence_acl.id = version.acl_version_id AND evidence_acl.source_id = version.source_id
|
|
332
|
+
AND evidence_acl.generation = version.acl_generation
|
|
333
|
+
LEFT JOIN knowledge_source_acl_versions current_acl ON current_acl.account_id = source.account_id
|
|
334
|
+
AND current_acl.source_id = source.id AND current_acl.generation = source.current_acl_generation
|
|
335
|
+
WHERE version.id = evidence_row.document_version_id AND version.account_id = p_account_id
|
|
336
|
+
AND version.scope_kind = 'workspace' AND version.scope_workspace_id = p_workspace_id
|
|
337
|
+
AND version.scope_subject_id IS NULL
|
|
338
|
+
FOR SHARE OF version, object, source, provider, evidence_acl;
|
|
339
|
+
IF document_authority.version_id IS NULL
|
|
340
|
+
OR document_authority.provider_state IS DISTINCT FROM 'active'
|
|
341
|
+
OR document_authority.source_state IS DISTINCT FROM 'active'
|
|
342
|
+
OR document_authority.object_state IS DISTINCT FROM 'active'
|
|
343
|
+
OR document_authority.current_version_id IS DISTINCT FROM document_authority.version_id
|
|
344
|
+
OR document_authority.object_version_generation IS DISTINCT FROM document_authority.version_generation
|
|
345
|
+
OR document_authority.current_acl_id IS NULL
|
|
346
|
+
OR NOT coalesce(document_authority.evidence_agent_access, false)
|
|
347
|
+
OR NOT coalesce(document_authority.current_agent_access, false)
|
|
348
|
+
OR (document_authority.document_id IS NOT NULL AND NOT EXISTS (
|
|
349
|
+
SELECT 1 FROM documents document WHERE document.id = document_authority.document_id
|
|
350
|
+
AND document.account_id = p_account_id AND document.workspace_id = p_workspace_id
|
|
351
|
+
AND document.status = 'ready' AND document.agent_access
|
|
352
|
+
AND (document.visibility <> 'private' OR document.created_by = caller_subject_id)
|
|
353
|
+
))
|
|
354
|
+
OR (evidence_row.document_chunk_id IS NOT NULL AND NOT EXISTS (
|
|
355
|
+
SELECT 1 FROM document_chunks chunk WHERE chunk.id = evidence_row.document_chunk_id
|
|
356
|
+
AND chunk.account_id = p_account_id AND chunk.workspace_id = p_workspace_id
|
|
357
|
+
AND chunk.document_id = document_authority.document_id
|
|
358
|
+
))
|
|
359
|
+
THEN
|
|
360
|
+
RAISE EXCEPTION 'governed-learning document authority was revoked' USING ERRCODE = '42501';
|
|
361
|
+
END IF;
|
|
362
|
+
current_authority_hash := encode(sha256(convert_to(jsonb_build_array(
|
|
363
|
+
'document-evidence-authority', 1, evidence_row.document_version_id,
|
|
364
|
+
document_authority.provider_id, document_authority.provider_state,
|
|
365
|
+
document_authority.provider_generation, document_authority.source_id,
|
|
366
|
+
document_authority.source_state, document_authority.source_generation,
|
|
367
|
+
document_authority.current_acl_generation, document_authority.object_id,
|
|
368
|
+
document_authority.object_state, document_authority.object_generation,
|
|
369
|
+
document_authority.object_version_generation, document_authority.current_version_id,
|
|
370
|
+
document_authority.version_id, document_authority.version_generation,
|
|
371
|
+
document_authority.content_sha256, document_authority.acl_version_id,
|
|
372
|
+
document_authority.acl_generation, document_authority.evidence_acl_hash,
|
|
373
|
+
document_authority.current_acl_id, document_authority.current_acl_hash,
|
|
374
|
+
evidence_row.document_chunk_id, false
|
|
375
|
+
)::text, 'UTF8')), 'hex');
|
|
376
|
+
END IF;
|
|
377
|
+
|
|
378
|
+
-- The exact human must have answered the bound question for this proposal
|
|
379
|
+
-- on the same session/turn, in the same execution generation, with `save`.
|
|
380
|
+
-- The question the human saw is reconstructed here from the proposal itself
|
|
381
|
+
-- (canonical prompt, exact proposal content as help text, fixed options), so
|
|
382
|
+
-- an agent cannot obtain confirmation through a misleading prompt.
|
|
383
|
+
human_question_id := 'remember:' || decision_row.proposal_id::text;
|
|
384
|
+
-- Only Task-note evidence (the exact user-directed text) is confirmable; the
|
|
385
|
+
-- proposal content of a preference is a normalized envelope, not the words
|
|
386
|
+
-- the human was shown.
|
|
387
|
+
IF task_note_row.id IS NULL THEN
|
|
388
|
+
RAISE EXCEPTION 'human confirmation requires exact Task-note evidence' USING ERRCODE = '42501';
|
|
389
|
+
END IF;
|
|
390
|
+
human_question_lane := proposal_row.target_kind;
|
|
391
|
+
human_question_help := left(task_note_row.text, 2000);
|
|
392
|
+
human_question_prompt := 'Save this as a ' || CASE human_question_lane
|
|
393
|
+
WHEN 'instruction_policy' THEN 'mandatory workspace rule'
|
|
394
|
+
WHEN 'preference' THEN 'workspace preference'
|
|
395
|
+
ELSE 'workspace knowledge' END
|
|
396
|
+
|| ' for everyone in this workspace?';
|
|
397
|
+
SELECT * INTO human_input_row FROM session_human_input_requests request
|
|
398
|
+
WHERE request.id = p_human_input_request_id
|
|
399
|
+
AND request.account_id = p_account_id AND request.workspace_id = p_workspace_id
|
|
400
|
+
AND request.session_id = decision_row.session_id
|
|
401
|
+
AND request.turn_id = decision_row.turn_id
|
|
402
|
+
AND request.turn_generation = decision_row.execution_generation
|
|
403
|
+
AND request.status = 'answered'
|
|
404
|
+
AND request.responded_by = caller_subject_id
|
|
405
|
+
AND request.response->>'outcome' = 'answered'
|
|
406
|
+
AND EXISTS (
|
|
407
|
+
SELECT 1 FROM jsonb_array_elements(request.questions) question(value)
|
|
408
|
+
WHERE question.value->>'id' = human_question_id
|
|
409
|
+
AND question.value->>'kind' = 'single_select'
|
|
410
|
+
AND question.value->>'prompt' = human_question_prompt
|
|
411
|
+
AND question.value->>'helpText' = human_question_help
|
|
412
|
+
AND question.value->'options' = human_question_options
|
|
413
|
+
AND coalesce((question.value->>'allowOther')::boolean, false) = false
|
|
414
|
+
)
|
|
415
|
+
AND EXISTS (
|
|
416
|
+
SELECT 1 FROM jsonb_array_elements(request.response->'answers') answer(value)
|
|
417
|
+
WHERE answer.value->>'questionId' = human_question_id
|
|
418
|
+
AND answer.value->'values' = to_jsonb(ARRAY['save'])
|
|
419
|
+
)
|
|
420
|
+
FOR SHARE;
|
|
421
|
+
IF NOT FOUND THEN
|
|
422
|
+
RAISE EXCEPTION 'human confirmation for this proposal is unavailable'
|
|
423
|
+
USING ERRCODE = '42501';
|
|
424
|
+
END IF;
|
|
425
|
+
IF current_authority_hash IS DISTINCT FROM decision_row.evidence_authority_hash THEN
|
|
426
|
+
RAISE EXCEPTION 'governed-learning evidence authority changed after evaluation'
|
|
427
|
+
USING ERRCODE = '40001';
|
|
428
|
+
END IF;
|
|
429
|
+
|
|
430
|
+
IF proposal_row.target_kind = 'instruction_policy' THEN
|
|
431
|
+
SELECT * INTO instruction_proposal
|
|
432
|
+
FROM workspace_instruction_policy_onboarding_proposals proposal
|
|
433
|
+
WHERE proposal.account_id = p_account_id AND proposal.workspace_id = p_workspace_id
|
|
434
|
+
AND proposal.source_id = proposal_row.id::text
|
|
435
|
+
AND proposal.source_version = proposal_row.content_hash
|
|
436
|
+
AND proposal.status = 'proposed'
|
|
437
|
+
FOR SHARE;
|
|
438
|
+
IF NOT FOUND OR instruction_proposal.draft_content_hash <> proposal_row.content_hash THEN
|
|
439
|
+
RAISE EXCEPTION 'exact inactive instruction-policy proposal is unavailable'
|
|
440
|
+
USING ERRCODE = '42501';
|
|
441
|
+
END IF;
|
|
442
|
+
SELECT * INTO current_instruction_head FROM workspace_instruction_policy_heads head
|
|
443
|
+
WHERE head.account_id = p_account_id AND head.workspace_id = p_workspace_id
|
|
444
|
+
AND head.kind = instruction_proposal.kind AND head.scope = instruction_proposal.scope
|
|
445
|
+
AND head.role_key IS NOT DISTINCT FROM instruction_proposal.role_key FOR SHARE;
|
|
446
|
+
IF NOT FOUND THEN
|
|
447
|
+
SELECT event.activation_version INTO current_instruction_activation_version
|
|
448
|
+
FROM workspace_instruction_policy_deactivation_events event
|
|
449
|
+
WHERE event.account_id = p_account_id AND event.workspace_id = p_workspace_id
|
|
450
|
+
AND event.kind = instruction_proposal.kind AND event.scope = instruction_proposal.scope
|
|
451
|
+
AND event.role_key IS NOT DISTINCT FROM instruction_proposal.role_key
|
|
452
|
+
ORDER BY event.activation_version DESC, event.id DESC
|
|
453
|
+
LIMIT 1;
|
|
454
|
+
END IF;
|
|
455
|
+
current_instruction_activation_version := coalesce(
|
|
456
|
+
current_instruction_head.activation_version,
|
|
457
|
+
current_instruction_activation_version,
|
|
458
|
+
0
|
|
459
|
+
);
|
|
460
|
+
IF current_instruction_head.revision_id IS DISTINCT FROM instruction_proposal.baseline_revision_id
|
|
461
|
+
OR current_instruction_activation_version
|
|
462
|
+
IS DISTINCT FROM instruction_proposal.baseline_activation_version
|
|
463
|
+
THEN
|
|
464
|
+
RAISE EXCEPTION 'instruction-policy proposal baseline is stale' USING ERRCODE = '40001';
|
|
465
|
+
END IF;
|
|
466
|
+
destination_value := 'instruction_policy';
|
|
467
|
+
destination_proposal_id_value := instruction_proposal.id;
|
|
468
|
+
destination_revision_id_value := instruction_proposal.draft_revision_id;
|
|
469
|
+
destination_old_revision_id_value := instruction_proposal.baseline_revision_id;
|
|
470
|
+
destination_old_content_hash_value := instruction_proposal.baseline_content_hash;
|
|
471
|
+
destination_old_version_value := instruction_proposal.baseline_activation_version;
|
|
472
|
+
destination_new_content_hash_value := instruction_proposal.draft_content_hash;
|
|
473
|
+
ELSIF proposal_row.target_kind = 'preference' THEN
|
|
474
|
+
SELECT * INTO preference_receipt FROM company_brain_preference_proposal_receipts receipt
|
|
475
|
+
WHERE receipt.account_id = p_account_id AND receipt.workspace_id = p_workspace_id
|
|
476
|
+
AND receipt.knowledge_proposal_id = proposal_row.id FOR SHARE;
|
|
477
|
+
SELECT * INTO preference_row FROM preference_registry_preferences preference
|
|
478
|
+
WHERE preference.account_id = p_account_id AND preference.id = preference_receipt.preference_id
|
|
479
|
+
AND preference.scope = 'workspace' AND preference.scope_workspace_id = p_workspace_id
|
|
480
|
+
AND preference.scope_subject_id IS NULL AND preference.status = 'proposed'
|
|
481
|
+
AND preference.active_revision_id IS NULL AND preference.activation_version = 0
|
|
482
|
+
AND preference.scope_version = 1 FOR SHARE;
|
|
483
|
+
SELECT * INTO preference_revision FROM preference_registry_revisions revision
|
|
484
|
+
WHERE revision.account_id = p_account_id AND revision.id = preference_receipt.revision_id
|
|
485
|
+
AND revision.preference_id = preference_receipt.preference_id
|
|
486
|
+
AND revision.provenance_source = 'knowledge_proposal'
|
|
487
|
+
AND revision.provenance_source_id = proposal_row.id::text
|
|
488
|
+
AND revision.trust = 'untrusted_proposal'
|
|
489
|
+
AND (revision.expires_at IS NULL OR revision.expires_at > transaction_timestamp())
|
|
490
|
+
FOR SHARE;
|
|
491
|
+
IF preference_receipt.id IS NULL OR preference_row.id IS NULL OR preference_revision.id IS NULL
|
|
492
|
+
OR proposal_row.target_scope <> 'workspace'
|
|
493
|
+
OR proposal_row.target_key <> preference_row.stable_key
|
|
494
|
+
THEN
|
|
495
|
+
RAISE EXCEPTION 'exact inactive preference proposal is unavailable'
|
|
496
|
+
USING ERRCODE = '42501';
|
|
497
|
+
END IF;
|
|
498
|
+
destination_value := 'preference';
|
|
499
|
+
destination_proposal_id_value := preference_row.id;
|
|
500
|
+
destination_revision_id_value := preference_revision.id;
|
|
501
|
+
destination_old_revision_id_value := NULL;
|
|
502
|
+
destination_old_content_hash_value := NULL;
|
|
503
|
+
destination_old_version_value := 0;
|
|
504
|
+
destination_new_content_hash_value := preference_revision.content_hash;
|
|
505
|
+
ELSE
|
|
506
|
+
RAISE EXCEPTION 'unsupported governed-learning destination' USING ERRCODE = '23514';
|
|
507
|
+
END IF;
|
|
508
|
+
|
|
509
|
+
service_actor := 'service:governed-learning-activation:' || input_hash_value;
|
|
510
|
+
review_operation_id := opengeni_private.governed_learning_derived_uuid(
|
|
511
|
+
p_operation_id, 'knowledge-approve'
|
|
512
|
+
);
|
|
513
|
+
destination_operation_id := opengeni_private.governed_learning_derived_uuid(
|
|
514
|
+
p_operation_id, 'destination-activate'
|
|
515
|
+
);
|
|
516
|
+
SELECT * INTO approval_row FROM governed_learning_apply_knowledge_review(
|
|
517
|
+
p_account_id, p_workspace_id, review_operation_id, decision_row.claim_id,
|
|
518
|
+
review_row.id, review_row.review_revision, 'approved', service_actor, caller_subject_id
|
|
519
|
+
);
|
|
520
|
+
IF destination_value = 'instruction_policy' THEN
|
|
521
|
+
SELECT * INTO instruction_event FROM governed_learning_apply_instruction_policy(
|
|
522
|
+
'activate', p_account_id, p_workspace_id, destination_operation_id,
|
|
523
|
+
destination_proposal_id_value, destination_old_revision_id_value,
|
|
524
|
+
destination_old_version_value, destination_revision_id_value, service_actor
|
|
525
|
+
);
|
|
526
|
+
destination_new_version_value := instruction_event.activation_version;
|
|
527
|
+
destination_event_id_value := instruction_event.id;
|
|
528
|
+
effective_at_value := instruction_event.created_at;
|
|
529
|
+
ELSE
|
|
530
|
+
SELECT * INTO preference_event FROM governed_learning_apply_preference(
|
|
531
|
+
'activate', p_account_id, p_workspace_id, destination_operation_id,
|
|
532
|
+
proposal_row.id, destination_proposal_id_value, destination_revision_id_value,
|
|
533
|
+
NULL, 0, service_actor
|
|
534
|
+
);
|
|
535
|
+
SELECT activation_version INTO destination_new_version_value
|
|
536
|
+
FROM preference_registry_preferences WHERE id = destination_proposal_id_value;
|
|
537
|
+
destination_event_id_value := preference_event.id;
|
|
538
|
+
effective_at_value := preference_event.created_at;
|
|
539
|
+
END IF;
|
|
540
|
+
|
|
541
|
+
INSERT INTO governed_learning_activation_receipts (
|
|
542
|
+
account_id, workspace_id, operation_id, input_hash, decision_receipt_id,
|
|
543
|
+
initiating_human_subject_id, service_actor_subject_id, policy_revision_id,
|
|
544
|
+
policy_hash, policy_activation_version, source_kind, source_id,
|
|
545
|
+
source_authority_hash, proposal_id, claim_id, evidence_id,
|
|
546
|
+
knowledge_previous_review_id, knowledge_previous_review_revision,
|
|
547
|
+
knowledge_approval_review_id, knowledge_approval_review_revision,
|
|
548
|
+
knowledge_approval_input_hash, destination, destination_proposal_id,
|
|
549
|
+
destination_revision_id, destination_old_revision_id,
|
|
550
|
+
destination_old_content_hash, destination_old_version,
|
|
551
|
+
destination_new_content_hash, destination_new_version, destination_event_id,
|
|
552
|
+
effective_at, authority_kind, human_input_request_id
|
|
553
|
+
) VALUES (
|
|
554
|
+
p_account_id, p_workspace_id, p_operation_id, input_hash_value,
|
|
555
|
+
p_decision_receipt_id, caller_subject_id, service_actor, policy_revision.id,
|
|
556
|
+
policy_revision.policy_hash, policy_head.activation_version,
|
|
557
|
+
decision_row.source_kind, decision_row.source_id, current_authority_hash,
|
|
558
|
+
decision_row.proposal_id, decision_row.claim_id, decision_row.evidence_id,
|
|
559
|
+
review_row.id, review_row.review_revision, approval_row.id,
|
|
560
|
+
approval_row.review_revision, approval_row.input_hash, destination_value,
|
|
561
|
+
destination_proposal_id_value, destination_revision_id_value,
|
|
562
|
+
destination_old_revision_id_value, destination_old_content_hash_value,
|
|
563
|
+
destination_old_version_value, destination_new_content_hash_value,
|
|
564
|
+
destination_new_version_value, destination_event_id_value, effective_at_value,
|
|
565
|
+
'human_confirmed', p_human_input_request_id
|
|
566
|
+
) RETURNING * INTO result_row;
|
|
567
|
+
RETURN NEXT result_row;
|
|
568
|
+
END;
|
|
569
|
+
$$;
|
|
570
|
+
REVOKE ALL ON FUNCTION activate_human_confirmed_learning_decision(uuid, uuid, uuid, uuid, uuid) FROM PUBLIC;
|
|
571
|
+
|
|
572
|
+
DO $governed_learning_human_confirmed_hardening$
|
|
573
|
+
DECLARE
|
|
574
|
+
target_schema text := current_schema();
|
|
575
|
+
runtime_role text := nullif(current_setting('opengeni.runtime_role', true), '');
|
|
576
|
+
BEGIN
|
|
577
|
+
IF runtime_role IS NULL AND EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'opengeni_app') THEN
|
|
578
|
+
runtime_role := 'opengeni_app';
|
|
579
|
+
END IF;
|
|
580
|
+
EXECUTE format(
|
|
581
|
+
'ALTER FUNCTION %I.activate_human_confirmed_learning_decision(uuid,uuid,uuid,uuid,uuid) '
|
|
582
|
+
|| 'SET search_path = pg_catalog, %I, pg_temp', target_schema, target_schema
|
|
583
|
+
);
|
|
584
|
+
IF runtime_role IS NOT NULL THEN
|
|
585
|
+
EXECUTE format(
|
|
586
|
+
'GRANT EXECUTE ON FUNCTION %I.activate_human_confirmed_learning_decision(uuid,uuid,uuid,uuid,uuid) TO %I',
|
|
587
|
+
target_schema, runtime_role
|
|
588
|
+
);
|
|
589
|
+
END IF;
|
|
590
|
+
END
|
|
591
|
+
$governed_learning_human_confirmed_hardening$;
|