@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.
- 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-LTG3S6ZY.js → chunk-EULAN2NP.js} +2 -2
- package/dist/{chunk-CUPM3FGB.js → chunk-NJVZPWXY.js} +3 -3
- package/dist/{chunk-4SR4HKKA.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-6ZYKCRFO.js → chunk-PR2WMLOQ.js} +7580 -4417
- package/dist/chunk-PR2WMLOQ.js.map +1 -0
- package/dist/{chunk-ZWREYEWA.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.js +2 -2
- 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 +1595 -81
- package/dist/index.js +21571 -8126
- 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/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 +22 -8
- package/dist/schema.d.ts +24914 -13703
- package/dist/schema.js +99 -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 +41 -4
- package/dist/session-realtime-ledger.d.ts +2 -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/0241_atomic_personal_resource_delegation.sql +1000 -0
- package/drizzle/0241_enrollment_agent_runtime.sql +70 -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-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 +11651 -1548
- 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/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 +877 -6
- package/src/runtime-posture-cli.ts +2 -0
- package/src/runtime-posture.ts +1291 -76
- package/src/schema.ts +3303 -428
- 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 +437 -84
- package/src/session-realtime-context.ts +3 -3
- package/src/session-realtime-ledger.ts +22 -11
- 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-4SR4HKKA.js.map +0 -1
- package/dist/chunk-6ZYKCRFO.js.map +0 -1
- package/dist/chunk-NXAFLCQX.js +0 -2468
- package/dist/chunk-NXAFLCQX.js.map +0 -1
- package/dist/chunk-SK7YYHBI.js.map +0 -1
- package/dist/chunk-WD5ROSLL.js +0 -117
- package/dist/chunk-WD5ROSLL.js.map +0 -1
- package/dist/chunk-ZWREYEWA.js.map +0 -1
- /package/dist/{chunk-LTG3S6ZY.js.map → chunk-EULAN2NP.js.map} +0 -0
- /package/dist/{chunk-CUPM3FGB.js.map → chunk-NJVZPWXY.js.map} +0 -0
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
-- deployment-mode: rolling
|
|
2
|
+
SET lock_timeout = '5s';
|
|
3
|
+
SET statement_timeout = '10min';
|
|
4
|
+
|
|
5
|
+
-- Confirm-time rebaseline for user-directed rules.
|
|
6
|
+
--
|
|
7
|
+
-- When the instruction-policy head moved between proposing a rule and the
|
|
8
|
+
-- human confirming it, activation failed with a stale baseline after the human
|
|
9
|
+
-- had already answered "save", with no way forward but re-asking them. The
|
|
10
|
+
-- confirm path now rebaselines instead: it adds a successor proposal for the
|
|
11
|
+
-- SAME knowledge proposal against the current head and retries. The human's
|
|
12
|
+
-- confirmation stays bound to `remember:<knowledgeProposalId>`, which does not
|
|
13
|
+
-- change, so the exact-binding guarantee in 0272/0274 is untouched.
|
|
14
|
+
--
|
|
15
|
+
-- That makes more than one inactive instruction-policy proposal per knowledge
|
|
16
|
+
-- proposal legitimate, so the uniqueness key moves from "one per source" to
|
|
17
|
+
-- "one per source per baseline" - a rebaselined successor is admissible, a
|
|
18
|
+
-- duplicate against the same baseline still is not.
|
|
19
|
+
|
|
20
|
+
DROP INDEX "workspace_instruction_policy_onboarding_proposals_source_versio";
|
|
21
|
+
CREATE UNIQUE INDEX "workspace_instruction_policy_onboarding_proposals_source_versio"
|
|
22
|
+
ON "workspace_instruction_policy_onboarding_proposals" (
|
|
23
|
+
"workspace_id", "kind", "scope", COALESCE("role_key", ''),
|
|
24
|
+
"source_id", "source_version", "baseline_activation_version"
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
-- Body is byte-identical to migration 0284 except the instruction-policy
|
|
28
|
+
-- branch, which now resolves the live head before selecting the proposal so
|
|
29
|
+
-- multiple candidates are disambiguated deterministically.
|
|
30
|
+
CREATE OR REPLACE FUNCTION activate_human_confirmed_learning_decision(
|
|
31
|
+
p_account_id uuid,
|
|
32
|
+
p_workspace_id uuid,
|
|
33
|
+
p_operation_id uuid,
|
|
34
|
+
p_decision_receipt_id uuid,
|
|
35
|
+
p_human_input_request_id uuid
|
|
36
|
+
) RETURNS SETOF governed_learning_activation_receipts
|
|
37
|
+
LANGUAGE plpgsql SECURITY DEFINER AS $$
|
|
38
|
+
DECLARE
|
|
39
|
+
caller_subject_id text := nullif(current_setting('opengeni.subject_id', true), '');
|
|
40
|
+
decision_row governed_learning_decision_receipts%ROWTYPE;
|
|
41
|
+
result_row governed_learning_activation_receipts%ROWTYPE;
|
|
42
|
+
proposal_row knowledge_change_proposals%ROWTYPE;
|
|
43
|
+
claim_row knowledge_claims%ROWTYPE;
|
|
44
|
+
evidence_row knowledge_claim_evidence%ROWTYPE;
|
|
45
|
+
review_row knowledge_claim_reviews%ROWTYPE;
|
|
46
|
+
approval_row knowledge_claim_reviews%ROWTYPE;
|
|
47
|
+
policy_head workspace_learning_policy_heads%ROWTYPE;
|
|
48
|
+
policy_revision workspace_learning_policy_revisions%ROWTYPE;
|
|
49
|
+
policy_snapshot workspace_learning_policy_snapshots%ROWTYPE;
|
|
50
|
+
task_note_row task_notes%ROWTYPE;
|
|
51
|
+
document_authority record;
|
|
52
|
+
instruction_proposal workspace_instruction_policy_onboarding_proposals%ROWTYPE;
|
|
53
|
+
instruction_event workspace_instruction_policy_activation_events%ROWTYPE;
|
|
54
|
+
preference_receipt company_brain_preference_proposal_receipts%ROWTYPE;
|
|
55
|
+
preference_row preference_registry_preferences%ROWTYPE;
|
|
56
|
+
preference_revision preference_registry_revisions%ROWTYPE;
|
|
57
|
+
preference_event preference_registry_events%ROWTYPE;
|
|
58
|
+
current_instruction_head workspace_instruction_policy_heads%ROWTYPE;
|
|
59
|
+
current_instruction_activation_version bigint;
|
|
60
|
+
input_hash_value text;
|
|
61
|
+
service_actor text;
|
|
62
|
+
current_authority_hash text;
|
|
63
|
+
current_mode text;
|
|
64
|
+
conflict_count integer;
|
|
65
|
+
destination_value text;
|
|
66
|
+
destination_proposal_id_value uuid;
|
|
67
|
+
destination_revision_id_value uuid;
|
|
68
|
+
destination_old_revision_id_value uuid;
|
|
69
|
+
destination_old_content_hash_value text;
|
|
70
|
+
destination_old_version_value bigint;
|
|
71
|
+
destination_new_content_hash_value text;
|
|
72
|
+
destination_new_version_value bigint;
|
|
73
|
+
destination_event_id_value uuid;
|
|
74
|
+
effective_at_value timestamptz;
|
|
75
|
+
review_operation_id uuid;
|
|
76
|
+
destination_operation_id uuid;
|
|
77
|
+
human_input_row session_human_input_requests%ROWTYPE;
|
|
78
|
+
human_question_id text;
|
|
79
|
+
human_question_lane text;
|
|
80
|
+
human_question_prompt text;
|
|
81
|
+
human_question_help text;
|
|
82
|
+
human_question_options jsonb := jsonb_build_array(
|
|
83
|
+
jsonb_build_object('id', 'save', 'label', 'Save'),
|
|
84
|
+
jsonb_build_object('id', 'skip', 'label', 'Don''t save')
|
|
85
|
+
);
|
|
86
|
+
BEGIN
|
|
87
|
+
IF p_account_id IS NULL OR p_workspace_id IS NULL OR p_operation_id IS NULL
|
|
88
|
+
OR p_decision_receipt_id IS NULL OR p_human_input_request_id IS NULL
|
|
89
|
+
OR caller_subject_id IS NULL
|
|
90
|
+
OR length(btrim(caller_subject_id)) NOT BETWEEN 1 AND 1024
|
|
91
|
+
OR p_account_id IS DISTINCT FROM opengeni_private.current_account_id()
|
|
92
|
+
OR p_workspace_id IS DISTINCT FROM opengeni_private.current_workspace_id()
|
|
93
|
+
THEN
|
|
94
|
+
RAISE EXCEPTION 'governed-learning activation requires exact tenant authority'
|
|
95
|
+
USING ERRCODE = '42501';
|
|
96
|
+
END IF;
|
|
97
|
+
input_hash_value := encode(sha256(convert_to(jsonb_build_array(
|
|
98
|
+
'governed-learning-human-confirmed-activation', 1, p_account_id, p_workspace_id,
|
|
99
|
+
p_operation_id, p_decision_receipt_id, p_human_input_request_id
|
|
100
|
+
)::text, 'UTF8')), 'hex');
|
|
101
|
+
SELECT * INTO result_row FROM governed_learning_activation_receipts receipt
|
|
102
|
+
WHERE receipt.workspace_id = p_workspace_id AND receipt.operation_id = p_operation_id
|
|
103
|
+
FOR SHARE;
|
|
104
|
+
IF FOUND THEN
|
|
105
|
+
IF result_row.account_id <> p_account_id OR result_row.input_hash <> input_hash_value
|
|
106
|
+
OR result_row.decision_receipt_id <> p_decision_receipt_id
|
|
107
|
+
OR result_row.initiating_human_subject_id <> caller_subject_id
|
|
108
|
+
OR result_row.authority_kind <> 'human_confirmed'
|
|
109
|
+
OR result_row.human_input_request_id IS DISTINCT FROM p_human_input_request_id
|
|
110
|
+
THEN
|
|
111
|
+
RAISE EXCEPTION 'governed-learning activation operation conflicts with immutable receipt'
|
|
112
|
+
USING ERRCODE = '23505';
|
|
113
|
+
END IF;
|
|
114
|
+
RETURN NEXT result_row;
|
|
115
|
+
RETURN;
|
|
116
|
+
END IF;
|
|
117
|
+
|
|
118
|
+
-- Canonical shared prefix. The workspace UPDATE lock also gives all three
|
|
119
|
+
-- destination adapters one order, avoiding claim/head inversion.
|
|
120
|
+
PERFORM 1 FROM workspaces workspace
|
|
121
|
+
WHERE workspace.account_id = p_account_id AND workspace.id = p_workspace_id
|
|
122
|
+
FOR UPDATE;
|
|
123
|
+
IF NOT FOUND THEN
|
|
124
|
+
RAISE EXCEPTION 'governed-learning workspace is unavailable' USING ERRCODE = '42501';
|
|
125
|
+
END IF;
|
|
126
|
+
SELECT * INTO result_row FROM governed_learning_activation_receipts receipt
|
|
127
|
+
WHERE receipt.workspace_id = p_workspace_id AND receipt.operation_id = p_operation_id
|
|
128
|
+
FOR SHARE;
|
|
129
|
+
IF FOUND THEN
|
|
130
|
+
IF result_row.account_id <> p_account_id OR result_row.input_hash <> input_hash_value
|
|
131
|
+
OR result_row.decision_receipt_id <> p_decision_receipt_id
|
|
132
|
+
OR result_row.initiating_human_subject_id <> caller_subject_id
|
|
133
|
+
OR result_row.authority_kind <> 'human_confirmed'
|
|
134
|
+
OR result_row.human_input_request_id IS DISTINCT FROM p_human_input_request_id
|
|
135
|
+
THEN
|
|
136
|
+
RAISE EXCEPTION 'governed-learning activation operation conflicts with immutable receipt'
|
|
137
|
+
USING ERRCODE = '23505';
|
|
138
|
+
END IF;
|
|
139
|
+
RETURN NEXT result_row;
|
|
140
|
+
RETURN;
|
|
141
|
+
END IF;
|
|
142
|
+
SELECT * INTO decision_row FROM governed_learning_decision_receipts receipt
|
|
143
|
+
WHERE receipt.id = p_decision_receipt_id AND receipt.account_id = p_account_id
|
|
144
|
+
AND receipt.workspace_id = p_workspace_id
|
|
145
|
+
AND receipt.initiating_human_subject_id = caller_subject_id
|
|
146
|
+
AND receipt.outcome IN ('suggest', 'automatic', 'confidence')
|
|
147
|
+
FOR UPDATE;
|
|
148
|
+
IF NOT FOUND OR NOT session_reference_visible(
|
|
149
|
+
decision_row.account_id, decision_row.workspace_id, decision_row.session_id
|
|
150
|
+
) THEN
|
|
151
|
+
RAISE EXCEPTION 'confirmable evaluator receipt is unavailable'
|
|
152
|
+
USING ERRCODE = '42501';
|
|
153
|
+
END IF;
|
|
154
|
+
IF EXISTS (
|
|
155
|
+
SELECT 1 FROM governed_learning_activation_receipts receipt
|
|
156
|
+
WHERE receipt.decision_receipt_id = p_decision_receipt_id
|
|
157
|
+
) THEN
|
|
158
|
+
RAISE EXCEPTION 'automatic evaluator receipt was consumed by another operation'
|
|
159
|
+
USING ERRCODE = '23505';
|
|
160
|
+
END IF;
|
|
161
|
+
PERFORM 1 FROM sessions session
|
|
162
|
+
WHERE session.account_id = p_account_id AND session.workspace_id = p_workspace_id
|
|
163
|
+
AND session.id = decision_row.session_id AND session.active_turn_id = decision_row.turn_id
|
|
164
|
+
AND session_reference_visible(session.account_id, session.workspace_id, session.id)
|
|
165
|
+
FOR SHARE;
|
|
166
|
+
IF NOT FOUND THEN
|
|
167
|
+
RAISE EXCEPTION 'governed-learning activation session is unavailable' USING ERRCODE = '42501';
|
|
168
|
+
END IF;
|
|
169
|
+
-- The decision receipt was minted before the human-input pause, so the
|
|
170
|
+
-- turn's active attempt is normally a later attempt of the same logical
|
|
171
|
+
-- turn and execution generation. Require that live attempt instead of the
|
|
172
|
+
-- exact minting attempt; the human answer is bound to the same generation.
|
|
173
|
+
PERFORM 1 FROM session_turns turn
|
|
174
|
+
WHERE turn.account_id = p_account_id AND turn.workspace_id = p_workspace_id
|
|
175
|
+
AND turn.session_id = decision_row.session_id AND turn.id = decision_row.turn_id
|
|
176
|
+
AND turn.execution_generation = decision_row.execution_generation
|
|
177
|
+
AND turn.status IN ('running', 'requires_action', 'recovering', 'waiting_capacity')
|
|
178
|
+
AND coalesce(turn.initiating_human_subject_id,
|
|
179
|
+
CASE WHEN turn.initiator_kind = 'subject' THEN turn.initiator_subject_id END
|
|
180
|
+
) = caller_subject_id
|
|
181
|
+
FOR SHARE;
|
|
182
|
+
IF NOT FOUND THEN
|
|
183
|
+
RAISE EXCEPTION 'governed-learning activation turn is unavailable' USING ERRCODE = '42501';
|
|
184
|
+
END IF;
|
|
185
|
+
PERFORM 1 FROM session_turns turn
|
|
186
|
+
JOIN session_turn_attempts attempt
|
|
187
|
+
ON attempt.account_id = turn.account_id AND attempt.workspace_id = turn.workspace_id
|
|
188
|
+
AND attempt.session_id = turn.session_id AND attempt.turn_id = turn.id
|
|
189
|
+
AND attempt.id = turn.active_attempt_id
|
|
190
|
+
WHERE turn.account_id = p_account_id AND turn.workspace_id = p_workspace_id
|
|
191
|
+
AND turn.session_id = decision_row.session_id AND turn.id = decision_row.turn_id
|
|
192
|
+
AND attempt.execution_generation = decision_row.execution_generation
|
|
193
|
+
AND attempt.state IN ('claimed', 'running')
|
|
194
|
+
AND NOT EXISTS (
|
|
195
|
+
SELECT 1 FROM session_attempt_interruptions interruption
|
|
196
|
+
WHERE interruption.workspace_id = p_workspace_id
|
|
197
|
+
AND interruption.attempt_id = attempt.id
|
|
198
|
+
AND interruption.state IN ('pending', 'delivered', 'acknowledged')
|
|
199
|
+
)
|
|
200
|
+
FOR SHARE OF attempt;
|
|
201
|
+
IF NOT FOUND THEN
|
|
202
|
+
RAISE EXCEPTION 'governed-learning activation attempt is unavailable' USING ERRCODE = '42501';
|
|
203
|
+
END IF;
|
|
204
|
+
|
|
205
|
+
SELECT * INTO policy_snapshot FROM workspace_learning_policy_snapshots snapshot
|
|
206
|
+
WHERE snapshot.id = decision_row.policy_snapshot_id
|
|
207
|
+
AND snapshot.account_id = p_account_id AND snapshot.workspace_id = p_workspace_id
|
|
208
|
+
AND snapshot.session_id = decision_row.session_id
|
|
209
|
+
AND snapshot.turn_id = decision_row.turn_id
|
|
210
|
+
AND snapshot.attempt_id = decision_row.attempt_id
|
|
211
|
+
AND snapshot.execution_generation = decision_row.execution_generation
|
|
212
|
+
AND snapshot.snapshot_hash = decision_row.policy_snapshot_hash
|
|
213
|
+
FOR SHARE;
|
|
214
|
+
SELECT * INTO policy_head FROM workspace_learning_policy_heads head
|
|
215
|
+
WHERE head.account_id = p_account_id AND head.workspace_id = p_workspace_id
|
|
216
|
+
FOR SHARE;
|
|
217
|
+
SELECT * INTO policy_revision FROM workspace_learning_policy_revisions revision
|
|
218
|
+
WHERE revision.account_id = p_account_id AND revision.workspace_id = p_workspace_id
|
|
219
|
+
AND revision.id = policy_head.revision_id
|
|
220
|
+
FOR SHARE;
|
|
221
|
+
IF policy_snapshot.id IS NULL
|
|
222
|
+
OR policy_head.revision_id IS DISTINCT FROM decision_row.policy_revision_id
|
|
223
|
+
OR policy_head.policy_hash IS DISTINCT FROM policy_snapshot.policy_hash
|
|
224
|
+
OR policy_head.activation_version IS DISTINCT FROM decision_row.policy_activation_version
|
|
225
|
+
OR policy_revision.policy_hash IS DISTINCT FROM policy_snapshot.policy_hash
|
|
226
|
+
THEN
|
|
227
|
+
RAISE EXCEPTION 'governed-learning policy authority changed after evaluation'
|
|
228
|
+
USING ERRCODE = '40001';
|
|
229
|
+
END IF;
|
|
230
|
+
current_mode := coalesce((
|
|
231
|
+
SELECT override.value->>'mode'
|
|
232
|
+
FROM jsonb_array_elements(policy_revision.source_overrides) override(value)
|
|
233
|
+
WHERE override.value->>'kind' = decision_row.source_kind
|
|
234
|
+
AND override.value->>'id' = decision_row.source_id::text LIMIT 1
|
|
235
|
+
), policy_revision.workspace_mode);
|
|
236
|
+
IF current_mode = 'off' THEN
|
|
237
|
+
RAISE EXCEPTION 'governed-learning source is off' USING ERRCODE = '42501';
|
|
238
|
+
END IF;
|
|
239
|
+
|
|
240
|
+
SELECT * INTO proposal_row FROM knowledge_change_proposals proposal
|
|
241
|
+
WHERE proposal.id = decision_row.proposal_id AND proposal.account_id = p_account_id
|
|
242
|
+
AND proposal.scope_kind = 'workspace' AND proposal.scope_workspace_id = p_workspace_id
|
|
243
|
+
AND proposal.scope_subject_id IS NULL AND proposal.status = 'proposed'
|
|
244
|
+
AND proposal.input_hash = decision_row.proposal_input_hash
|
|
245
|
+
AND proposal.content_hash = decision_row.proposal_content_hash
|
|
246
|
+
AND proposal.claim_id = decision_row.claim_id
|
|
247
|
+
AND proposal.evidence_id = decision_row.evidence_id
|
|
248
|
+
AND proposal.initiating_human_subject_id = caller_subject_id
|
|
249
|
+
FOR SHARE;
|
|
250
|
+
SELECT * INTO claim_row FROM knowledge_claims claim
|
|
251
|
+
WHERE claim.id = decision_row.claim_id AND claim.account_id = p_account_id
|
|
252
|
+
AND claim.scope_kind = 'workspace' AND claim.scope_workspace_id = p_workspace_id
|
|
253
|
+
AND claim.scope_subject_id IS NULL AND claim.input_hash = decision_row.claim_input_hash
|
|
254
|
+
AND claim.initiating_human_subject_id = caller_subject_id
|
|
255
|
+
AND claim.confidence_bps >= decision_row.confidence_floor_bps
|
|
256
|
+
AND claim.effective_at <= transaction_timestamp()
|
|
257
|
+
AND (claim.expires_at IS NULL OR claim.expires_at > transaction_timestamp())
|
|
258
|
+
FOR UPDATE;
|
|
259
|
+
SELECT * INTO evidence_row FROM knowledge_claim_evidence evidence
|
|
260
|
+
WHERE evidence.id = decision_row.evidence_id AND evidence.account_id = p_account_id
|
|
261
|
+
AND evidence.scope_kind = 'workspace' AND evidence.scope_workspace_id = p_workspace_id
|
|
262
|
+
AND evidence.scope_subject_id IS NULL AND evidence.claim_id = decision_row.claim_id
|
|
263
|
+
AND evidence.polarity = 'supports' AND evidence.input_hash = decision_row.evidence_input_hash
|
|
264
|
+
AND evidence.content_hash = decision_row.evidence_content_hash
|
|
265
|
+
AND evidence.initiating_human_subject_id = caller_subject_id
|
|
266
|
+
FOR SHARE;
|
|
267
|
+
IF proposal_row.id IS NULL OR claim_row.id IS NULL OR evidence_row.id IS NULL THEN
|
|
268
|
+
RAISE EXCEPTION 'governed-learning proposal lineage changed after evaluation'
|
|
269
|
+
USING ERRCODE = '42501';
|
|
270
|
+
END IF;
|
|
271
|
+
SELECT * INTO review_row FROM knowledge_claim_reviews review
|
|
272
|
+
WHERE review.account_id = p_account_id AND review.claim_id = decision_row.claim_id
|
|
273
|
+
ORDER BY review.review_revision DESC LIMIT 1 FOR SHARE;
|
|
274
|
+
IF review_row.id IS NULL OR review_row.state <> 'proposed'
|
|
275
|
+
OR review_row.review_revision <> decision_row.review_revision
|
|
276
|
+
THEN
|
|
277
|
+
RAISE EXCEPTION 'governed-learning review was superseded after evaluation'
|
|
278
|
+
USING ERRCODE = '40001';
|
|
279
|
+
END IF;
|
|
280
|
+
SELECT least(1000, count(*))::integer INTO conflict_count FROM (
|
|
281
|
+
SELECT relation.id FROM knowledge_claim_relations relation
|
|
282
|
+
WHERE relation.account_id = p_account_id AND relation.scope_kind = 'workspace'
|
|
283
|
+
AND relation.scope_workspace_id = p_workspace_id AND relation.scope_subject_id IS NULL
|
|
284
|
+
AND relation.relation_type = 'conflicts_with'
|
|
285
|
+
AND (relation.from_claim_id = decision_row.claim_id
|
|
286
|
+
OR relation.to_claim_id = decision_row.claim_id)
|
|
287
|
+
UNION ALL
|
|
288
|
+
SELECT contradiction.id FROM knowledge_claim_evidence contradiction
|
|
289
|
+
WHERE contradiction.account_id = p_account_id AND contradiction.scope_kind = 'workspace'
|
|
290
|
+
AND contradiction.scope_workspace_id = p_workspace_id
|
|
291
|
+
AND contradiction.scope_subject_id IS NULL
|
|
292
|
+
AND contradiction.claim_id = decision_row.claim_id
|
|
293
|
+
AND contradiction.polarity = 'contradicts'
|
|
294
|
+
) conflicts;
|
|
295
|
+
IF conflict_count <> 0 THEN
|
|
296
|
+
RAISE EXCEPTION 'governed-learning evidence now conflicts' USING ERRCODE = '23514';
|
|
297
|
+
END IF;
|
|
298
|
+
|
|
299
|
+
IF evidence_row.task_note_id IS NOT NULL THEN
|
|
300
|
+
SELECT * INTO task_note_row FROM task_notes note
|
|
301
|
+
WHERE note.id = evidence_row.task_note_id AND note.account_id = p_account_id
|
|
302
|
+
AND note.workspace_id = p_workspace_id
|
|
303
|
+
AND note.root_session_id = evidence_row.task_note_root_session_id
|
|
304
|
+
AND note.version = evidence_row.task_note_version
|
|
305
|
+
AND note.text_hash = evidence_row.content_hash
|
|
306
|
+
AND note.status = 'active' AND note.expires_at > transaction_timestamp()
|
|
307
|
+
AND session_reference_visible(note.account_id, note.workspace_id, note.root_session_id)
|
|
308
|
+
FOR SHARE;
|
|
309
|
+
IF NOT FOUND THEN
|
|
310
|
+
RAISE EXCEPTION 'governed-learning Task-note authority was revoked' USING ERRCODE = '42501';
|
|
311
|
+
END IF;
|
|
312
|
+
current_authority_hash := encode(sha256(convert_to(jsonb_build_array(
|
|
313
|
+
'task-note-authority', 1, evidence_row.task_note_id,
|
|
314
|
+
evidence_row.task_note_root_session_id, evidence_row.task_note_version,
|
|
315
|
+
evidence_row.content_hash, task_note_row.status, task_note_row.expires_at
|
|
316
|
+
)::text, 'UTF8')), 'hex');
|
|
317
|
+
ELSE
|
|
318
|
+
SELECT provider.id AS provider_id, provider.lifecycle_state AS provider_state,
|
|
319
|
+
provider.lifecycle_generation AS provider_generation, source.id AS source_id,
|
|
320
|
+
source.lifecycle_state AS source_state, source.lifecycle_generation AS source_generation,
|
|
321
|
+
source.current_acl_generation, object.id AS object_id,
|
|
322
|
+
object.lifecycle_state AS object_state, object.lifecycle_generation AS object_generation,
|
|
323
|
+
object.version_generation AS object_version_generation, object.current_version_id,
|
|
324
|
+
version.id AS version_id, version.version_generation, version.content_sha256,
|
|
325
|
+
version.acl_version_id, version.acl_generation, version.document_id,
|
|
326
|
+
evidence_acl.acl_hash AS evidence_acl_hash,
|
|
327
|
+
evidence_acl.agent_access AS evidence_agent_access,
|
|
328
|
+
current_acl.id AS current_acl_id, current_acl.acl_hash AS current_acl_hash,
|
|
329
|
+
current_acl.agent_access AS current_agent_access
|
|
330
|
+
INTO document_authority
|
|
331
|
+
FROM knowledge_document_versions version
|
|
332
|
+
JOIN knowledge_source_objects object ON object.account_id = version.account_id
|
|
333
|
+
AND object.id = version.object_id AND object.scope_key = version.scope_key
|
|
334
|
+
JOIN knowledge_sources source ON source.account_id = version.account_id
|
|
335
|
+
AND source.id = version.source_id AND source.scope_key = version.scope_key
|
|
336
|
+
JOIN knowledge_providers provider ON provider.account_id = source.account_id
|
|
337
|
+
AND provider.id = source.provider_id AND provider.scope_key = source.scope_key
|
|
338
|
+
JOIN knowledge_source_acl_versions evidence_acl ON evidence_acl.account_id = version.account_id
|
|
339
|
+
AND evidence_acl.id = version.acl_version_id AND evidence_acl.source_id = version.source_id
|
|
340
|
+
AND evidence_acl.generation = version.acl_generation
|
|
341
|
+
LEFT JOIN knowledge_source_acl_versions current_acl ON current_acl.account_id = source.account_id
|
|
342
|
+
AND current_acl.source_id = source.id AND current_acl.generation = source.current_acl_generation
|
|
343
|
+
WHERE version.id = evidence_row.document_version_id AND version.account_id = p_account_id
|
|
344
|
+
AND version.scope_kind = 'workspace' AND version.scope_workspace_id = p_workspace_id
|
|
345
|
+
AND version.scope_subject_id IS NULL
|
|
346
|
+
FOR SHARE OF version, object, source, provider, evidence_acl;
|
|
347
|
+
IF document_authority.version_id IS NULL
|
|
348
|
+
OR document_authority.provider_state IS DISTINCT FROM 'active'
|
|
349
|
+
OR document_authority.source_state IS DISTINCT FROM 'active'
|
|
350
|
+
OR document_authority.object_state IS DISTINCT FROM 'active'
|
|
351
|
+
OR document_authority.current_version_id IS DISTINCT FROM document_authority.version_id
|
|
352
|
+
OR document_authority.object_version_generation IS DISTINCT FROM document_authority.version_generation
|
|
353
|
+
OR document_authority.current_acl_id IS NULL
|
|
354
|
+
OR NOT coalesce(document_authority.evidence_agent_access, false)
|
|
355
|
+
OR NOT coalesce(document_authority.current_agent_access, false)
|
|
356
|
+
OR (document_authority.document_id IS NOT NULL AND NOT EXISTS (
|
|
357
|
+
SELECT 1 FROM documents document WHERE document.id = document_authority.document_id
|
|
358
|
+
AND document.account_id = p_account_id AND document.workspace_id = p_workspace_id
|
|
359
|
+
AND document.status = 'ready' AND document.agent_access
|
|
360
|
+
AND (document.visibility <> 'private' OR document.created_by = caller_subject_id)
|
|
361
|
+
))
|
|
362
|
+
OR (evidence_row.document_chunk_id IS NOT NULL AND NOT EXISTS (
|
|
363
|
+
SELECT 1 FROM document_chunks chunk WHERE chunk.id = evidence_row.document_chunk_id
|
|
364
|
+
AND chunk.account_id = p_account_id AND chunk.workspace_id = p_workspace_id
|
|
365
|
+
AND chunk.document_id = document_authority.document_id
|
|
366
|
+
))
|
|
367
|
+
THEN
|
|
368
|
+
RAISE EXCEPTION 'governed-learning document authority was revoked' USING ERRCODE = '42501';
|
|
369
|
+
END IF;
|
|
370
|
+
current_authority_hash := encode(sha256(convert_to(jsonb_build_array(
|
|
371
|
+
'document-evidence-authority', 1, evidence_row.document_version_id,
|
|
372
|
+
document_authority.provider_id, document_authority.provider_state,
|
|
373
|
+
document_authority.provider_generation, document_authority.source_id,
|
|
374
|
+
document_authority.source_state, document_authority.source_generation,
|
|
375
|
+
document_authority.current_acl_generation, document_authority.object_id,
|
|
376
|
+
document_authority.object_state, document_authority.object_generation,
|
|
377
|
+
document_authority.object_version_generation, document_authority.current_version_id,
|
|
378
|
+
document_authority.version_id, document_authority.version_generation,
|
|
379
|
+
document_authority.content_sha256, document_authority.acl_version_id,
|
|
380
|
+
document_authority.acl_generation, document_authority.evidence_acl_hash,
|
|
381
|
+
document_authority.current_acl_id, document_authority.current_acl_hash,
|
|
382
|
+
evidence_row.document_chunk_id, false
|
|
383
|
+
)::text, 'UTF8')), 'hex');
|
|
384
|
+
END IF;
|
|
385
|
+
|
|
386
|
+
-- The exact human must have answered the bound question for this proposal
|
|
387
|
+
-- on the same session/turn, in the same execution generation, with `save`.
|
|
388
|
+
-- The question the human saw is reconstructed here from the proposal itself
|
|
389
|
+
-- (canonical prompt, exact proposal content as help text, fixed options), so
|
|
390
|
+
-- an agent cannot obtain confirmation through a misleading prompt.
|
|
391
|
+
human_question_id := 'remember:' || decision_row.proposal_id::text;
|
|
392
|
+
-- Only Task-note evidence (the exact user-directed text) is confirmable; the
|
|
393
|
+
-- proposal content of a preference is a normalized envelope, not the words
|
|
394
|
+
-- the human was shown.
|
|
395
|
+
IF task_note_row.id IS NULL THEN
|
|
396
|
+
RAISE EXCEPTION 'human confirmation requires exact Task-note evidence' USING ERRCODE = '42501';
|
|
397
|
+
END IF;
|
|
398
|
+
human_question_lane := proposal_row.target_kind;
|
|
399
|
+
human_question_help := left(task_note_row.text, 2000);
|
|
400
|
+
human_question_prompt := 'Save this as a ' || CASE human_question_lane
|
|
401
|
+
WHEN 'instruction_policy' THEN 'mandatory workspace rule'
|
|
402
|
+
WHEN 'preference' THEN 'workspace preference'
|
|
403
|
+
ELSE 'workspace knowledge' END
|
|
404
|
+
|| ' for everyone in this workspace?';
|
|
405
|
+
SELECT * INTO human_input_row FROM session_human_input_requests request
|
|
406
|
+
WHERE request.id = p_human_input_request_id
|
|
407
|
+
AND request.account_id = p_account_id AND request.workspace_id = p_workspace_id
|
|
408
|
+
AND request.session_id = decision_row.session_id
|
|
409
|
+
AND request.turn_id = decision_row.turn_id
|
|
410
|
+
AND request.turn_generation = decision_row.execution_generation
|
|
411
|
+
AND request.status = 'answered'
|
|
412
|
+
AND request.responded_by = caller_subject_id
|
|
413
|
+
AND request.response->>'outcome' = 'answered'
|
|
414
|
+
AND EXISTS (
|
|
415
|
+
SELECT 1 FROM jsonb_array_elements(request.questions) question(value)
|
|
416
|
+
WHERE question.value->>'id' = human_question_id
|
|
417
|
+
AND question.value->>'kind' = 'single_select'
|
|
418
|
+
AND question.value->>'prompt' = human_question_prompt
|
|
419
|
+
AND question.value->>'helpText' = human_question_help
|
|
420
|
+
AND question.value->'options' = human_question_options
|
|
421
|
+
AND coalesce((question.value->>'allowOther')::boolean, false) = false
|
|
422
|
+
)
|
|
423
|
+
AND EXISTS (
|
|
424
|
+
SELECT 1 FROM jsonb_array_elements(request.response->'answers') answer(value)
|
|
425
|
+
WHERE answer.value->>'questionId' = human_question_id
|
|
426
|
+
AND answer.value->'values' = to_jsonb(ARRAY['save'])
|
|
427
|
+
)
|
|
428
|
+
FOR SHARE;
|
|
429
|
+
IF NOT FOUND THEN
|
|
430
|
+
RAISE EXCEPTION 'human confirmation for this proposal is unavailable'
|
|
431
|
+
USING ERRCODE = '42501';
|
|
432
|
+
END IF;
|
|
433
|
+
IF current_authority_hash IS DISTINCT FROM decision_row.evidence_authority_hash THEN
|
|
434
|
+
RAISE EXCEPTION 'governed-learning evidence authority changed after evaluation'
|
|
435
|
+
USING ERRCODE = '40001';
|
|
436
|
+
END IF;
|
|
437
|
+
|
|
438
|
+
IF proposal_row.target_kind = 'instruction_policy' THEN
|
|
439
|
+
-- One knowledge proposal may now own more than one inactive
|
|
440
|
+
-- instruction-policy proposal: when the head moves after the human already
|
|
441
|
+
-- confirmed, the confirm path rebaselines by adding a successor against the
|
|
442
|
+
-- current head rather than stranding the human's answer. Exactly one of
|
|
443
|
+
-- them can match the live head, so resolve the head first and select by it
|
|
444
|
+
-- instead of assuming a single row.
|
|
445
|
+
SELECT * INTO instruction_proposal
|
|
446
|
+
FROM workspace_instruction_policy_onboarding_proposals proposal
|
|
447
|
+
WHERE proposal.account_id = p_account_id AND proposal.workspace_id = p_workspace_id
|
|
448
|
+
AND proposal.source_id = proposal_row.id::text
|
|
449
|
+
AND proposal.source_version = proposal_row.content_hash
|
|
450
|
+
AND proposal.status = 'proposed'
|
|
451
|
+
ORDER BY proposal.baseline_activation_version DESC, proposal.created_at DESC, proposal.id DESC
|
|
452
|
+
LIMIT 1
|
|
453
|
+
FOR SHARE;
|
|
454
|
+
IF NOT FOUND OR instruction_proposal.draft_content_hash <> proposal_row.content_hash THEN
|
|
455
|
+
RAISE EXCEPTION 'exact inactive instruction-policy proposal is unavailable'
|
|
456
|
+
USING ERRCODE = '42501';
|
|
457
|
+
END IF;
|
|
458
|
+
SELECT * INTO current_instruction_head FROM workspace_instruction_policy_heads head
|
|
459
|
+
WHERE head.account_id = p_account_id AND head.workspace_id = p_workspace_id
|
|
460
|
+
AND head.kind = instruction_proposal.kind AND head.scope = instruction_proposal.scope
|
|
461
|
+
AND head.role_key IS NOT DISTINCT FROM instruction_proposal.role_key FOR SHARE;
|
|
462
|
+
IF NOT FOUND THEN
|
|
463
|
+
SELECT event.activation_version INTO current_instruction_activation_version
|
|
464
|
+
FROM workspace_instruction_policy_deactivation_events event
|
|
465
|
+
WHERE event.account_id = p_account_id AND event.workspace_id = p_workspace_id
|
|
466
|
+
AND event.kind = instruction_proposal.kind AND event.scope = instruction_proposal.scope
|
|
467
|
+
AND event.role_key IS NOT DISTINCT FROM instruction_proposal.role_key
|
|
468
|
+
ORDER BY event.activation_version DESC, event.id DESC
|
|
469
|
+
LIMIT 1;
|
|
470
|
+
END IF;
|
|
471
|
+
current_instruction_activation_version := coalesce(
|
|
472
|
+
current_instruction_head.activation_version,
|
|
473
|
+
current_instruction_activation_version,
|
|
474
|
+
0
|
|
475
|
+
);
|
|
476
|
+
-- Prefer the successor bound to the live head. The ordering above already
|
|
477
|
+
-- puts the newest baseline first, so this only re-selects when an older
|
|
478
|
+
-- proposal is the matching one.
|
|
479
|
+
IF current_instruction_head.revision_id IS DISTINCT FROM instruction_proposal.baseline_revision_id
|
|
480
|
+
OR current_instruction_activation_version
|
|
481
|
+
IS DISTINCT FROM instruction_proposal.baseline_activation_version
|
|
482
|
+
THEN
|
|
483
|
+
SELECT * INTO instruction_proposal
|
|
484
|
+
FROM workspace_instruction_policy_onboarding_proposals proposal
|
|
485
|
+
WHERE proposal.account_id = p_account_id AND proposal.workspace_id = p_workspace_id
|
|
486
|
+
AND proposal.source_id = proposal_row.id::text
|
|
487
|
+
AND proposal.source_version = proposal_row.content_hash
|
|
488
|
+
AND proposal.status = 'proposed'
|
|
489
|
+
AND proposal.baseline_revision_id IS NOT DISTINCT FROM current_instruction_head.revision_id
|
|
490
|
+
AND proposal.baseline_activation_version = current_instruction_activation_version
|
|
491
|
+
FOR SHARE;
|
|
492
|
+
IF NOT FOUND OR instruction_proposal.draft_content_hash <> proposal_row.content_hash THEN
|
|
493
|
+
RAISE EXCEPTION 'instruction-policy proposal baseline is stale' USING ERRCODE = '40001';
|
|
494
|
+
END IF;
|
|
495
|
+
END IF;
|
|
496
|
+
destination_value := 'instruction_policy';
|
|
497
|
+
destination_proposal_id_value := instruction_proposal.id;
|
|
498
|
+
destination_revision_id_value := instruction_proposal.draft_revision_id;
|
|
499
|
+
destination_old_revision_id_value := instruction_proposal.baseline_revision_id;
|
|
500
|
+
destination_old_content_hash_value := instruction_proposal.baseline_content_hash;
|
|
501
|
+
destination_old_version_value := instruction_proposal.baseline_activation_version;
|
|
502
|
+
destination_new_content_hash_value := instruction_proposal.draft_content_hash;
|
|
503
|
+
ELSIF proposal_row.target_kind = 'preference' THEN
|
|
504
|
+
SELECT * INTO preference_receipt FROM company_brain_preference_proposal_receipts receipt
|
|
505
|
+
WHERE receipt.account_id = p_account_id AND receipt.workspace_id = p_workspace_id
|
|
506
|
+
AND receipt.knowledge_proposal_id = proposal_row.id FOR SHARE;
|
|
507
|
+
SELECT * INTO preference_row FROM preference_registry_preferences preference
|
|
508
|
+
WHERE preference.account_id = p_account_id AND preference.id = preference_receipt.preference_id
|
|
509
|
+
AND preference.scope = 'workspace' AND preference.scope_workspace_id = p_workspace_id
|
|
510
|
+
AND preference.scope_subject_id IS NULL AND preference.status = 'proposed'
|
|
511
|
+
AND preference.active_revision_id IS NULL AND preference.activation_version = 0
|
|
512
|
+
AND preference.scope_version = 1 FOR SHARE;
|
|
513
|
+
SELECT * INTO preference_revision FROM preference_registry_revisions revision
|
|
514
|
+
WHERE revision.account_id = p_account_id AND revision.id = preference_receipt.revision_id
|
|
515
|
+
AND revision.preference_id = preference_receipt.preference_id
|
|
516
|
+
AND revision.provenance_source = 'knowledge_proposal'
|
|
517
|
+
AND revision.provenance_source_id = proposal_row.id::text
|
|
518
|
+
AND revision.trust = 'untrusted_proposal'
|
|
519
|
+
AND (revision.expires_at IS NULL OR revision.expires_at > transaction_timestamp())
|
|
520
|
+
FOR SHARE;
|
|
521
|
+
IF preference_receipt.id IS NULL OR preference_row.id IS NULL OR preference_revision.id IS NULL
|
|
522
|
+
OR proposal_row.target_scope <> 'workspace'
|
|
523
|
+
OR proposal_row.target_key <> preference_row.stable_key
|
|
524
|
+
THEN
|
|
525
|
+
RAISE EXCEPTION 'exact inactive preference proposal is unavailable'
|
|
526
|
+
USING ERRCODE = '42501';
|
|
527
|
+
END IF;
|
|
528
|
+
destination_value := 'preference';
|
|
529
|
+
destination_proposal_id_value := preference_row.id;
|
|
530
|
+
destination_revision_id_value := preference_revision.id;
|
|
531
|
+
destination_old_revision_id_value := NULL;
|
|
532
|
+
destination_old_content_hash_value := NULL;
|
|
533
|
+
destination_old_version_value := 0;
|
|
534
|
+
destination_new_content_hash_value := preference_revision.content_hash;
|
|
535
|
+
ELSE
|
|
536
|
+
RAISE EXCEPTION 'unsupported governed-learning destination' USING ERRCODE = '23514';
|
|
537
|
+
END IF;
|
|
538
|
+
|
|
539
|
+
service_actor := 'service:governed-learning-activation:' || input_hash_value;
|
|
540
|
+
review_operation_id := opengeni_private.governed_learning_derived_uuid(
|
|
541
|
+
p_operation_id, 'knowledge-approve'
|
|
542
|
+
);
|
|
543
|
+
destination_operation_id := opengeni_private.governed_learning_derived_uuid(
|
|
544
|
+
p_operation_id, 'destination-activate'
|
|
545
|
+
);
|
|
546
|
+
SELECT * INTO approval_row FROM governed_learning_apply_knowledge_review(
|
|
547
|
+
p_account_id, p_workspace_id, review_operation_id, decision_row.claim_id,
|
|
548
|
+
review_row.id, review_row.review_revision, 'approved', service_actor, caller_subject_id,
|
|
549
|
+
'Approved by the exact initiating human through human-confirmed learning activation.'
|
|
550
|
+
);
|
|
551
|
+
IF destination_value = 'instruction_policy' THEN
|
|
552
|
+
SELECT * INTO instruction_event FROM governed_learning_apply_instruction_policy(
|
|
553
|
+
'activate', p_account_id, p_workspace_id, destination_operation_id,
|
|
554
|
+
destination_proposal_id_value, destination_old_revision_id_value,
|
|
555
|
+
destination_old_version_value, destination_revision_id_value, service_actor
|
|
556
|
+
);
|
|
557
|
+
destination_new_version_value := instruction_event.activation_version;
|
|
558
|
+
destination_event_id_value := instruction_event.id;
|
|
559
|
+
effective_at_value := instruction_event.created_at;
|
|
560
|
+
ELSE
|
|
561
|
+
SELECT * INTO preference_event FROM governed_learning_apply_preference(
|
|
562
|
+
'activate', p_account_id, p_workspace_id, destination_operation_id,
|
|
563
|
+
proposal_row.id, destination_proposal_id_value, destination_revision_id_value,
|
|
564
|
+
NULL, 0, service_actor
|
|
565
|
+
);
|
|
566
|
+
SELECT activation_version INTO destination_new_version_value
|
|
567
|
+
FROM preference_registry_preferences WHERE id = destination_proposal_id_value;
|
|
568
|
+
destination_event_id_value := preference_event.id;
|
|
569
|
+
effective_at_value := preference_event.created_at;
|
|
570
|
+
END IF;
|
|
571
|
+
|
|
572
|
+
INSERT INTO governed_learning_activation_receipts (
|
|
573
|
+
account_id, workspace_id, operation_id, input_hash, decision_receipt_id,
|
|
574
|
+
initiating_human_subject_id, service_actor_subject_id, policy_revision_id,
|
|
575
|
+
policy_hash, policy_activation_version, source_kind, source_id,
|
|
576
|
+
source_authority_hash, proposal_id, claim_id, evidence_id,
|
|
577
|
+
knowledge_previous_review_id, knowledge_previous_review_revision,
|
|
578
|
+
knowledge_approval_review_id, knowledge_approval_review_revision,
|
|
579
|
+
knowledge_approval_input_hash, destination, destination_proposal_id,
|
|
580
|
+
destination_revision_id, destination_old_revision_id,
|
|
581
|
+
destination_old_content_hash, destination_old_version,
|
|
582
|
+
destination_new_content_hash, destination_new_version, destination_event_id,
|
|
583
|
+
effective_at, authority_kind, human_input_request_id
|
|
584
|
+
) VALUES (
|
|
585
|
+
p_account_id, p_workspace_id, p_operation_id, input_hash_value,
|
|
586
|
+
p_decision_receipt_id, caller_subject_id, service_actor, policy_revision.id,
|
|
587
|
+
policy_revision.policy_hash, policy_head.activation_version,
|
|
588
|
+
decision_row.source_kind, decision_row.source_id, current_authority_hash,
|
|
589
|
+
decision_row.proposal_id, decision_row.claim_id, decision_row.evidence_id,
|
|
590
|
+
review_row.id, review_row.review_revision, approval_row.id,
|
|
591
|
+
approval_row.review_revision, approval_row.input_hash, destination_value,
|
|
592
|
+
destination_proposal_id_value, destination_revision_id_value,
|
|
593
|
+
destination_old_revision_id_value, destination_old_content_hash_value,
|
|
594
|
+
destination_old_version_value, destination_new_content_hash_value,
|
|
595
|
+
destination_new_version_value, destination_event_id_value, effective_at_value,
|
|
596
|
+
'human_confirmed', p_human_input_request_id
|
|
597
|
+
) RETURNING * INTO result_row;
|
|
598
|
+
RETURN NEXT result_row;
|
|
599
|
+
END;
|
|
600
|
+
$$;
|
|
601
|
+
|
|
602
|
+
DO $confirm_rebaseline_hardening$
|
|
603
|
+
DECLARE
|
|
604
|
+
target_schema text := current_schema();
|
|
605
|
+
BEGIN
|
|
606
|
+
-- CREATE OR REPLACE drops proconfig; re-apply the pin 0272/0284 established.
|
|
607
|
+
EXECUTE format(
|
|
608
|
+
'ALTER FUNCTION %I.activate_human_confirmed_learning_decision(uuid,uuid,uuid,uuid,uuid) '
|
|
609
|
+
|| 'SET search_path = pg_catalog, %I, pg_temp', target_schema, target_schema
|
|
610
|
+
);
|
|
611
|
+
END
|
|
612
|
+
$confirm_rebaseline_hardening$;
|
|
613
|
+
|
|
614
|
+
RESET statement_timeout;
|
|
615
|
+
RESET lock_timeout;
|