@consiliency/agent-board-schema 1.76.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.
Files changed (121) hide show
  1. package/fixtures/already-applied.json +12 -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/package.json +1 -1
  6. package/schema-manifest.json +30 -3
  7. package/src/deployment-drift-main.test.mjs +89 -6
  8. package/src/deployment-drift.test.mjs +20 -1
  9. package/src/tooling.test.mjs +9 -2
  10. package/tests/agent_board_717_message_replay_scope.test.sql +39 -2
  11. package/tests/agent_board_717_tasking_replay_scope.test.sql +21 -2
  12. package/tests/agent_board_role_guard.test.sql +3 -1
  13. package/tests/agent_board_send_outcome.test.sql +39 -1
  14. package/tests/agent_board_session_identity_approval_revocation.test.sql +5 -0
  15. package/tests/agent_board_step_up.test.sql +991 -0
  16. package/tests/agent_board_step_up_helper.test.sql +132 -28
  17. package/tests/agent_board_thread_key_guards.test.sql +186 -0
  18. package/tests/agent_board_thread_key_serialize.test.sql +182 -0
  19. package/tests/agent_board_write_enforcement_class.test.sql +3 -2
  20. package/tests/catalog/step_up_gated_closure.json +24 -0
  21. package/tests/fixtures/pgtap-unexposed.sql +9 -4
  22. package/tests/isolation/expected/create_or_find_thread_same_key_race.out +641 -0
  23. package/tests/isolation/expected/session_identity_links.out +4 -0
  24. package/tests/isolation/specs/create_or_find_thread_same_key_race.spec +252 -0
  25. package/tests/isolation/specs/session_identity_links.spec +5 -0
  26. package/tests/mutations/669/run.sh +12 -4
  27. package/tests/mutations/669b/M11_ceil_first.sql +10 -0
  28. package/tests/mutations/669b/M11_gate_first.sql +10 -0
  29. package/tests/mutations/669b/M11_grant_first.sql +10 -0
  30. package/tests/mutations/669b/M11_inv_first.sql +10 -0
  31. package/tests/mutations/669b/M11_mem_first.sql +10 -0
  32. package/tests/mutations/669b/M11_readmit_first.sql +10 -0
  33. package/tests/mutations/669b/M13a_gate_member_preset.sql +12 -0
  34. package/tests/mutations/669b/M13b_ungate_admin_preset.sql +12 -0
  35. package/tests/mutations/669b/M16_actor_is_target.sql +14 -0
  36. package/tests/mutations/669b/M19_drop_readmit.sql +9 -0
  37. package/tests/mutations/669b/M1_drop_grant.sql +9 -0
  38. package/tests/mutations/669b/M20_drop_d5_refusal.sql +9 -0
  39. package/tests/mutations/669b/M21_drop_removal.sql +9 -0
  40. package/tests/mutations/669b/M21b_only_removed.sql +9 -0
  41. package/tests/mutations/669b/M21c_gate_members.sql +12 -0
  42. package/tests/mutations/669b/M21d_requested_state.sql +9 -0
  43. package/tests/mutations/669b/M23b_api_wrapper_over_audit_writer.sql +6 -0
  44. package/tests/mutations/669b/M23c_drop_register_type_refusal.sql +9 -0
  45. package/tests/mutations/669b/M24_drop_kind_refusal.sql +9 -0
  46. package/tests/mutations/669b/M24b_drop_auth_bind_refusal.sql +9 -0
  47. package/tests/mutations/669b/M2a_drop_gate.sql +9 -0
  48. package/tests/mutations/669b/M2b_drop_ceiling.sql +9 -0
  49. package/tests/mutations/669b/M2c_drop_invite.sql +9 -0
  50. package/tests/mutations/669b/MAL2_drop_otp.sql +9 -0
  51. package/tests/mutations/669b/MAL3_admit_passkey.sql +9 -0
  52. package/tests/mutations/669b/MD7a_no_revocation.sql +2 -0
  53. package/tests/mutations/669b/MD7b_overwrite_revoked.sql +2 -0
  54. package/tests/mutations/669b/MD7c_revoke_members.sql +2 -0
  55. package/tests/mutations/669b/MD7d_revoke_expired.sql +2 -0
  56. package/tests/mutations/669b/ME1_wrapper_over_gate.sql +7 -0
  57. package/tests/mutations/669b/MEC2_write_after_helper.sql +10 -0
  58. package/tests/mutations/669b/MQX_query_to_xml_definer.sql +6 -0
  59. package/tests/mutations/669b/MQXb_ts_stat_other_schema.sql +4 -0
  60. package/tests/mutations/669b/MTR_trigger_route.sql +9 -0
  61. package/tests/mutations/669b/MTRb_new_assign.sql +11 -0
  62. package/tests/mutations/669b/MTRc_sync_assigns_value.sql +47 -0
  63. package/tests/mutations/669b/MTX1_tombstone_second_gated_call.sql +15 -0
  64. package/tests/mutations/669b/MTX2_tombstone_non_delegate_target.sql +10 -0
  65. package/tests/mutations/669b/MXS_other_schema_names_gated.sql +5 -0
  66. package/tests/mutations/669b/run.sh +83 -0
  67. package/tests/mutations/670/M18a_read_operation_default_dropped.sql +11 -0
  68. package/tests/mutations/670/M18b_read_operation_default_changed.sql +7 -0
  69. package/tests/mutations/670/M3-DM_helper_on_replay_branch.sql +10 -0
  70. package/tests/mutations/670/M3-MC_helper_on_replay_branch.sql +10 -0
  71. package/tests/mutations/s12/M0_identity.sql +56 -0
  72. package/tests/mutations/s12/M1_serialise_actor_key.sql +57 -0
  73. package/tests/mutations/s12/M1b_serialise_key_only.sql +57 -0
  74. package/tests/mutations/s12/M2_unique_index_no_handler.sql +9 -0
  75. package/tests/mutations/s12/M3_no_trim.sql +56 -0
  76. package/tests/mutations/s12/M4_casefold.sql +56 -0
  77. package/tests/mutations/s12/M5_lookup_ignores_actor.sql +56 -0
  78. package/tests/mutations/s12/M6_reuse_reports_false.sql +56 -0
  79. package/tests/mutations/s12/run.sh +112 -0
  80. package/tests/mutations/s12-fix/M0_identity.sql +83 -0
  81. package/tests/mutations/s12-fix/M10_security_invoker.sql +9 -0
  82. package/tests/mutations/s12-fix/M11_search_path_changed.sql +9 -0
  83. package/tests/mutations/s12-fix/M12_reuse_writes_task.sql +84 -0
  84. package/tests/mutations/s12-fix/M13_dead_inbox_write.sql +84 -0
  85. package/tests/mutations/s12-fix/M14_metadata_unchecked.sql +78 -0
  86. package/tests/mutations/s12-fix/M15_metadata_check_after_lock.sql +84 -0
  87. package/tests/mutations/s12-fix/M16_sql_null_metadata_refused.sql +83 -0
  88. package/tests/mutations/s12-fix/M17_isolation_unchecked.sql +70 -0
  89. package/tests/mutations/s12-fix/M18_isolation_check_after_lock.sql +82 -0
  90. package/tests/mutations/s12-fix/M19_isolation_rr_only.sql +83 -0
  91. package/tests/mutations/s12-fix/M1_lock_dropped.sql +78 -0
  92. package/tests/mutations/s12-fix/M1b_lock_key_only.sql +83 -0
  93. package/tests/mutations/s12-fix/M20_isolation_unsafe_levels_only.sql +83 -0
  94. package/tests/mutations/s12-fix/M21_isolation_check_miss_path_only.sql +83 -0
  95. package/tests/mutations/s12-fix/M22_isolation_inverted.sql +83 -0
  96. package/tests/mutations/s12-fix/M23_isolation_wrong_sqlstate.sql +83 -0
  97. package/tests/mutations/s12-fix/M24_create_thread_key_unchecked.sql +37 -0
  98. package/tests/mutations/s12-fix/M25_create_thread_key_nonnull_only.sql +45 -0
  99. package/tests/mutations/s12-fix/M26_create_thread_key_casefold.sql +45 -0
  100. package/tests/mutations/s12-fix/M27_create_thread_no_object_guard.sql +45 -0
  101. package/tests/mutations/s12-fix/M28_create_thread_key_stripped.sql +40 -0
  102. package/tests/mutations/s12-fix/M29_create_thread_key_nested_refused.sql +45 -0
  103. package/tests/mutations/s12-fix/M2_lock_actor_only.sql +83 -0
  104. package/tests/mutations/s12-fix/M30_create_thread_security_invoker.sql +9 -0
  105. package/tests/mutations/s12-fix/M31_create_thread_search_path_changed.sql +9 -0
  106. package/tests/mutations/s12-fix/M32_class_new_thread_inserter.sql +14 -0
  107. package/tests/mutations/s12-fix/M33_class_metadata_update.sql +12 -0
  108. package/tests/mutations/s12-fix/M33b_class_metadata_update_upper.sql +17 -0
  109. package/tests/mutations/s12-fix/M33c_class_metadata_update_row_form.sql +16 -0
  110. package/tests/mutations/s12-fix/M33d_class_metadata_merge_update.sql +16 -0
  111. package/tests/mutations/s12-fix/M33e_class_metadata_upsert.sql +16 -0
  112. package/tests/mutations/s12-fix/M34_class_api_role_insert.sql +9 -0
  113. package/tests/mutations/s12-fix/M3_lock_after_lookup.sql +83 -0
  114. package/tests/mutations/s12-fix/M4_lock_raw_key.sql +83 -0
  115. package/tests/mutations/s12-fix/M5_no_trim.sql +83 -0
  116. package/tests/mutations/s12-fix/M6_casefold.sql +83 -0
  117. package/tests/mutations/s12-fix/M7_lookup_ignores_actor.sql +82 -0
  118. package/tests/mutations/s12-fix/M8_reuse_reports_false.sql +83 -0
  119. package/tests/mutations/s12-fix/M9_blank_key_admitted.sql +83 -0
  120. package/tests/mutations/s12-fix/run.sh +288 -0
  121. package/tests/seeds/20261124000100_agent_board_step_up_enforcement.sql.pre.sql +47 -0
@@ -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$;
@@ -0,0 +1,83 @@
1
+ -- Raise the #815 refusal with 25001 instead of 0A000.
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 M23_isolation_wrong_sqlstate
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 = '25001',
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 M23_isolation_wrong_sqlstate' 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 M23_isolation_wrong_sqlstate NOT applied';
82
+ end if;
83
+ end $chk$;
@@ -0,0 +1,37 @@
1
+ -- Drop the #816 refusal: create_thread stores a caller-supplied addressingThreadKey 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_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_thread_type text default 'coordination',
9
+ p_metadata jsonb default '{}'::jsonb
10
+ )
11
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz)
12
+ language plpgsql
13
+ security definer
14
+ set search_path = agent_board, public
15
+ as $$
16
+ declare
17
+ v_actor_agent_id uuid;
18
+ begin
19
+ -- S12FIX mutation M24_create_thread_key_unchecked
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+
22
+ if nullif(trim(p_subject), '') is null then
23
+ raise exception 'subject is required' using errcode = '22023';
24
+ end if;
25
+
26
+ return query
27
+ insert into agent_board.threads(created_by_agent_id, subject, thread_type, metadata)
28
+ values (v_actor_agent_id, trim(p_subject), coalesce(p_thread_type, 'coordination'), coalesce(p_metadata, '{}'::jsonb))
29
+ returning id, threads.thread_type, threads.subject, threads.status, threads.last_message_sequence, threads.created_at, threads.updated_at;
30
+ end;
31
+ $$;
32
+
33
+ do $chk$ begin
34
+ if position('S12FIX mutation M24_create_thread_key_unchecked' in (select prosrc from pg_proc where oid = 'agent_board.create_thread(uuid,text,text,jsonb)'::regprocedure)) = 0 then
35
+ raise exception 'S-12 fix mutation M24_create_thread_key_unchecked NOT applied';
36
+ end if;
37
+ end $chk$;
@@ -0,0 +1,45 @@
1
+ -- Refuse the reserved key only when its value is non-null: {"addressingThreadKey": null} 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_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_thread_type text default 'coordination',
9
+ p_metadata jsonb default '{}'::jsonb
10
+ )
11
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz)
12
+ language plpgsql
13
+ security definer
14
+ set search_path = agent_board, public
15
+ as $$
16
+ declare
17
+ v_actor_agent_id uuid;
18
+ begin
19
+ -- S12FIX mutation M25_create_thread_key_nonnull_only
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+
22
+ if nullif(trim(p_subject), '') is null then
23
+ raise exception 'subject is required' using errcode = '22023';
24
+ end if;
25
+ -- S-12 (#816): addressingThreadKey is the idempotency key create_or_find_thread stores and looks
26
+ -- up under its (actor, key) lock. This function takes no lock, so letting a caller write the
27
+ -- key here would put a second thread under a pair that function serialises. Reserved by name,
28
+ -- top level of an object only: that is exactly what the lookup reads.
29
+ if jsonb_typeof(p_metadata) = 'object' and p_metadata ->> 'addressingThreadKey' is not null then
30
+ raise exception 'metadata key addressingThreadKey is reserved for create_or_find_thread'
31
+ using errcode = '22023';
32
+ end if;
33
+
34
+ return query
35
+ insert into agent_board.threads(created_by_agent_id, subject, thread_type, metadata)
36
+ values (v_actor_agent_id, trim(p_subject), coalesce(p_thread_type, 'coordination'), coalesce(p_metadata, '{}'::jsonb))
37
+ returning id, threads.thread_type, threads.subject, threads.status, threads.last_message_sequence, threads.created_at, threads.updated_at;
38
+ end;
39
+ $$;
40
+
41
+ do $chk$ begin
42
+ if position('S12FIX mutation M25_create_thread_key_nonnull_only' in (select prosrc from pg_proc where oid = 'agent_board.create_thread(uuid,text,text,jsonb)'::regprocedure)) = 0 then
43
+ raise exception 'S-12 fix mutation M25_create_thread_key_nonnull_only NOT applied';
44
+ end if;
45
+ end $chk$;
@@ -0,0 +1,45 @@
1
+ -- Refuse the key in any letter case: over-broad, the lookup is case-sensitive.
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_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_thread_type text default 'coordination',
9
+ p_metadata jsonb default '{}'::jsonb
10
+ )
11
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz)
12
+ language plpgsql
13
+ security definer
14
+ set search_path = agent_board, public
15
+ as $$
16
+ declare
17
+ v_actor_agent_id uuid;
18
+ begin
19
+ -- S12FIX mutation M26_create_thread_key_casefold
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+
22
+ if nullif(trim(p_subject), '') is null then
23
+ raise exception 'subject is required' using errcode = '22023';
24
+ end if;
25
+ -- S-12 (#816): addressingThreadKey is the idempotency key create_or_find_thread stores and looks
26
+ -- up under its (actor, key) lock. This function takes no lock, so letting a caller write the
27
+ -- key here would put a second thread under a pair that function serialises. Reserved by name,
28
+ -- top level of an object only: that is exactly what the lookup reads.
29
+ if jsonb_typeof(p_metadata) = 'object' and exists (select 1 from jsonb_object_keys(p_metadata) k where lower(k) = lower('addressingThreadKey')) then
30
+ raise exception 'metadata key addressingThreadKey is reserved for create_or_find_thread'
31
+ using errcode = '22023';
32
+ end if;
33
+
34
+ return query
35
+ insert into agent_board.threads(created_by_agent_id, subject, thread_type, metadata)
36
+ values (v_actor_agent_id, trim(p_subject), coalesce(p_thread_type, 'coordination'), coalesce(p_metadata, '{}'::jsonb))
37
+ returning id, threads.thread_type, threads.subject, threads.status, threads.last_message_sequence, threads.created_at, threads.updated_at;
38
+ end;
39
+ $$;
40
+
41
+ do $chk$ begin
42
+ if position('S12FIX mutation M26_create_thread_key_casefold' in (select prosrc from pg_proc where oid = 'agent_board.create_thread(uuid,text,text,jsonb)'::regprocedure)) = 0 then
43
+ raise exception 'S-12 fix mutation M26_create_thread_key_casefold NOT applied';
44
+ end if;
45
+ end $chk$;
@@ -0,0 +1,45 @@
1
+ -- Drop the jsonb_typeof conjunct: `?` is true for an ARRAY holding the string, so it is 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_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_thread_type text default 'coordination',
9
+ p_metadata jsonb default '{}'::jsonb
10
+ )
11
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz)
12
+ language plpgsql
13
+ security definer
14
+ set search_path = agent_board, public
15
+ as $$
16
+ declare
17
+ v_actor_agent_id uuid;
18
+ begin
19
+ -- S12FIX mutation M27_create_thread_no_object_guard
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+
22
+ if nullif(trim(p_subject), '') is null then
23
+ raise exception 'subject is required' using errcode = '22023';
24
+ end if;
25
+ -- S-12 (#816): addressingThreadKey is the idempotency key create_or_find_thread stores and looks
26
+ -- up under its (actor, key) lock. This function takes no lock, so letting a caller write the
27
+ -- key here would put a second thread under a pair that function serialises. Reserved by name,
28
+ -- top level of an object only: that is exactly what the lookup reads.
29
+ if p_metadata ? 'addressingThreadKey' then
30
+ raise exception 'metadata key addressingThreadKey is reserved for create_or_find_thread'
31
+ using errcode = '22023';
32
+ end if;
33
+
34
+ return query
35
+ insert into agent_board.threads(created_by_agent_id, subject, thread_type, metadata)
36
+ values (v_actor_agent_id, trim(p_subject), coalesce(p_thread_type, 'coordination'), coalesce(p_metadata, '{}'::jsonb))
37
+ returning id, threads.thread_type, threads.subject, threads.status, threads.last_message_sequence, threads.created_at, threads.updated_at;
38
+ end;
39
+ $$;
40
+
41
+ do $chk$ begin
42
+ if position('S12FIX mutation M27_create_thread_no_object_guard' in (select prosrc from pg_proc where oid = 'agent_board.create_thread(uuid,text,text,jsonb)'::regprocedure)) = 0 then
43
+ raise exception 'S-12 fix mutation M27_create_thread_no_object_guard NOT applied';
44
+ end if;
45
+ end $chk$;
@@ -0,0 +1,40 @@
1
+ -- Strip the key and insert instead of refusing (the option the ruling rejected).
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_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_thread_type text default 'coordination',
9
+ p_metadata jsonb default '{}'::jsonb
10
+ )
11
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz)
12
+ language plpgsql
13
+ security definer
14
+ set search_path = agent_board, public
15
+ as $$
16
+ declare
17
+ v_actor_agent_id uuid;
18
+ begin
19
+ -- S12FIX mutation M28_create_thread_key_stripped
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+
22
+ if nullif(trim(p_subject), '') is null then
23
+ raise exception 'subject is required' using errcode = '22023';
24
+ end if;
25
+ if jsonb_typeof(p_metadata) = 'object' then
26
+ p_metadata := p_metadata - 'addressingThreadKey';
27
+ end if;
28
+
29
+ return query
30
+ insert into agent_board.threads(created_by_agent_id, subject, thread_type, metadata)
31
+ values (v_actor_agent_id, trim(p_subject), coalesce(p_thread_type, 'coordination'), coalesce(p_metadata, '{}'::jsonb))
32
+ returning id, threads.thread_type, threads.subject, threads.status, threads.last_message_sequence, threads.created_at, threads.updated_at;
33
+ end;
34
+ $$;
35
+
36
+ do $chk$ begin
37
+ if position('S12FIX mutation M28_create_thread_key_stripped' in (select prosrc from pg_proc where oid = 'agent_board.create_thread(uuid,text,text,jsonb)'::regprocedure)) = 0 then
38
+ raise exception 'S-12 fix mutation M28_create_thread_key_stripped NOT applied';
39
+ end if;
40
+ end $chk$;
@@ -0,0 +1,45 @@
1
+ -- Refuse the key at any depth: over-broad, the lookup reads the top level only.
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_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_thread_type text default 'coordination',
9
+ p_metadata jsonb default '{}'::jsonb
10
+ )
11
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz)
12
+ language plpgsql
13
+ security definer
14
+ set search_path = agent_board, public
15
+ as $$
16
+ declare
17
+ v_actor_agent_id uuid;
18
+ begin
19
+ -- S12FIX mutation M29_create_thread_key_nested_refused
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+
22
+ if nullif(trim(p_subject), '') is null then
23
+ raise exception 'subject is required' using errcode = '22023';
24
+ end if;
25
+ -- S-12 (#816): addressingThreadKey is the idempotency key create_or_find_thread stores and looks
26
+ -- up under its (actor, key) lock. This function takes no lock, so letting a caller write the
27
+ -- key here would put a second thread under a pair that function serialises. Reserved by name,
28
+ -- top level of an object only: that is exactly what the lookup reads.
29
+ if jsonb_typeof(p_metadata) = 'object' and jsonb_path_exists(p_metadata, '$.**.addressingThreadKey') then
30
+ raise exception 'metadata key addressingThreadKey is reserved for create_or_find_thread'
31
+ using errcode = '22023';
32
+ end if;
33
+
34
+ return query
35
+ insert into agent_board.threads(created_by_agent_id, subject, thread_type, metadata)
36
+ values (v_actor_agent_id, trim(p_subject), coalesce(p_thread_type, 'coordination'), coalesce(p_metadata, '{}'::jsonb))
37
+ returning id, threads.thread_type, threads.subject, threads.status, threads.last_message_sequence, threads.created_at, threads.updated_at;
38
+ end;
39
+ $$;
40
+
41
+ do $chk$ begin
42
+ if position('S12FIX mutation M29_create_thread_key_nested_refused' in (select prosrc from pg_proc where oid = 'agent_board.create_thread(uuid,text,text,jsonb)'::regprocedure)) = 0 then
43
+ raise exception 'S-12 fix mutation M29_create_thread_key_nested_refused NOT applied';
44
+ end if;
45
+ end $chk$;
@@ -0,0 +1,83 @@
1
+ -- Lock on the actor ALONE (over-broad): every key of one actor serialises. Invisible to the spec; pgTAP must catch it.
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 M2_lock_actor_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_actor_agent_id::text);
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 M2_lock_actor_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 M2_lock_actor_only NOT applied';
82
+ end if;
83
+ end $chk$;
@@ -0,0 +1,9 @@
1
+ -- Make create_thread SECURITY INVOKER: the redefinition must keep SECURITY DEFINER.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ alter function agent_board.create_thread(uuid,text,text,jsonb) security invoker;
4
+
5
+ do $chk$ begin
6
+ if (select prosecdef from pg_proc where oid = 'agent_board.create_thread(uuid,text,text,jsonb)'::regprocedure) then
7
+ raise exception 'S-12 fix mutation M30_create_thread_security_invoker NOT applied';
8
+ end if;
9
+ end $chk$;
@@ -0,0 +1,9 @@
1
+ -- Change create_thread's search_path: the redefinition must keep it pinned.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ alter function agent_board.create_thread(uuid,text,text,jsonb) set search_path = agent_board, public, pg_temp;
4
+
5
+ do $chk$ begin
6
+ if (select proconfig from pg_proc where oid = 'agent_board.create_thread(uuid,text,text,jsonb)'::regprocedure) = array['search_path=agent_board, public']::text[] then
7
+ raise exception 'S-12 fix mutation M31_create_thread_search_path_changed NOT applied';
8
+ end if;
9
+ end $chk$;
@@ -0,0 +1,14 @@
1
+ -- A new function INSERTs into agent_board.threads (any metadata, no key check): TKG-19 must name it.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ create function agent_board.s12_mut_plant_thread(p_actor uuid, p_metadata jsonb)
4
+ returns uuid language sql security definer set search_path = agent_board, public as $f$
5
+ insert into agent_board.threads(created_by_agent_id, subject, thread_type, metadata)
6
+ values (p_actor, 'planted', 'coordination', p_metadata)
7
+ returning id;
8
+ $f$;
9
+
10
+ do $chk$ begin
11
+ if to_regprocedure('agent_board.s12_mut_plant_thread(uuid,jsonb)') is null then
12
+ raise exception 'S-12 fix mutation M32_class_new_thread_inserter NOT applied';
13
+ end if;
14
+ end $chk$;
@@ -0,0 +1,12 @@
1
+ -- A new function assigns threads.metadata in an UPDATE (lower case, one line): TKG-20 must name it.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ create function agent_board.s12_mut_rekey_thread(p_thread uuid, p_key text)
4
+ returns void language sql security definer set search_path = agent_board, public as $f$
5
+ update agent_board.threads set metadata = metadata || jsonb_build_object('addressingThreadKey', p_key) where id = p_thread;
6
+ $f$;
7
+
8
+ do $chk$ begin
9
+ if to_regprocedure('agent_board.s12_mut_rekey_thread(uuid,text)') is null then
10
+ raise exception 'S-12 fix mutation M33_class_metadata_update NOT applied';
11
+ end if;
12
+ end $chk$;
@@ -0,0 +1,17 @@
1
+ -- As M33, but UPPER CASE, unqualified table and the assignment on a later line after another column: TKG-20 must still name it.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ CREATE FUNCTION agent_board.s12_mut_rekey_thread_upper(p_thread uuid, p_key text)
4
+ RETURNS void LANGUAGE plpgsql SECURITY DEFINER SET search_path = agent_board, public AS $f$
5
+ BEGIN
6
+ UPDATE threads
7
+ SET UPDATED_AT = now(),
8
+ METADATA = METADATA || jsonb_build_object('addressingThreadKey', p_key)
9
+ WHERE id = p_thread;
10
+ END
11
+ $f$;
12
+
13
+ do $chk$ begin
14
+ if to_regprocedure('agent_board.s12_mut_rekey_thread_upper(uuid,text)') is null then
15
+ raise exception 'S-12 fix mutation M33b_class_metadata_update_upper NOT applied';
16
+ end if;
17
+ end $chk$;
@@ -0,0 +1,16 @@
1
+ -- A new function assigns threads.metadata through the ROW-FORM SET (`set (updated_at, metadata) = (…)`): TKG-20 must name it. The pre-#820-CI hand regex required `metadata\s*=` and missed this shape.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ create function agent_board.s12_mut_rekey_thread_rowform(p_thread uuid, p_key text)
4
+ returns void language plpgsql security definer set search_path = agent_board, public as $f$
5
+ begin
6
+ update agent_board.threads
7
+ set (updated_at, metadata) = (now(), metadata || jsonb_build_object('addressingThreadKey', p_key))
8
+ where id = p_thread;
9
+ end
10
+ $f$;
11
+
12
+ do $chk$ begin
13
+ if to_regprocedure('agent_board.s12_mut_rekey_thread_rowform(uuid,text)') is null then
14
+ raise exception 'S-12 fix mutation M33c_class_metadata_update_row_form NOT applied';
15
+ end if;
16
+ end $chk$;
@@ -0,0 +1,16 @@
1
+ -- A new function assigns threads.metadata in a MERGE `when matched then update set`: TKG-20 must name it. The pre-#820-CI hand regex anchored on `update <threads>` and missed this shape; the statement is not an INSERT, so TKG-19 must stay silent.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ create function agent_board.s12_mut_rekey_thread_merge(p_thread uuid, p_key text)
4
+ returns void language plpgsql security definer set search_path = agent_board, public as $f$
5
+ begin
6
+ merge into agent_board.threads t
7
+ using (select p_thread as id) s on t.id = s.id
8
+ when matched then update set metadata = t.metadata || jsonb_build_object('addressingThreadKey', p_key);
9
+ end
10
+ $f$;
11
+
12
+ do $chk$ begin
13
+ if to_regprocedure('agent_board.s12_mut_rekey_thread_merge(uuid,text)') is null then
14
+ raise exception 'S-12 fix mutation M33d_class_metadata_merge_update NOT applied';
15
+ end if;
16
+ end $chk$;
@@ -0,0 +1,16 @@
1
+ -- A new function upserts into threads with `on conflict do update set metadata =`: TKG-19 (a new inserter) AND TKG-20 (a metadata assignment) must each name it. The pre-#820-CI hand regex had no upsert arm.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ create function agent_board.s12_mut_rekey_thread_upsert(p_thread uuid, p_key text)
4
+ returns void language plpgsql security definer set search_path = agent_board, public as $f$
5
+ begin
6
+ insert into agent_board.threads (id, subject, metadata)
7
+ values (p_thread, 'x', jsonb_build_object('addressingThreadKey', p_key))
8
+ on conflict (id) do update set metadata = excluded.metadata;
9
+ end
10
+ $f$;
11
+
12
+ do $chk$ begin
13
+ if to_regprocedure('agent_board.s12_mut_rekey_thread_upsert(uuid,text)') is null then
14
+ raise exception 'S-12 fix mutation M33e_class_metadata_upsert NOT applied';
15
+ end if;
16
+ end $chk$;
@@ -0,0 +1,9 @@
1
+ -- Grant an API role INSERT on agent_board.threads directly: TKG-21 must name it.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ grant insert on agent_board.threads to authenticated;
4
+
5
+ do $chk$ begin
6
+ if not has_table_privilege('authenticated', 'agent_board.threads', 'INSERT') then
7
+ raise exception 'S-12 fix mutation M34_class_api_role_insert NOT applied';
8
+ end if;
9
+ end $chk$;