@consiliency/agent-board-schema 1.74.0 → 1.75.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 (51) hide show
  1. package/fixtures/already-applied.json +4 -0
  2. package/migrations/20261108000100_agent_board_send_outcome.sql +708 -0
  3. package/package.json +1 -1
  4. package/schema-manifest.json +12 -3
  5. package/src/deployment-drift-main.test.mjs +20 -3
  6. package/src/deployment-drift-script.test.mjs +77 -2
  7. package/src/deployment-drift.test.mjs +60 -0
  8. package/src/tooling.test.mjs +3 -2
  9. package/tests/agent_board_667_history_class.test.sql +2 -0
  10. package/tests/agent_board_717_native_surface.test.sql +223 -0
  11. package/tests/agent_board_delegate_role_class.test.sql +2 -2
  12. package/tests/agent_board_delivery_writer_execute.test.sql +4 -2
  13. package/tests/agent_board_hosted_delegates.test.sql +8 -1
  14. package/tests/agent_board_send_outcome.test.sql +805 -0
  15. package/tests/agent_board_write_enforcement_class.test.sql +9 -5
  16. package/tests/catalog/function_execute_privileges.json +24 -0
  17. package/tests/catalog/p_metadata_parameters.json +8 -0
  18. package/tests/fixtures/isolation-substrate.sql +8 -0
  19. package/tests/fixtures/pgtap-unexposed.sql +61 -0
  20. package/tests/isolation/expected/send_outcome_cross_board.out +40 -0
  21. package/tests/isolation/expected/send_outcome_independence.out +295 -0
  22. package/tests/isolation/expected/send_outcome_membership.out +140 -0
  23. package/tests/isolation/expected/send_outcome_overlap.out +89 -0
  24. package/tests/isolation/expected/send_outcome_reader.out +87 -0
  25. package/tests/isolation/expected/send_outcome_recovery.out +107 -0
  26. package/tests/isolation/specs/send_outcome_cross_board.spec +66 -0
  27. package/tests/isolation/specs/send_outcome_independence.spec +95 -0
  28. package/tests/isolation/specs/send_outcome_membership.spec +73 -0
  29. package/tests/isolation/specs/send_outcome_overlap.spec +69 -0
  30. package/tests/isolation/specs/send_outcome_reader.spec +61 -0
  31. package/tests/isolation/specs/send_outcome_recovery.spec +68 -0
  32. package/tests/mutations/670/E1_helper_author_from_message.sql +7 -0
  33. package/tests/mutations/670/E2_helper_no_null_check.sql +13 -0
  34. package/tests/mutations/670/M10_lock_removed.sql +10 -0
  35. package/tests/mutations/670/M11_table_granted_to_authenticated.sql +2 -0
  36. package/tests/mutations/670/M12_helper_granted_to_service_role.sql +2 -0
  37. package/tests/mutations/670/M13_message_fk_no_cascade.sql +8 -0
  38. package/tests/mutations/670/M14_post_message_mints_from_metadata.sql +11 -0
  39. package/tests/mutations/670/M15_second_receipt_writer.sql +9 -0
  40. package/tests/mutations/670/M16_read_no_btrim.sql +7 -0
  41. package/tests/mutations/670/M17_read_volatile.sql +2 -0
  42. package/tests/mutations/670/M1_helper_no_service_role_guard.sql +13 -0
  43. package/tests/mutations/670/M3_helper_on_replay_branch.sql +10 -0
  44. package/tests/mutations/670/M4_read_checks_thread_existence.sql +10 -0
  45. package/tests/mutations/670/M5a_read_requires_thread_access.sql +10 -0
  46. package/tests/mutations/670/M5b_read_requires_board_membership.sql +11 -0
  47. package/tests/mutations/670/M6_read_admits_service_role.sql +14 -0
  48. package/tests/mutations/670/M7_confirmed_adds_message_sequence.sql +12 -0
  49. package/tests/mutations/670/M8_delivery_swallows_errors.sql +19 -0
  50. package/tests/mutations/670/M9_lock_key_includes_board.sql +10 -0
  51. package/tests/mutations/670/run.sh +84 -0
@@ -0,0 +1,73 @@
1
+ # #670 D12 spec 3: membership removal against the send and the read (author M, member of X, its own thread TM).
2
+ # (1) the send commits, then M leaves X: M's read is still confirmed, and a fresh send with a new key
3
+ # is refused 42501;
4
+ # (2) M leaves first: the send is refused 42501 and no receipt exists, so the read is unknown;
5
+ # (3) removal DURING a lock wait: h holds M's (thread, key) idempotency lock, M's send waits on it,
6
+ # M leaves and commits, h releases, and the send's post-wait access recheck refuses it (42501) --
7
+ # no message, no receipt.
8
+ setup
9
+ {
10
+ create table public.so670_iso3_pids(s text primary key, pid int not null);
11
+ create table public.so670_iso3_ret(s text not null, message_id uuid);
12
+ grant select, insert on public.so670_iso3_ret to authenticated, service_role;
13
+ insert into auth.users(id) values ('670b3000-0000-4000-8000-0000000000a1'),('670b3000-0000-4000-8000-0000000000b1'),('670b3000-0000-4000-8000-0000000000c1');
14
+ insert into agent_board.agents(id,agent_type,handle,display_name,status,auth_user_id)
15
+ values ('670b3000-0000-4000-8000-000000000001','agent','so670-iso3-x-a','SO670 iso A','active','670b3000-0000-4000-8000-0000000000a1'),
16
+ ('670b3000-0000-4000-8000-000000000002','agent','so670-iso3-x-b','SO670 iso B','active','670b3000-0000-4000-8000-0000000000b1'),
17
+ ('670b3000-0000-4000-8000-000000000003','agent','so670-iso3-x-m','SO670 iso M','active','670b3000-0000-4000-8000-0000000000c1');
18
+ insert into agent_board.boards(id,slug,name,created_by_agent_id,visibility,status)
19
+ values ('670b3000-0000-4000-8000-000000000020','so670-iso3-x','SO670 iso X','670b3000-0000-4000-8000-000000000001','private','active'),
20
+ ('670b3000-0000-4000-8000-000000000021','so670-iso3-y','SO670 iso Y','670b3000-0000-4000-8000-000000000001','private','active');
21
+ select set_config('request.jwt.claim.role','service_role',false);
22
+ insert into agent_board.board_memberships(board_id,agent_id,membership_state,created_by_agent_id,role)
23
+ values ('670b3000-0000-4000-8000-000000000020','670b3000-0000-4000-8000-000000000001','joined','670b3000-0000-4000-8000-000000000001','owner'),('670b3000-0000-4000-8000-000000000020','670b3000-0000-4000-8000-000000000002','joined','670b3000-0000-4000-8000-000000000001','member'),
24
+ ('670b3000-0000-4000-8000-000000000020','670b3000-0000-4000-8000-000000000003','joined','670b3000-0000-4000-8000-000000000001','member'),('670b3000-0000-4000-8000-000000000021','670b3000-0000-4000-8000-000000000001','joined','670b3000-0000-4000-8000-000000000001','owner');
25
+ select set_config('request.jwt.claim.role','',false);
26
+ insert into agent_board.threads(id,thread_type,created_by_agent_id,subject)
27
+ values ('670b3000-0000-4000-8000-000000000010','coordination','670b3000-0000-4000-8000-000000000001','so670 iso T'),('670b3000-0000-4000-8000-000000000011','coordination','670b3000-0000-4000-8000-000000000001','so670 iso T2'),
28
+ ('670b3000-0000-4000-8000-000000000012','coordination','670b3000-0000-4000-8000-000000000003','so670 iso TM');
29
+ }
30
+ teardown
31
+ {
32
+ delete from agent_board.inbox_items where recipient_agent_id in ('670b3000-0000-4000-8000-000000000001','670b3000-0000-4000-8000-000000000002','670b3000-0000-4000-8000-000000000003');
33
+ delete from agent_board.callback_outbox_events where recipient_agent_id in ('670b3000-0000-4000-8000-000000000001','670b3000-0000-4000-8000-000000000002','670b3000-0000-4000-8000-000000000003');
34
+ delete from agent_board.delivery_recipients where recipient_agent_id in ('670b3000-0000-4000-8000-000000000001','670b3000-0000-4000-8000-000000000002','670b3000-0000-4000-8000-000000000003');
35
+ delete from agent_board.messages where thread_id in ('670b3000-0000-4000-8000-000000000010','670b3000-0000-4000-8000-000000000011','670b3000-0000-4000-8000-000000000012');
36
+ delete from agent_board.threads where id in ('670b3000-0000-4000-8000-000000000010','670b3000-0000-4000-8000-000000000011','670b3000-0000-4000-8000-000000000012');
37
+ delete from agent_board.send_authz_telemetry where actor_agent_id in ('670b3000-0000-4000-8000-000000000001','670b3000-0000-4000-8000-000000000002','670b3000-0000-4000-8000-000000000003');
38
+ delete from agent_board.board_membership_events where board_id in ('670b3000-0000-4000-8000-000000000020','670b3000-0000-4000-8000-000000000021');
39
+ delete from agent_board.boards where id in ('670b3000-0000-4000-8000-000000000020','670b3000-0000-4000-8000-000000000021');
40
+ delete from agent_board.agents where id in ('670b3000-0000-4000-8000-000000000001','670b3000-0000-4000-8000-000000000002','670b3000-0000-4000-8000-000000000003');
41
+ delete from auth.users where id in ('670b3000-0000-4000-8000-0000000000a1','670b3000-0000-4000-8000-0000000000b1','670b3000-0000-4000-8000-0000000000c1');
42
+ drop table public.so670_iso3_pids;
43
+ drop table public.so670_iso3_ret;
44
+ }
45
+ session "p"
46
+ setup { reset role; insert into public.so670_iso3_pids values ('p', pg_backend_pid()) on conflict (s) do update set pid = excluded.pid; }
47
+ step "p_begin" { begin; set local role authenticated; select set_config('request.jwt.claim.role','',true), set_config('request.jwt.claim.sub','',true), set_config('request.jwt.claims',json_build_object('role','authenticated','sub','670b3000-0000-4000-8000-0000000000c1')::text,true); }
48
+ step "p_post" { insert into public.so670_iso3_ret select 'p', message_id from agent_board.post_board_message('670b3000-0000-4000-8000-000000000012','670b3000-0000-4000-8000-000000000003','670b3000-0000-4000-8000-000000000020','iso','so670 body','comment','medium','K',jsonb_build_object()); }
49
+ step "p_commit" { commit; }
50
+ step "p_end" { end; }
51
+ session "l"
52
+ setup { reset role; insert into public.so670_iso3_pids values ('l', pg_backend_pid()) on conflict (s) do update set pid = excluded.pid; set role authenticated; select set_config('request.jwt.claims',json_build_object('role','authenticated','sub','670b3000-0000-4000-8000-0000000000c1')::text,false); }
53
+ step "l_leave" { select count(*) as left_rows from agent_board.leave_board('670b3000-0000-4000-8000-000000000020','670b3000-0000-4000-8000-000000000003',jsonb_build_object()); }
54
+ step "r_read" { select status from agent_board.get_send_outcome('670b3000-0000-4000-8000-000000000012','K','post_board_message'); }
55
+ step "r_fresh" { insert into public.so670_iso3_ret select 'fresh', message_id from agent_board.post_board_message('670b3000-0000-4000-8000-000000000012','670b3000-0000-4000-8000-000000000003','670b3000-0000-4000-8000-000000000020','iso','so670 body','comment','medium','K2',jsonb_build_object()); }
56
+ session "h"
57
+ step "h_begin" { begin; }
58
+ step "h_lock" { select agent_board._lock_idempotency_key('post_board_message:' || '670b3000-0000-4000-8000-000000000012' || ':' || '670b3000-0000-4000-8000-000000000003', 'K'); }
59
+ step "h_commit" { commit; }
60
+ session "m"
61
+ step "m_lockwait" { select l.locktype, l.mode from pg_locks l join public.so670_iso3_pids p on p.pid = l.pid where p.s = 'p' and not l.granted order by 1, 2; }
62
+ session "check"
63
+ step "check" {
64
+ select (select count(*) from agent_board.messages where thread_id in ('670b3000-0000-4000-8000-000000000012')) as messages,
65
+ (select count(*) from agent_board.send_outcomes where thread_id in ('670b3000-0000-4000-8000-000000000012')) as receipts,
66
+ (select string_agg(so.operation || ':' || a.handle, ',' order by so.operation, a.handle) from agent_board.send_outcomes so join agent_board.agents a on a.id = so.author_agent_id where so.thread_id in ('670b3000-0000-4000-8000-000000000012')) as receipt_owners,
67
+ (select string_agg(r.s, ',' order by r.s) from public.so670_iso3_ret r) as returned_by,
68
+ (select count(distinct r.message_id) from public.so670_iso3_ret r) as distinct_returned,
69
+ (select coalesce(bool_and(exists (select 1 from agent_board.send_outcomes so where so.message_id = r.message_id)), false) from public.so670_iso3_ret r) as every_returned_has_receipt;
70
+ }
71
+ permutation "p_begin" "p_post" "p_commit" "l_leave" "r_read" "r_fresh" "check"
72
+ permutation "l_leave" "p_begin" "p_post" "p_end" "r_read" "check"
73
+ permutation "h_begin" "h_lock" "p_begin" "p_post" "m_lockwait" "l_leave" "h_commit" "p_end" "r_read" "check"
@@ -0,0 +1,69 @@
1
+ # #670 D12 spec 1: two same-author, same-thread, same-board, same-key post_board_message calls.
2
+ # The second waits on the (thread, author, key) advisory idempotency lock -- the lockwait step names the
3
+ # lock it waits on, so removing that lock (D11 M10) changes this trace even though the thread row lock
4
+ # would still serialize the two posts. First commit: the second returns the FIRST transaction's
5
+ # message_id (returned_by p,w with one distinct id), with one message and one receipt; its changed body
6
+ # is the #717-permitted replay (D2), and the replay re-fans out to the board without duplicating B's
7
+ # delivery. First rollback: the second succeeds once, as the only message and receipt.
8
+ setup
9
+ {
10
+ create table public.so670_iso1_pids(s text primary key, pid int not null);
11
+ create table public.so670_iso1_ret(s text not null, message_id uuid);
12
+ grant select, insert on public.so670_iso1_ret to authenticated, service_role;
13
+ insert into auth.users(id) values ('670b1000-0000-4000-8000-0000000000a1'),('670b1000-0000-4000-8000-0000000000b1'),('670b1000-0000-4000-8000-0000000000c1');
14
+ insert into agent_board.agents(id,agent_type,handle,display_name,status,auth_user_id)
15
+ values ('670b1000-0000-4000-8000-000000000001','agent','so670-iso1-x-a','SO670 iso A','active','670b1000-0000-4000-8000-0000000000a1'),
16
+ ('670b1000-0000-4000-8000-000000000002','agent','so670-iso1-x-b','SO670 iso B','active','670b1000-0000-4000-8000-0000000000b1'),
17
+ ('670b1000-0000-4000-8000-000000000003','agent','so670-iso1-x-m','SO670 iso M','active','670b1000-0000-4000-8000-0000000000c1');
18
+ insert into agent_board.boards(id,slug,name,created_by_agent_id,visibility,status)
19
+ values ('670b1000-0000-4000-8000-000000000020','so670-iso1-x','SO670 iso X','670b1000-0000-4000-8000-000000000001','private','active'),
20
+ ('670b1000-0000-4000-8000-000000000021','so670-iso1-y','SO670 iso Y','670b1000-0000-4000-8000-000000000001','private','active');
21
+ select set_config('request.jwt.claim.role','service_role',false);
22
+ insert into agent_board.board_memberships(board_id,agent_id,membership_state,created_by_agent_id,role)
23
+ values ('670b1000-0000-4000-8000-000000000020','670b1000-0000-4000-8000-000000000001','joined','670b1000-0000-4000-8000-000000000001','owner'),('670b1000-0000-4000-8000-000000000020','670b1000-0000-4000-8000-000000000002','joined','670b1000-0000-4000-8000-000000000001','member'),
24
+ ('670b1000-0000-4000-8000-000000000020','670b1000-0000-4000-8000-000000000003','joined','670b1000-0000-4000-8000-000000000001','member'),('670b1000-0000-4000-8000-000000000021','670b1000-0000-4000-8000-000000000001','joined','670b1000-0000-4000-8000-000000000001','owner');
25
+ select set_config('request.jwt.claim.role','',false);
26
+ insert into agent_board.threads(id,thread_type,created_by_agent_id,subject)
27
+ values ('670b1000-0000-4000-8000-000000000010','coordination','670b1000-0000-4000-8000-000000000001','so670 iso T'),('670b1000-0000-4000-8000-000000000011','coordination','670b1000-0000-4000-8000-000000000001','so670 iso T2'),
28
+ ('670b1000-0000-4000-8000-000000000012','coordination','670b1000-0000-4000-8000-000000000003','so670 iso TM');
29
+ }
30
+ teardown
31
+ {
32
+ delete from agent_board.inbox_items where recipient_agent_id in ('670b1000-0000-4000-8000-000000000001','670b1000-0000-4000-8000-000000000002','670b1000-0000-4000-8000-000000000003');
33
+ delete from agent_board.callback_outbox_events where recipient_agent_id in ('670b1000-0000-4000-8000-000000000001','670b1000-0000-4000-8000-000000000002','670b1000-0000-4000-8000-000000000003');
34
+ delete from agent_board.delivery_recipients where recipient_agent_id in ('670b1000-0000-4000-8000-000000000001','670b1000-0000-4000-8000-000000000002','670b1000-0000-4000-8000-000000000003');
35
+ delete from agent_board.messages where thread_id in ('670b1000-0000-4000-8000-000000000010','670b1000-0000-4000-8000-000000000011','670b1000-0000-4000-8000-000000000012');
36
+ delete from agent_board.threads where id in ('670b1000-0000-4000-8000-000000000010','670b1000-0000-4000-8000-000000000011','670b1000-0000-4000-8000-000000000012');
37
+ delete from agent_board.send_authz_telemetry where actor_agent_id in ('670b1000-0000-4000-8000-000000000001','670b1000-0000-4000-8000-000000000002','670b1000-0000-4000-8000-000000000003');
38
+ delete from agent_board.board_membership_events where board_id in ('670b1000-0000-4000-8000-000000000020','670b1000-0000-4000-8000-000000000021');
39
+ delete from agent_board.boards where id in ('670b1000-0000-4000-8000-000000000020','670b1000-0000-4000-8000-000000000021');
40
+ delete from agent_board.agents where id in ('670b1000-0000-4000-8000-000000000001','670b1000-0000-4000-8000-000000000002','670b1000-0000-4000-8000-000000000003');
41
+ delete from auth.users where id in ('670b1000-0000-4000-8000-0000000000a1','670b1000-0000-4000-8000-0000000000b1','670b1000-0000-4000-8000-0000000000c1');
42
+ drop table public.so670_iso1_pids;
43
+ drop table public.so670_iso1_ret;
44
+ }
45
+ session "p"
46
+ setup { reset role; insert into public.so670_iso1_pids values ('p', pg_backend_pid()) on conflict (s) do update set pid = excluded.pid; }
47
+ step "p_begin" { begin; set local role authenticated; select set_config('request.jwt.claim.role','',true), set_config('request.jwt.claim.sub','',true), set_config('request.jwt.claims',json_build_object('role','authenticated','sub','670b1000-0000-4000-8000-0000000000a1')::text,true); }
48
+ step "p_post" { insert into public.so670_iso1_ret select 'p', message_id from agent_board.post_board_message('670b1000-0000-4000-8000-000000000010','670b1000-0000-4000-8000-000000000001','670b1000-0000-4000-8000-000000000020','iso','first body','comment','medium','K',jsonb_build_object()); }
49
+ step "p_commit" { commit; }
50
+ step "p_rollback" { rollback; }
51
+ session "w"
52
+ setup { reset role; insert into public.so670_iso1_pids values ('w', pg_backend_pid()) on conflict (s) do update set pid = excluded.pid; }
53
+ step "w_begin" { begin; set local role authenticated; select set_config('request.jwt.claim.role','',true), set_config('request.jwt.claim.sub','',true), set_config('request.jwt.claims',json_build_object('role','authenticated','sub','670b1000-0000-4000-8000-0000000000a1')::text,true); }
54
+ step "w_post" { insert into public.so670_iso1_ret select 'w', message_id from agent_board.post_board_message('670b1000-0000-4000-8000-000000000010','670b1000-0000-4000-8000-000000000001','670b1000-0000-4000-8000-000000000020','iso','changed retry body','comment','medium','K',jsonb_build_object()); }
55
+ step "w_commit" { commit; }
56
+ session "m"
57
+ step "m_lockwait" { select l.locktype, l.mode from pg_locks l join public.so670_iso1_pids p on p.pid = l.pid where p.s = 'w' and not l.granted order by 1, 2; }
58
+ session "check"
59
+ step "check" {
60
+ select (select count(*) from agent_board.messages where thread_id in ('670b1000-0000-4000-8000-000000000010')) as messages,
61
+ (select count(*) from agent_board.send_outcomes where thread_id in ('670b1000-0000-4000-8000-000000000010')) as receipts,
62
+ (select string_agg(so.operation || ':' || a.handle, ',' order by so.operation, a.handle) from agent_board.send_outcomes so join agent_board.agents a on a.id = so.author_agent_id where so.thread_id in ('670b1000-0000-4000-8000-000000000010')) as receipt_owners,
63
+ (select string_agg(r.s, ',' order by r.s) from public.so670_iso1_ret r) as returned_by,
64
+ (select count(distinct r.message_id) from public.so670_iso1_ret r) as distinct_returned,
65
+ (select coalesce(bool_and(exists (select 1 from agent_board.send_outcomes so where so.message_id = r.message_id)), false) from public.so670_iso1_ret r) as every_returned_has_receipt;
66
+ }
67
+ step "b_deliveries" { select count(*) as b_deliveries from agent_board.delivery_recipients dr join agent_board.messages m on m.id = dr.source_message_id where m.thread_id = '670b1000-0000-4000-8000-000000000010' and dr.recipient_agent_id = '670b1000-0000-4000-8000-000000000002'; }
68
+ permutation "p_begin" "p_post" "w_begin" "w_post" "m_lockwait" "p_commit" "w_commit" "check" "b_deliveries"
69
+ permutation "p_begin" "p_post" "w_begin" "w_post" "m_lockwait" "p_rollback" "w_commit" "check" "b_deliveries"
@@ -0,0 +1,61 @@
1
+ # #670 D12 spec 2: the author reads get_send_outcome before, during and after its own writer commits.
2
+ # Before and during: unknown (the uncommitted receipt is invisible, and the read does not wait for it).
3
+ # After commit: confirmed. A read racing a rollback stays unknown before and after: absence is not a
4
+ # concurrency fence, and unknown never means "not sent".
5
+ setup
6
+ {
7
+ create table public.so670_iso2_pids(s text primary key, pid int not null);
8
+ create table public.so670_iso2_ret(s text not null, message_id uuid);
9
+ grant select, insert on public.so670_iso2_ret to authenticated, service_role;
10
+ insert into auth.users(id) values ('670b2000-0000-4000-8000-0000000000a1'),('670b2000-0000-4000-8000-0000000000b1'),('670b2000-0000-4000-8000-0000000000c1');
11
+ insert into agent_board.agents(id,agent_type,handle,display_name,status,auth_user_id)
12
+ values ('670b2000-0000-4000-8000-000000000001','agent','so670-iso2-x-a','SO670 iso A','active','670b2000-0000-4000-8000-0000000000a1'),
13
+ ('670b2000-0000-4000-8000-000000000002','agent','so670-iso2-x-b','SO670 iso B','active','670b2000-0000-4000-8000-0000000000b1'),
14
+ ('670b2000-0000-4000-8000-000000000003','agent','so670-iso2-x-m','SO670 iso M','active','670b2000-0000-4000-8000-0000000000c1');
15
+ insert into agent_board.boards(id,slug,name,created_by_agent_id,visibility,status)
16
+ values ('670b2000-0000-4000-8000-000000000020','so670-iso2-x','SO670 iso X','670b2000-0000-4000-8000-000000000001','private','active'),
17
+ ('670b2000-0000-4000-8000-000000000021','so670-iso2-y','SO670 iso Y','670b2000-0000-4000-8000-000000000001','private','active');
18
+ select set_config('request.jwt.claim.role','service_role',false);
19
+ insert into agent_board.board_memberships(board_id,agent_id,membership_state,created_by_agent_id,role)
20
+ values ('670b2000-0000-4000-8000-000000000020','670b2000-0000-4000-8000-000000000001','joined','670b2000-0000-4000-8000-000000000001','owner'),('670b2000-0000-4000-8000-000000000020','670b2000-0000-4000-8000-000000000002','joined','670b2000-0000-4000-8000-000000000001','member'),
21
+ ('670b2000-0000-4000-8000-000000000020','670b2000-0000-4000-8000-000000000003','joined','670b2000-0000-4000-8000-000000000001','member'),('670b2000-0000-4000-8000-000000000021','670b2000-0000-4000-8000-000000000001','joined','670b2000-0000-4000-8000-000000000001','owner');
22
+ select set_config('request.jwt.claim.role','',false);
23
+ insert into agent_board.threads(id,thread_type,created_by_agent_id,subject)
24
+ values ('670b2000-0000-4000-8000-000000000010','coordination','670b2000-0000-4000-8000-000000000001','so670 iso T'),('670b2000-0000-4000-8000-000000000011','coordination','670b2000-0000-4000-8000-000000000001','so670 iso T2'),
25
+ ('670b2000-0000-4000-8000-000000000012','coordination','670b2000-0000-4000-8000-000000000003','so670 iso TM');
26
+ }
27
+ teardown
28
+ {
29
+ delete from agent_board.inbox_items where recipient_agent_id in ('670b2000-0000-4000-8000-000000000001','670b2000-0000-4000-8000-000000000002','670b2000-0000-4000-8000-000000000003');
30
+ delete from agent_board.callback_outbox_events where recipient_agent_id in ('670b2000-0000-4000-8000-000000000001','670b2000-0000-4000-8000-000000000002','670b2000-0000-4000-8000-000000000003');
31
+ delete from agent_board.delivery_recipients where recipient_agent_id in ('670b2000-0000-4000-8000-000000000001','670b2000-0000-4000-8000-000000000002','670b2000-0000-4000-8000-000000000003');
32
+ delete from agent_board.messages where thread_id in ('670b2000-0000-4000-8000-000000000010','670b2000-0000-4000-8000-000000000011','670b2000-0000-4000-8000-000000000012');
33
+ delete from agent_board.threads where id in ('670b2000-0000-4000-8000-000000000010','670b2000-0000-4000-8000-000000000011','670b2000-0000-4000-8000-000000000012');
34
+ delete from agent_board.send_authz_telemetry where actor_agent_id in ('670b2000-0000-4000-8000-000000000001','670b2000-0000-4000-8000-000000000002','670b2000-0000-4000-8000-000000000003');
35
+ delete from agent_board.board_membership_events where board_id in ('670b2000-0000-4000-8000-000000000020','670b2000-0000-4000-8000-000000000021');
36
+ delete from agent_board.boards where id in ('670b2000-0000-4000-8000-000000000020','670b2000-0000-4000-8000-000000000021');
37
+ delete from agent_board.agents where id in ('670b2000-0000-4000-8000-000000000001','670b2000-0000-4000-8000-000000000002','670b2000-0000-4000-8000-000000000003');
38
+ delete from auth.users where id in ('670b2000-0000-4000-8000-0000000000a1','670b2000-0000-4000-8000-0000000000b1','670b2000-0000-4000-8000-0000000000c1');
39
+ drop table public.so670_iso2_pids;
40
+ drop table public.so670_iso2_ret;
41
+ }
42
+ session "p"
43
+ setup { reset role; insert into public.so670_iso2_pids values ('p', pg_backend_pid()) on conflict (s) do update set pid = excluded.pid; }
44
+ step "p_begin" { begin; set local role authenticated; select set_config('request.jwt.claim.role','',true), set_config('request.jwt.claim.sub','',true), set_config('request.jwt.claims',json_build_object('role','authenticated','sub','670b2000-0000-4000-8000-0000000000a1')::text,true); }
45
+ step "p_post" { insert into public.so670_iso2_ret select 'p', message_id from agent_board.post_board_message('670b2000-0000-4000-8000-000000000010','670b2000-0000-4000-8000-000000000001','670b2000-0000-4000-8000-000000000020','iso','so670 body','comment','medium','K',jsonb_build_object()); }
46
+ step "p_commit" { commit; }
47
+ step "p_rollback" { rollback; }
48
+ session "r"
49
+ setup { reset role; insert into public.so670_iso2_pids values ('r', pg_backend_pid()) on conflict (s) do update set pid = excluded.pid; set role authenticated; select set_config('request.jwt.claims',json_build_object('role','authenticated','sub','670b2000-0000-4000-8000-0000000000a1')::text,false); }
50
+ step "r_read" { select status from agent_board.get_send_outcome('670b2000-0000-4000-8000-000000000010','K','post_board_message'); }
51
+ session "check"
52
+ step "check" {
53
+ select (select count(*) from agent_board.messages where thread_id in ('670b2000-0000-4000-8000-000000000010')) as messages,
54
+ (select count(*) from agent_board.send_outcomes where thread_id in ('670b2000-0000-4000-8000-000000000010')) as receipts,
55
+ (select string_agg(so.operation || ':' || a.handle, ',' order by so.operation, a.handle) from agent_board.send_outcomes so join agent_board.agents a on a.id = so.author_agent_id where so.thread_id in ('670b2000-0000-4000-8000-000000000010')) as receipt_owners,
56
+ (select string_agg(r.s, ',' order by r.s) from public.so670_iso2_ret r) as returned_by,
57
+ (select count(distinct r.message_id) from public.so670_iso2_ret r) as distinct_returned,
58
+ (select coalesce(bool_and(exists (select 1 from agent_board.send_outcomes so where so.message_id = r.message_id)), false) from public.so670_iso2_ret r) as every_returned_has_receipt;
59
+ }
60
+ permutation "r_read" "p_begin" "p_post" "r_read" "p_commit" "r_read" "check"
61
+ permutation "p_begin" "p_post" "r_read" "p_rollback" "r_read" "check"
@@ -0,0 +1,68 @@
1
+ # #670 D12 spec 5: recovery racing a membership change and a content deletion.
2
+ # (1) M's receipt is committed; while M's own leave is in flight M reads confirmed, and after it
3
+ # commits M still reads confirmed (the read has no membership check and waits on nothing);
4
+ # (2) while a deletion of M's message is in flight the committed receipt still reads confirmed; once
5
+ # the deletion commits the receipt is gone with its message and M reads unknown;
6
+ # the reads write nothing and fan out nothing (fanout counts are equal before and after).
7
+ setup
8
+ {
9
+ create table public.so670_iso5_pids(s text primary key, pid int not null);
10
+ create table public.so670_iso5_ret(s text not null, message_id uuid);
11
+ grant select, insert on public.so670_iso5_ret to authenticated, service_role;
12
+ insert into auth.users(id) values ('670b5000-0000-4000-8000-0000000000a1'),('670b5000-0000-4000-8000-0000000000b1'),('670b5000-0000-4000-8000-0000000000c1');
13
+ insert into agent_board.agents(id,agent_type,handle,display_name,status,auth_user_id)
14
+ values ('670b5000-0000-4000-8000-000000000001','agent','so670-iso5-x-a','SO670 iso A','active','670b5000-0000-4000-8000-0000000000a1'),
15
+ ('670b5000-0000-4000-8000-000000000002','agent','so670-iso5-x-b','SO670 iso B','active','670b5000-0000-4000-8000-0000000000b1'),
16
+ ('670b5000-0000-4000-8000-000000000003','agent','so670-iso5-x-m','SO670 iso M','active','670b5000-0000-4000-8000-0000000000c1');
17
+ insert into agent_board.boards(id,slug,name,created_by_agent_id,visibility,status)
18
+ values ('670b5000-0000-4000-8000-000000000020','so670-iso5-x','SO670 iso X','670b5000-0000-4000-8000-000000000001','private','active'),
19
+ ('670b5000-0000-4000-8000-000000000021','so670-iso5-y','SO670 iso Y','670b5000-0000-4000-8000-000000000001','private','active');
20
+ select set_config('request.jwt.claim.role','service_role',false);
21
+ insert into agent_board.board_memberships(board_id,agent_id,membership_state,created_by_agent_id,role)
22
+ values ('670b5000-0000-4000-8000-000000000020','670b5000-0000-4000-8000-000000000001','joined','670b5000-0000-4000-8000-000000000001','owner'),('670b5000-0000-4000-8000-000000000020','670b5000-0000-4000-8000-000000000002','joined','670b5000-0000-4000-8000-000000000001','member'),
23
+ ('670b5000-0000-4000-8000-000000000020','670b5000-0000-4000-8000-000000000003','joined','670b5000-0000-4000-8000-000000000001','member'),('670b5000-0000-4000-8000-000000000021','670b5000-0000-4000-8000-000000000001','joined','670b5000-0000-4000-8000-000000000001','owner');
24
+ select set_config('request.jwt.claim.role','',false);
25
+ insert into agent_board.threads(id,thread_type,created_by_agent_id,subject)
26
+ values ('670b5000-0000-4000-8000-000000000010','coordination','670b5000-0000-4000-8000-000000000001','so670 iso T'),('670b5000-0000-4000-8000-000000000011','coordination','670b5000-0000-4000-8000-000000000001','so670 iso T2'),
27
+ ('670b5000-0000-4000-8000-000000000012','coordination','670b5000-0000-4000-8000-000000000003','so670 iso TM');
28
+ }
29
+ teardown
30
+ {
31
+ delete from agent_board.inbox_items where recipient_agent_id in ('670b5000-0000-4000-8000-000000000001','670b5000-0000-4000-8000-000000000002','670b5000-0000-4000-8000-000000000003');
32
+ delete from agent_board.callback_outbox_events where recipient_agent_id in ('670b5000-0000-4000-8000-000000000001','670b5000-0000-4000-8000-000000000002','670b5000-0000-4000-8000-000000000003');
33
+ delete from agent_board.delivery_recipients where recipient_agent_id in ('670b5000-0000-4000-8000-000000000001','670b5000-0000-4000-8000-000000000002','670b5000-0000-4000-8000-000000000003');
34
+ delete from agent_board.messages where thread_id in ('670b5000-0000-4000-8000-000000000010','670b5000-0000-4000-8000-000000000011','670b5000-0000-4000-8000-000000000012');
35
+ delete from agent_board.threads where id in ('670b5000-0000-4000-8000-000000000010','670b5000-0000-4000-8000-000000000011','670b5000-0000-4000-8000-000000000012');
36
+ delete from agent_board.send_authz_telemetry where actor_agent_id in ('670b5000-0000-4000-8000-000000000001','670b5000-0000-4000-8000-000000000002','670b5000-0000-4000-8000-000000000003');
37
+ delete from agent_board.board_membership_events where board_id in ('670b5000-0000-4000-8000-000000000020','670b5000-0000-4000-8000-000000000021');
38
+ delete from agent_board.boards where id in ('670b5000-0000-4000-8000-000000000020','670b5000-0000-4000-8000-000000000021');
39
+ delete from agent_board.agents where id in ('670b5000-0000-4000-8000-000000000001','670b5000-0000-4000-8000-000000000002','670b5000-0000-4000-8000-000000000003');
40
+ delete from auth.users where id in ('670b5000-0000-4000-8000-0000000000a1','670b5000-0000-4000-8000-0000000000b1','670b5000-0000-4000-8000-0000000000c1');
41
+ drop table public.so670_iso5_pids;
42
+ drop table public.so670_iso5_ret;
43
+ }
44
+ session "p"
45
+ setup { reset role; insert into public.so670_iso5_pids values ('p', pg_backend_pid()) on conflict (s) do update set pid = excluded.pid; set role authenticated; select set_config('request.jwt.claims',json_build_object('role','authenticated','sub','670b5000-0000-4000-8000-0000000000c1')::text,false); }
46
+ step "p_post" { insert into public.so670_iso5_ret select 'p', message_id from agent_board.post_board_message('670b5000-0000-4000-8000-000000000012','670b5000-0000-4000-8000-000000000003','670b5000-0000-4000-8000-000000000020','iso','so670 body','comment','medium','K',jsonb_build_object()); }
47
+ step "r_read" { select status from agent_board.get_send_outcome('670b5000-0000-4000-8000-000000000012','K','post_board_message'); }
48
+ session "l"
49
+ setup { reset role; insert into public.so670_iso5_pids values ('l', pg_backend_pid()) on conflict (s) do update set pid = excluded.pid; }
50
+ step "l_begin" { begin; set local role authenticated; select set_config('request.jwt.claim.role','',true), set_config('request.jwt.claim.sub','',true), set_config('request.jwt.claims',json_build_object('role','authenticated','sub','670b5000-0000-4000-8000-0000000000c1')::text,true); }
51
+ step "l_leave" { select count(*) as left_rows from agent_board.leave_board('670b5000-0000-4000-8000-000000000020','670b5000-0000-4000-8000-000000000003',jsonb_build_object()); }
52
+ step "l_commit" { commit; }
53
+ session "d"
54
+ step "d_begin" { begin; }
55
+ step "d_delete" { delete from agent_board.messages where thread_id = '670b5000-0000-4000-8000-000000000012'; }
56
+ step "d_commit" { commit; }
57
+ session "check"
58
+ step "fanout" { select (select count(*) from agent_board.delivery_recipients where recipient_agent_id in ('670b5000-0000-4000-8000-000000000001','670b5000-0000-4000-8000-000000000002')) as deliveries, (select count(*) from agent_board.inbox_items where recipient_agent_id in ('670b5000-0000-4000-8000-000000000001','670b5000-0000-4000-8000-000000000002')) as inbox, (select count(*) from agent_board.send_outcomes where thread_id = '670b5000-0000-4000-8000-000000000012') as receipts; }
59
+ step "check" {
60
+ select (select count(*) from agent_board.messages where thread_id in ('670b5000-0000-4000-8000-000000000012')) as messages,
61
+ (select count(*) from agent_board.send_outcomes where thread_id in ('670b5000-0000-4000-8000-000000000012')) as receipts,
62
+ (select string_agg(so.operation || ':' || a.handle, ',' order by so.operation, a.handle) from agent_board.send_outcomes so join agent_board.agents a on a.id = so.author_agent_id where so.thread_id in ('670b5000-0000-4000-8000-000000000012')) as receipt_owners,
63
+ (select string_agg(r.s, ',' order by r.s) from public.so670_iso5_ret r) as returned_by,
64
+ (select count(distinct r.message_id) from public.so670_iso5_ret r) as distinct_returned,
65
+ (select coalesce(bool_and(exists (select 1 from agent_board.send_outcomes so where so.message_id = r.message_id)), false) from public.so670_iso5_ret r) as every_returned_has_receipt;
66
+ }
67
+ permutation "p_post" "fanout" "l_begin" "l_leave" "r_read" "l_commit" "r_read" "fanout" "check"
68
+ permutation "p_post" "fanout" "d_begin" "d_delete" "r_read" "d_commit" "r_read" "fanout" "check"
@@ -0,0 +1,7 @@
1
+ -- EQUIVALENT: the helper takes the author from the message the writer inserted, not from the session
2
+ do $m$ declare d text := pg_get_functiondef('agent_board._record_send_outcome(text,uuid,text,uuid)'::regprocedure); n text; begin
3
+ if position($q$ v_author := agent_board.current_agent_id();$q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board._record_send_outcome(text,uuid,text,uuid)'; end if;
4
+ n := replace(d, $q$ v_author := agent_board.current_agent_id();$q$, $q$ v_author := (select m.author_agent_id from agent_board.messages m where m.id = p_message_id);$q$);
5
+ if n = d then raise exception 'mutation is a no-op'; end if;
6
+ execute n;
7
+ end $m$;
@@ -0,0 +1,13 @@
1
+ -- EQUIVALENT: the helper's null-principal check is removed
2
+ do $m$ declare d text := pg_get_functiondef('agent_board._record_send_outcome(text,uuid,text,uuid)'::regprocedure); n text; begin
3
+ if position($q$ if v_author is null then
4
+ raise exception 'send outcome requires a session principal' using errcode = '42501';
5
+ end if;
6
+ $q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board._record_send_outcome(text,uuid,text,uuid)'; end if;
7
+ n := replace(d, $q$ if v_author is null then
8
+ raise exception 'send outcome requires a session principal' using errcode = '42501';
9
+ end if;
10
+ $q$, $q$$q$);
11
+ if n = d then raise exception 'mutation is a no-op'; end if;
12
+ execute n;
13
+ end $m$;
@@ -0,0 +1,10 @@
1
+ -- post_board_message: the idempotency advisory lock is removed
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure); n text; begin
3
+ if position($q$ perform agent_board._lock_idempotency_key(
4
+ 'post_board_message:'$q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'; end if;
5
+ n := replace(d, $q$ perform agent_board._lock_idempotency_key(
6
+ 'post_board_message:'$q$, $q$ perform concat(
7
+ 'post_board_message:'$q$);
8
+ if n = d then raise exception 'mutation is a no-op'; end if;
9
+ execute n;
10
+ end $m$;
@@ -0,0 +1,2 @@
1
+ -- send_outcomes: SELECT granted to authenticated
2
+ grant select on agent_board.send_outcomes to authenticated;
@@ -0,0 +1,2 @@
1
+ -- helper: EXECUTE granted to service_role
2
+ grant execute on function agent_board._record_send_outcome(text,uuid,text,uuid) to service_role;
@@ -0,0 +1,8 @@
1
+ -- send_outcomes: the message foreign key loses ON DELETE CASCADE
2
+ do $m$ declare c text; begin
3
+ select conname into c from pg_constraint
4
+ where conrelid = 'agent_board.send_outcomes'::regclass and contype = 'f' and confrelid = 'agent_board.messages'::regclass;
5
+ if c is null then raise exception 'M13 anchor: no message FK'; end if;
6
+ execute format('alter table agent_board.send_outcomes drop constraint %I', c);
7
+ execute format('alter table agent_board.send_outcomes add constraint %I foreign key (message_id) references agent_board.messages(id)', c);
8
+ end $m$;
@@ -0,0 +1,11 @@
1
+ -- post_message: mints a receipt when its caller metadata carries an idempotencyKey
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.post_message(uuid,uuid,text,text,uuid,jsonb)'::regprocedure); n text; begin
3
+ if position($q$ return query select v_message.message_id,v_message.thread_id,v_message.task_id,$q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.post_message(uuid,uuid,text,text,uuid,jsonb)'; end if;
4
+ n := replace(d, $q$ return query select v_message.message_id,v_message.thread_id,v_message.task_id,$q$, $q$ if p_metadata ? 'idempotencyKey' then
5
+ perform agent_board._record_send_outcome(coalesce(p_metadata ->> 'rpcName', 'post_board_message'), v_message.message_id,
6
+ p_metadata ->> 'idempotencyKey', v_board_id);
7
+ end if;
8
+ return query select v_message.message_id,v_message.thread_id,v_message.task_id,$q$);
9
+ if n = d then raise exception 'mutation is a no-op'; end if;
10
+ execute n;
11
+ end $m$;
@@ -0,0 +1,9 @@
1
+ -- a second receipt writer: an overload of the helper that inserts directly
2
+ create function agent_board._record_send_outcome(p_operation text, p_message_id uuid, p_idempotency_key text)
3
+ returns void language plpgsql security definer set search_path = '' as $f$
4
+ begin
5
+ insert into agent_board.send_outcomes(operation, author_agent_id, thread_id, idempotency_key, message_id, created_at)
6
+ select p_operation, m.author_agent_id, m.thread_id, btrim(p_idempotency_key), m.id, m.created_at
7
+ from agent_board.messages m where m.id = p_message_id;
8
+ end $f$;
9
+ revoke all on function agent_board._record_send_outcome(text, uuid, text) from public, anon, authenticated, service_role, agent_board_delegate;
@@ -0,0 +1,7 @@
1
+ -- read: the key is no longer trimmed
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.get_send_outcome(uuid,text,text)'::regprocedure); n text; begin
3
+ if position($q$ v_key := nullif(btrim(p_idempotency_key), '');$q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.get_send_outcome(uuid,text,text)'; end if;
4
+ n := replace(d, $q$ v_key := nullif(btrim(p_idempotency_key), '');$q$, $q$ v_key := nullif(p_idempotency_key, '');$q$);
5
+ if n = d then raise exception 'mutation is a no-op'; end if;
6
+ execute n;
7
+ end $m$;
@@ -0,0 +1,2 @@
1
+ -- read: marked VOLATILE
2
+ alter function agent_board.get_send_outcome(uuid,text,text) volatile;
@@ -0,0 +1,13 @@
1
+ -- helper: the _is_service_role() guard is removed
2
+ do $m$ declare d text := pg_get_functiondef('agent_board._record_send_outcome(text,uuid,text,uuid)'::regprocedure); n text; begin
3
+ if position($q$ if agent_board._is_service_role() then
4
+ return;
5
+ end if;
6
+ $q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board._record_send_outcome(text,uuid,text,uuid)'; end if;
7
+ n := replace(d, $q$ if agent_board._is_service_role() then
8
+ return;
9
+ end if;
10
+ $q$, $q$$q$);
11
+ if n = d then raise exception 'mutation is a no-op'; end if;
12
+ execute n;
13
+ end $m$;
@@ -0,0 +1,10 @@
1
+ -- post_board_message: the helper is called on the replay branch too
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure); n text; begin
3
+ if position($q$ if v_inserted and v_idempotency_key is not null then
4
+ $q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'; end if;
5
+ n := replace(d, $q$ if v_inserted and v_idempotency_key is not null then
6
+ $q$, $q$ if v_idempotency_key is not null then
7
+ $q$);
8
+ if n = d then raise exception 'mutation is a no-op'; end if;
9
+ execute n;
10
+ end $m$;
@@ -0,0 +1,10 @@
1
+ -- read: checks thread existence and raises P0002
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.get_send_outcome(uuid,text,text)'::regprocedure); n text; begin
3
+ if position($q$ -- Step 3, the lookup:$q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.get_send_outcome(uuid,text,text)'; end if;
4
+ n := replace(d, $q$ -- Step 3, the lookup:$q$, $q$ if not exists (select 1 from agent_board.threads t where t.id = p_thread_id) then
5
+ raise exception 'thread not found' using errcode = 'P0002';
6
+ end if;
7
+ -- Step 3, the lookup:$q$);
8
+ if n = d then raise exception 'mutation is a no-op'; end if;
9
+ execute n;
10
+ end $m$;
@@ -0,0 +1,10 @@
1
+ -- read: requires thread access (M5a: the thread-access form)
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.get_send_outcome(uuid,text,text)'::regprocedure); n text; begin
3
+ if position($q$ -- Step 3, the lookup:$q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.get_send_outcome(uuid,text,text)'; end if;
4
+ n := replace(d, $q$ -- Step 3, the lookup:$q$, $q$ if not agent_board._current_agent_can_access_thread(p_thread_id) then
5
+ raise exception 'thread not accessible to current agent' using errcode = '42501';
6
+ end if;
7
+ -- Step 3, the lookup:$q$);
8
+ if n = d then raise exception 'mutation is a no-op'; end if;
9
+ execute n;
10
+ end $m$;
@@ -0,0 +1,11 @@
1
+ -- read: requires current board access to the thread's board (membership)
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.get_send_outcome(uuid,text,text)'::regprocedure); n text; begin
3
+ if position($q$ -- Step 3, the lookup:$q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.get_send_outcome(uuid,text,text)'; end if;
4
+ n := replace(d, $q$ -- Step 3, the lookup:$q$, $q$ if exists (select 1 from agent_board.threads t where t.id = p_thread_id and t.board_id is not null
5
+ and not agent_board._current_agent_has_board_access(t.board_id)) then
6
+ raise exception 'board not accessible to current agent' using errcode = '42501';
7
+ end if;
8
+ -- Step 3, the lookup:$q$);
9
+ if n = d then raise exception 'mutation is a no-op'; end if;
10
+ execute n;
11
+ end $m$;
@@ -0,0 +1,14 @@
1
+ -- read: the service_role refusal is dropped, and service_role gets a (test-only) grant
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.get_send_outcome(uuid,text,text)'::regprocedure); n text; begin
3
+ if position($q$ if agent_board._is_service_role() then
4
+ raise exception 'get_send_outcome requires an agent session' using errcode = '42501';
5
+ end if;
6
+ $q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.get_send_outcome(uuid,text,text)'; end if;
7
+ n := replace(d, $q$ if agent_board._is_service_role() then
8
+ raise exception 'get_send_outcome requires an agent session' using errcode = '42501';
9
+ end if;
10
+ $q$, $q$$q$);
11
+ if n = d then raise exception 'mutation is a no-op'; end if;
12
+ execute n;
13
+ end $m$;
14
+ grant execute on function agent_board.get_send_outcome(uuid,text,text) to service_role;
@@ -0,0 +1,12 @@
1
+ -- read: the confirmed row also carries message_sequence (drop + recreate, grants restated)
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.get_send_outcome(uuid,text,text)'::regprocedure); n text; begin
3
+ n := replace(d, $q$created_at timestamp with time zone)$q$, $q$created_at timestamp with time zone, message_sequence bigint)$q$);
4
+ n := replace(n, $q$null::uuid, null::timestamptz;$q$, $q$null::uuid, null::timestamptz, null::bigint;$q$);
5
+ n := replace(n, $q$v_row.board_id, v_row.created_at;$q$, $q$v_row.board_id, v_row.created_at, (select m.message_sequence from agent_board.messages m where m.id = v_row.message_id);$q$);
6
+ if n = d or position('message_sequence bigint' in n) = 0 or position('null::bigint' in n) = 0 or position('m.message_sequence' in n) = 0 then
7
+ raise exception 'M7 anchors not found';
8
+ end if;
9
+ execute 'drop function agent_board.get_send_outcome(uuid,text,text)';
10
+ execute n;
11
+ end $m$;
12
+ grant execute on function agent_board.get_send_outcome(uuid,text,text) to authenticated, agent_board_delegate;
@@ -0,0 +1,19 @@
1
+ -- post_board_message: delivery is wrapped in exception when others then null
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure); n text; begin
3
+ if position($q$ select d.delivered_recipient_count, d.callback_outbox_count
4
+ into v_delivered_count, v_callback_count
5
+ from agent_board._deliver_board_message(v_message.id, trim(p_subject), p_body, p_priority) d;
6
+ $q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'; end if;
7
+ n := replace(d, $q$ select d.delivered_recipient_count, d.callback_outbox_count
8
+ into v_delivered_count, v_callback_count
9
+ from agent_board._deliver_board_message(v_message.id, trim(p_subject), p_body, p_priority) d;
10
+ $q$, $q$ begin
11
+ select d.delivered_recipient_count, d.callback_outbox_count
12
+ into v_delivered_count, v_callback_count
13
+ from agent_board._deliver_board_message(v_message.id, trim(p_subject), p_body, p_priority) d;
14
+ exception when others then null;
15
+ end;
16
+ $q$);
17
+ if n = d then raise exception 'mutation is a no-op'; end if;
18
+ execute n;
19
+ end $m$;
@@ -0,0 +1,10 @@
1
+ -- post_board_message: the board is put back into the idempotency lock key
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure); n text; begin
3
+ if position($q$ 'post_board_message:' || p_thread_id::text || ':' || v_actor_agent_id::text,
4
+ $q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'; end if;
5
+ n := replace(d, $q$ 'post_board_message:' || p_thread_id::text || ':' || v_actor_agent_id::text,
6
+ $q$, $q$ 'post_board_message:' || p_thread_id::text || ':' || v_actor_agent_id::text || ':' || p_board_id::text,
7
+ $q$);
8
+ if n = d then raise exception 'mutation is a no-op'; end if;
9
+ execute n;
10
+ end $m$;
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env bash
2
+ # #670 SENDOUTCOME mutation receipts (D11). Connection comes ONLY from libpq environment (PGHOST/
3
+ # PGPORT/PGUSER/PGPASSWORD/PGDATABASE), never argv. The target must be an OWNED database holding this
4
+ # tree's applied bundle (CI: the dev job's throwaway Supabase; never a shared cluster).
5
+ #
6
+ # Per mutation:
7
+ # pgTAP the mutation and agent_board_send_outcome.test.sql run in ONE transaction that is
8
+ # rolled back. The receipt records the plan, emitted, ok and not ok counts and every
9
+ # failing line, counted from the TAP, never inferred from an absence. An aborted file
10
+ # (emitted < plan) or a mutation that failed to apply is NOT a kill.
11
+ # isolation (M9, M10 only) the mutation is COMMITTED, its target spec is diffed against the
12
+ # committed golden, and every touched definition is restored from text captured BEFORE
13
+ # mutating.
14
+ # Both legs require the agent_board catalog fingerprint (every routine's signature, ACL, source and
15
+ # comment, plus send_outcomes' constraints and ACL) to equal the pre-run value afterwards.
16
+ # Usage: run.sh <receipt-path> [mutation.sql ...] (default: every *.sql beside this script)
17
+ set -uo pipefail
18
+ HERE="$(cd "$(dirname "$0")" && pwd)"
19
+ PKG="$(cd "$HERE/../../.." && pwd)"
20
+ TEST="$PKG/tests/agent_board_send_outcome.test.sql"
21
+ SPECS="$PKG/tests/isolation/specs"
22
+ GOLDEN="$PKG/tests/isolation/expected"
23
+ OUT="${1:?receipt path}"; shift
24
+ if [ "$#" -gt 0 ]; then MUTS=("$@"); else mapfile -t MUTS < <(find "$HERE" -maxdepth 1 -name '*.sql' | sort -V); fi
25
+ [ -n "${PGDATABASE:-}" ] || { echo "FATAL: PGDATABASE unset (libpq environment required)" >&2; exit 3; }
26
+ ISO="$(ls -1 /usr/lib/postgresql/*/lib/pgxs/src/test/isolation/isolationtester 2>/dev/null | sort -V | tail -1)"
27
+ WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT
28
+ P=(psql -X -qtA)
29
+ FNS=("agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)")
30
+ fp() { "${P[@]}" -c "select md5(coalesce((select string_agg(p.oid::regprocedure::text || coalesce(p.proacl::text, '') || p.prosrc || p.provolatile::text || coalesce(obj_description(p.oid, 'pg_proc'), ''), '|' order by p.oid::regprocedure::text) from pg_proc p where p.pronamespace = 'agent_board'::regnamespace), '') || coalesce((select string_agg(conname || pg_get_constraintdef(oid), '|' order by conname) from pg_constraint where conrelid = 'agent_board.send_outcomes'::regclass), '') || coalesce((select relacl::text from pg_class where oid = 'agent_board.send_outcomes'::regclass), ''))"; }
31
+ : > "$WORK/restore.sql"
32
+ for f in "${FNS[@]}"; do
33
+ "${P[@]}" -c "select pg_get_functiondef('$f'::regprocedure)" >> "$WORK/restore.sql" || exit 3
34
+ printf ';\n' >> "$WORK/restore.sql"
35
+ done
36
+ ORIG="$(fp)"
37
+
38
+ tap() { # $1 label, $2 mutation file or empty
39
+ local out plan ok nok
40
+ if [ -n "$2" ]; then out="$(printf 'begin;\n\\i %s\n\\i %s\nrollback;\n' "$2" "$TEST" | "${P[@]}" 2>&1)"
41
+ else out="$(printf 'begin;\n\\i %s\nrollback;\n' "$TEST" | "${P[@]}" 2>&1)"; fi
42
+ plan="$(sed -nE 's/^1\.\.([0-9]+)$/\1/p' <<<"$out" | head -1)"
43
+ ok="$(grep -cE '^ok [0-9]+' <<<"$out")"; nok="$(grep -cE '^not ok [0-9]+' <<<"$out")"
44
+ printf '%s: pgtap plan=%s emitted=%s ok=%s not_ok=%s%s\n' "$1" "${plan:-NONE}" "$((ok + nok))" "$ok" "$nok" \
45
+ "$([ "${plan:-x}" = "$((ok + nok))" ] || echo ' ABORTED')"
46
+ grep -E '^not ok [0-9]+' <<<"$out" | sed 's/^/ /'
47
+ grep -E '(ERROR|FATAL):' <<<"$out" | head -3 | sed 's/^/ ERR /'
48
+ }
49
+ iso() { # $1 label, $2 spec basename
50
+ [ -x "$ISO" ] || { echo " isolation=UNDETERMINED (no isolationtester)"; return; }
51
+ "$ISO" "dbname=$PGDATABASE" < "$SPECS/$2.spec" > "$WORK/iso.out" 2>&1
52
+ if diff -q "$GOLDEN/$2.out" "$WORK/iso.out" >/dev/null; then echo " isolation[$2]=identical"
53
+ else
54
+ echo " isolation[$2]=DIFFERS ($(diff "$GOLDEN/$2.out" "$WORK/iso.out" | grep -cE '^[<>]') lines)"
55
+ diff "$GOLDEN/$2.out" "$WORK/iso.out" | grep -E '^[<>]' | head -12 | sed 's/^/ /'
56
+ fi
57
+ }
58
+ spec_for() { case "$1" in M9_*) echo send_outcome_cross_board ;; M10_*) echo send_outcome_overlap ;; *) echo "" ;; esac; }
59
+
60
+ {
61
+ echo "# #670 SENDOUTCOME mutation receipt -- $(date -u +%FT%TZ)"
62
+ echo "# tree $(git -C "$PKG" rev-parse HEAD)$(git -C "$PKG" diff --quiet HEAD -- . || echo ' + uncommitted changes')"
63
+ echo "# catalog fingerprint before: $ORIG"
64
+ tap "BASELINE" ""
65
+ iso "BASELINE" send_outcome_cross_board; iso "BASELINE" send_outcome_overlap
66
+ for m in "${MUTS[@]}"; do
67
+ name="$(basename "$m" .sql)"
68
+ echo "-- $name: $(head -1 "$m" | sed 's/^-- //') [sha256 $(sha256sum "$m" | cut -c1-16)]"
69
+ tap "$name" "$m"
70
+ [ "$(fp)" = "$ORIG" ] || { echo "FATAL: fingerprint changed after the rolled-back pgTAP leg of $name"; exit 3; }
71
+ spec="$(spec_for "$name")"
72
+ if [ -n "$spec" ]; then
73
+ "${P[@]}" -v ON_ERROR_STOP=1 -1 -f "$m" >/dev/null 2>&1 || { echo "FATAL: $name did not apply for the isolation leg"; exit 3; }
74
+ [ "$(fp)" != "$ORIG" ] || { echo "FATAL: $name applied but the fingerprint is unchanged (no-op)"; exit 3; }
75
+ iso "$name" "$spec"
76
+ "${P[@]}" -v ON_ERROR_STOP=1 -1 -f "$WORK/restore.sql" >/dev/null || { echo "FATAL: restore failed after $name"; exit 3; }
77
+ [ "$(fp)" = "$ORIG" ] || { echo "FATAL: restore fingerprint differs after $name"; exit 3; }
78
+ echo " restored: fingerprint identical"
79
+ fi
80
+ done
81
+ tap "AFTER-RESTORE" ""
82
+ iso "AFTER-RESTORE" send_outcome_cross_board; iso "AFTER-RESTORE" send_outcome_overlap
83
+ echo "# catalog fingerprint after: $(fp)"
84
+ } | tee "$OUT"