@consiliency/agent-board-schema 1.80.0 → 1.81.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 (111) hide show
  1. package/fixtures/already-applied.json +4 -0
  2. package/migrations/20261201000100_agent_board_reply_to_message.sql +1241 -0
  3. package/package.json +1 -1
  4. package/schema-manifest.json +12 -3
  5. package/src/tooling.test.mjs +4 -2
  6. package/tests/agent_board_661_reply_delivery.test.sql +12 -12
  7. package/tests/agent_board_667_history_class.test.sql +31 -13
  8. package/tests/agent_board_717_message_replay_scope.test.sql +29 -16
  9. package/tests/agent_board_717_tasking_replay_scope.test.sql +3 -3
  10. package/tests/agent_board_bundle_endpoint_compat.test.sql +2 -2
  11. package/tests/agent_board_canary_signed_fleet_enforcement.test.sql +3 -3
  12. package/tests/agent_board_claim_subject_gate.test.sql +3 -1
  13. package/tests/agent_board_delegate_role_class.test.sql +1 -1
  14. package/tests/agent_board_delivery_writer_execute.test.sql +7 -7
  15. package/tests/agent_board_hosted_delegates.test.sql +3 -3
  16. package/tests/agent_board_inbox_rpcs.test.sql +6 -6
  17. package/tests/agent_board_refusal_atomicity.test.sql +958 -0
  18. package/tests/agent_board_reply_to.test.sql +874 -0
  19. package/tests/agent_board_scoped_send_regrant.test.sql +8 -8
  20. package/tests/agent_board_send_outcome.test.sql +10 -10
  21. package/tests/agent_board_send_role_gate.test.sql +2 -2
  22. package/tests/agent_board_write_enforcement_class.test.sql +7 -7
  23. package/tests/catalog/function_execute_privileges.json +23 -14
  24. package/tests/catalog/p_metadata_parameters.json +12 -8
  25. package/tests/isolation/expected/reply_parent_vs_thread_delete.out +55 -0
  26. package/tests/isolation/expected/reply_vs_parent_author_delete.out +56 -0
  27. package/tests/isolation/specs/reply_parent_vs_thread_delete.spec +42 -0
  28. package/tests/isolation/specs/reply_vs_parent_author_delete.spec +46 -0
  29. package/tests/mutations/655/M01_no_writer_check.sql +22 -0
  30. package/tests/mutations/655/M02_drop_fk.sql +2 -0
  31. package/tests/mutations/655/M03_existence_only.sql +13 -0
  32. package/tests/mutations/655/M04_distinct_cross_thread_error.sql +17 -0
  33. package/tests/mutations/655/M05_view_no_thread_conjunct.sql +46 -0
  34. package/tests/mutations/655/M06_thread_feed_not_invoker.sql +46 -0
  35. package/tests/mutations/655/M06b_board_feed_not_invoker.sql +49 -0
  36. package/tests/mutations/655/M07_excerpt_via_definer.sql +49 -0
  37. package/tests/mutations/655/M08_excerpt_200.sql +46 -0
  38. package/tests/mutations/655/M09_fk_restrict.sql +4 -0
  39. package/tests/mutations/655/M10_post_message_drops_arg.sql +13 -0
  40. package/tests/mutations/655/M10b_post_message_passes_null.sql +12 -0
  41. package/tests/mutations/655/M11_slug_drops_arg.sql +15 -0
  42. package/tests/mutations/655/M11b_dm_descriptor_positional.sql +31 -0
  43. package/tests/mutations/655/M12_signed_pbm_nine_args.sql +19 -0
  44. package/tests/mutations/655/M12b_signed_mc_one_branch.sql +15 -0
  45. package/tests/mutations/655/M13_undriven_entrypoint.sql +8 -0
  46. package/tests/mutations/655/M14_second_writer.sql +6 -0
  47. package/tests/mutations/655/M15_insert_primitive_public.sql +2 -0
  48. package/tests/mutations/655/M16_comment_missing.sql +2 -0
  49. package/tests/mutations/655/M17_old_allow_set_rows.sql +5 -0
  50. package/tests/mutations/655/M18_board_excerpt_139.sql +49 -0
  51. package/tests/mutations/655/M19_column_in_middle.sql +47 -0
  52. package/tests/mutations/655/M20_projects_parent_id.sql +46 -0
  53. package/tests/mutations/655/M21_replay_applies_new_parent.sql +22 -0
  54. package/tests/mutations/655/M22_fk_not_valid.sql +4 -0
  55. package/tests/mutations/655/MB4a_no_readability.sql +13 -0
  56. package/tests/mutations/655/MB4b_own_readability_message.sql +20 -0
  57. package/tests/mutations/655/MB4c_service_role_reads_all.sql +12 -0
  58. package/tests/mutations/655/MB4d_delegate_uses_authenticated_arm.sql +12 -0
  59. package/tests/mutations/655/MB4f_no_board_member_branch.sql +14 -0
  60. package/tests/mutations/655/MB4g_helper_granted_authenticated.sql +2 -0
  61. package/tests/mutations/655/MB4j_writer_uses_current_agent.sql +12 -0
  62. package/tests/mutations/655/MB4k_delegate_policy_loses_initplan.sql +5 -0
  63. package/tests/mutations/655/MB4l_board_memberships_policy_narrowed.sql +3 -0
  64. package/tests/mutations/655/W1_auth_no_membership_state.sql +13 -0
  65. package/tests/mutations/655/W2_auth_no_agent_binding.sql +13 -0
  66. package/tests/mutations/655/W3_delivery_no_recipient_binding.sql +12 -0
  67. package/tests/mutations/655/W4_delegate_no_board_conjunct.sql +18 -0
  68. package/tests/mutations/655/W5_boardset_no_membership_state.sql +13 -0
  69. package/tests/mutations/655/W6_boardset_no_agent_binding.sql +14 -0
  70. package/tests/mutations/655/run.sh +93 -0
  71. package/tests/mutations/670/M10_lock_removed.sql +2 -2
  72. package/tests/mutations/670/M14_post_message_mints_from_metadata.sql +2 -2
  73. package/tests/mutations/670/M3-DM_helper_on_replay_branch.sql +2 -2
  74. package/tests/mutations/670/M3-MC_helper_on_replay_branch.sql +2 -2
  75. package/tests/mutations/670/M3_helper_on_replay_branch.sql +2 -2
  76. package/tests/mutations/670/M8_delivery_swallows_errors.sql +2 -2
  77. package/tests/mutations/670/M9_lock_key_includes_board.sql +2 -2
  78. package/tests/mutations/670/run.sh +1 -1
  79. package/tests/mutations/717-f1/M10_wrapper_stops_delegating.sql +1 -1
  80. package/tests/mutations/717-f1/M9_public_route_revoked_from_service_role.sql +1 -1
  81. package/tests/mutations/717-f2b/L1_dm_lock_split_by_recipient.sql +2 -2
  82. package/tests/mutations/717-f2b/L2_dm_lock_removed.sql +2 -2
  83. package/tests/mutations/717-f2b/L3_mc_lock_split_by_recipients.sql +2 -2
  84. package/tests/mutations/717-f2b/L4_mc_lock_removed.sql +2 -2
  85. package/tests/mutations/717-f2b/M0_prefix_definitions.sql +1 -1
  86. package/tests/mutations/717-f2b/M10_mc_lookup_any_author.sql +2 -2
  87. package/tests/mutations/717-f2b/M11_dm_swallow_refusal.sql +2 -2
  88. package/tests/mutations/717-f2b/M12_mc_swallow_refusal.sql +2 -2
  89. package/tests/mutations/717-f2b/M15_mc_any_source.sql +1 -1
  90. package/tests/mutations/717-f2b/M1_dm_drop_recipient_proof.sql +2 -2
  91. package/tests/mutations/717-f2b/M2_mc_drop_recipient_proof.sql +2 -2
  92. package/tests/mutations/717-f2b/M3_dm_metadata_as_proof.sql +2 -2
  93. package/tests/mutations/717-f2b/M4_mc_metadata_as_proof.sql +2 -2
  94. package/tests/mutations/717-f2b/M5_dm_any_mode.sql +2 -2
  95. package/tests/mutations/717-f2b/M6_dm_board_delivery_counts.sql +2 -2
  96. package/tests/mutations/717-f2b/M7_mc_any_mode.sql +2 -2
  97. package/tests/mutations/717-f2b/M8_dm_thread_stamp_as_proof.sql +2 -2
  98. package/tests/mutations/717-f2b/M9_dm_lookup_any_author.sql +2 -2
  99. package/tests/mutations/717-f2b/run.sh +2 -2
  100. package/tests/mutations/919/M10_quoted_net_http_post_scratch.sql +5 -0
  101. package/tests/mutations/919/M11_pbm_calls_public.sql +11 -0
  102. package/tests/mutations/919/M1_pbm_refusal_swallowed_after_write.sql +14 -0
  103. package/tests/mutations/919/M2_cof_refusal_swallowed_after_write.sql +13 -0
  104. package/tests/mutations/919/M3_pbm_receipt_fault_swallowed.sql +10 -0
  105. package/tests/mutations/919/M4_pg_notify_scratch.sql +5 -0
  106. package/tests/mutations/919/M5_dynamic_notify_scratch.sql +5 -0
  107. package/tests/mutations/919/M6_trigger_outside_schema.sql +3 -0
  108. package/tests/mutations/919/M7_pbm_step_up_gate.sql +10 -0
  109. package/tests/mutations/919/M8_pbm_positive_writes_nothing.sql +10 -0
  110. package/tests/mutations/919/M9_cof_positive_writes_nothing.sql +10 -0
  111. package/tests/mutations/919/run.sh +81 -0
@@ -0,0 +1,958 @@
1
+ -- REFUSALATOMICITY pgTAP (Consiliency/message-board#919; consumer: Consiliency/consiliency-portal#997).
2
+ --
3
+ -- THE PROPERTY. A refused post_board_message or create_or_find_thread leaves NO rows behind:
4
+ -- no message, delivery, inbox item, callback outbox event or #670 send_outcomes receipt, and no
5
+ -- thread change. Portal maps every attributed MB 4xx SQLSTATE by its class instead of
6
+ -- outcome_unverified, and treats MBS01 (step_up_required) as session_expired recovery rather than
7
+ -- as a refusal. Both are sound only if a raise never commits a partial write. The rollback
8
+ -- argument in (1) covers every SQLSTATE a raise carries, not only the classes listed here.
9
+ -- DRIVEN here, exactly:
10
+ -- post_board_message: 42501 (non-member at the board check, non-member at the thread check,
11
+ -- spoofed author, observer, no agent principal); 22023 (blank body,
12
+ -- unknown priority); 23514 (message type, at the INSERT); P0001 (fault
13
+ -- injected after the receipt).
14
+ -- create_or_find_thread: 22023 (blank subject, non-object metadata); 42501 (spoofed creator, no
15
+ -- agent principal); 23514 (thread type, at the INSERT).
16
+ -- #655 reply parent: 22023 'reply_to_message_id not found in thread' for a missing, other-thread
17
+ -- and unreadable parent, on post_board_message (session, delegate, and the
18
+ -- signed service_role route; once after the empty-thread association),
19
+ -- post_message, send_direct_message and send_multicast_message (uuid cores).
20
+ -- NOT driven: P0002 (thread / board not found, raised before any write) is covered by the rollback
21
+ -- argument but has no throws_ok here; 0A000 and MBS01 are below.
22
+ --
23
+ -- WHY IT HOLDS, and what each part of this file pins:
24
+ -- (1) A refusal is a RAISE that reaches the caller. PostgREST runs an RPC as one transaction, so
25
+ -- the raise rolls back every write the call made before it. RA-R* / RA-C* drive each refusal
26
+ -- class through the REAL RPC under the caller's own role and JWT claims with throws_ok (exact
27
+ -- SQLSTATE, and the message where it names the refusal branch). In a SEPARATE statement they
28
+ -- assert that (a) a board-, thread- and agent-scoped state vector is unchanged (row counts of
29
+ -- the six sinks, the fixture threads' summed last_message_sequence, an md5 of the fixture
30
+ -- thread rows), and (b) a fingerprint of EVERY agent_board table (row count and an
31
+ -- order-independent row hash, catalog-derived from pg_class) is unchanged. (b) is what makes
32
+ -- "each written table" a catalog claim rather than the six names written here (AGENTS.md
33
+ -- rule 1); a table this header does not mention still fails it by name.
34
+ -- R7 and C4 are refused AT the write (a check constraint on the INSERT), after the call has
35
+ -- already written: R7 has stamped the empty thread to the board and advanced its sequence.
36
+ -- R8 injects a fault after the LAST write of a send, the receipt; every sink holds the
37
+ -- call's rows when it raises, and the raise message proves it.
38
+ -- (2) The call cannot be refused and still return a row. A swallowed raise is the one way a
39
+ -- write survives, and every RA-R*/RA-C* throws_ok goes red if the RPC returns normally.
40
+ -- (3) No out-of-transaction side channel. RA-SE-1: no agent_board function body references
41
+ -- pg_notify / NOTIFY, pg_net (`net.` objects), an http*() call or `extensions.http*`
42
+ -- (pgsql-http), dblink*, pg_background*, or pg_logical_emit_message. The population is every
43
+ -- agent_board routine from pg_proc. Bodies are read literal- and comment-STRIPPED (so prose
44
+ -- and strings neither defeat nor trigger it), plus the literal-KEPT body of any routine with
45
+ -- an EXECUTE token (dynamic SQL, where a literal IS code). pg_notify, NOTIFY and pg_net
46
+ -- queue work that is only delivered on commit. They are banned anyway: the claim should
47
+ -- not depend on reasoning about each extension's commit semantics. RA-TRG-1 keeps the
48
+ -- population closed under triggers: every user trigger on an agent_board table runs an
49
+ -- agent_board function. RA-SE-0 is the in-file control. It plants one probe per family plus
50
+ -- a dynamic-SQL probe and an inert probe that names every family only in comments and
51
+ -- literals, then requires the scan to report exactly the code probes. Every probe has a
52
+ -- QUOTED twin (`"net".x`, `"http_get"(`, `pg_catalog."pg_notify"(`) wherever the object can be
53
+ -- named by a quoted identifier, and the inert probe carries the quoted spellings too.
54
+ -- (4) No call out of the schema the guard reads. RA-EXT-1: the routines OUTSIDE agent_board that
55
+ -- either writer's call closure calls (qualified, quoted or bare; a bare call counts only
56
+ -- where no pg_catalog routine shares the name, since pg_catalog wins it) are exactly the
57
+ -- allowlist. RA-EXT-2 asserts each allowlisted callee positively: it exists, it is not
58
+ -- VOLATILE (so it cannot write), and its body hits no RA-SE family.
59
+ -- MBS01. Neither writer can raise it. RA-MB-1 asserts that no routine in either writer's call
60
+ -- closure (agent_board_test.fn_call_closure) carries the MBS01 SQLSTATE, so the class
61
+ -- is unreachable from these RPCs rather than untested. RA-MB-0 is the control: grant_board_role's
62
+ -- closure does carry it. If a writer gains a step-up gate, MB-1 fails and an MBS01 row belongs
63
+ -- here.
64
+ --
65
+ -- WHAT THIS FILE DOES NOT COVER, stated so a green run is not read as more:
66
+ -- * A call into pg_catalog itself (RA-EXT-1 excludes it; the side-channel built-ins there are
67
+ -- named by RA-SE-1's families), effects in a RULE, or a name the patterns cannot see: one
68
+ -- assembled by concatenation inside dynamic SQL, or a U&"..." Unicode-escaped identifier.
69
+ -- * RA-EXT-1 reads literal-STRIPPED bodies only (unlike RA-SE-1, it has no literal-kept reading for
70
+ -- EXECUTE-bearing routines), so a call to an out-of-schema routine assembled inside EXECUTE,
71
+ -- whether as a string literal or through format('... %I ...'), is not seen.
72
+ -- * fn_call_closure follows calls between agent_board routines and does NOT follow triggers. A
73
+ -- side channel reached through a trigger on a table a writer writes, whose function then calls
74
+ -- a routine outside agent_board, is not seen by RA-EXT-1 or RA-MB-1. RA-TRG-1 bounds only the
75
+ -- first hop: the trigger function itself must live in agent_board.
76
+ -- * RA-EXT-1's pg_catalog name-shadow rule (a bare call is not counted when pg_catalog has a
77
+ -- routine of that name) assumes exact signature resolution. A bare call whose argument types
78
+ -- resolve to an overload outside pg_catalog, e.g. in public on the caller's search_path, is
79
+ -- not counted.
80
+ -- * create_or_find_thread's 0A000 isolation refusal: an isolation level is fixed by a
81
+ -- transaction's first statement and this file already runs inside one. It is pinned in
82
+ -- tests/isolation/specs/create_or_find_thread_same_key_race.spec.
83
+ -- * The signed_* and service_role routes, except the #655 reply-parent refusal RA-R14. This file
84
+ -- drives the authenticated session routes Portal relays.
85
+ --
86
+ -- METHOD. Every call runs under its caller's own role AND claims (pg_temp.enter / leave). Allow-side
87
+ -- calls use lives_ok and then a separate state read (#113). Nothing raising is ever a bare select.
88
+ -- State and fingerprints are read as the table owner, outside the role switch.
89
+ -- Mutation receipts: tests/mutations/919/ (run.sh), listed in the PR with the assertion each mutant
90
+ -- turns red.
91
+ --
92
+ -- HAND-COUNTED plan: catalog FP-0, SE-0, SE-1, TRG-1, MB-0, MB-1, EXT-1, EXT-2 (8) + FIX-1 (1)
93
+ -- + post_board_message R1..R9 x3 (27) + P1, P2 x3 (6)
94
+ -- + create_or_find_thread C1..C5 x3 (15) + P3 x3 (3) = 60
95
+ -- + #655 FIX-2 (1) + 15 reply-parent cases x4 (60) + RT-R-ORACLE x4 (4) = 125.
96
+ create extension if not exists pgtap with schema extensions;
97
+ set search_path = extensions, public, agent_board;
98
+
99
+ begin;
100
+
101
+ select plan(125);
102
+
103
+ grant usage on schema extensions to authenticated;
104
+
105
+ -- ===========================================================================
106
+ -- Helpers
107
+ -- ===========================================================================
108
+ -- Fixture ids. Read by the call builders, which run under the caller's role.
109
+ create temp table _rf(k text primary key, v uuid not null);
110
+ grant select on _rf to authenticated;
111
+ create function pg_temp.u(p_k text) returns uuid language sql stable as $$ select v from _rf where k = p_k $$;
112
+
113
+ -- Become fixture actor p_k (an authenticated session for p_k's auth user), and back.
114
+ create function pg_temp.enter(p_k text) returns void language plpgsql as $$
115
+ declare v_sub uuid := (select v from _rf where k = p_k || '.auth');
116
+ begin
117
+ if v_sub is null then
118
+ raise exception 'fixture: actor % has no auth user', p_k;
119
+ end if;
120
+ perform set_config('request.jwt.claim.sub', '', true);
121
+ perform set_config('request.jwt.claim.role', '', true);
122
+ perform set_config('request.jwt.claims', jsonb_build_object('role', 'authenticated', 'sub', v_sub)::text, true);
123
+ execute 'set local role authenticated';
124
+ end $$;
125
+ create function pg_temp.leave() returns void language plpgsql as $$
126
+ begin
127
+ reset role;
128
+ perform set_config('request.jwt.claims', '', true);
129
+ end $$;
130
+ -- current_agent_id() as p_k: the fixture sanity read.
131
+ create function pg_temp.who(p_k text) returns uuid language plpgsql as $$
132
+ declare v uuid;
133
+ begin
134
+ perform pg_temp.enter(p_k);
135
+ v := agent_board.current_agent_id();
136
+ perform pg_temp.leave();
137
+ return v;
138
+ end $$;
139
+
140
+ -- The calls, as SQL text. p_author / p_creator NULL means the session's own agent.
141
+ create function pg_temp.pbm(p_thread text, p_board text, p_subject text, p_body text,
142
+ p_author text default null, p_type text default 'comment',
143
+ p_priority text default 'medium', p_key text default null)
144
+ returns text language sql stable as $$
145
+ select format('select * from agent_board.post_board_message(%L::uuid, %L::uuid, %L::uuid, %L, %L, %L, %L, %L, ''{}''::jsonb)',
146
+ pg_temp.u(p_thread), pg_temp.u(p_author), pg_temp.u(p_board), p_subject, p_body, p_type, p_priority, p_key)
147
+ $$;
148
+ create function pg_temp.cof(p_subject text, p_key text, p_creator text default null,
149
+ p_type text default 'coordination', p_metadata text default '{}')
150
+ returns text language sql stable as $$
151
+ select format('select * from agent_board.create_or_find_thread(%L::uuid, %L, %L, %L, %L::jsonb)',
152
+ pg_temp.u(p_creator), p_subject, p_key, p_type, p_metadata)
153
+ $$;
154
+
155
+ -- The SCOPED state vector: everything the two writers can write, restricted to this file's board,
156
+ -- threads (the three fixture threads and any thread a fixture agent creates) and agents. Read as
157
+ -- the owner (send_outcomes has RLS and no policy).
158
+ create function pg_temp.st() returns jsonb language sql stable as $$
159
+ with ag as (select v from _rf where k in ('A', 'B', 'C', 'OB', 'O')),
160
+ bd as (select v from _rf where k = 'X'),
161
+ th as (select t.id from agent_board.threads t
162
+ where t.id in (select v from _rf where k in ('T', 'TO', 'TE'))
163
+ or t.created_by_agent_id in (select v from ag)),
164
+ ms as (select m.id from agent_board.messages m where m.thread_id in (select id from th))
165
+ select jsonb_build_object(
166
+ 'messages', (select count(*) from ms),
167
+ 'delivery_recipients', (select count(*) from agent_board.delivery_recipients d
168
+ where d.board_id in (select v from bd) or d.source_message_id in (select id from ms)
169
+ or d.recipient_agent_id in (select v from ag)),
170
+ 'inbox_items', (select count(*) from agent_board.inbox_items i
171
+ where i.board_id in (select v from bd) or i.source_message_id in (select id from ms)
172
+ or i.recipient_agent_id in (select v from ag)),
173
+ 'callback_outbox_events', (select count(*) from agent_board.callback_outbox_events e
174
+ where e.board_id in (select v from bd) or e.source_message_id in (select id from ms)
175
+ or e.recipient_agent_id in (select v from ag)),
176
+ 'send_outcomes', (select count(*) from agent_board.send_outcomes s
177
+ where s.thread_id in (select id from th) or s.author_agent_id in (select v from ag)),
178
+ 'threads', (select count(*) from th),
179
+ 'seq', (select coalesce(sum(t.last_message_sequence), 0) from agent_board.threads t where t.id in (select id from th)),
180
+ 'thread_rows', (select md5(coalesce(string_agg(t::text, '|' order by t.id), ''))
181
+ from agent_board.threads t where t.id in (select id from th)))
182
+ $$;
183
+
184
+ -- The CATALOG fingerprint: every agent_board table, from pg_class, as "<rows>:<sum of row hashes>".
185
+ -- row_security = off makes a table whose rows RLS would hide RAISE instead of reading short; the
186
+ -- raise is recorded as UNREADABLE and RA-FP-0 names it (an unreadable table would otherwise
187
+ -- compare equal to itself and pass every "unchanged" row below).
188
+ create function pg_temp.fp() returns jsonb language plpgsql set row_security = off as $$
189
+ declare r record; v jsonb := '{}'::jsonb; x text;
190
+ begin
191
+ for r in select c.relname::text as rel from pg_class c
192
+ where c.relnamespace = 'agent_board'::regnamespace and c.relkind in ('r', 'p')
193
+ order by 1 loop
194
+ begin
195
+ execute format('select count(*)::text || '':'' || coalesce(sum(hashtextextended(q::text, 0)), 0)::text from agent_board.%I q', r.rel)
196
+ into x;
197
+ exception when others then
198
+ x := 'UNREADABLE ' || sqlstate;
199
+ end;
200
+ v := v || jsonb_build_object(r.rel, x);
201
+ end loop;
202
+ return v;
203
+ end $$;
204
+
205
+ create temp table _snap(k text primary key, st jsonb not null, fp jsonb not null);
206
+ create function pg_temp.snap(p_k text) returns void language sql as $$
207
+ insert into _snap values (p_k, pg_temp.st(), pg_temp.fp())
208
+ $$;
209
+ -- agent_board tables whose fingerprint differs from snapshot p_k (NULL = none).
210
+ create function pg_temp.changed(p_k text) returns text language sql as $$
211
+ select string_agg(key, ', ' order by key collate "C")
212
+ from jsonb_each_text(pg_temp.fp()) n
213
+ full join jsonb_each_text((select fp from _snap where k = p_k)) b using (key)
214
+ where n.value is distinct from b.value
215
+ $$;
216
+ -- Of p_rels, the tables whose fingerprint did NOT change since p_k (a name absent from the
217
+ -- fingerprint counts as unchanged, so a renamed sink fails closed).
218
+ create function pg_temp.unmoved(p_k text, p_rels text[]) returns text language sql as $$
219
+ select string_agg(r, ', ' order by r collate "C") from unnest(p_rels) r
220
+ where r not in (select key from jsonb_each_text(pg_temp.fp()) n
221
+ join jsonb_each_text((select fp from _snap where k = p_k)) b using (key)
222
+ where n.value <> b.value)
223
+ $$;
224
+ -- The scoped state keys that changed since p_k (NULL = none).
225
+ create function pg_temp.moved(p_k text) returns text language sql as $$
226
+ select string_agg(key, ',' order by key collate "C")
227
+ from jsonb_each(pg_temp.st()) n join jsonb_each((select st from _snap where k = p_k)) b using (key)
228
+ where n.value <> b.value
229
+ $$;
230
+
231
+ -- ===========================================================================
232
+ -- (3) The catalog guard: no out-of-transaction side channel
233
+ -- ===========================================================================
234
+ -- One row per family. Every pattern is composed from the matcher family in
235
+ -- tests/fixtures/test-prereqs.sql and wrapped once by re(). Names go through ident_re (unquoted
236
+ -- in any case, or "quoted" exactly), so `"net"."http_post"(` is a hit like `net.http_post(` (#920
237
+ -- R1 B1). The leading boundary is "no identifier character before", which admits the `"` of a
238
+ -- quoted name and the `.` of a qualified one. lead_guard() refuses a preceding dot, which is
239
+ -- right for "this exact object" and wrong here, where any qualifier counts. A PREFIX family
240
+ -- (dblink*, pg_background*, http*) spells its quoted alternative as an open quote and the prefix,
241
+ -- because ident_re matches whole names only.
242
+ create temp table _sefam as
243
+ select 'pg_notify'::text as family, agent_board_test.re('(?<![' || agent_board_test.ident_chars() || '])' || agent_board_test.ident_re('pg_notify') || agent_board_test.trail_guard()) as pat
244
+ union all select 'notify' as family, agent_board_test.re('\m' || agent_board_test.ci_re('notify') || '\M') as pat
245
+ union all select 'pg_net' as family, agent_board_test.re('(?<![' || agent_board_test.ident_chars() || '])' || agent_board_test.ident_re('net') || '\s*\.') as pat
246
+ union all select 'http_call' as family, agent_board_test.re('(?<![' || agent_board_test.ident_chars() || '])(?:' || agent_board_test.ci_re('http') || '[' || agent_board_test.ident_chars() || ']*|"http[^"]*")\s*\(') as pat
247
+ union all select 'extensions_http' as family, agent_board_test.re('(?<![' || agent_board_test.ident_chars() || '])' || agent_board_test.ident_re('extensions') || '\s*\.\s*(?:' || agent_board_test.ci_re('http') || '|"http)') as pat
248
+ union all select 'dblink' as family, agent_board_test.re('(?<![' || agent_board_test.ident_chars() || '])(?:' || agent_board_test.ci_re('dblink') || '|"dblink)') as pat
249
+ union all select 'pg_background' as family, agent_board_test.re('(?<![' || agent_board_test.ident_chars() || '])(?:' || agent_board_test.ci_re('pg_background') || '|"pg_background)') as pat
250
+ union all select 'pg_logical_emit_message' as family, agent_board_test.re('(?<![' || agent_board_test.ident_chars() || '])' || agent_board_test.ident_re('pg_logical_emit_message') || agent_board_test.trail_guard()) as pat;
251
+
252
+ -- Every agent_board routine whose body hits a family: "<schema>.<name>(<args>) [<family>]".
253
+ create function pg_temp.se_hits() returns text language sql stable as $$
254
+ select string_agg(s.fn || ' [' || f.family || ']', ', ' order by s.fn collate "C", f.family collate "C")
255
+ from (select n.nspname || '.' || p.proname || '(' || pg_get_function_identity_arguments(p.oid) || ')' as fn,
256
+ agent_board_test.stripped_body(p.oid) as body,
257
+ -- Literals KEPT, read only for a routine with an EXECUTE token: inside dynamic SQL
258
+ -- the string literal is the code, so stripping it would hide the call.
259
+ agent_board_test.stripped_body(p.oid, true) as body_lit
260
+ from pg_proc p join pg_namespace n on n.oid = p.pronamespace
261
+ where n.nspname = 'agent_board' and p.prokind in ('f', 'p')) s
262
+ cross join _sefam as f
263
+ where s.body ~ f.pat
264
+ or (s.body ~ agent_board_test.re('\m' || agent_board_test.ci_re('execute') || '\M') and s.body_lit ~ f.pat)
265
+ $$;
266
+
267
+ -- RA-SE-0 control: one probe per family, a dynamic-SQL probe, and an inert probe naming every
268
+ -- family in comments and literals only.
269
+ create function agent_board.zz919_probe_pg_notify() returns void language plpgsql as $p$ begin perform pg_notify('rf919', 'x'); end $p$;
270
+ create function agent_board.zz919_probe_notify() returns void language plpgsql as $p$ begin notify rf919; end $p$;
271
+ create function agent_board.zz919_probe_pg_net() returns void language plpgsql as $p$ begin perform 1 from net.http_request_queue; end $p$;
272
+ create function agent_board.zz919_probe_http_call() returns void language plpgsql as $p$ begin perform http_get('https://example.test'); end $p$;
273
+ create function agent_board.zz919_probe_extensions_http() returns void language plpgsql as $p$ begin perform null::extensions.http_response; end $p$;
274
+ create function agent_board.zz919_probe_dblink() returns void language plpgsql as $p$ begin perform dblink_exec('rf919', 'select 1'); end $p$;
275
+ create function agent_board.zz919_probe_pg_background() returns void language plpgsql as $p$ begin perform pg_background_launch('select 1'); end $p$;
276
+ create function agent_board.zz919_probe_pg_logical_emit_message() returns void language plpgsql as $p$ begin perform pg_logical_emit_message(false, 'rf919', 'x'); end $p$;
277
+ create function agent_board.zz919_probe_dynamic() returns void language plpgsql as $p$ begin execute 'select pg_notify(''rf919'', ''x'')'; end $p$;
278
+ -- The QUOTED spellings (#920 R1 B1): every family whose object can be named by a quoted identifier.
279
+ create function agent_board.zz919_probe_q_pg_notify() returns void language plpgsql as $p$ begin perform pg_catalog."pg_notify"('rf919', 'x'); end $p$;
280
+ create function agent_board.zz919_probe_q_pg_net() returns void language plpgsql as $p$ begin perform 1 from "net".http_request_queue; end $p$;
281
+ create function agent_board.zz919_probe_q_http_call() returns void language plpgsql as $p$ begin perform "http_get"('https://example.test'); end $p$;
282
+ create function agent_board.zz919_probe_q_extensions_http() returns void language plpgsql as $p$ begin perform null::"extensions"."http_response"; end $p$;
283
+ create function agent_board.zz919_probe_q_dblink() returns void language plpgsql as $p$ begin perform "dblink_exec"('rf919', 'select 1'); end $p$;
284
+ create function agent_board.zz919_probe_q_pg_background() returns void language plpgsql as $p$ begin perform "pg_background_launch"('select 1'); end $p$;
285
+ create function agent_board.zz919_probe_q_pg_logical_emit_message() returns void language plpgsql as $p$ begin perform "pg_logical_emit_message"(false, 'rf919', 'x'); end $p$;
286
+ create function agent_board.zz919_probe_inert() returns void language plpgsql as $p$
287
+ begin
288
+ -- perform pg_notify('rf919', 'x'); notify rf919; perform net.http_post('u'); perform http_get('u');
289
+ -- perform "net"."http_post"('u'); perform "http_get"('u'); perform "dblink_exec"('c', 'q');
290
+ /* perform extensions.http_get('u') /* nested */ perform dblink_exec('c', 'q'); */
291
+ raise notice 'pg_notify( notify net.http_post( http_get( extensions.http_get dblink_exec( pg_background_launch( pg_logical_emit_message(';
292
+ raise notice '"net"."http_post"( "http_get"( "extensions"."http_get" "dblink_exec"( "pg_background_launch"( "pg_notify"(';
293
+ end $p$;
294
+
295
+ select is(pg_temp.se_hits(),
296
+ 'agent_board.zz919_probe_dblink() [dblink], agent_board.zz919_probe_dynamic() [pg_notify], '
297
+ || 'agent_board.zz919_probe_extensions_http() [extensions_http], agent_board.zz919_probe_http_call() [http_call], '
298
+ || 'agent_board.zz919_probe_notify() [notify], agent_board.zz919_probe_pg_background() [pg_background], '
299
+ || 'agent_board.zz919_probe_pg_logical_emit_message() [pg_logical_emit_message], '
300
+ || 'agent_board.zz919_probe_pg_net() [pg_net], agent_board.zz919_probe_pg_notify() [pg_notify], '
301
+ || 'agent_board.zz919_probe_q_dblink() [dblink], agent_board.zz919_probe_q_extensions_http() [extensions_http], '
302
+ || 'agent_board.zz919_probe_q_http_call() [http_call], agent_board.zz919_probe_q_pg_background() [pg_background], '
303
+ || 'agent_board.zz919_probe_q_pg_logical_emit_message() [pg_logical_emit_message], '
304
+ || 'agent_board.zz919_probe_q_pg_net() [pg_net], agent_board.zz919_probe_q_pg_notify() [pg_notify]',
305
+ 'RA-SE-0: control: the scan reports exactly the code probes, unquoted and quoted, each under its family, including dynamic SQL, and not the inert probe');
306
+
307
+ drop function agent_board.zz919_probe_pg_notify(), agent_board.zz919_probe_notify(), agent_board.zz919_probe_pg_net(),
308
+ agent_board.zz919_probe_http_call(), agent_board.zz919_probe_extensions_http(), agent_board.zz919_probe_dblink(),
309
+ agent_board.zz919_probe_pg_background(), agent_board.zz919_probe_pg_logical_emit_message(),
310
+ agent_board.zz919_probe_dynamic(), agent_board.zz919_probe_inert(),
311
+ agent_board.zz919_probe_q_pg_notify(), agent_board.zz919_probe_q_pg_net(), agent_board.zz919_probe_q_http_call(),
312
+ agent_board.zz919_probe_q_extensions_http(), agent_board.zz919_probe_q_dblink(), agent_board.zz919_probe_q_pg_background(),
313
+ agent_board.zz919_probe_q_pg_logical_emit_message();
314
+
315
+ select is(pg_temp.se_hits(), null,
316
+ 'RA-SE-1: no agent_board routine body references pg_notify/NOTIFY, pg_net, http*() or extensions.http, dblink, pg_background or pg_logical_emit_message');
317
+
318
+ select is(
319
+ (select string_agg(c.relname || '.' || t.tgname || ' -> ' || pn.nspname || '.' || p.proname, ', ' order by c.relname, t.tgname)
320
+ from pg_trigger t
321
+ join pg_class c on c.oid = t.tgrelid
322
+ join pg_proc p on p.oid = t.tgfoid
323
+ join pg_namespace pn on pn.oid = p.pronamespace
324
+ where c.relnamespace = 'agent_board'::regnamespace and not t.tgisinternal and pn.nspname <> 'agent_board'),
325
+ null,
326
+ 'RA-TRG-1: every user trigger on an agent_board table runs an agent_board function, so RA-SE-1''s population covers it');
327
+
328
+ create temp table _clos as select * from agent_board_test.fn_call_closure();
329
+ select ok(
330
+ exists (select 1 from _clos c
331
+ where c.root_name = 'grant_board_role'
332
+ -- Literals KEPT: the SQLSTATE 'MBS01' is itself a string literal.
333
+ and agent_board_test.stripped_body(c.reached_oid, true) ~ agent_board_test.re('\m' || agent_board_test.ci_re('mbs01') || '\M')),
334
+ 'RA-MB-0: control: grant_board_role''s call closure carries the MBS01 SQLSTATE');
335
+ select is(
336
+ (select string_agg(distinct c.root_name || ' -> ' || c.reached_name, ', ')
337
+ from _clos c
338
+ where c.root_name in ('post_board_message', 'create_or_find_thread')
339
+ -- Literals KEPT: the SQLSTATE 'MBS01' is itself a string literal.
340
+ and agent_board_test.stripped_body(c.reached_oid, true) ~ agent_board_test.re('\m' || agent_board_test.ci_re('mbs01') || '\M')),
341
+ null,
342
+ 'RA-MB-1: no routine in either writer''s call closure carries MBS01 (the class is unreachable from these RPCs)');
343
+
344
+ -- RA-EXT-1/2 (#920 R1 F2). fn_call_closure and RA-SE-1 see agent_board only, so a writer calling
345
+ -- out of the schema would leave both blind. The candidates are every routine in pg_proc outside
346
+ -- pg_catalog, information_schema, the temp/toast schemas, agent_board and agent_board_test. pgTAP's
347
+ -- own routines are excluded because the test environment installs it and production does not.
348
+ -- A candidate counts as called when a closure body names it QUALIFIED by its schema (call_re:
349
+ -- either case, or quoted). A BARE call resolves through the calling function's search_path, so it
350
+ -- counts when the candidate's schema is on that function's pinned search_path, or the function pins
351
+ -- none (then any schema could be on the caller's path, so it counts), and no pg_catalog routine
352
+ -- shares the name (pg_catalog wins an unqualified call). RA-EXT-1 requires that set to be exactly
353
+ -- the allowlist. RA-EXT-2 then asserts each allowlisted callee positively: it is not
354
+ -- VOLATILE, so it cannot write, and its body hits no RA-SE family.
355
+ create temp table _wb as
356
+ select distinct c.reached_oid as oid, agent_board_test.stripped_body(c.reached_oid) as body,
357
+ -- The pinned search_path as schema names; NULL when the function pins none.
358
+ (select array_agg(btrim(e, ' "')) from pg_proc q, unnest(q.proconfig) g, unnest(string_to_array(substr(g, 13), ',')) e
359
+ where q.oid = c.reached_oid and g like 'search\_path=%') as path
360
+ from _clos c where c.root_name in ('post_board_message', 'create_or_find_thread');
361
+ create temp table _xc as
362
+ select p.oid, n.nspname::text as nspname,
363
+ n.nspname || '.' || p.proname || '(' || pg_get_function_identity_arguments(p.oid) || ')' as sig,
364
+ not exists (select 1 from pg_proc b where b.pronamespace = 'pg_catalog'::regnamespace and b.proname = p.proname) as bare_ok,
365
+ agent_board_test.call_re(n.nspname || '.' || p.proname, false) as pat_q,
366
+ agent_board_test.call_re(n.nspname || '.' || p.proname, true) as pat_b
367
+ from pg_proc p join pg_namespace n on n.oid = p.pronamespace
368
+ where n.nspname not in ('pg_catalog', 'information_schema', 'agent_board', 'agent_board_test')
369
+ and n.nspname not like 'pg\_temp\_%' and n.nspname not like 'pg\_toast%'
370
+ and not exists (select 1 from pg_depend d join pg_extension e on e.oid = d.refobjid
371
+ where d.classid = 'pg_proc'::regclass and d.objid = p.oid and d.deptype = 'e' and e.extname = 'pgtap');
372
+ create temp table _xallow(sig text primary key);
373
+ -- auth.uid(): current_agent_id() resolves the session's auth user. auth.role(): _is_service_role()
374
+ -- reads the request role. Both are Supabase's STABLE claim readers (RA-EXT-2).
375
+ insert into _xallow values ('auth.role()'), ('auth.uid()');
376
+ select is(
377
+ (select string_agg(distinct x.sig collate "C", ', ' order by x.sig collate "C")
378
+ from _wb as w join _xc as x
379
+ on w.body ~ x.pat_q
380
+ or (x.bare_ok and (w.path is null or x.nspname = any(w.path)) and w.body ~ x.pat_b)),
381
+ (select string_agg(sig, ', ' order by sig collate "C") from _xallow),
382
+ 'RA-EXT-1: the routines outside agent_board that either writer''s call closure calls are exactly the allowlist');
383
+ select is(
384
+ nullif(coalesce((select string_agg(x.sig || case when p.provolatile = 'v' then ' [volatile]' else '' end
385
+ || coalesce((select ' [' || string_agg(f.family, ',' order by f.family) || ']'
386
+ from _sefam as f where agent_board_test.stripped_body(p.oid) ~ f.pat), ''), ', ' order by x.sig)
387
+ from _xallow a join _xc x on x.sig = a.sig join pg_proc p on p.oid = x.oid
388
+ where p.provolatile = 'v'
389
+ or exists (select 1 from _sefam as f where agent_board_test.stripped_body(p.oid) ~ f.pat)), '')
390
+ || coalesce(' missing: ' || (select string_agg(a.sig, ', ') from _xallow a where not exists (select 1 from _xc x where x.sig = a.sig)), ''), ''),
391
+ null,
392
+ 'RA-EXT-2: each allowlisted external callee exists, is not VOLATILE and references no side channel');
393
+
394
+ -- ===========================================================================
395
+ -- Fixture
396
+ -- ===========================================================================
397
+ -- Board X: A owner, B and C members, OB a subscribed observer. O is an agent outside X.
398
+ -- NOAGENT is an auth user with no agent row. T is A's thread on X; TO is O's own board-less thread;
399
+ -- TE is A's EMPTY board-less thread (R7/P2 stamp it to X). B has a verified callback endpoint
400
+ -- subscribed to its own inbox and to X's board feed, so a post queues outbox events.
401
+ do $$
402
+ declare h text; n int := 0;
403
+ begin
404
+ foreach h in array array['A', 'B', 'C', 'OB', 'O', 'NOAGENT'] loop
405
+ n := n + 1;
406
+ insert into _rf values (h || '.auth', ('919a0000-0000-4000-8000-' || lpad(n::text, 12, '0'))::uuid);
407
+ if h <> 'NOAGENT' then
408
+ insert into _rf values (h, ('919a1000-0000-4000-8000-' || lpad(n::text, 12, '0'))::uuid);
409
+ end if;
410
+ end loop;
411
+ end $$;
412
+ insert into _rf values
413
+ ('X', '919a2000-0000-4000-8000-000000000001'),
414
+ ('T', '919a3000-0000-4000-8000-000000000001'),
415
+ ('TO', '919a3000-0000-4000-8000-000000000002'),
416
+ ('TE', '919a3000-0000-4000-8000-000000000003'),
417
+ ('EB', '919a4000-0000-4000-8000-000000000001');
418
+
419
+ insert into auth.users(id, instance_id, aud, role, email)
420
+ select v, '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'rf919-' || lower(split_part(k, '.', 1)) || '@t.test'
421
+ from _rf where k like '%.auth';
422
+ insert into agent_board.agents(id, agent_type, kind, handle, display_name, status, auth_user_id)
423
+ select a.v, 'agent', 'agent', 'rf919-' || lower(a.k), 'RF919 ' || a.k, 'active', u.v
424
+ from _rf a join _rf u on u.k = a.k || '.auth'
425
+ where a.k in ('A', 'B', 'C', 'OB', 'O');
426
+ insert into agent_board.boards(id, slug, name, created_by_agent_id, visibility)
427
+ values (pg_temp.u('X'), 'rf919-x', 'RF919 X', pg_temp.u('A'), 'private');
428
+ insert into agent_board.board_memberships(board_id, agent_id, membership_state, role) values
429
+ (pg_temp.u('X'), pg_temp.u('A'), 'joined', 'owner'),
430
+ (pg_temp.u('X'), pg_temp.u('B'), 'joined', 'member'),
431
+ (pg_temp.u('X'), pg_temp.u('C'), 'joined', 'member'),
432
+ (pg_temp.u('X'), pg_temp.u('OB'), 'subscribed', 'observer');
433
+ insert into agent_board.threads(id, thread_type, created_by_agent_id, subject, board_id) values
434
+ (pg_temp.u('T'), 'coordination', pg_temp.u('A'), 'RF919 T', pg_temp.u('X')),
435
+ (pg_temp.u('TO'), 'coordination', pg_temp.u('O'), 'RF919 TO', null),
436
+ (pg_temp.u('TE'), 'coordination', pg_temp.u('A'), 'RF919 TE', null);
437
+ insert into agent_board.callback_endpoints(id, owner_agent_id, endpoint_url, endpoint_status)
438
+ values (pg_temp.u('EB'), pg_temp.u('B'), 'https://example.test/rf919-b', 'verified');
439
+ insert into agent_board.callback_subscriptions(endpoint_id, agent_id, subscription_scope, event_category)
440
+ values (pg_temp.u('EB'), pg_temp.u('B'), 'own_inbox', 'inbox.item.delivered');
441
+ insert into agent_board.callback_subscriptions(endpoint_id, agent_id, subscription_scope, event_category, board_id, board_membership_id)
442
+ select pg_temp.u('EB'), pg_temp.u('B'), 'board', 'board.post.delivered', pg_temp.u('X'), bm.id
443
+ from agent_board.board_memberships bm where bm.board_id = pg_temp.u('X') and bm.agent_id = pg_temp.u('B');
444
+
445
+ select is(
446
+ (select string_agg(k || '=' || (pg_temp.who(k) is not distinct from pg_temp.u(k))::text, ' ' order by k collate "C")
447
+ from unnest(array['A', 'B', 'O', 'OB']) k)
448
+ || ' NOAGENT=' || coalesce(pg_temp.who('NOAGENT')::text, 'null'),
449
+ 'A=true B=true O=true OB=true NOAGENT=null',
450
+ 'RA-FIX-1: each fixture session resolves to its own agent, and NOAGENT to none');
451
+
452
+ select is(
453
+ (select coalesce(string_agg(key, ', ' order by key collate "C") filter (where value like 'UNREADABLE%'), '(none)')
454
+ from jsonb_each_text(pg_temp.fp())),
455
+ '(none)',
456
+ 'RA-FP-0: the fingerprint reads every agent_board table (an unreadable one would compare equal to itself)');
457
+
458
+ -- ===========================================================================
459
+ -- (1)+(2) post_board_message refusals
460
+ -- ===========================================================================
461
+ -- R1: a NON-MEMBER of the board, posting on its own accessible thread: 42501 at the board check.
462
+ select pg_temp.snap('R1');
463
+ select pg_temp.enter('O');
464
+ select throws_ok(pg_temp.pbm('TO', 'X', 'RF919 R1', 'r1 body'), '42501', 'board not accessible to current agent',
465
+ 'RA-R1: a non-member posting to the board is refused 42501 (board not accessible)');
466
+ select pg_temp.leave();
467
+ select is(pg_temp.st(), (select st from _snap where k = 'R1'), 'RA-R1 rows: the non-member refusal left the scoped sinks and threads unchanged');
468
+ select is(pg_temp.changed('R1'), null, 'RA-R1 catalog: the non-member refusal changed no agent_board table');
469
+
470
+ -- R2: a non-member on the board's thread: 42501 at the thread check.
471
+ select pg_temp.snap('R2');
472
+ select pg_temp.enter('O');
473
+ select throws_ok(pg_temp.pbm('T', 'X', 'RF919 R2', 'r2 body'), '42501', 'thread not accessible to current agent',
474
+ 'RA-R2: a non-member posting on the board thread is refused 42501 (thread not accessible)');
475
+ select pg_temp.leave();
476
+ select is(pg_temp.st(), (select st from _snap where k = 'R2'), 'RA-R2 rows: the thread-access refusal left the scoped sinks and threads unchanged');
477
+ select is(pg_temp.changed('R2'), null, 'RA-R2 catalog: the thread-access refusal changed no agent_board table');
478
+
479
+ -- R3: a member naming another member as the author: 42501 before anything else runs.
480
+ select pg_temp.snap('R3');
481
+ select pg_temp.enter('A');
482
+ select throws_ok(pg_temp.pbm('T', 'X', 'RF919 R3', 'r3 body', p_author => 'B'), '42501', 'p_author_agent_id must match current_agent_id()',
483
+ 'RA-R3: a spoofed author is refused 42501');
484
+ select pg_temp.leave();
485
+ select is(pg_temp.st(), (select st from _snap where k = 'R3'), 'RA-R3 rows: the spoofed-author refusal left the scoped sinks and threads unchanged');
486
+ select is(pg_temp.changed('R3'), null, 'RA-R3 catalog: the spoofed-author refusal changed no agent_board table');
487
+
488
+ -- R4: an observer: 42501 at the send-role check.
489
+ select pg_temp.snap('R4');
490
+ select pg_temp.enter('OB');
491
+ select throws_ok(pg_temp.pbm('T', 'X', 'RF919 R4', 'r4 body'), '42501', null,
492
+ 'RA-R4: an observer is refused 42501');
493
+ select pg_temp.leave();
494
+ select is(pg_temp.st(), (select st from _snap where k = 'R4'), 'RA-R4 rows: the observer refusal left the scoped sinks and threads unchanged');
495
+ select is(pg_temp.changed('R4'), null, 'RA-R4 catalog: the observer refusal changed no agent_board table');
496
+
497
+ -- R5: bad input, a blank body: 22023.
498
+ select pg_temp.snap('R5');
499
+ select pg_temp.enter('A');
500
+ select throws_ok(pg_temp.pbm('T', 'X', 'RF919 R5', ' '), '22023', 'subject and body are required',
501
+ 'RA-R5: a blank body is refused 22023');
502
+ select pg_temp.leave();
503
+ select is(pg_temp.st(), (select st from _snap where k = 'R5'), 'RA-R5 rows: the blank-body refusal left the scoped sinks and threads unchanged');
504
+ select is(pg_temp.changed('R5'), null, 'RA-R5 catalog: the blank-body refusal changed no agent_board table');
505
+
506
+ -- R6: bad input, an unknown priority: 22023.
507
+ select pg_temp.snap('R6');
508
+ select pg_temp.enter('A');
509
+ select throws_ok(pg_temp.pbm('T', 'X', 'RF919 R6', 'r6 body', p_priority => 'rf919-bogus'), '22023', 'invalid priority',
510
+ 'RA-R6: an unknown priority is refused 22023');
511
+ select pg_temp.leave();
512
+ select is(pg_temp.st(), (select st from _snap where k = 'R6'), 'RA-R6 rows: the bad-priority refusal left the scoped sinks and threads unchanged');
513
+ select is(pg_temp.changed('R6'), null, 'RA-R6 catalog: the bad-priority refusal changed no agent_board table');
514
+
515
+ -- R7: refused AT the message INSERT (check constraint on message_type), AFTER the call stamped the
516
+ -- empty thread TE to X and advanced TE's message sequence. P2 is the same call with a valid type.
517
+ select pg_temp.snap('R7');
518
+ select pg_temp.enter('A');
519
+ select throws_ok(pg_temp.pbm('TE', 'X', 'RF919 R7', 'r7 body', p_type => 'rf919-bogus'), '23514', null,
520
+ 'RA-R7: a message type the table refuses fails the INSERT (23514) after the thread association and sequence writes');
521
+ select pg_temp.leave();
522
+ select is(pg_temp.st(), (select st from _snap where k = 'R7'), 'RA-R7 rows: the late refusal rolled back the thread association and sequence writes');
523
+ select is(pg_temp.changed('R7'), null, 'RA-R7 catalog: the late refusal changed no agent_board table');
524
+
525
+ -- R8: a fault AFTER the last write of a keyed send (an AFTER INSERT trigger on send_outcomes). At
526
+ -- that moment the message, its deliveries, inbox items, outbox events and receipt all exist, and
527
+ -- the raise message says so.
528
+ create function agent_board_test.zz919_fault_after_receipt() returns trigger
529
+ language plpgsql security definer set search_path = '' as $f$
530
+ begin
531
+ raise exception 'rf919 fault after receipt: messages=% deliveries=% inbox=% outbox=% receipts=%',
532
+ (select count(*) from agent_board.messages m where m.id = new.message_id),
533
+ (select case when count(*) > 0 then 'yes' else 'no' end from agent_board.delivery_recipients d where d.source_message_id = new.message_id),
534
+ (select case when count(*) > 0 then 'yes' else 'no' end from agent_board.inbox_items i where i.source_message_id = new.message_id),
535
+ (select case when count(*) > 0 then 'yes' else 'no' end from agent_board.callback_outbox_events e where e.source_message_id = new.message_id),
536
+ (select count(*) from agent_board.send_outcomes s where s.message_id = new.message_id);
537
+ end $f$;
538
+ create trigger zz919_fault_after_receipt after insert on agent_board.send_outcomes
539
+ for each row execute function agent_board_test.zz919_fault_after_receipt();
540
+ select pg_temp.snap('R8');
541
+ select pg_temp.enter('A');
542
+ select throws_ok(pg_temp.pbm('T', 'X', 'RF919 R8', 'r8 body', p_key => 'rf919-r8'), 'P0001',
543
+ 'rf919 fault after receipt: messages=1 deliveries=yes inbox=yes outbox=yes receipts=1',
544
+ 'RA-R8: a fault after the receipt reaches the caller, raised when every sink held the call''s rows');
545
+ select pg_temp.leave();
546
+ drop trigger zz919_fault_after_receipt on agent_board.send_outcomes;
547
+ drop function agent_board_test.zz919_fault_after_receipt();
548
+ select is(pg_temp.st(), (select st from _snap where k = 'R8'), 'RA-R8 rows: the fault rolled back the message, deliveries, inbox items, outbox events and receipt');
549
+ select is(pg_temp.changed('R8'), null, 'RA-R8 catalog: the fault left no agent_board table changed');
550
+
551
+ -- R9: an authenticated session with no agent principal: 42501.
552
+ select pg_temp.snap('R9');
553
+ select pg_temp.enter('NOAGENT');
554
+ select throws_ok(pg_temp.pbm('T', 'X', 'RF919 R9', 'r9 body'), '42501', 'current agent principal not resolved',
555
+ 'RA-R9: a session with no agent principal is refused 42501');
556
+ select pg_temp.leave();
557
+ select is(pg_temp.st(), (select st from _snap where k = 'R9'), 'RA-R9 rows: the principal-less refusal left the scoped sinks and threads unchanged');
558
+ select is(pg_temp.changed('R9'), null, 'RA-R9 catalog: the principal-less refusal changed no agent_board table');
559
+
560
+ -- ===========================================================================
561
+ -- post_board_message positive controls: the same reads DO see a write
562
+ -- ===========================================================================
563
+ -- P1: A's keyed post on T, the shape of R1..R6/R8/R9 when allowed.
564
+ select pg_temp.snap('P1');
565
+ select pg_temp.enter('A');
566
+ select lives_ok(pg_temp.pbm('T', 'X', 'RF919 P1', 'p1 body', p_key => 'rf919-p1'),
567
+ 'RA-P1: a member''s keyed board post succeeds');
568
+ select pg_temp.leave();
569
+ select is(pg_temp.moved('P1'), 'callback_outbox_events,delivery_recipients,inbox_items,messages,send_outcomes,seq,thread_rows',
570
+ 'RA-P1 rows: the allowed post moved every scoped sink, the sequence and the thread row');
571
+ select is(pg_temp.unmoved('P1', array['callback_outbox_events', 'delivery_recipients', 'inbox_items', 'messages', 'send_outcomes', 'threads']), null,
572
+ 'RA-P1 catalog: the allowed post changed all six named sink tables in the fingerprint');
573
+
574
+ -- P2: R7 with a valid message type: the association, sequence and delivery writes are visible.
575
+ select pg_temp.snap('P2');
576
+ select pg_temp.enter('A');
577
+ select lives_ok(pg_temp.pbm('TE', 'X', 'RF919 P2', 'p2 body'),
578
+ 'RA-P2: the R7 call with a valid message type succeeds');
579
+ select pg_temp.leave();
580
+ select is(pg_temp.moved('P2'), 'callback_outbox_events,delivery_recipients,inbox_items,messages,seq,thread_rows',
581
+ 'RA-P2 rows: the allowed call stamped TE (thread row), advanced its sequence and delivered');
582
+ select is(pg_temp.unmoved('P2', array['callback_outbox_events', 'delivery_recipients', 'inbox_items', 'messages', 'threads']), null,
583
+ 'RA-P2 catalog: the allowed call changed the five unkeyed sink tables in the fingerprint');
584
+
585
+ -- ===========================================================================
586
+ -- (1)+(2) create_or_find_thread refusals
587
+ -- ===========================================================================
588
+ -- C1: bad input, a blank subject: 22023.
589
+ select pg_temp.snap('C1');
590
+ select pg_temp.enter('A');
591
+ select throws_ok(pg_temp.cof(' ', 'rf919-c1'), '22023', 'subject and idempotency key are required',
592
+ 'RA-C1: a blank subject is refused 22023');
593
+ select pg_temp.leave();
594
+ select is(pg_temp.st(), (select st from _snap where k = 'C1'), 'RA-C1 rows: the blank-subject refusal created no thread');
595
+ select is(pg_temp.changed('C1'), null, 'RA-C1 catalog: the blank-subject refusal changed no agent_board table');
596
+
597
+ -- C2: bad input, non-object metadata: 22023.
598
+ select pg_temp.snap('C2');
599
+ select pg_temp.enter('A');
600
+ select throws_ok(pg_temp.cof('RF919 C2', 'rf919-c2', p_metadata => '[]'), '22023', 'metadata must be a JSON object',
601
+ 'RA-C2: non-object metadata is refused 22023');
602
+ select pg_temp.leave();
603
+ select is(pg_temp.st(), (select st from _snap where k = 'C2'), 'RA-C2 rows: the bad-metadata refusal created no thread');
604
+ select is(pg_temp.changed('C2'), null, 'RA-C2 catalog: the bad-metadata refusal changed no agent_board table');
605
+
606
+ -- C3: a spoofed creator: 42501.
607
+ select pg_temp.snap('C3');
608
+ select pg_temp.enter('A');
609
+ select throws_ok(pg_temp.cof('RF919 C3', 'rf919-c3', p_creator => 'B'), '42501', 'p_created_by_agent_id must match current_agent_id()',
610
+ 'RA-C3: a spoofed creator is refused 42501');
611
+ select pg_temp.leave();
612
+ select is(pg_temp.st(), (select st from _snap where k = 'C3'), 'RA-C3 rows: the spoofed-creator refusal created no thread');
613
+ select is(pg_temp.changed('C3'), null, 'RA-C3 catalog: the spoofed-creator refusal changed no agent_board table');
614
+
615
+ -- C4: refused AT the thread INSERT (check constraint on thread_type), after the (actor, key) lock.
616
+ select pg_temp.snap('C4');
617
+ select pg_temp.enter('A');
618
+ select throws_ok(pg_temp.cof('RF919 C4', 'rf919-c4', p_type => 'rf919-bogus'), '23514', null,
619
+ 'RA-C4: a thread type the table refuses fails the INSERT (23514)');
620
+ select pg_temp.leave();
621
+ select is(pg_temp.st(), (select st from _snap where k = 'C4'), 'RA-C4 rows: the late refusal created no thread');
622
+ select is(pg_temp.changed('C4'), null, 'RA-C4 catalog: the late refusal changed no agent_board table');
623
+
624
+ -- C5: an authenticated session with no agent principal: 42501.
625
+ select pg_temp.snap('C5');
626
+ select pg_temp.enter('NOAGENT');
627
+ select throws_ok(pg_temp.cof('RF919 C5', 'rf919-c5'), '42501', 'current agent principal not resolved',
628
+ 'RA-C5: a session with no agent principal is refused 42501');
629
+ select pg_temp.leave();
630
+ select is(pg_temp.st(), (select st from _snap where k = 'C5'), 'RA-C5 rows: the principal-less refusal created no thread');
631
+ select is(pg_temp.changed('C5'), null, 'RA-C5 catalog: the principal-less refusal changed no agent_board table');
632
+
633
+ -- P3: the allowed call creates a thread, and the same reads see it.
634
+ select pg_temp.snap('P3');
635
+ select pg_temp.enter('A');
636
+ select lives_ok(pg_temp.cof('RF919 P3', 'rf919-p3'),
637
+ 'RA-P3: create_or_find_thread with a new key succeeds');
638
+ select pg_temp.leave();
639
+ select is(pg_temp.moved('P3'), 'thread_rows,threads',
640
+ 'RA-P3 rows: the allowed call created a thread for its caller');
641
+ select is(pg_temp.unmoved('P3', array['threads']), null,
642
+ 'RA-P3 catalog: the allowed call changed the threads table in the fingerprint');
643
+
644
+ -- ===========================================================================
645
+ -- (1)+(2) #655 REPLYTO: the single reply-parent refusal, and zero rows (plan §8.4)
646
+ -- ===========================================================================
647
+ -- Every unusable parent (missing, in another thread, or unreadable by the session-derived actor)
648
+ -- is refused 22023 'reply_to_message_id not found in thread'. Each case is driven TWICE (code only,
649
+ -- then code and message), then the scoped state and the catalog fingerprint are read. The refusal
650
+ -- comes after writes on post_board_message (telemetry for service_role, the idempotency lock, the
651
+ -- empty-thread association: R10b drives the association), so these are "refused after a write"
652
+ -- rows. RT-R-ORACLE then requires the three cases of each writer to give byte-identical errors.
653
+ -- Names: the plan's RA-P1..3 / RA-D1..3 / RA-MC1..3 are RA-PM*, RA-DM*, RA-MC* here, because this
654
+ -- file already has RA-P1..P3 (positive controls) and D1 is a fixture message.
655
+ --
656
+ -- Fixture additions (after every earlier row, so none of them moves): thread U on X with A's post
657
+ -- M2; A's board post M1 and A's DM D1 to B on T (C cannot read D1); TE2, A's EMPTY board-less
658
+ -- thread; the hosted delegate H (principal HO, joined on X) placed on X; a fresh uuid MISSING.
659
+ insert into _rf values
660
+ ('U', '919a3000-0000-4000-8000-000000000004'),
661
+ ('TE2', '919a3000-0000-4000-8000-000000000005'),
662
+ ('MISSING', '919a9000-0000-4000-8000-000000000001');
663
+ insert into agent_board.threads(id, thread_type, created_by_agent_id, subject, board_id) values
664
+ (pg_temp.u('U'), 'coordination', pg_temp.u('A'), 'RF919 U', pg_temp.u('X')),
665
+ (pg_temp.u('TE2'), 'coordination', pg_temp.u('A'), 'RF919 TE2', null);
666
+
667
+ -- Role entry for a delegate and for service_role (the gateway), beside enter().
668
+ grant usage on schema extensions to agent_board_delegate, service_role;
669
+ do $$
670
+ begin
671
+ if not (select rolsuper from pg_roles where rolname = current_user) then
672
+ execute format('grant agent_board_delegate to %I with set true', current_user);
673
+ end if;
674
+ end $$;
675
+ create temp table _rfc(k text primary key, v text not null);
676
+ grant select on _rfc to authenticated, agent_board_delegate, service_role;
677
+ grant select on _rf to agent_board_delegate, service_role;
678
+ create function pg_temp.enter_d(p_k text) returns void language plpgsql as $$
679
+ begin
680
+ perform set_config('request.jwt.claim.sub', '', true);
681
+ perform set_config('request.jwt.claim.role', '', true);
682
+ perform set_config('request.jwt.claims', jsonb_build_object('role', 'agent_board_delegate', 'sub', (select v from _rf where k = p_k || '.auth'),
683
+ 'agent_credential_id', (select v from _rfc where k = p_k || '.cred'))::text, true);
684
+ execute 'set local role agent_board_delegate';
685
+ end $$;
686
+ create function pg_temp.enter_svc() returns void language plpgsql as $$
687
+ begin
688
+ perform set_config('request.jwt.claim.sub', '', true);
689
+ perform set_config('request.jwt.claim.role', '', true);
690
+ perform set_config('request.jwt.claims', '{"role": "service_role"}', true);
691
+ execute 'set local role service_role';
692
+ end $$;
693
+
694
+ insert into agent_board.identity_providers
695
+ (issuer, audience, supported, policy_approved, allowed_scopes, allowed_board_scopes, allowed_agent_kinds)
696
+ values ('https://portal.rf655.test', 'message-board', true, true, '{}', '{}', '{agent,human}');
697
+ do $$
698
+ declare r record;
699
+ begin
700
+ perform set_config('request.jwt.claims', '', true);
701
+ select * into r from agent_board.enroll_federated_human('https://portal.rf655.test', 'message-board', 'rf655-owner',
702
+ '{"sub": "rf655-owner", "email": "rf655-owner@t.test", "email_verified": true}'::jsonb, 'RF655 Owner');
703
+ insert into _rf values ('HO', r.agent_id), ('HO.auth', r.auth_user_id);
704
+ select * into r from agent_board.exchange_delegate_credential(jsonb_build_object(
705
+ 'iss', 'https://portal.rf655.test', 'sub', 'rf655-owner', 'slot_resource', null,
706
+ 'agent_board_delegate_instance', jsonb_build_object('instance_id', 'rf655_instance_00001', 'grant_id', 'rf655_grant_000000001',
707
+ 'generation', 1, 'owner_sub', 'rf655-owner', 'client_id', 'rf655-client', 'status', 'active', 'client_change', null)));
708
+ insert into _rf values ('H', r.agent_id), ('H.auth', r.auth_user_id);
709
+ insert into _rfc values ('H.cred', r.credential_id::text);
710
+ end $$;
711
+ insert into agent_board.board_memberships(board_id, agent_id, membership_state, role)
712
+ values (pg_temp.u('X'), pg_temp.u('HO'), 'joined', 'member');
713
+ select pg_temp.enter('HO');
714
+ select agent_board.add_delegate_to_board(pg_temp.u('H'), pg_temp.u('X'));
715
+ select pg_temp.leave();
716
+
717
+ -- The fixture messages, through the real RPCs (A records their ids in _rf; a NULL id under a mutant
718
+ -- is skipped so the rows below fail instead of the file aborting).
719
+ grant insert on _rf to authenticated;
720
+ select pg_temp.enter('A');
721
+ insert into _rf select 'M1', message_id from agent_board.post_board_message(pg_temp.u('T'), null, pg_temp.u('X'), 'RF655 M1', 'rf655 m1 body') where message_id is not null;
722
+ insert into _rf select 'M2', message_id from agent_board.post_board_message(pg_temp.u('U'), null, pg_temp.u('X'), 'RF655 M2', 'rf655 m2 body') where message_id is not null;
723
+ insert into _rf select 'D1', message_id from agent_board.send_direct_message(pg_temp.u('T'), null, pg_temp.u('B'), 'RF655 D1', 'rf655 d1 private to B') where message_id is not null;
724
+ select pg_temp.leave();
725
+
726
+ -- The reply-carrying call builders (p_reply is a fixture key).
727
+ create function pg_temp.pbmr(p_thread text, p_reply text) returns text language sql stable as $$
728
+ select format('select * from agent_board.post_board_message(%L::uuid, null, %L::uuid, ''RF655 reply'', ''rf655 reply body'', ''comment'', ''medium'', null, ''{}''::jsonb, p_reply_to_message_id => %L::uuid)',
729
+ pg_temp.u(p_thread), pg_temp.u('X'), pg_temp.u(p_reply))
730
+ $$;
731
+ create function pg_temp.pmr(p_reply text) returns text language sql stable as $$
732
+ select format('select * from agent_board.post_message(%L::uuid, null, ''rf655 reply body'', p_reply_to_message_id => %L::uuid)',
733
+ pg_temp.u('T'), pg_temp.u(p_reply))
734
+ $$;
735
+ create function pg_temp.dmr(p_to text, p_reply text) returns text language sql stable as $$
736
+ select format('select * from agent_board.send_direct_message(%L::uuid, null, %L::uuid, ''RF655 reply'', ''rf655 reply body'', p_reply_to_message_id => %L::uuid)',
737
+ pg_temp.u('T'), pg_temp.u(p_to), pg_temp.u(p_reply))
738
+ $$;
739
+ create function pg_temp.mcr(p_to text, p_reply text) returns text language sql stable as $$
740
+ select format('select * from agent_board.send_multicast_message(%L::uuid, null, array[%L]::uuid[], ''RF655 reply'', ''rf655 reply body'', p_reply_to_message_id => %L::uuid)',
741
+ pg_temp.u('T'), pg_temp.u(p_to), pg_temp.u(p_reply))
742
+ $$;
743
+ create function pg_temp.spbmr(p_actor text, p_reply text) returns text language sql stable as $$
744
+ select format('select * from agent_board.signed_post_board_message(%L::jsonb, %L::jsonb)',
745
+ jsonb_build_object('p_thread_id', pg_temp.u('T'), 'p_board_id', pg_temp.u('X'), 'p_subject', 'RF655 reply', 'p_body', 'rf655 reply body',
746
+ 'p_reply_to_message_id', pg_temp.u(p_reply))::text,
747
+ jsonb_build_object('agent_id', pg_temp.u(p_actor), 'verification_status', 'warn', 'payload_digest', 'sha256:' || repeat('7', 64),
748
+ 'enforcement_mode', 'warn')::text)
749
+ $$;
750
+ -- A call's error as '<sqlstate>:<message>' ('ok' if it succeeded), under p_k's session.
751
+ create function pg_temp.err(p_k text, p_sql text) returns text language plpgsql as $$
752
+ declare v text;
753
+ begin
754
+ perform pg_temp.enter(p_k);
755
+ begin
756
+ execute p_sql;
757
+ v := 'ok';
758
+ exception when others then
759
+ v := sqlstate || ':' || sqlerrm;
760
+ end;
761
+ perform pg_temp.leave();
762
+ return v;
763
+ end $$;
764
+
765
+ create function pg_temp.reads(p_k text, p_msg text) returns bigint language plpgsql as $$
766
+ declare v bigint;
767
+ begin
768
+ perform pg_temp.enter(p_k);
769
+ execute 'select count(*) from agent_board.messages where id = $1' into v using pg_temp.u(p_msg);
770
+ perform pg_temp.leave();
771
+ return v;
772
+ end $$;
773
+ select is(format('B:D1=%s C:D1=%s C:M1=%s A:M2=%s', pg_temp.reads('B', 'D1'), pg_temp.reads('C', 'D1'), pg_temp.reads('C', 'M1'), pg_temp.reads('A', 'M2')),
774
+ 'B:D1=1 C:D1=0 C:M1=1 A:M2=1',
775
+ 'RA-FIX-2: the fixture parents exist and the premise holds: B reads its DM D1, C cannot, and the board posts are readable');
776
+
777
+ -- RA-R10: post_board_message: a parent that does not exist.
778
+ select pg_temp.snap('R10');
779
+ select pg_temp.enter('A');
780
+ select throws_ok(pg_temp.pbmr('T', 'MISSING'), '22023', null,
781
+ 'RA-R10: missing parent refused 22023 (post_board_message)');
782
+ select throws_ok(pg_temp.pbmr('T', 'MISSING'), '22023', 'reply_to_message_id not found in thread',
783
+ 'RA-R10 message: the missing parent gets the single message');
784
+ select pg_temp.leave();
785
+ select is(pg_temp.st(), (select st from _snap where k = 'R10'), 'RA-R10 rows: the refusal left the scoped sinks and threads unchanged');
786
+ select is(pg_temp.changed('R10'), null, 'RA-R10 catalog: the refusal changed no agent_board table');
787
+
788
+ -- RA-R10b: post_board_message on the EMPTY thread TE2: refused AFTER _associate_empty_thread stamped it.
789
+ select pg_temp.snap('R10b');
790
+ select pg_temp.enter('A');
791
+ select throws_ok(pg_temp.pbmr('TE2', 'MISSING'), '22023', null,
792
+ 'RA-R10b: missing parent refused 22023 (post_board_message on the EMPTY thread TE2)');
793
+ select throws_ok(pg_temp.pbmr('TE2', 'MISSING'), '22023', 'reply_to_message_id not found in thread',
794
+ 'RA-R10b message: the missing parent gets the single message');
795
+ select pg_temp.leave();
796
+ select is(pg_temp.st(), (select st from _snap where k = 'R10b'), 'RA-R10b rows: the refusal left the scoped sinks and threads unchanged');
797
+ select is(pg_temp.changed('R10b'), null, 'RA-R10b catalog: the refusal changed no agent_board table');
798
+
799
+ -- RA-R11: post_board_message: a parent in another thread (M2 is in U).
800
+ select pg_temp.snap('R11');
801
+ select pg_temp.enter('A');
802
+ select throws_ok(pg_temp.pbmr('T', 'M2'), '22023', null,
803
+ 'RA-R11: other parent refused 22023 (post_board_message)');
804
+ select throws_ok(pg_temp.pbmr('T', 'M2'), '22023', 'reply_to_message_id not found in thread',
805
+ 'RA-R11 message: the other parent gets the single message');
806
+ select pg_temp.leave();
807
+ select is(pg_temp.st(), (select st from _snap where k = 'R11'), 'RA-R11 rows: the refusal left the scoped sinks and threads unchanged');
808
+ select is(pg_temp.changed('R11'), null, 'RA-R11 catalog: the refusal changed no agent_board table');
809
+
810
+ -- RA-R12: post_board_message: C replies to D1, which C cannot read.
811
+ select pg_temp.snap('R12');
812
+ select pg_temp.enter('C');
813
+ select throws_ok(pg_temp.pbmr('T', 'D1'), '22023', null,
814
+ 'RA-R12: unreadable parent refused 22023 (post_board_message)');
815
+ select throws_ok(pg_temp.pbmr('T', 'D1'), '22023', 'reply_to_message_id not found in thread',
816
+ 'RA-R12 message: the unreadable parent gets the single message');
817
+ select pg_temp.leave();
818
+ select is(pg_temp.st(), (select st from _snap where k = 'R12'), 'RA-R12 rows: the refusal left the scoped sinks and threads unchanged');
819
+ select is(pg_temp.changed('R12'), null, 'RA-R12 catalog: the refusal changed no agent_board table');
820
+
821
+ -- RA-R13: post_board_message: the delegate H replies to D1, which H cannot read.
822
+ select pg_temp.snap('R13');
823
+ select pg_temp.enter_d('H');
824
+ select throws_ok(pg_temp.pbmr('T', 'D1'), '22023', null,
825
+ 'RA-R13: unreadable parent refused 22023 (post_board_message)');
826
+ select throws_ok(pg_temp.pbmr('T', 'D1'), '22023', 'reply_to_message_id not found in thread',
827
+ 'RA-R13 message: the unreadable parent gets the single message');
828
+ select pg_temp.leave();
829
+ select is(pg_temp.st(), (select st from _snap where k = 'R13'), 'RA-R13 rows: the refusal left the scoped sinks and threads unchanged');
830
+ select is(pg_temp.changed('R13'), null, 'RA-R13 catalog: the refusal changed no agent_board table');
831
+
832
+ -- RA-R14: signed_post_board_message: service_role acting for C replies to D1, which C cannot read.
833
+ select pg_temp.snap('R14');
834
+ select pg_temp.enter_svc();
835
+ select throws_ok(pg_temp.spbmr('C', 'D1'), '22023', null,
836
+ 'RA-R14: unreadable parent refused 22023 (signed_post_board_message)');
837
+ select throws_ok(pg_temp.spbmr('C', 'D1'), '22023', 'reply_to_message_id not found in thread',
838
+ 'RA-R14 message: the unreadable parent gets the single message');
839
+ select pg_temp.leave();
840
+ select is(pg_temp.st(), (select st from _snap where k = 'R14'), 'RA-R14 rows: the refusal left the scoped sinks and threads unchanged');
841
+ select is(pg_temp.changed('R14'), null, 'RA-R14 catalog: the refusal changed no agent_board table');
842
+
843
+ -- RA-PM1: post_message: a parent that does not exist.
844
+ select pg_temp.snap('PM1');
845
+ select pg_temp.enter('A');
846
+ select throws_ok(pg_temp.pmr('MISSING'), '22023', null,
847
+ 'RA-PM1: missing parent refused 22023 (post_message)');
848
+ select throws_ok(pg_temp.pmr('MISSING'), '22023', 'reply_to_message_id not found in thread',
849
+ 'RA-PM1 message: the missing parent gets the single message');
850
+ select pg_temp.leave();
851
+ select is(pg_temp.st(), (select st from _snap where k = 'PM1'), 'RA-PM1 rows: the refusal left the scoped sinks and threads unchanged');
852
+ select is(pg_temp.changed('PM1'), null, 'RA-PM1 catalog: the refusal changed no agent_board table');
853
+
854
+ -- RA-PM2: post_message: a parent in another thread.
855
+ select pg_temp.snap('PM2');
856
+ select pg_temp.enter('A');
857
+ select throws_ok(pg_temp.pmr('M2'), '22023', null,
858
+ 'RA-PM2: other parent refused 22023 (post_message)');
859
+ select throws_ok(pg_temp.pmr('M2'), '22023', 'reply_to_message_id not found in thread',
860
+ 'RA-PM2 message: the other parent gets the single message');
861
+ select pg_temp.leave();
862
+ select is(pg_temp.st(), (select st from _snap where k = 'PM2'), 'RA-PM2 rows: the refusal left the scoped sinks and threads unchanged');
863
+ select is(pg_temp.changed('PM2'), null, 'RA-PM2 catalog: the refusal changed no agent_board table');
864
+
865
+ -- RA-PM3: post_message: C replies to D1.
866
+ select pg_temp.snap('PM3');
867
+ select pg_temp.enter('C');
868
+ select throws_ok(pg_temp.pmr('D1'), '22023', null,
869
+ 'RA-PM3: unreadable parent refused 22023 (post_message)');
870
+ select throws_ok(pg_temp.pmr('D1'), '22023', 'reply_to_message_id not found in thread',
871
+ 'RA-PM3 message: the unreadable parent gets the single message');
872
+ select pg_temp.leave();
873
+ select is(pg_temp.st(), (select st from _snap where k = 'PM3'), 'RA-PM3 rows: the refusal left the scoped sinks and threads unchanged');
874
+ select is(pg_temp.changed('PM3'), null, 'RA-PM3 catalog: the refusal changed no agent_board table');
875
+
876
+ -- RA-DM1: send_direct_message: a parent that does not exist.
877
+ select pg_temp.snap('DM1');
878
+ select pg_temp.enter('A');
879
+ select throws_ok(pg_temp.dmr('B', 'MISSING'), '22023', null,
880
+ 'RA-DM1: missing parent refused 22023 (send_direct_message)');
881
+ select throws_ok(pg_temp.dmr('B', 'MISSING'), '22023', 'reply_to_message_id not found in thread',
882
+ 'RA-DM1 message: the missing parent gets the single message');
883
+ select pg_temp.leave();
884
+ select is(pg_temp.st(), (select st from _snap where k = 'DM1'), 'RA-DM1 rows: the refusal left the scoped sinks and threads unchanged');
885
+ select is(pg_temp.changed('DM1'), null, 'RA-DM1 catalog: the refusal changed no agent_board table');
886
+
887
+ -- RA-DM2: send_direct_message: a parent in another thread.
888
+ select pg_temp.snap('DM2');
889
+ select pg_temp.enter('A');
890
+ select throws_ok(pg_temp.dmr('B', 'M2'), '22023', null,
891
+ 'RA-DM2: other parent refused 22023 (send_direct_message)');
892
+ select throws_ok(pg_temp.dmr('B', 'M2'), '22023', 'reply_to_message_id not found in thread',
893
+ 'RA-DM2 message: the other parent gets the single message');
894
+ select pg_temp.leave();
895
+ select is(pg_temp.st(), (select st from _snap where k = 'DM2'), 'RA-DM2 rows: the refusal left the scoped sinks and threads unchanged');
896
+ select is(pg_temp.changed('DM2'), null, 'RA-DM2 catalog: the refusal changed no agent_board table');
897
+
898
+ -- RA-DM3: send_direct_message: C replies privately to D1.
899
+ select pg_temp.snap('DM3');
900
+ select pg_temp.enter('C');
901
+ select throws_ok(pg_temp.dmr('A', 'D1'), '22023', null,
902
+ 'RA-DM3: unreadable parent refused 22023 (send_direct_message)');
903
+ select throws_ok(pg_temp.dmr('A', 'D1'), '22023', 'reply_to_message_id not found in thread',
904
+ 'RA-DM3 message: the unreadable parent gets the single message');
905
+ select pg_temp.leave();
906
+ select is(pg_temp.st(), (select st from _snap where k = 'DM3'), 'RA-DM3 rows: the refusal left the scoped sinks and threads unchanged');
907
+ select is(pg_temp.changed('DM3'), null, 'RA-DM3 catalog: the refusal changed no agent_board table');
908
+
909
+ -- RA-MC1: send_multicast_message: a parent that does not exist.
910
+ select pg_temp.snap('MC1');
911
+ select pg_temp.enter('A');
912
+ select throws_ok(pg_temp.mcr('B', 'MISSING'), '22023', null,
913
+ 'RA-MC1: missing parent refused 22023 (send_multicast_message)');
914
+ select throws_ok(pg_temp.mcr('B', 'MISSING'), '22023', 'reply_to_message_id not found in thread',
915
+ 'RA-MC1 message: the missing parent gets the single message');
916
+ select pg_temp.leave();
917
+ select is(pg_temp.st(), (select st from _snap where k = 'MC1'), 'RA-MC1 rows: the refusal left the scoped sinks and threads unchanged');
918
+ select is(pg_temp.changed('MC1'), null, 'RA-MC1 catalog: the refusal changed no agent_board table');
919
+
920
+ -- RA-MC2: send_multicast_message: a parent in another thread.
921
+ select pg_temp.snap('MC2');
922
+ select pg_temp.enter('A');
923
+ select throws_ok(pg_temp.mcr('B', 'M2'), '22023', null,
924
+ 'RA-MC2: other parent refused 22023 (send_multicast_message)');
925
+ select throws_ok(pg_temp.mcr('B', 'M2'), '22023', 'reply_to_message_id not found in thread',
926
+ 'RA-MC2 message: the other parent gets the single message');
927
+ select pg_temp.leave();
928
+ select is(pg_temp.st(), (select st from _snap where k = 'MC2'), 'RA-MC2 rows: the refusal left the scoped sinks and threads unchanged');
929
+ select is(pg_temp.changed('MC2'), null, 'RA-MC2 catalog: the refusal changed no agent_board table');
930
+
931
+ -- RA-MC3: send_multicast_message: C replies by multicast to D1.
932
+ select pg_temp.snap('MC3');
933
+ select pg_temp.enter('C');
934
+ select throws_ok(pg_temp.mcr('A', 'D1'), '22023', null,
935
+ 'RA-MC3: unreadable parent refused 22023 (send_multicast_message)');
936
+ select throws_ok(pg_temp.mcr('A', 'D1'), '22023', 'reply_to_message_id not found in thread',
937
+ 'RA-MC3 message: the unreadable parent gets the single message');
938
+ select pg_temp.leave();
939
+ select is(pg_temp.st(), (select st from _snap where k = 'MC3'), 'RA-MC3 rows: the refusal left the scoped sinks and threads unchanged');
940
+ select is(pg_temp.changed('MC3'), null, 'RA-MC3 catalog: the refusal changed no agent_board table');
941
+
942
+ -- RT-R-ORACLE: for each writer the missing, other-thread and unreadable cases give ONE distinct,
943
+ -- byte-identical error, so the refusal answers nothing about which case it was.
944
+ select is((select string_agg(distinct e, ' || ') from unnest(array[
945
+ pg_temp.err('A', pg_temp.pbmr('T', 'MISSING')), pg_temp.err('A', pg_temp.pbmr('T', 'M2')), pg_temp.err('C', pg_temp.pbmr('T', 'D1'))]) e),
946
+ '22023:reply_to_message_id not found in thread', 'RT-R-ORACLE post_board_message: the three cases give one identical error');
947
+ select is((select string_agg(distinct e, ' || ') from unnest(array[
948
+ pg_temp.err('A', pg_temp.pmr('MISSING')), pg_temp.err('A', pg_temp.pmr('M2')), pg_temp.err('C', pg_temp.pmr('D1'))]) e),
949
+ '22023:reply_to_message_id not found in thread', 'RT-R-ORACLE post_message: the three cases give one identical error');
950
+ select is((select string_agg(distinct e, ' || ') from unnest(array[
951
+ pg_temp.err('A', pg_temp.dmr('B', 'MISSING')), pg_temp.err('A', pg_temp.dmr('B', 'M2')), pg_temp.err('C', pg_temp.dmr('A', 'D1'))]) e),
952
+ '22023:reply_to_message_id not found in thread', 'RT-R-ORACLE send_direct_message: the three cases give one identical error');
953
+ select is((select string_agg(distinct e, ' || ') from unnest(array[
954
+ pg_temp.err('A', pg_temp.mcr('B', 'MISSING')), pg_temp.err('A', pg_temp.mcr('B', 'M2')), pg_temp.err('C', pg_temp.mcr('A', 'D1'))]) e),
955
+ '22023:reply_to_message_id not found in thread', 'RT-R-ORACLE send_multicast_message: the three cases give one identical error');
956
+
957
+ select * from finish();
958
+ rollback;