@consiliency/agent-board-schema 1.75.0 → 1.79.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/fixtures/already-applied.json +16 -0
- package/migrations/20261109000100_agent_board_step_up_groundwork.sql +343 -0
- package/migrations/20261120000200_agent_board_thread_key_serialize.sql +118 -0
- package/migrations/20261121000100_agent_board_thread_key_guards.sql +196 -0
- package/migrations/20261124000100_agent_board_step_up_enforcement.sql +1213 -0
- package/package.json +1 -1
- package/schema-manifest.json +39 -3
- package/src/deployment-drift-main.test.mjs +89 -6
- package/src/deployment-drift.test.mjs +20 -1
- package/src/tooling.test.mjs +10 -2
- package/tests/agent_board_717_message_replay_scope.test.sql +39 -2
- package/tests/agent_board_717_tasking_replay_scope.test.sql +21 -2
- package/tests/agent_board_enrollment_audit_content.test.sql +6 -1
- package/tests/agent_board_role_guard.test.sql +3 -1
- package/tests/agent_board_send_outcome.test.sql +39 -1
- package/tests/agent_board_session_identity_approval_revocation.test.sql +5 -0
- package/tests/agent_board_step_up.test.sql +991 -0
- package/tests/agent_board_step_up_helper.test.sql +815 -0
- package/tests/agent_board_thread_key_guards.test.sql +186 -0
- package/tests/agent_board_thread_key_serialize.test.sql +182 -0
- package/tests/agent_board_write_enforcement_class.test.sql +8 -4
- package/tests/catalog/function_execute_privileges.json +72 -0
- package/tests/catalog/p_metadata_parameters.json +32 -0
- package/tests/catalog/step_up_gated_closure.json +24 -0
- package/tests/fixtures/pgtap-unexposed.sql +9 -4
- package/tests/isolation/expected/create_or_find_thread_same_key_race.out +641 -0
- package/tests/isolation/expected/session_identity_links.out +4 -0
- package/tests/isolation/specs/create_or_find_thread_same_key_race.spec +252 -0
- package/tests/isolation/specs/session_identity_links.spec +5 -0
- package/tests/mutations/669/M10_service_role_blanket.sql +10 -0
- package/tests/mutations/669/M12a_no_parse_wrapper.sql +17 -0
- package/tests/mutations/669/M12b_no_session_wrapper.sql +23 -0
- package/tests/mutations/669/M12c_no_claim_type_guard.sql +13 -0
- package/tests/mutations/669/M12d_no_amr_element_guards.sql +15 -0
- package/tests/mutations/669/M14_now_not_clock.sql +9 -0
- package/tests/mutations/669/M15_classifier_reads_metadata.sql +17 -0
- package/tests/mutations/669/M17_skip_ledger.sql +15 -0
- package/tests/mutations/669/M18_service_role_sub_insensitive.sql +9 -0
- package/tests/mutations/669/M18b_drop_uid_conjunct.sql +9 -0
- package/tests/mutations/669/M23_definer_wrapper_over_writer.sql +5 -0
- package/tests/mutations/669/M3_age_from_iat.sql +9 -0
- package/tests/mutations/669/M4_window_900.sql +9 -0
- package/tests/mutations/669/M4b_lt_to_le.sql +9 -0
- package/tests/mutations/669/M5_no_skew_bound.sql +9 -0
- package/tests/mutations/669/M6a_user_metadata_amr.sql +9 -0
- package/tests/mutations/669/M6b_app_metadata_claim.sql +9 -0
- package/tests/mutations/669/M6c_legacy_guc.sql +9 -0
- package/tests/mutations/669/M7_either_claim_any_human.sql +9 -0
- package/tests/mutations/669/M8_any_amr_method.sql +9 -0
- package/tests/mutations/669/M8b_accept_aal2.sql +9 -0
- package/tests/mutations/669/M9_every_kind.sql +9 -0
- package/tests/mutations/669/MAL_allowlist_nonempty.sql +9 -0
- package/tests/mutations/669/MAR_writer_arg_renamed.sql +14 -0
- package/tests/mutations/669/MAU_audit_mode_retyped.sql +2 -0
- package/tests/mutations/669/MA_helper_always_raises.sql +10 -0
- package/tests/mutations/669/MDX_dynamic_execute_definer.sql +5 -0
- package/tests/mutations/669/MD_detail_carries_value.sql +12 -0
- package/tests/mutations/669/MEC0_caller_of_helper.sql +4 -0
- package/tests/mutations/669/MEC0b_overload_caller.sql +4 -0
- package/tests/mutations/669/MG1_helper_granted_authenticated.sql +2 -0
- package/tests/mutations/669/MG2_ledger_insert_authenticated.sql +2 -0
- package/tests/mutations/669/MG3_ledger_policy.sql +2 -0
- package/tests/mutations/669/MR1_writer_no_class_check.sql +9 -0
- package/tests/mutations/669/MR2_writer_wide_window.sql +9 -0
- package/tests/mutations/669/MR3_writer_prunes_everyone.sql +11 -0
- package/tests/mutations/669/MR4_writer_lower_exclusive.sql +9 -0
- package/tests/mutations/669/MR5_writer_upper_exclusive.sql +9 -0
- package/tests/mutations/669/MR6_writer_upper_61.sql +9 -0
- package/tests/mutations/669/MW2_second_ledger_writer.sql +4 -0
- package/tests/mutations/669/MXS_cross_schema_wrapper.sql +4 -0
- package/tests/mutations/669/run.sh +80 -0
- package/tests/mutations/669b/M11_ceil_first.sql +10 -0
- package/tests/mutations/669b/M11_gate_first.sql +10 -0
- package/tests/mutations/669b/M11_grant_first.sql +10 -0
- package/tests/mutations/669b/M11_inv_first.sql +10 -0
- package/tests/mutations/669b/M11_mem_first.sql +10 -0
- package/tests/mutations/669b/M11_readmit_first.sql +10 -0
- package/tests/mutations/669b/M13a_gate_member_preset.sql +12 -0
- package/tests/mutations/669b/M13b_ungate_admin_preset.sql +12 -0
- package/tests/mutations/669b/M16_actor_is_target.sql +14 -0
- package/tests/mutations/669b/M19_drop_readmit.sql +9 -0
- package/tests/mutations/669b/M1_drop_grant.sql +9 -0
- package/tests/mutations/669b/M20_drop_d5_refusal.sql +9 -0
- package/tests/mutations/669b/M21_drop_removal.sql +9 -0
- package/tests/mutations/669b/M21b_only_removed.sql +9 -0
- package/tests/mutations/669b/M21c_gate_members.sql +12 -0
- package/tests/mutations/669b/M21d_requested_state.sql +9 -0
- package/tests/mutations/669b/M23b_api_wrapper_over_audit_writer.sql +6 -0
- package/tests/mutations/669b/M23c_drop_register_type_refusal.sql +9 -0
- package/tests/mutations/669b/M24_drop_kind_refusal.sql +9 -0
- package/tests/mutations/669b/M24b_drop_auth_bind_refusal.sql +9 -0
- package/tests/mutations/669b/M2a_drop_gate.sql +9 -0
- package/tests/mutations/669b/M2b_drop_ceiling.sql +9 -0
- package/tests/mutations/669b/M2c_drop_invite.sql +9 -0
- package/tests/mutations/669b/MAL2_drop_otp.sql +9 -0
- package/tests/mutations/669b/MAL3_admit_passkey.sql +9 -0
- package/tests/mutations/669b/MD7a_no_revocation.sql +2 -0
- package/tests/mutations/669b/MD7b_overwrite_revoked.sql +2 -0
- package/tests/mutations/669b/MD7c_revoke_members.sql +2 -0
- package/tests/mutations/669b/MD7d_revoke_expired.sql +2 -0
- package/tests/mutations/669b/ME1_wrapper_over_gate.sql +7 -0
- package/tests/mutations/669b/MEC2_write_after_helper.sql +10 -0
- package/tests/mutations/669b/MQX_query_to_xml_definer.sql +6 -0
- package/tests/mutations/669b/MQXb_ts_stat_other_schema.sql +4 -0
- package/tests/mutations/669b/MTR_trigger_route.sql +9 -0
- package/tests/mutations/669b/MTRb_new_assign.sql +11 -0
- package/tests/mutations/669b/MTRc_sync_assigns_value.sql +47 -0
- package/tests/mutations/669b/MTX1_tombstone_second_gated_call.sql +15 -0
- package/tests/mutations/669b/MTX2_tombstone_non_delegate_target.sql +10 -0
- package/tests/mutations/669b/MXS_other_schema_names_gated.sql +5 -0
- package/tests/mutations/669b/run.sh +83 -0
- package/tests/mutations/670/M18a_read_operation_default_dropped.sql +11 -0
- package/tests/mutations/670/M18b_read_operation_default_changed.sql +7 -0
- package/tests/mutations/670/M3-DM_helper_on_replay_branch.sql +10 -0
- package/tests/mutations/670/M3-MC_helper_on_replay_branch.sql +10 -0
- package/tests/mutations/s12/M0_identity.sql +56 -0
- package/tests/mutations/s12/M1_serialise_actor_key.sql +57 -0
- package/tests/mutations/s12/M1b_serialise_key_only.sql +57 -0
- package/tests/mutations/s12/M2_unique_index_no_handler.sql +9 -0
- package/tests/mutations/s12/M3_no_trim.sql +56 -0
- package/tests/mutations/s12/M4_casefold.sql +56 -0
- package/tests/mutations/s12/M5_lookup_ignores_actor.sql +56 -0
- package/tests/mutations/s12/M6_reuse_reports_false.sql +56 -0
- package/tests/mutations/s12/run.sh +112 -0
- package/tests/mutations/s12-fix/M0_identity.sql +83 -0
- package/tests/mutations/s12-fix/M10_security_invoker.sql +9 -0
- package/tests/mutations/s12-fix/M11_search_path_changed.sql +9 -0
- package/tests/mutations/s12-fix/M12_reuse_writes_task.sql +84 -0
- package/tests/mutations/s12-fix/M13_dead_inbox_write.sql +84 -0
- package/tests/mutations/s12-fix/M14_metadata_unchecked.sql +78 -0
- package/tests/mutations/s12-fix/M15_metadata_check_after_lock.sql +84 -0
- package/tests/mutations/s12-fix/M16_sql_null_metadata_refused.sql +83 -0
- package/tests/mutations/s12-fix/M17_isolation_unchecked.sql +70 -0
- package/tests/mutations/s12-fix/M18_isolation_check_after_lock.sql +82 -0
- package/tests/mutations/s12-fix/M19_isolation_rr_only.sql +83 -0
- package/tests/mutations/s12-fix/M1_lock_dropped.sql +78 -0
- package/tests/mutations/s12-fix/M1b_lock_key_only.sql +83 -0
- package/tests/mutations/s12-fix/M20_isolation_unsafe_levels_only.sql +83 -0
- package/tests/mutations/s12-fix/M21_isolation_check_miss_path_only.sql +83 -0
- package/tests/mutations/s12-fix/M22_isolation_inverted.sql +83 -0
- package/tests/mutations/s12-fix/M23_isolation_wrong_sqlstate.sql +83 -0
- package/tests/mutations/s12-fix/M24_create_thread_key_unchecked.sql +37 -0
- package/tests/mutations/s12-fix/M25_create_thread_key_nonnull_only.sql +45 -0
- package/tests/mutations/s12-fix/M26_create_thread_key_casefold.sql +45 -0
- package/tests/mutations/s12-fix/M27_create_thread_no_object_guard.sql +45 -0
- package/tests/mutations/s12-fix/M28_create_thread_key_stripped.sql +40 -0
- package/tests/mutations/s12-fix/M29_create_thread_key_nested_refused.sql +45 -0
- package/tests/mutations/s12-fix/M2_lock_actor_only.sql +83 -0
- package/tests/mutations/s12-fix/M30_create_thread_security_invoker.sql +9 -0
- package/tests/mutations/s12-fix/M31_create_thread_search_path_changed.sql +9 -0
- package/tests/mutations/s12-fix/M32_class_new_thread_inserter.sql +14 -0
- package/tests/mutations/s12-fix/M33_class_metadata_update.sql +12 -0
- package/tests/mutations/s12-fix/M33b_class_metadata_update_upper.sql +17 -0
- package/tests/mutations/s12-fix/M33c_class_metadata_update_row_form.sql +16 -0
- package/tests/mutations/s12-fix/M33d_class_metadata_merge_update.sql +16 -0
- package/tests/mutations/s12-fix/M33e_class_metadata_upsert.sql +16 -0
- package/tests/mutations/s12-fix/M34_class_api_role_insert.sql +9 -0
- package/tests/mutations/s12-fix/M3_lock_after_lookup.sql +83 -0
- package/tests/mutations/s12-fix/M4_lock_raw_key.sql +83 -0
- package/tests/mutations/s12-fix/M5_no_trim.sql +83 -0
- package/tests/mutations/s12-fix/M6_casefold.sql +83 -0
- package/tests/mutations/s12-fix/M7_lookup_ignores_actor.sql +82 -0
- package/tests/mutations/s12-fix/M8_reuse_reports_false.sql +83 -0
- package/tests/mutations/s12-fix/M9_blank_key_admitted.sql +83 -0
- package/tests/mutations/s12-fix/run.sh +288 -0
- package/tests/seeds/20261124000100_agent_board_step_up_enforcement.sql.pre.sql +47 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
-- Drop the #815 isolation guard: a REPEATABLE READ waiter inserts a duplicate again.
|
|
2
|
+
-- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
|
|
3
|
+
-- transaction (pgTAP), never to a shipped migration. Derived from the body in
|
|
4
|
+
-- migrations/20261121000100_agent_board_thread_key_guards.sql.
|
|
5
|
+
create or replace function agent_board.create_or_find_thread(
|
|
6
|
+
p_created_by_agent_id uuid,
|
|
7
|
+
p_subject text,
|
|
8
|
+
p_idempotency_key text,
|
|
9
|
+
p_thread_type text default 'coordination',
|
|
10
|
+
p_metadata jsonb default '{}'::jsonb
|
|
11
|
+
)
|
|
12
|
+
returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
|
|
13
|
+
language plpgsql
|
|
14
|
+
security definer
|
|
15
|
+
set search_path = agent_board, public
|
|
16
|
+
as $$
|
|
17
|
+
declare
|
|
18
|
+
v_actor_agent_id uuid;
|
|
19
|
+
v_key text := nullif(trim(p_idempotency_key), '');
|
|
20
|
+
v_thread agent_board.threads%rowtype;
|
|
21
|
+
v_isolation text := current_setting('transaction_isolation');
|
|
22
|
+
begin
|
|
23
|
+
-- S12FIX mutation M17_isolation_unchecked
|
|
24
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
|
|
25
|
+
if nullif(trim(p_subject), '') is null or v_key is null then
|
|
26
|
+
raise exception 'subject and idempotency key are required' using errcode = '22023';
|
|
27
|
+
end if;
|
|
28
|
+
-- S-12: a non-object would make the stored metadata an array or scalar with no top-level
|
|
29
|
+
-- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
|
|
30
|
+
if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
|
|
31
|
+
raise exception 'metadata must be a JSON object' using errcode = '22023';
|
|
32
|
+
end if;
|
|
33
|
+
|
|
34
|
+
-- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
|
|
35
|
+
-- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
|
|
36
|
+
-- never wait on each other.
|
|
37
|
+
perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
|
|
38
|
+
|
|
39
|
+
select *
|
|
40
|
+
into v_thread
|
|
41
|
+
from agent_board.threads t
|
|
42
|
+
where t.created_by_agent_id = v_actor_agent_id
|
|
43
|
+
and t.metadata ->> 'addressingThreadKey' = v_key
|
|
44
|
+
order by t.created_at desc
|
|
45
|
+
limit 1
|
|
46
|
+
for update;
|
|
47
|
+
|
|
48
|
+
if v_thread.id is not null then
|
|
49
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
|
|
50
|
+
return;
|
|
51
|
+
end if;
|
|
52
|
+
|
|
53
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
|
|
54
|
+
values (
|
|
55
|
+
coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
|
|
56
|
+
v_actor_agent_id,
|
|
57
|
+
trim(p_subject),
|
|
58
|
+
coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
|
|
59
|
+
)
|
|
60
|
+
returning * into v_thread;
|
|
61
|
+
|
|
62
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
|
|
63
|
+
end;
|
|
64
|
+
$$;
|
|
65
|
+
|
|
66
|
+
do $chk$ begin
|
|
67
|
+
if position('S12FIX mutation M17_isolation_unchecked' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
|
|
68
|
+
raise exception 'S-12 fix mutation M17_isolation_unchecked NOT applied';
|
|
69
|
+
end if;
|
|
70
|
+
end $chk$;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
-- Move the #815 isolation guard AFTER the lock: a refused caller now waits on the lock first.
|
|
2
|
+
-- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
|
|
3
|
+
-- transaction (pgTAP), never to a shipped migration. Derived from the body in
|
|
4
|
+
-- migrations/20261121000100_agent_board_thread_key_guards.sql.
|
|
5
|
+
create or replace function agent_board.create_or_find_thread(
|
|
6
|
+
p_created_by_agent_id uuid,
|
|
7
|
+
p_subject text,
|
|
8
|
+
p_idempotency_key text,
|
|
9
|
+
p_thread_type text default 'coordination',
|
|
10
|
+
p_metadata jsonb default '{}'::jsonb
|
|
11
|
+
)
|
|
12
|
+
returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
|
|
13
|
+
language plpgsql
|
|
14
|
+
security definer
|
|
15
|
+
set search_path = agent_board, public
|
|
16
|
+
as $$
|
|
17
|
+
declare
|
|
18
|
+
v_actor_agent_id uuid;
|
|
19
|
+
v_key text := nullif(trim(p_idempotency_key), '');
|
|
20
|
+
v_thread agent_board.threads%rowtype;
|
|
21
|
+
v_isolation text := current_setting('transaction_isolation');
|
|
22
|
+
begin
|
|
23
|
+
-- S12FIX mutation M18_isolation_check_after_lock
|
|
24
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
|
|
25
|
+
if nullif(trim(p_subject), '') is null or v_key is null then
|
|
26
|
+
raise exception 'subject and idempotency key are required' using errcode = '22023';
|
|
27
|
+
end if;
|
|
28
|
+
-- S-12: a non-object would make the stored metadata an array or scalar with no top-level
|
|
29
|
+
-- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
|
|
30
|
+
if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
|
|
31
|
+
raise exception 'metadata must be a JSON object' using errcode = '22023';
|
|
32
|
+
end if;
|
|
33
|
+
-- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
|
|
34
|
+
-- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
|
|
35
|
+
-- never wait on each other.
|
|
36
|
+
perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
|
|
37
|
+
-- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
|
|
38
|
+
-- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
|
|
39
|
+
-- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
|
|
40
|
+
-- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
|
|
41
|
+
if v_isolation <> 'read committed' then
|
|
42
|
+
raise exception
|
|
43
|
+
'create_or_find_thread requires transaction isolation level read committed (got %)',
|
|
44
|
+
v_isolation
|
|
45
|
+
using errcode = '0A000',
|
|
46
|
+
hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
|
|
47
|
+
'where each statement takes a fresh snapshot. Retry this call in a READ '
|
|
48
|
+
'COMMITTED transaction.';
|
|
49
|
+
end if;
|
|
50
|
+
|
|
51
|
+
select *
|
|
52
|
+
into v_thread
|
|
53
|
+
from agent_board.threads t
|
|
54
|
+
where t.created_by_agent_id = v_actor_agent_id
|
|
55
|
+
and t.metadata ->> 'addressingThreadKey' = v_key
|
|
56
|
+
order by t.created_at desc
|
|
57
|
+
limit 1
|
|
58
|
+
for update;
|
|
59
|
+
|
|
60
|
+
if v_thread.id is not null then
|
|
61
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
|
|
62
|
+
return;
|
|
63
|
+
end if;
|
|
64
|
+
|
|
65
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
|
|
66
|
+
values (
|
|
67
|
+
coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
|
|
68
|
+
v_actor_agent_id,
|
|
69
|
+
trim(p_subject),
|
|
70
|
+
coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
|
|
71
|
+
)
|
|
72
|
+
returning * into v_thread;
|
|
73
|
+
|
|
74
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
|
|
75
|
+
end;
|
|
76
|
+
$$;
|
|
77
|
+
|
|
78
|
+
do $chk$ begin
|
|
79
|
+
if position('S12FIX mutation M18_isolation_check_after_lock' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
|
|
80
|
+
raise exception 'S-12 fix mutation M18_isolation_check_after_lock NOT applied';
|
|
81
|
+
end if;
|
|
82
|
+
end $chk$;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
-- Narrow the #815 guard to REPEATABLE READ alone: SERIALIZABLE and READ UNCOMMITTED pass.
|
|
2
|
+
-- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
|
|
3
|
+
-- transaction (pgTAP), never to a shipped migration. Derived from the body in
|
|
4
|
+
-- migrations/20261121000100_agent_board_thread_key_guards.sql.
|
|
5
|
+
create or replace function agent_board.create_or_find_thread(
|
|
6
|
+
p_created_by_agent_id uuid,
|
|
7
|
+
p_subject text,
|
|
8
|
+
p_idempotency_key text,
|
|
9
|
+
p_thread_type text default 'coordination',
|
|
10
|
+
p_metadata jsonb default '{}'::jsonb
|
|
11
|
+
)
|
|
12
|
+
returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
|
|
13
|
+
language plpgsql
|
|
14
|
+
security definer
|
|
15
|
+
set search_path = agent_board, public
|
|
16
|
+
as $$
|
|
17
|
+
declare
|
|
18
|
+
v_actor_agent_id uuid;
|
|
19
|
+
v_key text := nullif(trim(p_idempotency_key), '');
|
|
20
|
+
v_thread agent_board.threads%rowtype;
|
|
21
|
+
v_isolation text := current_setting('transaction_isolation');
|
|
22
|
+
begin
|
|
23
|
+
-- S12FIX mutation M19_isolation_rr_only
|
|
24
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
|
|
25
|
+
if nullif(trim(p_subject), '') is null or v_key is null then
|
|
26
|
+
raise exception 'subject and idempotency key are required' using errcode = '22023';
|
|
27
|
+
end if;
|
|
28
|
+
-- S-12: a non-object would make the stored metadata an array or scalar with no top-level
|
|
29
|
+
-- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
|
|
30
|
+
if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
|
|
31
|
+
raise exception 'metadata must be a JSON object' using errcode = '22023';
|
|
32
|
+
end if;
|
|
33
|
+
-- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
|
|
34
|
+
-- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
|
|
35
|
+
-- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
|
|
36
|
+
-- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
|
|
37
|
+
if v_isolation = 'repeatable read' then
|
|
38
|
+
raise exception
|
|
39
|
+
'create_or_find_thread requires transaction isolation level read committed (got %)',
|
|
40
|
+
v_isolation
|
|
41
|
+
using errcode = '0A000',
|
|
42
|
+
hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
|
|
43
|
+
'where each statement takes a fresh snapshot. Retry this call in a READ '
|
|
44
|
+
'COMMITTED transaction.';
|
|
45
|
+
end if;
|
|
46
|
+
|
|
47
|
+
-- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
|
|
48
|
+
-- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
|
|
49
|
+
-- never wait on each other.
|
|
50
|
+
perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
|
|
51
|
+
|
|
52
|
+
select *
|
|
53
|
+
into v_thread
|
|
54
|
+
from agent_board.threads t
|
|
55
|
+
where t.created_by_agent_id = v_actor_agent_id
|
|
56
|
+
and t.metadata ->> 'addressingThreadKey' = v_key
|
|
57
|
+
order by t.created_at desc
|
|
58
|
+
limit 1
|
|
59
|
+
for update;
|
|
60
|
+
|
|
61
|
+
if v_thread.id is not null then
|
|
62
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
|
|
63
|
+
return;
|
|
64
|
+
end if;
|
|
65
|
+
|
|
66
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
|
|
67
|
+
values (
|
|
68
|
+
coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
|
|
69
|
+
v_actor_agent_id,
|
|
70
|
+
trim(p_subject),
|
|
71
|
+
coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
|
|
72
|
+
)
|
|
73
|
+
returning * into v_thread;
|
|
74
|
+
|
|
75
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
|
|
76
|
+
end;
|
|
77
|
+
$$;
|
|
78
|
+
|
|
79
|
+
do $chk$ begin
|
|
80
|
+
if position('S12FIX mutation M19_isolation_rr_only' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
|
|
81
|
+
raise exception 'S-12 fix mutation M19_isolation_rr_only NOT applied';
|
|
82
|
+
end if;
|
|
83
|
+
end $chk$;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
-- Drop the advisory lock: the pre-fix lock behaviour. Permutations 1 and 2 must revert to the S-12 defect.
|
|
2
|
+
-- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
|
|
3
|
+
-- transaction (pgTAP), never to a shipped migration. Derived from the body in
|
|
4
|
+
-- migrations/20261121000100_agent_board_thread_key_guards.sql.
|
|
5
|
+
create or replace function agent_board.create_or_find_thread(
|
|
6
|
+
p_created_by_agent_id uuid,
|
|
7
|
+
p_subject text,
|
|
8
|
+
p_idempotency_key text,
|
|
9
|
+
p_thread_type text default 'coordination',
|
|
10
|
+
p_metadata jsonb default '{}'::jsonb
|
|
11
|
+
)
|
|
12
|
+
returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
|
|
13
|
+
language plpgsql
|
|
14
|
+
security definer
|
|
15
|
+
set search_path = agent_board, public
|
|
16
|
+
as $$
|
|
17
|
+
declare
|
|
18
|
+
v_actor_agent_id uuid;
|
|
19
|
+
v_key text := nullif(trim(p_idempotency_key), '');
|
|
20
|
+
v_thread agent_board.threads%rowtype;
|
|
21
|
+
v_isolation text := current_setting('transaction_isolation');
|
|
22
|
+
begin
|
|
23
|
+
-- S12FIX mutation M1_lock_dropped
|
|
24
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
|
|
25
|
+
if nullif(trim(p_subject), '') is null or v_key is null then
|
|
26
|
+
raise exception 'subject and idempotency key are required' using errcode = '22023';
|
|
27
|
+
end if;
|
|
28
|
+
-- S-12: a non-object would make the stored metadata an array or scalar with no top-level
|
|
29
|
+
-- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
|
|
30
|
+
if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
|
|
31
|
+
raise exception 'metadata must be a JSON object' using errcode = '22023';
|
|
32
|
+
end if;
|
|
33
|
+
-- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
|
|
34
|
+
-- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
|
|
35
|
+
-- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
|
|
36
|
+
-- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
|
|
37
|
+
if v_isolation <> 'read committed' then
|
|
38
|
+
raise exception
|
|
39
|
+
'create_or_find_thread requires transaction isolation level read committed (got %)',
|
|
40
|
+
v_isolation
|
|
41
|
+
using errcode = '0A000',
|
|
42
|
+
hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
|
|
43
|
+
'where each statement takes a fresh snapshot. Retry this call in a READ '
|
|
44
|
+
'COMMITTED transaction.';
|
|
45
|
+
end if;
|
|
46
|
+
|
|
47
|
+
select *
|
|
48
|
+
into v_thread
|
|
49
|
+
from agent_board.threads t
|
|
50
|
+
where t.created_by_agent_id = v_actor_agent_id
|
|
51
|
+
and t.metadata ->> 'addressingThreadKey' = v_key
|
|
52
|
+
order by t.created_at desc
|
|
53
|
+
limit 1
|
|
54
|
+
for update;
|
|
55
|
+
|
|
56
|
+
if v_thread.id is not null then
|
|
57
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
|
|
58
|
+
return;
|
|
59
|
+
end if;
|
|
60
|
+
|
|
61
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
|
|
62
|
+
values (
|
|
63
|
+
coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
|
|
64
|
+
v_actor_agent_id,
|
|
65
|
+
trim(p_subject),
|
|
66
|
+
coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
|
|
67
|
+
)
|
|
68
|
+
returning * into v_thread;
|
|
69
|
+
|
|
70
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
|
|
71
|
+
end;
|
|
72
|
+
$$;
|
|
73
|
+
|
|
74
|
+
do $chk$ begin
|
|
75
|
+
if position('S12FIX mutation M1_lock_dropped' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
|
|
76
|
+
raise exception 'S-12 fix mutation M1_lock_dropped NOT applied';
|
|
77
|
+
end if;
|
|
78
|
+
end $chk$;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
-- Lock on the key ALONE, without the actor (over-broad, #808's M1b). Different actors must not wait on each other.
|
|
2
|
+
-- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
|
|
3
|
+
-- transaction (pgTAP), never to a shipped migration. Derived from the body in
|
|
4
|
+
-- migrations/20261121000100_agent_board_thread_key_guards.sql.
|
|
5
|
+
create or replace function agent_board.create_or_find_thread(
|
|
6
|
+
p_created_by_agent_id uuid,
|
|
7
|
+
p_subject text,
|
|
8
|
+
p_idempotency_key text,
|
|
9
|
+
p_thread_type text default 'coordination',
|
|
10
|
+
p_metadata jsonb default '{}'::jsonb
|
|
11
|
+
)
|
|
12
|
+
returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
|
|
13
|
+
language plpgsql
|
|
14
|
+
security definer
|
|
15
|
+
set search_path = agent_board, public
|
|
16
|
+
as $$
|
|
17
|
+
declare
|
|
18
|
+
v_actor_agent_id uuid;
|
|
19
|
+
v_key text := nullif(trim(p_idempotency_key), '');
|
|
20
|
+
v_thread agent_board.threads%rowtype;
|
|
21
|
+
v_isolation text := current_setting('transaction_isolation');
|
|
22
|
+
begin
|
|
23
|
+
-- S12FIX mutation M1b_lock_key_only
|
|
24
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
|
|
25
|
+
if nullif(trim(p_subject), '') is null or v_key is null then
|
|
26
|
+
raise exception 'subject and idempotency key are required' using errcode = '22023';
|
|
27
|
+
end if;
|
|
28
|
+
-- S-12: a non-object would make the stored metadata an array or scalar with no top-level
|
|
29
|
+
-- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
|
|
30
|
+
if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
|
|
31
|
+
raise exception 'metadata must be a JSON object' using errcode = '22023';
|
|
32
|
+
end if;
|
|
33
|
+
-- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
|
|
34
|
+
-- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
|
|
35
|
+
-- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
|
|
36
|
+
-- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
|
|
37
|
+
if v_isolation <> 'read committed' then
|
|
38
|
+
raise exception
|
|
39
|
+
'create_or_find_thread requires transaction isolation level read committed (got %)',
|
|
40
|
+
v_isolation
|
|
41
|
+
using errcode = '0A000',
|
|
42
|
+
hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
|
|
43
|
+
'where each statement takes a fresh snapshot. Retry this call in a READ '
|
|
44
|
+
'COMMITTED transaction.';
|
|
45
|
+
end if;
|
|
46
|
+
|
|
47
|
+
-- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
|
|
48
|
+
-- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
|
|
49
|
+
-- never wait on each other.
|
|
50
|
+
perform agent_board._lock_idempotency_key('create_or_find_thread', v_key);
|
|
51
|
+
|
|
52
|
+
select *
|
|
53
|
+
into v_thread
|
|
54
|
+
from agent_board.threads t
|
|
55
|
+
where t.created_by_agent_id = v_actor_agent_id
|
|
56
|
+
and t.metadata ->> 'addressingThreadKey' = v_key
|
|
57
|
+
order by t.created_at desc
|
|
58
|
+
limit 1
|
|
59
|
+
for update;
|
|
60
|
+
|
|
61
|
+
if v_thread.id is not null then
|
|
62
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
|
|
63
|
+
return;
|
|
64
|
+
end if;
|
|
65
|
+
|
|
66
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
|
|
67
|
+
values (
|
|
68
|
+
coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
|
|
69
|
+
v_actor_agent_id,
|
|
70
|
+
trim(p_subject),
|
|
71
|
+
coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
|
|
72
|
+
)
|
|
73
|
+
returning * into v_thread;
|
|
74
|
+
|
|
75
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
|
|
76
|
+
end;
|
|
77
|
+
$$;
|
|
78
|
+
|
|
79
|
+
do $chk$ begin
|
|
80
|
+
if position('S12FIX mutation M1b_lock_key_only' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
|
|
81
|
+
raise exception 'S-12 fix mutation M1b_lock_key_only NOT applied';
|
|
82
|
+
end if;
|
|
83
|
+
end $chk$;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
-- List the two unsafe levels instead of requiring READ COMMITTED: READ UNCOMMITTED passes.
|
|
2
|
+
-- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
|
|
3
|
+
-- transaction (pgTAP), never to a shipped migration. Derived from the body in
|
|
4
|
+
-- migrations/20261121000100_agent_board_thread_key_guards.sql.
|
|
5
|
+
create or replace function agent_board.create_or_find_thread(
|
|
6
|
+
p_created_by_agent_id uuid,
|
|
7
|
+
p_subject text,
|
|
8
|
+
p_idempotency_key text,
|
|
9
|
+
p_thread_type text default 'coordination',
|
|
10
|
+
p_metadata jsonb default '{}'::jsonb
|
|
11
|
+
)
|
|
12
|
+
returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
|
|
13
|
+
language plpgsql
|
|
14
|
+
security definer
|
|
15
|
+
set search_path = agent_board, public
|
|
16
|
+
as $$
|
|
17
|
+
declare
|
|
18
|
+
v_actor_agent_id uuid;
|
|
19
|
+
v_key text := nullif(trim(p_idempotency_key), '');
|
|
20
|
+
v_thread agent_board.threads%rowtype;
|
|
21
|
+
v_isolation text := current_setting('transaction_isolation');
|
|
22
|
+
begin
|
|
23
|
+
-- S12FIX mutation M20_isolation_unsafe_levels_only
|
|
24
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
|
|
25
|
+
if nullif(trim(p_subject), '') is null or v_key is null then
|
|
26
|
+
raise exception 'subject and idempotency key are required' using errcode = '22023';
|
|
27
|
+
end if;
|
|
28
|
+
-- S-12: a non-object would make the stored metadata an array or scalar with no top-level
|
|
29
|
+
-- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
|
|
30
|
+
if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
|
|
31
|
+
raise exception 'metadata must be a JSON object' using errcode = '22023';
|
|
32
|
+
end if;
|
|
33
|
+
-- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
|
|
34
|
+
-- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
|
|
35
|
+
-- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
|
|
36
|
+
-- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
|
|
37
|
+
if v_isolation in ('repeatable read', 'serializable') then
|
|
38
|
+
raise exception
|
|
39
|
+
'create_or_find_thread requires transaction isolation level read committed (got %)',
|
|
40
|
+
v_isolation
|
|
41
|
+
using errcode = '0A000',
|
|
42
|
+
hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
|
|
43
|
+
'where each statement takes a fresh snapshot. Retry this call in a READ '
|
|
44
|
+
'COMMITTED transaction.';
|
|
45
|
+
end if;
|
|
46
|
+
|
|
47
|
+
-- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
|
|
48
|
+
-- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
|
|
49
|
+
-- never wait on each other.
|
|
50
|
+
perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
|
|
51
|
+
|
|
52
|
+
select *
|
|
53
|
+
into v_thread
|
|
54
|
+
from agent_board.threads t
|
|
55
|
+
where t.created_by_agent_id = v_actor_agent_id
|
|
56
|
+
and t.metadata ->> 'addressingThreadKey' = v_key
|
|
57
|
+
order by t.created_at desc
|
|
58
|
+
limit 1
|
|
59
|
+
for update;
|
|
60
|
+
|
|
61
|
+
if v_thread.id is not null then
|
|
62
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
|
|
63
|
+
return;
|
|
64
|
+
end if;
|
|
65
|
+
|
|
66
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
|
|
67
|
+
values (
|
|
68
|
+
coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
|
|
69
|
+
v_actor_agent_id,
|
|
70
|
+
trim(p_subject),
|
|
71
|
+
coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
|
|
72
|
+
)
|
|
73
|
+
returning * into v_thread;
|
|
74
|
+
|
|
75
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
|
|
76
|
+
end;
|
|
77
|
+
$$;
|
|
78
|
+
|
|
79
|
+
do $chk$ begin
|
|
80
|
+
if position('S12FIX mutation M20_isolation_unsafe_levels_only' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
|
|
81
|
+
raise exception 'S-12 fix mutation M20_isolation_unsafe_levels_only NOT applied';
|
|
82
|
+
end if;
|
|
83
|
+
end $chk$;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
-- Move the #815 guard after the reuse return: a caller who finds the thread is no longer refused.
|
|
2
|
+
-- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
|
|
3
|
+
-- transaction (pgTAP), never to a shipped migration. Derived from the body in
|
|
4
|
+
-- migrations/20261121000100_agent_board_thread_key_guards.sql.
|
|
5
|
+
create or replace function agent_board.create_or_find_thread(
|
|
6
|
+
p_created_by_agent_id uuid,
|
|
7
|
+
p_subject text,
|
|
8
|
+
p_idempotency_key text,
|
|
9
|
+
p_thread_type text default 'coordination',
|
|
10
|
+
p_metadata jsonb default '{}'::jsonb
|
|
11
|
+
)
|
|
12
|
+
returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
|
|
13
|
+
language plpgsql
|
|
14
|
+
security definer
|
|
15
|
+
set search_path = agent_board, public
|
|
16
|
+
as $$
|
|
17
|
+
declare
|
|
18
|
+
v_actor_agent_id uuid;
|
|
19
|
+
v_key text := nullif(trim(p_idempotency_key), '');
|
|
20
|
+
v_thread agent_board.threads%rowtype;
|
|
21
|
+
v_isolation text := current_setting('transaction_isolation');
|
|
22
|
+
begin
|
|
23
|
+
-- S12FIX mutation M21_isolation_check_miss_path_only
|
|
24
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
|
|
25
|
+
if nullif(trim(p_subject), '') is null or v_key is null then
|
|
26
|
+
raise exception 'subject and idempotency key are required' using errcode = '22023';
|
|
27
|
+
end if;
|
|
28
|
+
-- S-12: a non-object would make the stored metadata an array or scalar with no top-level
|
|
29
|
+
-- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
|
|
30
|
+
if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
|
|
31
|
+
raise exception 'metadata must be a JSON object' using errcode = '22023';
|
|
32
|
+
end if;
|
|
33
|
+
|
|
34
|
+
-- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
|
|
35
|
+
-- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
|
|
36
|
+
-- never wait on each other.
|
|
37
|
+
perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
|
|
38
|
+
|
|
39
|
+
select *
|
|
40
|
+
into v_thread
|
|
41
|
+
from agent_board.threads t
|
|
42
|
+
where t.created_by_agent_id = v_actor_agent_id
|
|
43
|
+
and t.metadata ->> 'addressingThreadKey' = v_key
|
|
44
|
+
order by t.created_at desc
|
|
45
|
+
limit 1
|
|
46
|
+
for update;
|
|
47
|
+
|
|
48
|
+
if v_thread.id is not null then
|
|
49
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
|
|
50
|
+
return;
|
|
51
|
+
end if;
|
|
52
|
+
-- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
|
|
53
|
+
-- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
|
|
54
|
+
-- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
|
|
55
|
+
-- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
|
|
56
|
+
if v_isolation <> 'read committed' then
|
|
57
|
+
raise exception
|
|
58
|
+
'create_or_find_thread requires transaction isolation level read committed (got %)',
|
|
59
|
+
v_isolation
|
|
60
|
+
using errcode = '0A000',
|
|
61
|
+
hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
|
|
62
|
+
'where each statement takes a fresh snapshot. Retry this call in a READ '
|
|
63
|
+
'COMMITTED transaction.';
|
|
64
|
+
end if;
|
|
65
|
+
|
|
66
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
|
|
67
|
+
values (
|
|
68
|
+
coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
|
|
69
|
+
v_actor_agent_id,
|
|
70
|
+
trim(p_subject),
|
|
71
|
+
coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
|
|
72
|
+
)
|
|
73
|
+
returning * into v_thread;
|
|
74
|
+
|
|
75
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
|
|
76
|
+
end;
|
|
77
|
+
$$;
|
|
78
|
+
|
|
79
|
+
do $chk$ begin
|
|
80
|
+
if position('S12FIX mutation M21_isolation_check_miss_path_only' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
|
|
81
|
+
raise exception 'S-12 fix mutation M21_isolation_check_miss_path_only NOT applied';
|
|
82
|
+
end if;
|
|
83
|
+
end $chk$;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
-- Invert the #815 comparison: READ COMMITTED is refused and every other level admitted.
|
|
2
|
+
-- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
|
|
3
|
+
-- transaction (pgTAP), never to a shipped migration. Derived from the body in
|
|
4
|
+
-- migrations/20261121000100_agent_board_thread_key_guards.sql.
|
|
5
|
+
create or replace function agent_board.create_or_find_thread(
|
|
6
|
+
p_created_by_agent_id uuid,
|
|
7
|
+
p_subject text,
|
|
8
|
+
p_idempotency_key text,
|
|
9
|
+
p_thread_type text default 'coordination',
|
|
10
|
+
p_metadata jsonb default '{}'::jsonb
|
|
11
|
+
)
|
|
12
|
+
returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
|
|
13
|
+
language plpgsql
|
|
14
|
+
security definer
|
|
15
|
+
set search_path = agent_board, public
|
|
16
|
+
as $$
|
|
17
|
+
declare
|
|
18
|
+
v_actor_agent_id uuid;
|
|
19
|
+
v_key text := nullif(trim(p_idempotency_key), '');
|
|
20
|
+
v_thread agent_board.threads%rowtype;
|
|
21
|
+
v_isolation text := current_setting('transaction_isolation');
|
|
22
|
+
begin
|
|
23
|
+
-- S12FIX mutation M22_isolation_inverted
|
|
24
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
|
|
25
|
+
if nullif(trim(p_subject), '') is null or v_key is null then
|
|
26
|
+
raise exception 'subject and idempotency key are required' using errcode = '22023';
|
|
27
|
+
end if;
|
|
28
|
+
-- S-12: a non-object would make the stored metadata an array or scalar with no top-level
|
|
29
|
+
-- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
|
|
30
|
+
if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
|
|
31
|
+
raise exception 'metadata must be a JSON object' using errcode = '22023';
|
|
32
|
+
end if;
|
|
33
|
+
-- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
|
|
34
|
+
-- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
|
|
35
|
+
-- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
|
|
36
|
+
-- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
|
|
37
|
+
if v_isolation = 'read committed' then
|
|
38
|
+
raise exception
|
|
39
|
+
'create_or_find_thread requires transaction isolation level read committed (got %)',
|
|
40
|
+
v_isolation
|
|
41
|
+
using errcode = '0A000',
|
|
42
|
+
hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
|
|
43
|
+
'where each statement takes a fresh snapshot. Retry this call in a READ '
|
|
44
|
+
'COMMITTED transaction.';
|
|
45
|
+
end if;
|
|
46
|
+
|
|
47
|
+
-- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
|
|
48
|
+
-- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
|
|
49
|
+
-- never wait on each other.
|
|
50
|
+
perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
|
|
51
|
+
|
|
52
|
+
select *
|
|
53
|
+
into v_thread
|
|
54
|
+
from agent_board.threads t
|
|
55
|
+
where t.created_by_agent_id = v_actor_agent_id
|
|
56
|
+
and t.metadata ->> 'addressingThreadKey' = v_key
|
|
57
|
+
order by t.created_at desc
|
|
58
|
+
limit 1
|
|
59
|
+
for update;
|
|
60
|
+
|
|
61
|
+
if v_thread.id is not null then
|
|
62
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
|
|
63
|
+
return;
|
|
64
|
+
end if;
|
|
65
|
+
|
|
66
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
|
|
67
|
+
values (
|
|
68
|
+
coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
|
|
69
|
+
v_actor_agent_id,
|
|
70
|
+
trim(p_subject),
|
|
71
|
+
coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
|
|
72
|
+
)
|
|
73
|
+
returning * into v_thread;
|
|
74
|
+
|
|
75
|
+
return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
|
|
76
|
+
end;
|
|
77
|
+
$$;
|
|
78
|
+
|
|
79
|
+
do $chk$ begin
|
|
80
|
+
if position('S12FIX mutation M22_isolation_inverted' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
|
|
81
|
+
raise exception 'S-12 fix mutation M22_isolation_inverted NOT applied';
|
|
82
|
+
end if;
|
|
83
|
+
end $chk$;
|