@consiliency/agent-board-schema 1.76.0 → 1.80.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/fixtures/already-applied.json +16 -0
  2. package/migrations/20261120000200_agent_board_thread_key_serialize.sql +118 -0
  3. package/migrations/20261121000100_agent_board_thread_key_guards.sql +196 -0
  4. package/migrations/20261124000100_agent_board_step_up_enforcement.sql +1213 -0
  5. package/migrations/20261126000100_agent_board_membership_removal_requires_active.sql +243 -0
  6. package/package.json +1 -1
  7. package/schema-manifest.json +39 -3
  8. package/src/deployment-drift-main.test.mjs +89 -6
  9. package/src/deployment-drift.test.mjs +20 -1
  10. package/src/tooling.test.mjs +11 -2
  11. package/tests/agent_board_717_message_replay_scope.test.sql +39 -2
  12. package/tests/agent_board_717_tasking_replay_scope.test.sql +21 -2
  13. package/tests/agent_board_membership_removal_state.test.sql +228 -0
  14. package/tests/agent_board_role_guard.test.sql +3 -1
  15. package/tests/agent_board_send_outcome.test.sql +39 -1
  16. package/tests/agent_board_session_identity_approval_revocation.test.sql +5 -0
  17. package/tests/agent_board_step_up.test.sql +991 -0
  18. package/tests/agent_board_step_up_helper.test.sql +132 -28
  19. package/tests/agent_board_thread_key_guards.test.sql +186 -0
  20. package/tests/agent_board_thread_key_serialize.test.sql +182 -0
  21. package/tests/agent_board_write_enforcement_class.test.sql +3 -2
  22. package/tests/catalog/step_up_gated_closure.json +24 -0
  23. package/tests/fixtures/pgtap-unexposed.sql +9 -4
  24. package/tests/isolation/expected/create_or_find_thread_same_key_race.out +641 -0
  25. package/tests/isolation/expected/session_identity_links.out +4 -0
  26. package/tests/isolation/specs/create_or_find_thread_same_key_race.spec +252 -0
  27. package/tests/isolation/specs/session_identity_links.spec +5 -0
  28. package/tests/mutations/669/run.sh +12 -4
  29. package/tests/mutations/669b/M11_ceil_first.sql +10 -0
  30. package/tests/mutations/669b/M11_gate_first.sql +10 -0
  31. package/tests/mutations/669b/M11_grant_first.sql +10 -0
  32. package/tests/mutations/669b/M11_inv_first.sql +10 -0
  33. package/tests/mutations/669b/M11_mem_first.sql +10 -0
  34. package/tests/mutations/669b/M11_readmit_first.sql +10 -0
  35. package/tests/mutations/669b/M13a_gate_member_preset.sql +12 -0
  36. package/tests/mutations/669b/M13b_ungate_admin_preset.sql +12 -0
  37. package/tests/mutations/669b/M16_actor_is_target.sql +14 -0
  38. package/tests/mutations/669b/M19_drop_readmit.sql +9 -0
  39. package/tests/mutations/669b/M1_drop_grant.sql +9 -0
  40. package/tests/mutations/669b/M20_drop_d5_refusal.sql +9 -0
  41. package/tests/mutations/669b/M21_drop_removal.sql +9 -0
  42. package/tests/mutations/669b/M21b_only_removed.sql +9 -0
  43. package/tests/mutations/669b/M21c_gate_members.sql +12 -0
  44. package/tests/mutations/669b/M21d_requested_state.sql +9 -0
  45. package/tests/mutations/669b/M23b_api_wrapper_over_audit_writer.sql +6 -0
  46. package/tests/mutations/669b/M23c_drop_register_type_refusal.sql +9 -0
  47. package/tests/mutations/669b/M24_drop_kind_refusal.sql +9 -0
  48. package/tests/mutations/669b/M24b_drop_auth_bind_refusal.sql +9 -0
  49. package/tests/mutations/669b/M2a_drop_gate.sql +9 -0
  50. package/tests/mutations/669b/M2b_drop_ceiling.sql +9 -0
  51. package/tests/mutations/669b/M2c_drop_invite.sql +9 -0
  52. package/tests/mutations/669b/MAL2_drop_otp.sql +9 -0
  53. package/tests/mutations/669b/MAL3_admit_passkey.sql +9 -0
  54. package/tests/mutations/669b/MD7a_no_revocation.sql +2 -0
  55. package/tests/mutations/669b/MD7b_overwrite_revoked.sql +2 -0
  56. package/tests/mutations/669b/MD7c_revoke_members.sql +2 -0
  57. package/tests/mutations/669b/MD7d_revoke_expired.sql +2 -0
  58. package/tests/mutations/669b/ME1_wrapper_over_gate.sql +7 -0
  59. package/tests/mutations/669b/MEC2_write_after_helper.sql +10 -0
  60. package/tests/mutations/669b/MQX_query_to_xml_definer.sql +6 -0
  61. package/tests/mutations/669b/MQXb_ts_stat_other_schema.sql +4 -0
  62. package/tests/mutations/669b/MTR_trigger_route.sql +9 -0
  63. package/tests/mutations/669b/MTRb_new_assign.sql +11 -0
  64. package/tests/mutations/669b/MTRc_sync_assigns_value.sql +47 -0
  65. package/tests/mutations/669b/MTX1_tombstone_second_gated_call.sql +15 -0
  66. package/tests/mutations/669b/MTX2_tombstone_non_delegate_target.sql +10 -0
  67. package/tests/mutations/669b/MXS_other_schema_names_gated.sql +5 -0
  68. package/tests/mutations/669b/run.sh +83 -0
  69. package/tests/mutations/670/M18a_read_operation_default_dropped.sql +11 -0
  70. package/tests/mutations/670/M18b_read_operation_default_changed.sql +7 -0
  71. package/tests/mutations/670/M3-DM_helper_on_replay_branch.sql +10 -0
  72. package/tests/mutations/670/M3-MC_helper_on_replay_branch.sql +10 -0
  73. package/tests/mutations/s12/M0_identity.sql +56 -0
  74. package/tests/mutations/s12/M1_serialise_actor_key.sql +57 -0
  75. package/tests/mutations/s12/M1b_serialise_key_only.sql +57 -0
  76. package/tests/mutations/s12/M2_unique_index_no_handler.sql +9 -0
  77. package/tests/mutations/s12/M3_no_trim.sql +56 -0
  78. package/tests/mutations/s12/M4_casefold.sql +56 -0
  79. package/tests/mutations/s12/M5_lookup_ignores_actor.sql +56 -0
  80. package/tests/mutations/s12/M6_reuse_reports_false.sql +56 -0
  81. package/tests/mutations/s12/run.sh +112 -0
  82. package/tests/mutations/s12-fix/M0_identity.sql +83 -0
  83. package/tests/mutations/s12-fix/M10_security_invoker.sql +9 -0
  84. package/tests/mutations/s12-fix/M11_search_path_changed.sql +9 -0
  85. package/tests/mutations/s12-fix/M12_reuse_writes_task.sql +84 -0
  86. package/tests/mutations/s12-fix/M13_dead_inbox_write.sql +84 -0
  87. package/tests/mutations/s12-fix/M14_metadata_unchecked.sql +78 -0
  88. package/tests/mutations/s12-fix/M15_metadata_check_after_lock.sql +84 -0
  89. package/tests/mutations/s12-fix/M16_sql_null_metadata_refused.sql +83 -0
  90. package/tests/mutations/s12-fix/M17_isolation_unchecked.sql +70 -0
  91. package/tests/mutations/s12-fix/M18_isolation_check_after_lock.sql +82 -0
  92. package/tests/mutations/s12-fix/M19_isolation_rr_only.sql +83 -0
  93. package/tests/mutations/s12-fix/M1_lock_dropped.sql +78 -0
  94. package/tests/mutations/s12-fix/M1b_lock_key_only.sql +83 -0
  95. package/tests/mutations/s12-fix/M20_isolation_unsafe_levels_only.sql +83 -0
  96. package/tests/mutations/s12-fix/M21_isolation_check_miss_path_only.sql +83 -0
  97. package/tests/mutations/s12-fix/M22_isolation_inverted.sql +83 -0
  98. package/tests/mutations/s12-fix/M23_isolation_wrong_sqlstate.sql +83 -0
  99. package/tests/mutations/s12-fix/M24_create_thread_key_unchecked.sql +37 -0
  100. package/tests/mutations/s12-fix/M25_create_thread_key_nonnull_only.sql +45 -0
  101. package/tests/mutations/s12-fix/M26_create_thread_key_casefold.sql +45 -0
  102. package/tests/mutations/s12-fix/M27_create_thread_no_object_guard.sql +45 -0
  103. package/tests/mutations/s12-fix/M28_create_thread_key_stripped.sql +40 -0
  104. package/tests/mutations/s12-fix/M29_create_thread_key_nested_refused.sql +45 -0
  105. package/tests/mutations/s12-fix/M2_lock_actor_only.sql +83 -0
  106. package/tests/mutations/s12-fix/M30_create_thread_security_invoker.sql +9 -0
  107. package/tests/mutations/s12-fix/M31_create_thread_search_path_changed.sql +9 -0
  108. package/tests/mutations/s12-fix/M32_class_new_thread_inserter.sql +14 -0
  109. package/tests/mutations/s12-fix/M33_class_metadata_update.sql +12 -0
  110. package/tests/mutations/s12-fix/M33b_class_metadata_update_upper.sql +17 -0
  111. package/tests/mutations/s12-fix/M33c_class_metadata_update_row_form.sql +16 -0
  112. package/tests/mutations/s12-fix/M33d_class_metadata_merge_update.sql +16 -0
  113. package/tests/mutations/s12-fix/M33e_class_metadata_upsert.sql +16 -0
  114. package/tests/mutations/s12-fix/M34_class_api_role_insert.sql +9 -0
  115. package/tests/mutations/s12-fix/M3_lock_after_lookup.sql +83 -0
  116. package/tests/mutations/s12-fix/M4_lock_raw_key.sql +83 -0
  117. package/tests/mutations/s12-fix/M5_no_trim.sql +83 -0
  118. package/tests/mutations/s12-fix/M6_casefold.sql +83 -0
  119. package/tests/mutations/s12-fix/M7_lookup_ignores_actor.sql +82 -0
  120. package/tests/mutations/s12-fix/M8_reuse_reports_false.sql +83 -0
  121. package/tests/mutations/s12-fix/M9_blank_key_admitted.sql +83 -0
  122. package/tests/mutations/s12-fix/run.sh +288 -0
  123. package/tests/seeds/20261124000100_agent_board_step_up_enforcement.sql.pre.sql +47 -0
@@ -0,0 +1,83 @@
1
+ -- Take the lock AFTER the reuse lookup: the second caller waits but has already missed, so it still inserts.
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 M3_lock_after_lookup
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
+ -- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
57
+ -- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
58
+ -- never wait on each other.
59
+ perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
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 M3_lock_after_lookup' 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 M3_lock_after_lookup NOT applied';
82
+ end if;
83
+ end $chk$;
@@ -0,0 +1,83 @@
1
+ -- Lock on the RAW key instead of the normalized one: a trim-equivalent key takes a different lock.
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 M4_lock_raw_key
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 pg_advisory_xact_lock(hashtextextended('create_or_find_thread:' || v_actor_agent_id::text || ':' || p_idempotency_key, 0));
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 M4_lock_raw_key' 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 M4_lock_raw_key NOT applied';
82
+ end if;
83
+ end $chk$;
@@ -0,0 +1,83 @@
1
+ -- Drop the trim() normalization of the key (the lock helper still trims).
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(p_idempotency_key, '');
20
+ v_thread agent_board.threads%rowtype;
21
+ v_isolation text := current_setting('transaction_isolation');
22
+ begin
23
+ -- S12FIX mutation M5_no_trim
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 M5_no_trim' 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 M5_no_trim NOT applied';
82
+ end if;
83
+ end $chk$;
@@ -0,0 +1,83 @@
1
+ -- Add case-folding the shipped contract does not have.
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 := lower(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 M6_casefold
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 M6_casefold' 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 M6_casefold NOT applied';
82
+ end if;
83
+ end $chk$;
@@ -0,0 +1,82 @@
1
+ -- Reuse lookup ignores the actor: any actor's thread under the key is reused.
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 M7_lookup_ignores_actor
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.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 M7_lookup_ignores_actor' 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 M7_lookup_ignores_actor NOT applied';
81
+ end if;
82
+ end $chk$;
@@ -0,0 +1,83 @@
1
+ -- Reuse path reports reused=false.
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 M8_reuse_reports_false
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, false;
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 M8_reuse_reports_false' 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 M8_reuse_reports_false NOT applied';
82
+ end if;
83
+ end $chk$;
@@ -0,0 +1,83 @@
1
+ -- Admit a blank key: drop the `v_key is null` refusal.
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 M9_blank_key_admitted
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 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 M9_blank_key_admitted' 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 M9_blank_key_admitted NOT applied';
82
+ end if;
83
+ end $chk$;