@consiliency/agent-board-schema 1.79.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.
- package/fixtures/already-applied.json +8 -0
- package/migrations/20261126000100_agent_board_membership_removal_requires_active.sql +243 -0
- package/migrations/20261201000100_agent_board_reply_to_message.sql +1241 -0
- package/package.json +1 -1
- package/schema-manifest.json +21 -3
- package/src/tooling.test.mjs +6 -2
- package/tests/agent_board_661_reply_delivery.test.sql +12 -12
- package/tests/agent_board_667_history_class.test.sql +31 -13
- package/tests/agent_board_717_message_replay_scope.test.sql +29 -16
- package/tests/agent_board_717_tasking_replay_scope.test.sql +3 -3
- package/tests/agent_board_bundle_endpoint_compat.test.sql +2 -2
- package/tests/agent_board_canary_signed_fleet_enforcement.test.sql +3 -3
- package/tests/agent_board_claim_subject_gate.test.sql +3 -1
- package/tests/agent_board_delegate_role_class.test.sql +1 -1
- package/tests/agent_board_delivery_writer_execute.test.sql +7 -7
- package/tests/agent_board_hosted_delegates.test.sql +3 -3
- package/tests/agent_board_inbox_rpcs.test.sql +6 -6
- package/tests/agent_board_membership_removal_state.test.sql +228 -0
- package/tests/agent_board_refusal_atomicity.test.sql +958 -0
- package/tests/agent_board_reply_to.test.sql +874 -0
- package/tests/agent_board_scoped_send_regrant.test.sql +8 -8
- package/tests/agent_board_send_outcome.test.sql +10 -10
- package/tests/agent_board_send_role_gate.test.sql +2 -2
- package/tests/agent_board_write_enforcement_class.test.sql +7 -7
- package/tests/catalog/function_execute_privileges.json +23 -14
- package/tests/catalog/p_metadata_parameters.json +12 -8
- package/tests/isolation/expected/reply_parent_vs_thread_delete.out +55 -0
- package/tests/isolation/expected/reply_vs_parent_author_delete.out +56 -0
- package/tests/isolation/specs/reply_parent_vs_thread_delete.spec +42 -0
- package/tests/isolation/specs/reply_vs_parent_author_delete.spec +46 -0
- package/tests/mutations/655/M01_no_writer_check.sql +22 -0
- package/tests/mutations/655/M02_drop_fk.sql +2 -0
- package/tests/mutations/655/M03_existence_only.sql +13 -0
- package/tests/mutations/655/M04_distinct_cross_thread_error.sql +17 -0
- package/tests/mutations/655/M05_view_no_thread_conjunct.sql +46 -0
- package/tests/mutations/655/M06_thread_feed_not_invoker.sql +46 -0
- package/tests/mutations/655/M06b_board_feed_not_invoker.sql +49 -0
- package/tests/mutations/655/M07_excerpt_via_definer.sql +49 -0
- package/tests/mutations/655/M08_excerpt_200.sql +46 -0
- package/tests/mutations/655/M09_fk_restrict.sql +4 -0
- package/tests/mutations/655/M10_post_message_drops_arg.sql +13 -0
- package/tests/mutations/655/M10b_post_message_passes_null.sql +12 -0
- package/tests/mutations/655/M11_slug_drops_arg.sql +15 -0
- package/tests/mutations/655/M11b_dm_descriptor_positional.sql +31 -0
- package/tests/mutations/655/M12_signed_pbm_nine_args.sql +19 -0
- package/tests/mutations/655/M12b_signed_mc_one_branch.sql +15 -0
- package/tests/mutations/655/M13_undriven_entrypoint.sql +8 -0
- package/tests/mutations/655/M14_second_writer.sql +6 -0
- package/tests/mutations/655/M15_insert_primitive_public.sql +2 -0
- package/tests/mutations/655/M16_comment_missing.sql +2 -0
- package/tests/mutations/655/M17_old_allow_set_rows.sql +5 -0
- package/tests/mutations/655/M18_board_excerpt_139.sql +49 -0
- package/tests/mutations/655/M19_column_in_middle.sql +47 -0
- package/tests/mutations/655/M20_projects_parent_id.sql +46 -0
- package/tests/mutations/655/M21_replay_applies_new_parent.sql +22 -0
- package/tests/mutations/655/M22_fk_not_valid.sql +4 -0
- package/tests/mutations/655/MB4a_no_readability.sql +13 -0
- package/tests/mutations/655/MB4b_own_readability_message.sql +20 -0
- package/tests/mutations/655/MB4c_service_role_reads_all.sql +12 -0
- package/tests/mutations/655/MB4d_delegate_uses_authenticated_arm.sql +12 -0
- package/tests/mutations/655/MB4f_no_board_member_branch.sql +14 -0
- package/tests/mutations/655/MB4g_helper_granted_authenticated.sql +2 -0
- package/tests/mutations/655/MB4j_writer_uses_current_agent.sql +12 -0
- package/tests/mutations/655/MB4k_delegate_policy_loses_initplan.sql +5 -0
- package/tests/mutations/655/MB4l_board_memberships_policy_narrowed.sql +3 -0
- package/tests/mutations/655/W1_auth_no_membership_state.sql +13 -0
- package/tests/mutations/655/W2_auth_no_agent_binding.sql +13 -0
- package/tests/mutations/655/W3_delivery_no_recipient_binding.sql +12 -0
- package/tests/mutations/655/W4_delegate_no_board_conjunct.sql +18 -0
- package/tests/mutations/655/W5_boardset_no_membership_state.sql +13 -0
- package/tests/mutations/655/W6_boardset_no_agent_binding.sql +14 -0
- package/tests/mutations/655/run.sh +93 -0
- package/tests/mutations/670/M10_lock_removed.sql +2 -2
- package/tests/mutations/670/M14_post_message_mints_from_metadata.sql +2 -2
- package/tests/mutations/670/M3-DM_helper_on_replay_branch.sql +2 -2
- package/tests/mutations/670/M3-MC_helper_on_replay_branch.sql +2 -2
- package/tests/mutations/670/M3_helper_on_replay_branch.sql +2 -2
- package/tests/mutations/670/M8_delivery_swallows_errors.sql +2 -2
- package/tests/mutations/670/M9_lock_key_includes_board.sql +2 -2
- package/tests/mutations/670/run.sh +1 -1
- package/tests/mutations/717-f1/M10_wrapper_stops_delegating.sql +1 -1
- package/tests/mutations/717-f1/M9_public_route_revoked_from_service_role.sql +1 -1
- package/tests/mutations/717-f2b/L1_dm_lock_split_by_recipient.sql +2 -2
- package/tests/mutations/717-f2b/L2_dm_lock_removed.sql +2 -2
- package/tests/mutations/717-f2b/L3_mc_lock_split_by_recipients.sql +2 -2
- package/tests/mutations/717-f2b/L4_mc_lock_removed.sql +2 -2
- package/tests/mutations/717-f2b/M0_prefix_definitions.sql +1 -1
- package/tests/mutations/717-f2b/M10_mc_lookup_any_author.sql +2 -2
- package/tests/mutations/717-f2b/M11_dm_swallow_refusal.sql +2 -2
- package/tests/mutations/717-f2b/M12_mc_swallow_refusal.sql +2 -2
- package/tests/mutations/717-f2b/M15_mc_any_source.sql +1 -1
- package/tests/mutations/717-f2b/M1_dm_drop_recipient_proof.sql +2 -2
- package/tests/mutations/717-f2b/M2_mc_drop_recipient_proof.sql +2 -2
- package/tests/mutations/717-f2b/M3_dm_metadata_as_proof.sql +2 -2
- package/tests/mutations/717-f2b/M4_mc_metadata_as_proof.sql +2 -2
- package/tests/mutations/717-f2b/M5_dm_any_mode.sql +2 -2
- package/tests/mutations/717-f2b/M6_dm_board_delivery_counts.sql +2 -2
- package/tests/mutations/717-f2b/M7_mc_any_mode.sql +2 -2
- package/tests/mutations/717-f2b/M8_dm_thread_stamp_as_proof.sql +2 -2
- package/tests/mutations/717-f2b/M9_dm_lookup_any_author.sql +2 -2
- package/tests/mutations/717-f2b/run.sh +2 -2
- package/tests/mutations/919/M10_quoted_net_http_post_scratch.sql +5 -0
- package/tests/mutations/919/M11_pbm_calls_public.sql +11 -0
- package/tests/mutations/919/M1_pbm_refusal_swallowed_after_write.sql +14 -0
- package/tests/mutations/919/M2_cof_refusal_swallowed_after_write.sql +13 -0
- package/tests/mutations/919/M3_pbm_receipt_fault_swallowed.sql +10 -0
- package/tests/mutations/919/M4_pg_notify_scratch.sql +5 -0
- package/tests/mutations/919/M5_dynamic_notify_scratch.sql +5 -0
- package/tests/mutations/919/M6_trigger_outside_schema.sql +3 -0
- package/tests/mutations/919/M7_pbm_step_up_gate.sql +10 -0
- package/tests/mutations/919/M8_pbm_positive_writes_nothing.sql +10 -0
- package/tests/mutations/919/M9_cof_positive_writes_nothing.sql +10 -0
- package/tests/mutations/919/run.sh +81 -0
|
@@ -0,0 +1,874 @@
|
|
|
1
|
+
-- REPLYTO pgTAP (Consiliency/message-board#655; plan plans/REPLYTO-655-plan.md, revision R3).
|
|
2
|
+
--
|
|
3
|
+
-- THE PROPERTY. A message may name the message it replies to (messages.reply_to_message_id). The
|
|
4
|
+
-- parent is always in the same thread (a table invariant: the composite FK), every API message
|
|
5
|
+
-- entrypoint accepts the argument, the writer refuses a parent its session-derived actor cannot
|
|
6
|
+
-- read with the ONE error every unusable parent gets, and the feed views show the parent's excerpt
|
|
7
|
+
-- only to a reader whose own RLS admits the parent.
|
|
8
|
+
--
|
|
9
|
+
-- WHAT THIS FILE PINS (plan section in brackets):
|
|
10
|
+
-- RT-CLASS-2..4 [§8.2] the FK and unique key from pg_constraint; every census entrypoint takes
|
|
11
|
+
-- the argument and is driven here; every direct caller passes it BY NAME.
|
|
12
|
+
-- (RT-CLASS-1 and the transitive census are the re-keyed 661 rows.)
|
|
13
|
+
-- A-1..A-10, RT-DEL-1 [§8.3] allow rows on all ten entrypoints, D8 replay, chains, a delegate.
|
|
14
|
+
-- RT-INV-1 [§8.4] a raw owner INSERT of a cross-thread reply is refused by the FK (23503).
|
|
15
|
+
-- RT-V-0..7 [§8.5] the views: dependency set, security_invoker, the leak row, defence in
|
|
16
|
+
-- depth, the 140-character excerpt, view agreement, exact column lists.
|
|
17
|
+
-- RT-B4-1/2/3, RT-B4-EQ, RT-VIS, RA-NULL [§8.6] the policies frozen as they are today; the
|
|
18
|
+
-- writer copy agrees with policy and oracle on every fixture pair; it is
|
|
19
|
+
-- callable by no API role; NULL readability fails closed.
|
|
20
|
+
-- RT-ACL-1..4 [§8.8] grants, PUBLIC, comments and the delegate allow set.
|
|
21
|
+
-- The single-error refusal rows with zero-row proof (RA-R10..R14, RA-PM/DM/MC, RT-R-ORACLE) are
|
|
22
|
+
-- in agent_board_refusal_atomicity.test.sql, which owns that machinery (§8.4).
|
|
23
|
+
--
|
|
24
|
+
-- METHOD. Every call runs under its caller's own role AND JWT claims (pg_temp.as_ / val, the #717
|
|
25
|
+
-- F2b form). pg_temp.as_ traps the call and returns 'ok' or '<sqlstate>:<message>', so an allow row
|
|
26
|
+
-- `is(pg_temp.as_(...), 'ok')` is the lives_ok form that also runs under the caller's role and can
|
|
27
|
+
-- never abort the file; its state is then read in a SEPARATE assertion as the owner (#113). Every
|
|
28
|
+
-- in-file mutation (RA-NULL, RT-V-4) runs inside a plpgsql subtransaction that is always rolled
|
|
29
|
+
-- back, so pgTAP's own bookkeeping is never inside a rolled-back savepoint.
|
|
30
|
+
--
|
|
31
|
+
-- DEVIATIONS FROM THE PLAN TEXT, stated so a reader can check them:
|
|
32
|
+
-- * RT-V-3's second half ("a parent H can see whose author is not _delegate_visible_agent") is
|
|
33
|
+
-- unreachable by construction: _delegate_visible_agent admits the author of every message
|
|
34
|
+
-- delivered on a board the delegate is active on, which is exactly the delegate's messages
|
|
35
|
+
-- policy. RT-V-3 pins instead that H loses no row (its view row count equals its readable
|
|
36
|
+
-- message count) and that the author join is a LEFT join (RT-V-7 / viewdef).
|
|
37
|
+
-- * RT-B4-1 pins the WHOLE messages policy census (the two SELECT policies plus the service_role
|
|
38
|
+
-- ALL policy, none restrictive), because "the only permissive SELECT policies" is false as
|
|
39
|
+
-- worded: agent_board_service_all_messages (cmd *) also applies to SELECT for service_role.
|
|
40
|
+
--
|
|
41
|
+
-- HAND-COUNTED plan:
|
|
42
|
+
-- RT-CLASS-2, 2b, 3a, 3b, 4a, 4b (6)
|
|
43
|
+
-- A-1..A-5b allow+state (7 x 2 = 14), A-6 (signed board 2, direct 2, multicast ids 1 + descriptors 1
|
|
44
|
+
-- + state 1, no reply key 2 = 9), A-6b (2), A-7 (2), A-8 (3), A-9 (2 x 2 = 4), A-10 (2),
|
|
45
|
+
-- A-11 (2), RT-DEL-1 (2) = 40
|
|
46
|
+
-- RT-INV-1 (1)
|
|
47
|
+
-- RT-V-0, 0b, 1, 2 (C thread, C board, B), 3 (2), 4, 5 (3), 6 (2), 7 (2) = 16
|
|
48
|
+
-- RT-B4-1 (3 + 5 sub-read rows), RT-B4-2 (1), RT-B4-EQ (2), RT-VIS (10), RT-B4-D (1), RA-NULL (1), RT-B4-3 (1) = 24
|
|
49
|
+
-- RT-ACL-1..4 (4)
|
|
50
|
+
-- = 6 + 40 + 1 + 16 + 24 + 4 = 91.
|
|
51
|
+
create extension if not exists pgtap with schema extensions;
|
|
52
|
+
set search_path = extensions, public, agent_board;
|
|
53
|
+
|
|
54
|
+
begin;
|
|
55
|
+
|
|
56
|
+
select plan(91);
|
|
57
|
+
|
|
58
|
+
do $$
|
|
59
|
+
begin
|
|
60
|
+
if not (select rolsuper from pg_roles where rolname = current_user) then
|
|
61
|
+
execute format('grant agent_board_delegate to %I with set true', current_user);
|
|
62
|
+
end if;
|
|
63
|
+
end $$;
|
|
64
|
+
|
|
65
|
+
-- ===========================================================================
|
|
66
|
+
-- Helpers
|
|
67
|
+
-- ===========================================================================
|
|
68
|
+
create temp table _h(k text primary key, v text);
|
|
69
|
+
create function pg_temp.v(p_k text) returns text language sql as $$ select v from _h where k = p_k $$;
|
|
70
|
+
-- Never raises: a non-uuid value (an 'ERR ...' under a mutation) reads as NULL.
|
|
71
|
+
create function pg_temp.u(p_k text) returns uuid language sql as $$
|
|
72
|
+
select case when v ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' then v::uuid end from _h where k = p_k $$;
|
|
73
|
+
create function pg_temp.put(p_k text, p_v text) returns void language sql as $$
|
|
74
|
+
insert into _h values (p_k, p_v) on conflict (k) do update set v = excluded.v $$;
|
|
75
|
+
|
|
76
|
+
-- One JSON claim set, every flattened claim GUC cleared. A delegate carries its credential claim.
|
|
77
|
+
create function pg_temp.ident(p_role text, p_sub uuid, p_cred text) returns void language plpgsql as $$
|
|
78
|
+
begin
|
|
79
|
+
perform set_config('request.jwt.claim.sub', '', true);
|
|
80
|
+
perform set_config('request.jwt.claim.role', '', true);
|
|
81
|
+
perform set_config('request.jwt.claim.agent_credential_id', '', true);
|
|
82
|
+
perform set_config('request.jwt.claim.identity_lease_epoch', '', true);
|
|
83
|
+
perform set_config('request.jwt.claims',
|
|
84
|
+
case when p_role is null then ''
|
|
85
|
+
else jsonb_strip_nulls(jsonb_build_object('role', p_role, 'sub', p_sub, 'agent_credential_id', p_cred))::text end,
|
|
86
|
+
true);
|
|
87
|
+
end $$;
|
|
88
|
+
|
|
89
|
+
-- Run p_sql AS actor p_who (its role and claims; an actor with no role, e.g. OWNER, runs as the table
|
|
90
|
+
-- owner with no claims). Returns 'ok' or '<sqlstate>:<message>'.
|
|
91
|
+
create function pg_temp.as_(p_who text, p_sql text) returns text language plpgsql as $$
|
|
92
|
+
declare
|
|
93
|
+
v text;
|
|
94
|
+
v_role text := pg_temp.v(p_who || '.role');
|
|
95
|
+
begin
|
|
96
|
+
perform pg_temp.ident(v_role, pg_temp.u(p_who || '.auth'), pg_temp.v(p_who || '.cred'));
|
|
97
|
+
if v_role is not null then execute format('set local role %I', v_role); end if; -- no role: the owner
|
|
98
|
+
begin
|
|
99
|
+
execute p_sql;
|
|
100
|
+
v := 'ok';
|
|
101
|
+
exception when others then
|
|
102
|
+
v := sqlstate || ':' || sqlerrm;
|
|
103
|
+
end;
|
|
104
|
+
reset role;
|
|
105
|
+
perform pg_temp.ident(null, null, null);
|
|
106
|
+
return v;
|
|
107
|
+
end $$;
|
|
108
|
+
|
|
109
|
+
-- Run p_sql AS actor p_who and return its single scalar ('(null)' for NULL, 'ERR ...' on error).
|
|
110
|
+
create function pg_temp.val(p_who text, p_sql text) returns text language plpgsql as $$
|
|
111
|
+
declare
|
|
112
|
+
v text;
|
|
113
|
+
v_role text := pg_temp.v(p_who || '.role');
|
|
114
|
+
begin
|
|
115
|
+
perform pg_temp.ident(v_role, pg_temp.u(p_who || '.auth'), pg_temp.v(p_who || '.cred'));
|
|
116
|
+
if v_role is not null then execute format('set local role %I', v_role); end if; -- no role: the owner
|
|
117
|
+
begin
|
|
118
|
+
execute p_sql into v;
|
|
119
|
+
exception when others then
|
|
120
|
+
v := 'ERR ' || sqlstate || ':' || sqlerrm;
|
|
121
|
+
end;
|
|
122
|
+
reset role;
|
|
123
|
+
perform pg_temp.ident(null, null, null);
|
|
124
|
+
return coalesce(v, '(null)');
|
|
125
|
+
end $$;
|
|
126
|
+
|
|
127
|
+
-- The call builders. p_reply NULL omits the argument entirely (the reply-less call shape).
|
|
128
|
+
create function pg_temp.rarg(p_reply text) returns text language sql as $$
|
|
129
|
+
select case when p_reply is null then '' else format(', p_reply_to_message_id => %L::uuid', pg_temp.v(p_reply)) end $$;
|
|
130
|
+
create function pg_temp.pm(p_thread text, p_body text, p_reply text default null) returns text language sql as $$
|
|
131
|
+
select format('select * from agent_board.post_message(%L::uuid, null, %L%s)', pg_temp.v(p_thread), p_body, pg_temp.rarg(p_reply)) $$;
|
|
132
|
+
create function pg_temp.pbm(p_thread text, p_board text, p_body text, p_reply text default null, p_key text default null) returns text language sql as $$
|
|
133
|
+
select format('select * from agent_board.post_board_message(%L::uuid, null, %L::uuid, ''subj'', %L, ''comment'', ''medium'', %L, ''{}''::jsonb%s)',
|
|
134
|
+
pg_temp.v(p_thread), pg_temp.v(p_board), p_body, p_key, pg_temp.rarg(p_reply)) $$;
|
|
135
|
+
create function pg_temp.pbms(p_thread text, p_slug text, p_body text, p_reply text default null) returns text language sql as $$
|
|
136
|
+
select format('select * from agent_board.post_board_message(%L::uuid, null, %L, ''addressing_v1'', ''subj'', %L, ''comment'', ''medium'', null, ''{}''::jsonb%s)',
|
|
137
|
+
pg_temp.v(p_thread), p_slug, p_body, pg_temp.rarg(p_reply)) $$;
|
|
138
|
+
create function pg_temp.dm(p_thread text, p_to text, p_body text, p_reply text default null, p_key text default null) returns text language sql as $$
|
|
139
|
+
select format('select * from agent_board.send_direct_message(%L::uuid, null, %L::uuid, ''subj'', %L, ''comment'', ''medium'', %L, ''{}''::jsonb%s)',
|
|
140
|
+
pg_temp.v(p_thread), pg_temp.v(p_to), p_body, p_key, pg_temp.rarg(p_reply)) $$;
|
|
141
|
+
create function pg_temp.dmd(p_thread text, p_to text, p_body text, p_reply text default null) returns text language sql as $$
|
|
142
|
+
select format('select * from agent_board.send_direct_message(%L::uuid, null, %L::jsonb, ''addressing_v1'', ''subj'', %L, ''comment'', ''medium'', null, ''{}''::jsonb%s)',
|
|
143
|
+
pg_temp.v(p_thread), jsonb_build_object('kind', 'agent_id', 'agentId', pg_temp.v(p_to))::text, p_body, pg_temp.rarg(p_reply)) $$;
|
|
144
|
+
create function pg_temp.mc(p_thread text, p_to text, p_body text, p_reply text default null, p_key text default null) returns text language sql as $$
|
|
145
|
+
select format('select * from agent_board.send_multicast_message(%L::uuid, null, array[%L]::uuid[], ''subj'', %L, ''comment'', ''medium'', %L, ''{}''::jsonb%s)',
|
|
146
|
+
pg_temp.v(p_thread), pg_temp.v(p_to), p_body, p_key, pg_temp.rarg(p_reply)) $$;
|
|
147
|
+
create function pg_temp.mcd(p_thread text, p_to text, p_body text, p_reply text default null) returns text language sql as $$
|
|
148
|
+
select format('select * from agent_board.send_multicast_message(%L::uuid, null, %L::jsonb, ''addressing_v1'', ''subj'', %L, ''comment'', ''medium'', null, ''{}''::jsonb%s)',
|
|
149
|
+
pg_temp.v(p_thread), jsonb_build_array(jsonb_build_object('kind', 'agent_id', 'agentId', pg_temp.v(p_to)))::text, p_body, pg_temp.rarg(p_reply)) $$;
|
|
150
|
+
-- Signed wrappers, as service_role with a gateway decision for p_actor (a SQL fixture: this proves
|
|
151
|
+
-- the wrappers' routing, not signature verification).
|
|
152
|
+
create function pg_temp.signed(p_fn text, p_payload jsonb, p_actor text) returns text language sql as $$
|
|
153
|
+
select format('select * from agent_board.%I(%L::jsonb, %L::jsonb)', p_fn, p_payload::text,
|
|
154
|
+
jsonb_build_object('agent_id', pg_temp.v(p_actor), 'verification_status', 'warn',
|
|
155
|
+
'payload_digest', 'sha256:' || repeat('7', 64), 'enforcement_mode', 'warn')::text) $$;
|
|
156
|
+
-- The message a body names (bodies are unique in this file).
|
|
157
|
+
create function pg_temp.msg(p_body text) returns uuid language sql as $$
|
|
158
|
+
select id from agent_board.messages where body = p_body $$;
|
|
159
|
+
create function pg_temp.keyrows(p_key text) returns bigint language sql volatile as $$
|
|
160
|
+
select count(*) from agent_board.messages where metadata ->> 'idempotencyKey' = p_key $$;
|
|
161
|
+
create function pg_temp.reply_of(p_body text) returns text language sql as $$
|
|
162
|
+
select coalesce((select coalesce(k.k, m.reply_to_message_id::text, 'NULL')
|
|
163
|
+
from agent_board.messages m
|
|
164
|
+
left join _h k on k.v = m.reply_to_message_id::text and k.k ~ '^[A-Z0-9]+$'
|
|
165
|
+
where m.body = p_body), 'NO ROW') $$;
|
|
166
|
+
|
|
167
|
+
-- The census of entrypoints this file drives (RT-CLASS-3), by oid::regprocedure::text.
|
|
168
|
+
create temp table _driven(sig text primary key);
|
|
169
|
+
|
|
170
|
+
-- ===========================================================================
|
|
171
|
+
-- Fixtures (§8.1)
|
|
172
|
+
-- ===========================================================================
|
|
173
|
+
-- Keyed agents: A, B, C joined on X; I invited, R requested, L left on X; Q on no board; Y1 joined
|
|
174
|
+
-- on Y; N joins X only AFTER the fixture messages exist, so it holds no delivery row of its own and
|
|
175
|
+
-- reads board posts through the policy's board-member branch alone (M-B4f). O is the hosted delegate H's principal (joined on X). H is placed on X by O and holds a
|
|
176
|
+
-- NOT-joined row on Y.
|
|
177
|
+
insert into auth.users(id, instance_id, aud, role, email)
|
|
178
|
+
select ('65500000-0000-4000-8000-' || lpad(n::text, 12, '0'))::uuid,
|
|
179
|
+
'00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'rt655-' || n || '@t.test'
|
|
180
|
+
from generate_series(1, 9) n;
|
|
181
|
+
insert into agent_board.agents(id, agent_type, kind, handle, display_name, status, auth_user_id)
|
|
182
|
+
select ('65510000-0000-4000-8000-' || lpad(n::text, 12, '0'))::uuid, 'agent', 'agent',
|
|
183
|
+
'rt655-' || lower(h), 'RT655 ' || h, 'active', ('65500000-0000-4000-8000-' || lpad(n::text, 12, '0'))::uuid
|
|
184
|
+
from unnest(array['A', 'B', 'C', 'I', 'R', 'L', 'Q', 'Y1', 'N']) with ordinality as x(h, n);
|
|
185
|
+
do $$
|
|
186
|
+
declare h text; n int := 0;
|
|
187
|
+
begin
|
|
188
|
+
foreach h in array array['A', 'B', 'C', 'I', 'R', 'L', 'Q', 'Y1', 'N'] loop
|
|
189
|
+
n := n + 1;
|
|
190
|
+
perform pg_temp.put(h, '65510000-0000-4000-8000-' || lpad(n::text, 12, '0'));
|
|
191
|
+
perform pg_temp.put(h || '.auth', '65500000-0000-4000-8000-' || lpad(n::text, 12, '0'));
|
|
192
|
+
perform pg_temp.put(h || '.role', 'authenticated');
|
|
193
|
+
end loop;
|
|
194
|
+
end $$;
|
|
195
|
+
select pg_temp.put('SVC.role', 'service_role');
|
|
196
|
+
select pg_temp.put('MISSING', '65599999-0000-4000-8000-000000000001');
|
|
197
|
+
|
|
198
|
+
insert into agent_board.identity_providers
|
|
199
|
+
(issuer, audience, supported, policy_approved, allowed_scopes, allowed_board_scopes, allowed_agent_kinds)
|
|
200
|
+
values ('https://portal.rt655.test', 'message-board', true, true, '{}', '{}', '{agent,human}');
|
|
201
|
+
do $$
|
|
202
|
+
declare r record;
|
|
203
|
+
begin
|
|
204
|
+
perform set_config('request.jwt.claims', '', true);
|
|
205
|
+
select * into r from agent_board.enroll_federated_human('https://portal.rt655.test', 'message-board', 'rt655-owner',
|
|
206
|
+
'{"sub": "rt655-owner", "email": "rt655-owner@t.test", "email_verified": true}'::jsonb, 'RT655 Owner');
|
|
207
|
+
perform pg_temp.put('O', r.agent_id::text);
|
|
208
|
+
perform pg_temp.put('O.auth', r.auth_user_id::text);
|
|
209
|
+
perform pg_temp.put('O.role', 'authenticated');
|
|
210
|
+
select * into r from agent_board.exchange_delegate_credential(jsonb_build_object(
|
|
211
|
+
'iss', 'https://portal.rt655.test', 'sub', 'rt655-owner', 'slot_resource', null,
|
|
212
|
+
'agent_board_delegate_instance', jsonb_build_object('instance_id', 'rt655_instance_00001', 'grant_id', 'rt655_grant_000000001',
|
|
213
|
+
'generation', 1, 'owner_sub', 'rt655-owner', 'client_id', 'rt655-client', 'status', 'active', 'client_change', null)));
|
|
214
|
+
perform pg_temp.put('H', r.agent_id::text);
|
|
215
|
+
perform pg_temp.put('H.auth', r.auth_user_id::text);
|
|
216
|
+
perform pg_temp.put('H.cred', r.credential_id::text);
|
|
217
|
+
perform pg_temp.put('H.role', 'agent_board_delegate');
|
|
218
|
+
end $$;
|
|
219
|
+
|
|
220
|
+
insert into agent_board.boards(id, slug, name, created_by_agent_id, visibility, status) values
|
|
221
|
+
('65520000-0000-4000-8000-000000000001', 'rt655-x', 'RT655 X', pg_temp.u('A'), 'private', 'active'),
|
|
222
|
+
('65520000-0000-4000-8000-000000000002', 'rt655-y', 'RT655 Y', pg_temp.u('Y1'), 'private', 'active');
|
|
223
|
+
select pg_temp.put('X', '65520000-0000-4000-8000-000000000001'), pg_temp.put('Y', '65520000-0000-4000-8000-000000000002');
|
|
224
|
+
insert into agent_board.board_memberships(board_id, agent_id, role, membership_state, left_at, created_by_agent_id) values
|
|
225
|
+
(pg_temp.u('X'), pg_temp.u('A'), 'owner', 'joined', null, pg_temp.u('A')),
|
|
226
|
+
(pg_temp.u('X'), pg_temp.u('B'), 'member', 'joined', null, pg_temp.u('A')),
|
|
227
|
+
(pg_temp.u('X'), pg_temp.u('C'), 'member', 'joined', null, pg_temp.u('A')),
|
|
228
|
+
(pg_temp.u('X'), pg_temp.u('O'), 'member', 'joined', null, pg_temp.u('A')),
|
|
229
|
+
(pg_temp.u('X'), pg_temp.u('I'), 'member', 'invited', null, pg_temp.u('A')),
|
|
230
|
+
(pg_temp.u('X'), pg_temp.u('R'), 'member', 'requested', null, pg_temp.u('A')),
|
|
231
|
+
(pg_temp.u('X'), pg_temp.u('L'), 'member', 'left', now(), pg_temp.u('A')),
|
|
232
|
+
(pg_temp.u('Y'), pg_temp.u('Y1'), 'owner', 'joined', null, pg_temp.u('Y1'));
|
|
233
|
+
-- O places its delegate on X through the real RPC.
|
|
234
|
+
select pg_temp.put('H.place', pg_temp.as_('O', format('select agent_board.add_delegate_to_board(%L::uuid, %L::uuid)', pg_temp.v('H'), pg_temp.v('X'))));
|
|
235
|
+
-- H's Y row: NOT joined. `left` is tried first, `invited` if the placement guard refuses it.
|
|
236
|
+
do $$
|
|
237
|
+
begin
|
|
238
|
+
begin
|
|
239
|
+
insert into agent_board.board_memberships(board_id, agent_id, role, membership_state, left_at, created_by_agent_id)
|
|
240
|
+
values (pg_temp.u('Y'), pg_temp.u('H'), 'member', 'left', now(), pg_temp.u('Y1'));
|
|
241
|
+
perform pg_temp.put('H.Ystate', 'left');
|
|
242
|
+
exception when others then
|
|
243
|
+
insert into agent_board.board_memberships(board_id, agent_id, role, membership_state, left_at, created_by_agent_id)
|
|
244
|
+
values (pg_temp.u('Y'), pg_temp.u('H'), 'member', 'invited', null, pg_temp.u('Y1'));
|
|
245
|
+
perform pg_temp.put('H.Ystate', 'invited (left refused: ' || sqlstate || ')');
|
|
246
|
+
end;
|
|
247
|
+
end $$;
|
|
248
|
+
select diag('H placement on X: ' || pg_temp.v('H.place') || '; H row on Y: ' || pg_temp.v('H.Ystate'));
|
|
249
|
+
|
|
250
|
+
-- Threads: T and U on X, TY on Y, TD on X (deleted by RT-DEL-1).
|
|
251
|
+
insert into agent_board.threads(id, thread_type, created_by_agent_id, subject, board_id) values
|
|
252
|
+
('65530000-0000-4000-8000-000000000001', 'coordination', pg_temp.u('A'), 'RT655 T', pg_temp.u('X')),
|
|
253
|
+
('65530000-0000-4000-8000-000000000002', 'coordination', pg_temp.u('A'), 'RT655 U', pg_temp.u('X')),
|
|
254
|
+
('65530000-0000-4000-8000-000000000003', 'coordination', pg_temp.u('Y1'), 'RT655 TY', pg_temp.u('Y')),
|
|
255
|
+
('65530000-0000-4000-8000-000000000004', 'coordination', pg_temp.u('A'), 'RT655 TD', pg_temp.u('X'));
|
|
256
|
+
select pg_temp.put('T', '65530000-0000-4000-8000-000000000001'), pg_temp.put('U', '65530000-0000-4000-8000-000000000002'),
|
|
257
|
+
pg_temp.put('TY', '65530000-0000-4000-8000-000000000003'), pg_temp.put('TD', '65530000-0000-4000-8000-000000000004');
|
|
258
|
+
|
|
259
|
+
-- Messages, through the real RPCs. M1 is longer than 140 characters.
|
|
260
|
+
select pg_temp.put('M1.r', pg_temp.as_('A', pg_temp.pbm('T', 'X', 'M1 ' || repeat('m', 197))));
|
|
261
|
+
select pg_temp.put('M1', pg_temp.msg('M1 ' || repeat('m', 197))::text);
|
|
262
|
+
select pg_temp.put('M2.r', pg_temp.as_('A', pg_temp.pbm('U', 'X', 'M2 body')));
|
|
263
|
+
select pg_temp.put('M2', pg_temp.msg('M2 body')::text);
|
|
264
|
+
select pg_temp.put('D1.r', pg_temp.as_('A', pg_temp.dm('T', 'B', 'D1 private A to B')));
|
|
265
|
+
select pg_temp.put('D1', pg_temp.msg('D1 private A to B')::text);
|
|
266
|
+
select pg_temp.put('MC1.r', pg_temp.as_('A', pg_temp.mc('T', 'B', 'MC1 private A to {B}')));
|
|
267
|
+
select pg_temp.put('MC1', pg_temp.msg('MC1 private A to {B}')::text);
|
|
268
|
+
select pg_temp.put('MY.r', pg_temp.as_('Y1', pg_temp.pbm('TY', 'Y', 'MY on Y')));
|
|
269
|
+
select pg_temp.put('MY', pg_temp.msg('MY on Y')::text);
|
|
270
|
+
-- N, the late joiner (see above).
|
|
271
|
+
insert into agent_board.board_memberships(board_id, agent_id, role, membership_state, created_by_agent_id)
|
|
272
|
+
values (pg_temp.u('X'), pg_temp.u('N'), 'member', 'joined', pg_temp.u('A'));
|
|
273
|
+
-- DH: a message of A's on T whose ONLY delivery is a private one to the delegate H (raw, as the owner:
|
|
274
|
+
-- the API never addresses a delegate privately). The authenticated predicate admits H as its
|
|
275
|
+
-- recipient; the delegate predicate does not (no board), so the two arms diverge here (M-B4d).
|
|
276
|
+
insert into agent_board.messages(thread_id, author_agent_id, message_sequence, body)
|
|
277
|
+
values (pg_temp.u('T'), pg_temp.u('A'), 900010, 'DH private to the delegate');
|
|
278
|
+
select pg_temp.put('DH', pg_temp.msg('DH private to the delegate')::text);
|
|
279
|
+
insert into agent_board.delivery_recipients(delivery_mode, recipient_agent_id, source_kind, source_message_id)
|
|
280
|
+
values ('direct', pg_temp.u('H'), 'message', pg_temp.u('DH'));
|
|
281
|
+
select diag('fixture calls: M1=' || pg_temp.v('M1.r') || ' M2=' || pg_temp.v('M2.r') || ' D1=' || pg_temp.v('D1.r')
|
|
282
|
+
|| ' MC1=' || pg_temp.v('MC1.r') || ' MY=' || pg_temp.v('MY.r'));
|
|
283
|
+
|
|
284
|
+
-- ===========================================================================
|
|
285
|
+
-- §8.2 Catalog class
|
|
286
|
+
-- ===========================================================================
|
|
287
|
+
-- RT-CLASS-2: the invariant, read from pg_constraint.
|
|
288
|
+
select is(
|
|
289
|
+
(select string_agg(c.conname || ' conkey=' || (select string_agg(a.attname, ',' order by k.o) from unnest(c.conkey) with ordinality k(n, o) join pg_attribute a on a.attrelid = c.conrelid and a.attnum = k.n)
|
|
290
|
+
|| ' confkey=' || (select string_agg(a.attname, ',' order by k.o) from unnest(c.confkey) with ordinality k(n, o) join pg_attribute a on a.attrelid = c.confrelid and a.attnum = k.n)
|
|
291
|
+
|| ' match=' || c.confmatchtype::text || ' del=' || c.confdeltype::text || ' upd=' || c.confupdtype::text || ' valid=' || c.convalidated::text, '; ')
|
|
292
|
+
from pg_constraint c
|
|
293
|
+
where c.contype = 'f' and c.conrelid = 'agent_board.messages'::regclass and c.confrelid = 'agent_board.messages'::regclass),
|
|
294
|
+
'messages_reply_to_same_thread_fkey conkey=thread_id,reply_to_message_id confkey=thread_id,id match=s del=a upd=a valid=true',
|
|
295
|
+
'RT-CLASS-2: exactly one messages->messages FK: (thread_id, reply_to_message_id) -> (thread_id, id), MATCH SIMPLE, NO ACTION both ways, validated');
|
|
296
|
+
select is(
|
|
297
|
+
(select string_agg(c.conname || ' ' || pg_get_constraintdef(c.oid), '; ')
|
|
298
|
+
from pg_constraint c
|
|
299
|
+
where c.conrelid = 'agent_board.messages'::regclass and c.contype = 'u'
|
|
300
|
+
and (select array_agg(a.attname::text order by k.o) from unnest(c.conkey) with ordinality k(n, o) join pg_attribute a on a.attrelid = c.conrelid and a.attnum = k.n) = array['thread_id', 'id']),
|
|
301
|
+
'messages_thread_id_id_key UNIQUE (thread_id, id)',
|
|
302
|
+
'RT-CLASS-2b: the referenced unique constraint on (thread_id, id) exists');
|
|
303
|
+
|
|
304
|
+
-- RT-CLASS-3a: every API-executable transitive entrypoint of the primitive takes the argument,
|
|
305
|
+
-- or is a signed_* wrapper (which reads it from its payload). No NULL-only exemption.
|
|
306
|
+
create temp table _census as
|
|
307
|
+
select distinct p.oid::regprocedure::text as sig, p.proname::text as name, p.proargnames
|
|
308
|
+
from agent_board_test.fn_call_closure() c join pg_proc p on p.oid = c.root_oid
|
|
309
|
+
where c.reached_name = '_insert_thread_message'
|
|
310
|
+
and (has_function_privilege('authenticated', p.oid, 'execute') or has_function_privilege('service_role', p.oid, 'execute')
|
|
311
|
+
or has_function_privilege('agent_board_delegate', p.oid, 'execute'));
|
|
312
|
+
select is(
|
|
313
|
+
(select string_agg(sig, ', ' order by sig) from _census
|
|
314
|
+
where not ('p_reply_to_message_id' = any(coalesce(proargnames, '{}'))) and name not like 'signed\_%'),
|
|
315
|
+
null,
|
|
316
|
+
'RT-CLASS-3a: every API message entrypoint takes p_reply_to_message_id or is a signed_* payload wrapper');
|
|
317
|
+
|
|
318
|
+
-- RT-CLASS-4: the direct callers of the primitive (by the call_re matcher), and each of them passes
|
|
319
|
+
-- the argument BY NAME (one presence check per named body, so a spelling miss is loud).
|
|
320
|
+
select is(
|
|
321
|
+
(select string_agg(p.oid::regprocedure::text, ', ' order by p.oid::regprocedure::text)
|
|
322
|
+
from pg_proc p
|
|
323
|
+
where p.pronamespace = 'agent_board'::regnamespace
|
|
324
|
+
and agent_board_test.stripped_body(p.oid) ~ agent_board_test.call_re('_insert_thread_message')),
|
|
325
|
+
'post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid), post_message(uuid,uuid,text,text,uuid,jsonb,uuid), '
|
|
326
|
+
|| 'send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid), send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb,uuid)',
|
|
327
|
+
'RT-CLASS-4a: the direct callers of _insert_thread_message are exactly the four uuid cores');
|
|
328
|
+
select ok(
|
|
329
|
+
coalesce(agent_board_test.stripped_body(to_regprocedure('agent_board.post_message(uuid,uuid,text,text,uuid,jsonb,uuid)')) ~ 'p_reply_to_message_id\s*=>', false)
|
|
330
|
+
and coalesce(agent_board_test.stripped_body(to_regprocedure('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid)')) ~ 'p_reply_to_message_id\s*=>', false)
|
|
331
|
+
and coalesce(agent_board_test.stripped_body(to_regprocedure('agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid)')) ~ 'p_reply_to_message_id\s*=>', false)
|
|
332
|
+
and coalesce(agent_board_test.stripped_body(to_regprocedure('agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb,uuid)')) ~ 'p_reply_to_message_id\s*=>', false),
|
|
333
|
+
'RT-CLASS-4b: each of the four passes p_reply_to_message_id to the primitive BY NAME');
|
|
334
|
+
|
|
335
|
+
-- ===========================================================================
|
|
336
|
+
-- §8.3 Allow rows
|
|
337
|
+
-- ===========================================================================
|
|
338
|
+
-- A-1: post_message.
|
|
339
|
+
select pg_temp.put('T.seq', (select last_message_sequence::text from agent_board.threads where id = pg_temp.u('T')));
|
|
340
|
+
insert into _driven values ('agent_board.post_message(uuid,uuid,text,text,uuid,jsonb,uuid)');
|
|
341
|
+
select is(pg_temp.as_('B', pg_temp.pm('T', 'A-1 reply', 'M1')), 'ok', 'A-1: B replies to M1 with post_message');
|
|
342
|
+
select is((select format('reply=%s thread=%s seq_step=%s', pg_temp.reply_of('A-1 reply'), (thread_id = pg_temp.u('T'))::text,
|
|
343
|
+
message_sequence - pg_temp.v('T.seq')::bigint)
|
|
344
|
+
from agent_board.messages where body = 'A-1 reply'),
|
|
345
|
+
'reply=M1 thread=true seq_step=1', 'A-1 state: reply_to_message_id = M1 in thread T, sequence advanced by one');
|
|
346
|
+
|
|
347
|
+
-- A-2: post_board_message, the board-id overload.
|
|
348
|
+
insert into _driven values ('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid)');
|
|
349
|
+
select is(pg_temp.as_('B', pg_temp.pbm('T', 'X', 'A-2 reply', 'M1')), 'ok', 'A-2: B replies to M1 with post_board_message (board id)');
|
|
350
|
+
select is(format('reply=%s deliveries=%s', pg_temp.reply_of('A-2 reply'),
|
|
351
|
+
(select (count(*) > 0)::text from agent_board.delivery_recipients where source_message_id = pg_temp.msg('A-2 reply') and board_id = pg_temp.u('X'))),
|
|
352
|
+
'reply=M1 deliveries=true', 'A-2 state: the reply is set and board-delivered');
|
|
353
|
+
|
|
354
|
+
-- A-3: post_board_message, the slug overload.
|
|
355
|
+
insert into _driven values ('agent_board.post_board_message(uuid,uuid,text,text,text,text,text,text,text,jsonb,uuid)');
|
|
356
|
+
select is(pg_temp.as_('B', pg_temp.pbms('T', 'rt655-x', 'A-3 reply', 'M1')), 'ok', 'A-3: B replies to M1 with post_board_message (slug)');
|
|
357
|
+
select is(pg_temp.reply_of('A-3 reply'), 'M1', 'A-3 state: the slug overload forwards the reply');
|
|
358
|
+
|
|
359
|
+
-- A-4: send_direct_message, uuid overload; B can read D1 as its recipient.
|
|
360
|
+
insert into _driven values ('agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid)');
|
|
361
|
+
select is(pg_temp.as_('B', pg_temp.dm('T', 'A', 'A-4 reply', 'D1')), 'ok', 'A-4: B replies to D1 privately with send_direct_message (uuid)');
|
|
362
|
+
select is(format('reply=%s private_to_A=%s', pg_temp.reply_of('A-4 reply'),
|
|
363
|
+
(select count(*) from agent_board.delivery_recipients where source_message_id = pg_temp.msg('A-4 reply')
|
|
364
|
+
and recipient_agent_id = pg_temp.u('A') and board_id is null and delivery_mode = 'direct')),
|
|
365
|
+
'reply=D1 private_to_A=1', 'A-4 state: the reply is set and privately delivered to A');
|
|
366
|
+
|
|
367
|
+
-- A-4b: send_direct_message, descriptor overload.
|
|
368
|
+
insert into _driven values ('agent_board.send_direct_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb,uuid)');
|
|
369
|
+
select is(pg_temp.as_('B', pg_temp.dmd('T', 'A', 'A-4b reply', 'D1')), 'ok', 'A-4b: B replies to D1 with send_direct_message (descriptor)');
|
|
370
|
+
select is(pg_temp.reply_of('A-4b reply'), 'D1', 'A-4b state: the descriptor overload forwards the reply');
|
|
371
|
+
|
|
372
|
+
-- A-5: send_multicast_message, uuid[] overload.
|
|
373
|
+
insert into _driven values ('agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb,uuid)');
|
|
374
|
+
select is(pg_temp.as_('B', pg_temp.mc('T', 'A', 'A-5 reply', 'MC1')), 'ok', 'A-5: B replies to MC1 with send_multicast_message (uuid[])');
|
|
375
|
+
select is(pg_temp.reply_of('A-5 reply'), 'MC1', 'A-5 state: the reply is set');
|
|
376
|
+
|
|
377
|
+
-- A-5b: send_multicast_message, descriptor overload.
|
|
378
|
+
insert into _driven values ('agent_board.send_multicast_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb,uuid)');
|
|
379
|
+
select is(pg_temp.as_('B', pg_temp.mcd('T', 'A', 'A-5b reply', 'MC1')), 'ok', 'A-5b: B replies to MC1 with send_multicast_message (descriptor)');
|
|
380
|
+
select is(pg_temp.reply_of('A-5b reply'), 'MC1', 'A-5b state: the descriptor overload forwards the reply');
|
|
381
|
+
|
|
382
|
+
-- A-6: the signed wrappers as service_role, acting for B; multicast on BOTH of its branches.
|
|
383
|
+
insert into _driven values ('agent_board.signed_post_board_message(jsonb,jsonb)'), ('agent_board.signed_send_direct_message(jsonb,jsonb)'),
|
|
384
|
+
('agent_board.signed_send_multicast_message(jsonb,jsonb)');
|
|
385
|
+
select is(pg_temp.as_('SVC', pg_temp.signed('signed_post_board_message', jsonb_build_object('p_thread_id', pg_temp.v('T'), 'p_board_id', pg_temp.v('X'),
|
|
386
|
+
'p_subject', 'subj', 'p_body', 'A-6 signed board', 'p_reply_to_message_id', pg_temp.v('M1')), 'B')),
|
|
387
|
+
'ok', 'A-6 signed_post_board_message: service_role posts B''s signed board reply to M1');
|
|
388
|
+
select is(pg_temp.reply_of('A-6 signed board'), 'M1', 'A-6 signed_post_board_message state: the payload reply is set');
|
|
389
|
+
select is(pg_temp.as_('SVC', pg_temp.signed('signed_send_direct_message', jsonb_build_object('p_thread_id', pg_temp.v('T'), 'p_recipient_agent_id', pg_temp.v('A'),
|
|
390
|
+
'p_subject', 'subj', 'p_body', 'A-6 signed direct', 'p_reply_to_message_id', pg_temp.v('D1')), 'B')),
|
|
391
|
+
'ok', 'A-6 signed_send_direct_message: service_role sends B''s signed direct reply to D1');
|
|
392
|
+
select is(pg_temp.reply_of('A-6 signed direct'), 'D1', 'A-6 signed_send_direct_message state: the payload reply is set');
|
|
393
|
+
select is(pg_temp.as_('SVC', pg_temp.signed('signed_send_multicast_message', jsonb_build_object('p_thread_id', pg_temp.v('T'),
|
|
394
|
+
'p_recipient_agent_ids', jsonb_build_array(pg_temp.v('A')),
|
|
395
|
+
'p_subject', 'subj', 'p_body', 'A-6 signed multicast ids', 'p_reply_to_message_id', pg_temp.v('MC1')), 'B')),
|
|
396
|
+
'ok', 'A-6 signed_send_multicast_message (ids branch): service_role sends B''s signed multicast reply to MC1');
|
|
397
|
+
select is(pg_temp.as_('SVC', pg_temp.signed('signed_send_multicast_message', jsonb_build_object('p_thread_id', pg_temp.v('T'),
|
|
398
|
+
'p_recipient_descriptors', jsonb_build_array(jsonb_build_object('kind', 'agent_id', 'agentId', pg_temp.v('A'))),
|
|
399
|
+
'p_subject', 'subj', 'p_body', 'A-6 signed multicast descriptors', 'p_reply_to_message_id', pg_temp.v('MC1')), 'B')),
|
|
400
|
+
'ok', 'A-6 signed_send_multicast_message (descriptor branch): service_role sends B''s signed multicast reply to MC1');
|
|
401
|
+
select is(pg_temp.reply_of('A-6 signed multicast ids') || ' ' || pg_temp.reply_of('A-6 signed multicast descriptors'), 'MC1 MC1',
|
|
402
|
+
'A-6 signed_send_multicast_message state: BOTH branches forward the payload reply');
|
|
403
|
+
select is(pg_temp.as_('SVC', pg_temp.signed('signed_post_board_message', jsonb_build_object('p_thread_id', pg_temp.v('T'), 'p_board_id', pg_temp.v('X'),
|
|
404
|
+
'p_subject', 'subj', 'p_body', 'A-6 signed board, no reply'), 'B')),
|
|
405
|
+
'ok', 'A-6 signed, no reply key: the payload without p_reply_to_message_id still posts');
|
|
406
|
+
select is(pg_temp.reply_of('A-6 signed board, no reply'), 'NULL', 'A-6 signed, no reply key state: reply_to_message_id is NULL');
|
|
407
|
+
|
|
408
|
+
-- A-6b: the primitive called directly by the owner (the owner session acts for the supplied B).
|
|
409
|
+
select is((select pg_temp.as_('OWNER', format('select * from agent_board._insert_thread_message(%L::uuid, %L::uuid, ''A-6b owner'', ''comment'', null, ''{}''::jsonb, %L::uuid)',
|
|
410
|
+
pg_temp.v('T'), pg_temp.v('B'), pg_temp.v('M1')))),
|
|
411
|
+
'ok', 'A-6b: the owner calls _insert_thread_message directly with a reply');
|
|
412
|
+
select is(pg_temp.reply_of('A-6b owner'), 'M1', 'A-6b state: set');
|
|
413
|
+
|
|
414
|
+
-- A-7: each entrypoint WITHOUT the argument: NULL, and the return shapes are unchanged.
|
|
415
|
+
select is(
|
|
416
|
+
pg_temp.as_('B', pg_temp.pm('T', 'A-7 pm')) || ' ' || pg_temp.as_('B', pg_temp.pbm('T', 'X', 'A-7 pbm')) || ' '
|
|
417
|
+
|| pg_temp.as_('B', pg_temp.pbms('T', 'rt655-x', 'A-7 pbms')) || ' ' || pg_temp.as_('B', pg_temp.dm('T', 'A', 'A-7 dm')) || ' '
|
|
418
|
+
|| pg_temp.as_('B', pg_temp.dmd('T', 'A', 'A-7 dmd')) || ' ' || pg_temp.as_('B', pg_temp.mc('T', 'A', 'A-7 mc')) || ' '
|
|
419
|
+
|| pg_temp.as_('B', pg_temp.mcd('T', 'A', 'A-7 mcd')) || ' | '
|
|
420
|
+
|| (select string_agg(pg_temp.reply_of(b), ',' order by b) from unnest(array['A-7 pm', 'A-7 pbm', 'A-7 pbms', 'A-7 dm', 'A-7 dmd', 'A-7 mc', 'A-7 mcd']) b),
|
|
421
|
+
'ok ok ok ok ok ok ok | NULL,NULL,NULL,NULL,NULL,NULL,NULL',
|
|
422
|
+
'A-7: every entrypoint called without the argument succeeds and writes reply_to_message_id NULL');
|
|
423
|
+
select is(
|
|
424
|
+
(select string_agg(p.proname || ': ' || pg_get_function_result(p.oid), ' | ' order by p.proname, p.oid::regprocedure::text)
|
|
425
|
+
from pg_proc p where p.pronamespace = 'agent_board'::regnamespace
|
|
426
|
+
and p.proname in ('_insert_thread_message', 'post_message', 'post_board_message', 'send_direct_message', 'send_multicast_message')),
|
|
427
|
+
'_insert_thread_message: TABLE(message_id uuid, thread_id uuid, task_id uuid, message_sequence bigint, created_at timestamp with time zone)'
|
|
428
|
+
|| ' | post_board_message: TABLE(message_id uuid, board_id uuid, delivered_recipient_count integer, callback_outbox_count integer, created_at timestamp with time zone)'
|
|
429
|
+
|| ' | post_board_message: TABLE(message_id uuid, board_id uuid, delivered_recipient_count integer, callback_outbox_count integer, created_at timestamp with time zone)'
|
|
430
|
+
|| ' | post_message: TABLE(message_id uuid, thread_id uuid, task_id uuid, message_sequence bigint, created_at timestamp with time zone)'
|
|
431
|
+
|| ' | send_direct_message: TABLE(message_id uuid, delivery_recipient_id uuid, inbox_item_id uuid, callback_outbox_count integer, created_at timestamp with time zone)'
|
|
432
|
+
|| ' | send_direct_message: TABLE(message_id uuid, delivery_recipient_id uuid, inbox_item_id uuid, callback_outbox_count integer, created_at timestamp with time zone)'
|
|
433
|
+
|| ' | send_multicast_message: TABLE(message_id uuid, delivered_recipient_count integer, inbox_item_count integer, callback_outbox_count integer, created_at timestamp with time zone)'
|
|
434
|
+
|| ' | send_multicast_message: TABLE(message_id uuid, delivered_recipient_count integer, inbox_item_count integer, callback_outbox_count integer, created_at timestamp with time zone)',
|
|
435
|
+
'A-7 shape: the return shape of every writer is unchanged');
|
|
436
|
+
|
|
437
|
+
-- A-8 (D8): a keyed send replayed with the same key and a DIFFERENT parent keeps one row and the
|
|
438
|
+
-- original reply. The second parent is a valid reply target (the A-1 reply), so only D8 explains
|
|
439
|
+
-- the original standing.
|
|
440
|
+
select is(
|
|
441
|
+
pg_temp.as_('B', pg_temp.pbm('T', 'X', 'A-8 pbm', 'M1', 'rt655-a8-pbm')) || ' ' || pg_temp.as_('B', replace(pg_temp.pbm('T', 'X', 'A-8 pbm', 'M1', 'rt655-a8-pbm'),
|
|
442
|
+
pg_temp.v('M1'), pg_temp.msg('A-1 reply')::text))
|
|
443
|
+
|| format(' rows=%s reply=%s', pg_temp.keyrows('rt655-a8-pbm'), pg_temp.reply_of('A-8 pbm')),
|
|
444
|
+
'ok ok rows=1 reply=M1', 'A-8 post_board_message: a same-key replay with another parent keeps one row and the original parent');
|
|
445
|
+
select is(
|
|
446
|
+
pg_temp.as_('B', pg_temp.dm('T', 'A', 'A-8 dm', 'D1', 'rt655-a8-dm')) || ' ' || pg_temp.as_('B', replace(pg_temp.dm('T', 'A', 'A-8 dm', 'D1', 'rt655-a8-dm'),
|
|
447
|
+
pg_temp.v('D1'), pg_temp.msg('A-4 reply')::text))
|
|
448
|
+
|| format(' rows=%s reply=%s', pg_temp.keyrows('rt655-a8-dm'), pg_temp.reply_of('A-8 dm')),
|
|
449
|
+
'ok ok rows=1 reply=D1', 'A-8 send_direct_message: a same-key replay with another parent keeps one row and the original parent');
|
|
450
|
+
select is(
|
|
451
|
+
pg_temp.as_('B', pg_temp.mc('T', 'A', 'A-8 mc', 'MC1', 'rt655-a8-mc')) || ' ' || pg_temp.as_('B', replace(pg_temp.mc('T', 'A', 'A-8 mc', 'MC1', 'rt655-a8-mc'),
|
|
452
|
+
pg_temp.v('MC1'), pg_temp.msg('A-5 reply')::text))
|
|
453
|
+
|| format(' rows=%s reply=%s', pg_temp.keyrows('rt655-a8-mc'), pg_temp.reply_of('A-8 mc')),
|
|
454
|
+
'ok ok rows=1 reply=MC1', 'A-8 send_multicast_message: a same-key replay with another parent keeps one row and the original parent');
|
|
455
|
+
|
|
456
|
+
-- A-9: a chain (a reply to a reply) and a reply to a `status` message.
|
|
457
|
+
select pg_temp.put('A1R', pg_temp.msg('A-1 reply')::text);
|
|
458
|
+
select is(pg_temp.as_('A', pg_temp.pm('T', 'A-9 chain', 'A1R')), 'ok', 'A-9 chain: A replies to the A-1 reply');
|
|
459
|
+
select is(pg_temp.reply_of('A-9 chain'), 'A1R', 'A-9 chain state: set to the reply');
|
|
460
|
+
select pg_temp.put('ST.r', pg_temp.as_('A', format('select * from agent_board.post_message(%L::uuid, null, ''A-9 status parent'', ''status'')', pg_temp.v('T'))));
|
|
461
|
+
select pg_temp.put('ST', pg_temp.msg('A-9 status parent')::text);
|
|
462
|
+
select is(pg_temp.as_('B', pg_temp.pm('T', 'A-9 reply to status', 'ST')), 'ok', 'A-9 status: B replies to a status message');
|
|
463
|
+
select is(pg_temp.reply_of('A-9 reply to status') || ' ' || (select message_type from agent_board.messages where id = pg_temp.u('ST')), 'ST status',
|
|
464
|
+
'A-9 status state: set, and the parent is a status message');
|
|
465
|
+
|
|
466
|
+
-- A-10: the hosted delegate H replies to M1 (board-delivered on X, so H can read it).
|
|
467
|
+
select is(pg_temp.as_('H', pg_temp.pbm('T', 'X', 'A-10 delegate reply', 'M1')), 'ok', 'A-10: delegate H replies to M1 with post_board_message');
|
|
468
|
+
select is(pg_temp.reply_of('A-10 delegate reply') || ' author_is_H=' || ((select author_agent_id from agent_board.messages where body = 'A-10 delegate reply') = pg_temp.u('H')),
|
|
469
|
+
'M1 author_is_H=true', 'A-10 state: set, authored by H');
|
|
470
|
+
|
|
471
|
+
-- A-11: N, who joined after M1 was posted and holds no delivery of it, replies to it (the board-member
|
|
472
|
+
-- branch alone admits it).
|
|
473
|
+
select is(pg_temp.as_('N', pg_temp.pm('T', 'A-11 late joiner reply', 'M1')), 'ok', 'A-11: a member who joined after M1 was posted (no delivery row of its own) replies to it');
|
|
474
|
+
select is(pg_temp.reply_of('A-11 late joiner reply'), 'M1', 'A-11 state: set');
|
|
475
|
+
|
|
476
|
+
-- RT-CLASS-3b: the census equals the set driven above (by oid::regprocedure::text), so a new
|
|
477
|
+
-- entrypoint with no driven row fails here by name.
|
|
478
|
+
select is(
|
|
479
|
+
(select string_agg(coalesce(c.sig, '(not in census) ' || d.sig) || case when d.sig is null then ' (not driven)' else '' end, ', '
|
|
480
|
+
order by coalesce(c.sig, d.sig))
|
|
481
|
+
from (select 'agent_board.' || sig as sig from _census) c full join _driven d on d.sig = c.sig
|
|
482
|
+
where c.sig is null or d.sig is null),
|
|
483
|
+
null,
|
|
484
|
+
'RT-CLASS-3b: the transitive entrypoint census equals the ten entrypoints this file drives');
|
|
485
|
+
|
|
486
|
+
-- RT-DEL-1: deleting a thread that holds a reply chain (NO ACTION is checked at statement end).
|
|
487
|
+
select pg_temp.put('TD.p0', pg_temp.as_('A', pg_temp.pbm('TD', 'X', 'TD p0')));
|
|
488
|
+
select pg_temp.put('TDP0', pg_temp.msg('TD p0')::text);
|
|
489
|
+
select pg_temp.put('TD.p1', pg_temp.as_('B', pg_temp.pm('TD', 'TD p1', 'TDP0')));
|
|
490
|
+
select pg_temp.put('TDP1', pg_temp.msg('TD p1')::text);
|
|
491
|
+
select pg_temp.put('TD.p2', pg_temp.as_('A', pg_temp.pm('TD', 'TD p2', 'TDP1')));
|
|
492
|
+
select lives_ok(format('delete from agent_board.threads where id = %L', pg_temp.v('TD')),
|
|
493
|
+
'RT-DEL-1: deleting a thread whose messages form a reply chain succeeds');
|
|
494
|
+
select is((select count(*) from agent_board.messages where thread_id = pg_temp.u('TD'))::text || ' chain=' || pg_temp.v('TD.p0') || pg_temp.v('TD.p1') || pg_temp.v('TD.p2'),
|
|
495
|
+
'0 chain=okokok', 'RT-DEL-1 state: the cascade removed the whole chain');
|
|
496
|
+
|
|
497
|
+
-- ===========================================================================
|
|
498
|
+
-- §8.4 The table invariant without the writer
|
|
499
|
+
-- ===========================================================================
|
|
500
|
+
select throws_ok(format($q$insert into agent_board.messages(thread_id, author_agent_id, message_sequence, body, reply_to_message_id)
|
|
501
|
+
values (%L, %L, 900001, 'RT-INV-1 raw', %L)$q$, pg_temp.v('T'), pg_temp.v('A'), pg_temp.v('M2')),
|
|
502
|
+
'23503', null, 'RT-INV-1: an owner''s raw INSERT of a reply in T to M2 (thread U) is refused by the FK');
|
|
503
|
+
|
|
504
|
+
-- ===========================================================================
|
|
505
|
+
-- §8.5 Read side
|
|
506
|
+
-- ===========================================================================
|
|
507
|
+
select is(
|
|
508
|
+
(select string_agg(distinct c.oid::regclass::text, ', ' order by c.oid::regclass::text)
|
|
509
|
+
from pg_rewrite rw join pg_depend d on d.classid = 'pg_rewrite'::regclass and d.objid = rw.oid
|
|
510
|
+
join pg_class c on c.oid = rw.ev_class
|
|
511
|
+
where d.refobjid = 'agent_board.messages'::regclass and c.oid <> 'agent_board.messages'::regclass),
|
|
512
|
+
'v_board_feed, v_fleet_delivery_posture, v_thread_feed',
|
|
513
|
+
'RT-V-0: the views over messages are exactly v_board_feed, v_fleet_delivery_posture and v_thread_feed');
|
|
514
|
+
select is(
|
|
515
|
+
(select string_agg(a.attname, ', ' order by a.attnum) from pg_attribute a
|
|
516
|
+
where a.attrelid = 'agent_board.v_fleet_delivery_posture'::regclass and a.attnum > 0 and not a.attisdropped
|
|
517
|
+
and (a.attname = 'body' or a.attname like 'reply\_to%')),
|
|
518
|
+
null,
|
|
519
|
+
'RT-V-0b: v_fleet_delivery_posture projects no body and no reply_to_* column');
|
|
520
|
+
select is(
|
|
521
|
+
(select string_agg(c.relname || '=' || coalesce(array_to_string(c.reloptions, ','), ''), ' ' order by c.relname)
|
|
522
|
+
from pg_class c where c.oid in ('agent_board.v_thread_feed'::regclass, 'agent_board.v_board_feed'::regclass)),
|
|
523
|
+
'v_board_feed=security_invoker=true v_thread_feed=security_invoker=true',
|
|
524
|
+
'RT-V-1: both feed views are security_invoker');
|
|
525
|
+
|
|
526
|
+
-- RT-V-2, the leak row. B (who can read D1) posts board reply RV2 on T to D1 (the plan's "R"; renamed
|
|
527
|
+
-- because R is the requested-member fixture agent).
|
|
528
|
+
select pg_temp.put('RV2.r', pg_temp.as_('B', pg_temp.pbm('T', 'X', 'RT-V-2 board reply to D1', 'D1')));
|
|
529
|
+
select pg_temp.put('RV2', pg_temp.msg('RT-V-2 board reply to D1')::text);
|
|
530
|
+
create function pg_temp.reply_cols(p_who text, p_view text, p_msg text) returns text language sql as $$
|
|
531
|
+
select pg_temp.val(p_who, format($q$
|
|
532
|
+
select coalesce((select format('id=%%s seq=%%s author=%%s handle=%%s name=%%s excerpt=%%s trunc=%%s',
|
|
533
|
+
coalesce(reply_to_message_id::text, 'NULL'), coalesce(reply_to_message_sequence::text, 'NULL'),
|
|
534
|
+
coalesce(reply_to_author_agent_id::text, 'NULL'), coalesce(reply_to_author_handle, 'NULL'),
|
|
535
|
+
coalesce(reply_to_author_display_name, 'NULL'), coalesce(reply_to_excerpt, 'NULL'),
|
|
536
|
+
coalesce(reply_to_excerpt_truncated::text, 'NULL'))
|
|
537
|
+
from agent_board.%I where message_id = %L limit 1), 'NO ROW')$q$, p_view, pg_temp.v(p_msg)))
|
|
538
|
+
$$;
|
|
539
|
+
select is(pg_temp.reply_cols('C', 'v_thread_feed', 'RV2'),
|
|
540
|
+
format('id=%s seq=NULL author=NULL handle=NULL name=NULL excerpt=NULL trunc=NULL', pg_temp.v('D1')),
|
|
541
|
+
'RT-V-2 thread: C sees R in v_thread_feed with reply_to_message_id = D1 and every other reply_to_* column NULL');
|
|
542
|
+
select is(pg_temp.reply_cols('C', 'v_board_feed', 'RV2'),
|
|
543
|
+
format('id=%s seq=NULL author=NULL handle=NULL name=NULL excerpt=NULL trunc=NULL', pg_temp.v('D1')),
|
|
544
|
+
'RT-V-2 board: C sees R in v_board_feed with reply_to_message_id = D1 and every other reply_to_* column NULL');
|
|
545
|
+
select is(pg_temp.reply_cols('B', 'v_thread_feed', 'RV2'),
|
|
546
|
+
format('id=%s seq=%s author=%s handle=rt655-a name=RT655 A excerpt=D1 private A to B trunc=false', pg_temp.v('D1'),
|
|
547
|
+
(select message_sequence from agent_board.messages where id = pg_temp.u('D1')), pg_temp.v('A')),
|
|
548
|
+
'RT-V-2 positive: B, who can read D1, sees its sequence, author and excerpt');
|
|
549
|
+
|
|
550
|
+
-- RT-V-3: the delegate H sees R like C does, and loses no row to the parent joins.
|
|
551
|
+
select is(pg_temp.reply_cols('H', 'v_thread_feed', 'RV2'),
|
|
552
|
+
format('id=%s seq=NULL author=NULL handle=NULL name=NULL excerpt=NULL trunc=NULL', pg_temp.v('D1')),
|
|
553
|
+
'RT-V-3: delegate H sees R with reply_to_message_id = D1 and the parent''s columns NULL');
|
|
554
|
+
select is(pg_temp.val('H', format($q$select (select count(*) from agent_board.v_thread_feed where thread_id = %1$L)
|
|
555
|
+
= (select count(*) from agent_board.messages where thread_id = %1$L)
|
|
556
|
+
and (select count(*) from agent_board.v_thread_feed where thread_id = %1$L and reply_to_message_id is not null) > 0$q$, pg_temp.v('T'))),
|
|
557
|
+
'true', 'RT-V-3 rows: H''s thread feed has one row per message H can read, replies included');
|
|
558
|
+
|
|
559
|
+
-- RT-V-4: defence in depth. With the FK dropped (in an always-rolled-back subtransaction), a raw row
|
|
560
|
+
-- in T replying to M2 (thread U) shows no snippet even to A, who can read M2.
|
|
561
|
+
create function pg_temp.rt_v4() returns text language plpgsql as $$
|
|
562
|
+
declare v text;
|
|
563
|
+
begin
|
|
564
|
+
begin
|
|
565
|
+
alter table agent_board.messages drop constraint if exists messages_reply_to_same_thread_fkey;
|
|
566
|
+
insert into agent_board.messages(thread_id, author_agent_id, message_sequence, body, reply_to_message_id)
|
|
567
|
+
values (pg_temp.u('T'), pg_temp.u('A'), 900002, 'RT-V-4 cross-thread raw', pg_temp.u('M2'));
|
|
568
|
+
perform pg_temp.put('RV4', pg_temp.msg('RT-V-4 cross-thread raw')::text);
|
|
569
|
+
v := pg_temp.reply_cols('A', 'v_thread_feed', 'RV4');
|
|
570
|
+
raise exception using errcode = 'P0655', message = 'rt_v4 rollback';
|
|
571
|
+
exception when sqlstate 'P0655' then null;
|
|
572
|
+
when others then v := 'ERR ' || sqlstate || ':' || sqlerrm; -- fail the row, never abort the file
|
|
573
|
+
end;
|
|
574
|
+
return v;
|
|
575
|
+
end $$;
|
|
576
|
+
select is(pg_temp.rt_v4(), format('id=%s seq=NULL author=NULL handle=NULL name=NULL excerpt=NULL trunc=NULL', pg_temp.v('M2')),
|
|
577
|
+
'RT-V-4: a cross-thread parent (FK dropped) yields no snippet: the view join requires the same thread');
|
|
578
|
+
|
|
579
|
+
-- RT-V-5: the excerpt is left(body, 140) in CHARACTERS; truncated iff longer than 140.
|
|
580
|
+
select pg_temp.as_('A', pg_temp.pbm('T', 'X', repeat('e', 140)));
|
|
581
|
+
select pg_temp.put('P140', pg_temp.msg(repeat('e', 140))::text);
|
|
582
|
+
select pg_temp.as_('A', pg_temp.pbm('T', 'X', repeat('f', 141)));
|
|
583
|
+
select pg_temp.put('P141', pg_temp.msg(repeat('f', 141))::text);
|
|
584
|
+
select pg_temp.as_('A', pg_temp.pbm('T', 'X', repeat('é', 141)));
|
|
585
|
+
select pg_temp.put('PMB', pg_temp.msg(repeat('é', 141))::text);
|
|
586
|
+
select pg_temp.as_('A', pg_temp.pbm('T', 'X', 'RT-V-5 r140', 'P140'));
|
|
587
|
+
select pg_temp.as_('A', pg_temp.pbm('T', 'X', 'RT-V-5 r141', 'P141'));
|
|
588
|
+
select pg_temp.as_('A', pg_temp.pbm('T', 'X', 'RT-V-5 rmb', 'PMB'));
|
|
589
|
+
select is(pg_temp.val('A', format($q$select (reply_to_excerpt = repeat('e', 140))::text || ' ' || reply_to_excerpt_truncated::text
|
|
590
|
+
from agent_board.v_thread_feed where message_id = %L$q$, pg_temp.msg('RT-V-5 r140'))),
|
|
591
|
+
'true false', 'RT-V-5 at 140: the whole body, not truncated');
|
|
592
|
+
select is(pg_temp.val('A', format($q$select (reply_to_excerpt = repeat('f', 140))::text || ' ' || reply_to_excerpt_truncated::text
|
|
593
|
+
from agent_board.v_thread_feed where message_id = %L$q$, pg_temp.msg('RT-V-5 r141'))),
|
|
594
|
+
'true true', 'RT-V-5 at 141: the first 140 characters, truncated');
|
|
595
|
+
select is(pg_temp.val('A', format($q$select char_length(reply_to_excerpt) || '/' || octet_length(reply_to_excerpt) || ' ' || reply_to_excerpt_truncated::text
|
|
596
|
+
from agent_board.v_board_feed where message_id = %L$q$, pg_temp.msg('RT-V-5 rmb'))),
|
|
597
|
+
'140/280 true', 'RT-V-5 multibyte: 140 characters (280 bytes), counted in characters, in v_board_feed');
|
|
598
|
+
|
|
599
|
+
-- RT-V-6: both views agree on every reply both show to one reader (B).
|
|
600
|
+
select is(pg_temp.val('B', $q$
|
|
601
|
+
select coalesce(string_agg(t.message_id::text, ', '), 'none')
|
|
602
|
+
from agent_board.v_thread_feed t join agent_board.v_board_feed b on b.message_id = t.message_id
|
|
603
|
+
where t.reply_to_message_id is not null
|
|
604
|
+
and (t.reply_to_message_id, t.reply_to_message_sequence, t.reply_to_author_agent_id, t.reply_to_author_handle,
|
|
605
|
+
t.reply_to_author_display_name, t.reply_to_excerpt, t.reply_to_excerpt_truncated)
|
|
606
|
+
is distinct from
|
|
607
|
+
(b.reply_to_message_id, b.reply_to_message_sequence, b.reply_to_author_agent_id, b.reply_to_author_handle,
|
|
608
|
+
b.reply_to_author_display_name, b.reply_to_excerpt, b.reply_to_excerpt_truncated)$q$),
|
|
609
|
+
'none', 'RT-V-6: v_thread_feed and v_board_feed give equal reply_to_* columns for every reply both show to B');
|
|
610
|
+
select cmp_ok(pg_temp.val('B', $q$select count(*) from agent_board.v_thread_feed t join agent_board.v_board_feed b on b.message_id = t.message_id
|
|
611
|
+
where t.reply_to_message_id is not null and t.reply_to_excerpt_truncated$q$)::int, '>=', 2,
|
|
612
|
+
'RT-V-6 control: the comparison covers at least two truncated replies shown in both views');
|
|
613
|
+
|
|
614
|
+
-- RT-V-7: the full ordered column lists.
|
|
615
|
+
select is(
|
|
616
|
+
(select string_agg(a.attname, ',' order by a.attnum) from pg_attribute a
|
|
617
|
+
where a.attrelid = 'agent_board.v_thread_feed'::regclass and a.attnum > 0 and not a.attisdropped),
|
|
618
|
+
'thread_id,message_id,message_sequence,message_created_at,author_agent_id,author_handle,author_display_name,message_type,body,task_id,'
|
|
619
|
+
|| 'verification_status,key_fingerprint,key_version,algorithm,verified_at,failure_class,'
|
|
620
|
+
|| 'reply_to_message_id,reply_to_message_sequence,reply_to_author_agent_id,reply_to_author_handle,reply_to_author_display_name,reply_to_excerpt,reply_to_excerpt_truncated',
|
|
621
|
+
'RT-V-7 v_thread_feed: the existing columns unchanged, the seven reply_to_* columns appended in order');
|
|
622
|
+
select is(
|
|
623
|
+
(select string_agg(a.attname, ',' order by a.attnum) from pg_attribute a
|
|
624
|
+
where a.attrelid = 'agent_board.v_board_feed'::regclass and a.attnum > 0 and not a.attisdropped),
|
|
625
|
+
'board_id,board_slug,board_name,message_id,thread_id,message_sequence,message_created_at,author_agent_id,author_handle,author_display_name,'
|
|
626
|
+
|| 'subject,preview,priority,fetch_descriptor,first_delivered_at,'
|
|
627
|
+
|| 'reply_to_message_id,reply_to_message_sequence,reply_to_author_agent_id,reply_to_author_handle,reply_to_author_display_name,reply_to_excerpt,reply_to_excerpt_truncated',
|
|
628
|
+
'RT-V-7 v_board_feed: the existing columns unchanged, the seven reply_to_* columns appended in order');
|
|
629
|
+
|
|
630
|
+
-- ===========================================================================
|
|
631
|
+
-- §8.6 B4: the policies frozen; the writer copy pinned to them
|
|
632
|
+
-- ===========================================================================
|
|
633
|
+
-- pg_get_expr qualifies a name only when its schema is off the search_path, and the texts were captured
|
|
634
|
+
-- with agent_board off it, so the comparison runs with agent_board off it too.
|
|
635
|
+
set local search_path = extensions, public;
|
|
636
|
+
-- RT-B4-1. The expected texts were captured from a replay of origin/main ad2a913b4 (throwaway CI lab
|
|
637
|
+
-- run 46345569332, pg_get_expr dump), never hand-typed.
|
|
638
|
+
select is(
|
|
639
|
+
(select pg_get_expr(polqual, polrelid) from pg_policy
|
|
640
|
+
where polrelid = 'agent_board.messages'::regclass and polname = 'agent_board_authenticated_select_messages'),
|
|
641
|
+
$p$((author_agent_id = agent_board.current_agent_id()) OR (EXISTS ( SELECT 1
|
|
642
|
+
FROM agent_board.delivery_recipients dr
|
|
643
|
+
WHERE ((dr.source_message_id = messages.id) AND ((dr.recipient_agent_id = agent_board.current_agent_id()) OR ((dr.board_id IS NOT NULL) AND (EXISTS ( SELECT 1
|
|
644
|
+
FROM agent_board.board_memberships bm
|
|
645
|
+
WHERE ((bm.board_id = dr.board_id) AND (bm.agent_id = agent_board.current_agent_id()) AND (bm.membership_state = ANY (ARRAY['joined'::text, 'subscribed'::text])))))))))))$p$,
|
|
646
|
+
'RT-B4-1 authenticated: agent_board_authenticated_select_messages polqual is exactly today''s');
|
|
647
|
+
select is(
|
|
648
|
+
(select pg_get_expr(polqual, polrelid) from pg_policy
|
|
649
|
+
where polrelid = 'agent_board.messages'::regclass and polname = 'agent_board_delegate_select_messages'),
|
|
650
|
+
$p$(EXISTS ( SELECT 1
|
|
651
|
+
FROM agent_board.delivery_recipients dr
|
|
652
|
+
WHERE ((dr.source_message_id = messages.id) AND (dr.board_id = ANY (COALESCE(( SELECT agent_board._delegate_visible_board_ids() AS _delegate_visible_board_ids), '{}'::uuid[]))))))$p$,
|
|
653
|
+
'RT-B4-1 delegate: agent_board_delegate_select_messages polqual is exactly today''s (delegate_role_class assertion 17''s messages line)');
|
|
654
|
+
select is(
|
|
655
|
+
(select string_agg(polname || ' cmd=' || polcmd::text || ' permissive=' || polpermissive::text || ' roles='
|
|
656
|
+
|| (select string_agg(case when x = 0 then 'public' else x::regrole::text end, ',' order by 1) from unnest(polroles) x)
|
|
657
|
+
|| ' check=' || (polwithcheck is not null)::text, '; ' order by polname)
|
|
658
|
+
from pg_policy where polrelid = 'agent_board.messages'::regclass),
|
|
659
|
+
'agent_board_authenticated_select_messages cmd=r permissive=true roles=authenticated check=false; '
|
|
660
|
+
|| 'agent_board_delegate_select_messages cmd=r permissive=true roles=agent_board_delegate check=false; '
|
|
661
|
+
|| 'agent_board_service_all_messages cmd=* permissive=true roles=service_role check=true',
|
|
662
|
+
'RT-B4-1 census: messages has exactly these three permissive policies and no restrictive one');
|
|
663
|
+
-- RT-B4-1 sub-reads (panel #921 R1 F1). The writer copy equals the messages policies only because of
|
|
664
|
+
-- what those policies SUB-READ: the authenticated policy reads delivery_recipients and, through it,
|
|
665
|
+
-- board_memberships under the reader's RLS; the delegate policy reads delivery_recipients and the
|
|
666
|
+
-- board set from _delegate_visible_board_ids(). Each is frozen here, exactly as on origin/main
|
|
667
|
+
-- ad2a913b4 (same lab dump), so narrowing any of them turns this red and forces a re-review of the
|
|
668
|
+
-- writer copy, as RT-B4-1 does for the messages policies themselves.
|
|
669
|
+
select is(
|
|
670
|
+
(select pg_get_expr(polqual, polrelid) from pg_policy
|
|
671
|
+
where polrelid = 'agent_board.delivery_recipients'::regclass and polname = 'agent_board_authenticated_select_delivery_recipients'),
|
|
672
|
+
$p$((recipient_agent_id = agent_board.current_agent_id()) OR ((board_id IS NOT NULL) AND (EXISTS ( SELECT 1
|
|
673
|
+
FROM agent_board.board_memberships bm
|
|
674
|
+
WHERE ((bm.board_id = delivery_recipients.board_id) AND (bm.agent_id = agent_board.current_agent_id()) AND (bm.membership_state = ANY (ARRAY['joined'::text, 'subscribed'::text])))))))$p$,
|
|
675
|
+
'RT-B4-1 sub-read: agent_board_authenticated_select_delivery_recipients polqual is exactly today''s');
|
|
676
|
+
select is(
|
|
677
|
+
(select pg_get_expr(polqual, polrelid) from pg_policy
|
|
678
|
+
where polrelid = 'agent_board.delivery_recipients'::regclass and polname = 'agent_board_delegate_select_delivery_recipients'),
|
|
679
|
+
$p$(board_id = ANY (COALESCE(( SELECT agent_board._delegate_visible_board_ids() AS _delegate_visible_board_ids), '{}'::uuid[])))$p$,
|
|
680
|
+
'RT-B4-1 sub-read: agent_board_delegate_select_delivery_recipients polqual is exactly today''s');
|
|
681
|
+
select is(
|
|
682
|
+
(select pg_get_expr(polqual, polrelid) from pg_policy
|
|
683
|
+
where polrelid = 'agent_board.board_memberships'::regclass and polname = 'agent_board_authenticated_select_board_memberships'),
|
|
684
|
+
$p$(agent_id = agent_board.current_agent_id())$p$,
|
|
685
|
+
'RT-B4-1 sub-read: agent_board_authenticated_select_board_memberships polqual is exactly today''s');
|
|
686
|
+
select is(
|
|
687
|
+
(select string_agg(polrelid::regclass::text || '.' || polname || ' cmd=' || polcmd::text || ' permissive=' || polpermissive::text || ' roles='
|
|
688
|
+
|| (select string_agg(case when x = 0 then 'public' else x::regrole::text end, ',' order by 1) from unnest(polroles) x),
|
|
689
|
+
'; ' order by polrelid::regclass::text, polname)
|
|
690
|
+
from pg_policy where polrelid in ('agent_board.delivery_recipients'::regclass, 'agent_board.board_memberships'::regclass)),
|
|
691
|
+
'agent_board.board_memberships.agent_board_authenticated_select_board_memberships cmd=r permissive=true roles=authenticated; '
|
|
692
|
+
|| 'agent_board.board_memberships.agent_board_delegate_select_board_memberships cmd=r permissive=true roles=agent_board_delegate; '
|
|
693
|
+
|| 'agent_board.board_memberships.agent_board_service_all_board_memberships cmd=* permissive=true roles=service_role; '
|
|
694
|
+
|| 'agent_board.delivery_recipients.agent_board_authenticated_select_delivery_recipients cmd=r permissive=true roles=authenticated; '
|
|
695
|
+
|| 'agent_board.delivery_recipients.agent_board_delegate_select_delivery_recipients cmd=r permissive=true roles=agent_board_delegate; '
|
|
696
|
+
|| 'agent_board.delivery_recipients.agent_board_service_all_delivery_recipients cmd=* permissive=true roles=service_role',
|
|
697
|
+
'RT-B4-1 sub-read census: delivery_recipients and board_memberships carry exactly these permissive policies and no restrictive one');
|
|
698
|
+
-- _delegate_visible_board_ids() is a definer, so the delegate's board set is read unfiltered; its
|
|
699
|
+
-- body is pinned by md5 of prosrc (the body text 20261008000100:500-504 created), with its definer
|
|
700
|
+
-- flag and empty search_path.
|
|
701
|
+
select is(
|
|
702
|
+
(select format('definer=%s config=%s body_md5=%s', p.prosecdef::text, array_to_string(p.proconfig, ','), md5(p.prosrc))
|
|
703
|
+
from pg_proc p where p.oid = to_regprocedure('agent_board._delegate_visible_board_ids()')),
|
|
704
|
+
'definer=true config=search_path="" body_md5=0a970d38b18a0a234a761cc6073c9513',
|
|
705
|
+
'RT-B4-1 sub-read: _delegate_visible_board_ids() is the same definer with the same body as today');
|
|
706
|
+
set local search_path = extensions, public, agent_board;
|
|
707
|
+
|
|
708
|
+
-- RT-B4-2: a definer with a pinned search_path, whose only caller is the primitive.
|
|
709
|
+
select is(
|
|
710
|
+
(select format('definer=%s search_path=%s callers=%s', p.prosecdef::text,
|
|
711
|
+
(exists (select 1 from unnest(p.proconfig) g where g like 'search\_path=%'))::text,
|
|
712
|
+
(select string_agg(q.oid::regprocedure::text, ', ' order by q.oid::regprocedure::text) from pg_proc q
|
|
713
|
+
where q.pronamespace = 'agent_board'::regnamespace and q.oid <> p.oid
|
|
714
|
+
and agent_board_test.stripped_body(q.oid) ~ agent_board_test.call_re('_actor_can_read_message')))
|
|
715
|
+
from pg_proc p where p.oid = to_regprocedure('agent_board._actor_can_read_message(uuid,uuid)')),
|
|
716
|
+
'definer=true search_path=true callers=_insert_thread_message(uuid,uuid,text,text,uuid,jsonb,uuid)',
|
|
717
|
+
'RT-B4-2: _actor_can_read_message is a SECURITY DEFINER with a pinned search_path, called only by _insert_thread_message');
|
|
718
|
+
|
|
719
|
+
-- RT-B4-EQ: writer copy, policy (a read under the agent's own claims) and the frozen oracle agree on
|
|
720
|
+
-- every (agent, message) pair. The oracle is today's two predicates, transcribed once here and
|
|
721
|
+
-- evaluated as the owner with the agent bound.
|
|
722
|
+
create function pg_temp.oracle(p_agent uuid, p_msg uuid, p_is_delegate boolean) returns boolean language sql stable as $$
|
|
723
|
+
select case when p_is_delegate then
|
|
724
|
+
exists (select 1 from agent_board.delivery_recipients dr
|
|
725
|
+
where dr.source_message_id = p_msg
|
|
726
|
+
and dr.board_id = any (coalesce((select coalesce(array_agg(bm.board_id order by bm.board_id), '{}'::uuid[])
|
|
727
|
+
from agent_board.board_memberships bm
|
|
728
|
+
where bm.agent_id = p_agent and bm.membership_state in ('joined', 'subscribed')), '{}'::uuid[])))
|
|
729
|
+
else
|
|
730
|
+
(select m.author_agent_id = p_agent from agent_board.messages m where m.id = p_msg)
|
|
731
|
+
or exists (select 1 from agent_board.delivery_recipients dr
|
|
732
|
+
where dr.source_message_id = p_msg
|
|
733
|
+
and (dr.recipient_agent_id = p_agent
|
|
734
|
+
or (dr.board_id is not null
|
|
735
|
+
and exists (select 1 from agent_board.board_memberships bm
|
|
736
|
+
where bm.board_id = dr.board_id and bm.agent_id = p_agent
|
|
737
|
+
and bm.membership_state in ('joined', 'subscribed')))))
|
|
738
|
+
end
|
|
739
|
+
$$;
|
|
740
|
+
create temp table _eq(agent text, msg text, writer boolean, policy boolean, oracle boolean);
|
|
741
|
+
do $$
|
|
742
|
+
declare
|
|
743
|
+
a text; m text; v_aid uuid; v_mid uuid; v_del boolean; v_pol boolean; v_role text; v_sub uuid; v_cred text;
|
|
744
|
+
begin
|
|
745
|
+
foreach a in array array['A', 'B', 'C', 'I', 'R', 'L', 'Q', 'H', 'Y1', 'N'] loop
|
|
746
|
+
v_aid := pg_temp.u(a); v_role := pg_temp.v(a || '.role'); v_sub := pg_temp.u(a || '.auth'); v_cred := pg_temp.v(a || '.cred');
|
|
747
|
+
v_del := (select is_delegate from agent_board.agents where id = v_aid);
|
|
748
|
+
foreach m in array array['M1', 'M2', 'D1', 'MC1', 'MY', 'DH'] loop
|
|
749
|
+
v_mid := pg_temp.u(m);
|
|
750
|
+
perform pg_temp.ident(v_role, v_sub, v_cred);
|
|
751
|
+
execute format('set local role %I', v_role);
|
|
752
|
+
execute 'select exists (select 1 from agent_board.messages where id = $1)' into v_pol using v_mid;
|
|
753
|
+
reset role;
|
|
754
|
+
perform pg_temp.ident(null, null, null);
|
|
755
|
+
insert into _eq values (a, m, agent_board._actor_can_read_message(v_aid, v_mid), v_pol, pg_temp.oracle(v_aid, v_mid, v_del));
|
|
756
|
+
end loop;
|
|
757
|
+
end loop;
|
|
758
|
+
end $$;
|
|
759
|
+
select is(
|
|
760
|
+
(select string_agg(format('(%s,%s,writer=%s,policy=%s,oracle=%s)', agent, msg, writer, policy, oracle), ' ' order by agent, msg)
|
|
761
|
+
from _eq where writer is distinct from policy or policy is distinct from oracle),
|
|
762
|
+
null,
|
|
763
|
+
'RT-B4-EQ: the writer copy, the policy and the frozen oracle agree on every (agent, message) fixture pair');
|
|
764
|
+
select is((select count(*) || ' pairs, ' || count(*) filter (where policy) || ' readable' from _eq),
|
|
765
|
+
'60 pairs, 16 readable',
|
|
766
|
+
'RT-B4-EQ control: 10 agents x 6 messages were compared, 16 of the pairs readable (A 5, B 4, C 2, H 2, N 2, Y1 1; I, R, L, Q none)');
|
|
767
|
+
|
|
768
|
+
-- RT-VIS: named visibility rows under each reader's own claims (policy side).
|
|
769
|
+
create function pg_temp.sees(p_who text, p_msg text) returns text language sql as $$
|
|
770
|
+
select pg_temp.val(p_who, format('select count(*) from agent_board.messages where id = %L', pg_temp.v(p_msg))) $$;
|
|
771
|
+
select is(pg_temp.sees('I', 'M1'), '0', 'RT-VIS-1 I: an invited member cannot read M1');
|
|
772
|
+
select is(pg_temp.sees('R', 'M1'), '0', 'RT-VIS-1 R: a requested member cannot read M1');
|
|
773
|
+
select is(pg_temp.sees('L', 'M1'), '0', 'RT-VIS-1 L: a member who left cannot read M1');
|
|
774
|
+
select is(pg_temp.sees('Q', 'M1'), '0', 'RT-VIS-2: a non-member cannot read M1');
|
|
775
|
+
select is(pg_temp.sees('C', 'D1'), '0', 'RT-VIS-3: the unrelated member C cannot read A''s DM to B');
|
|
776
|
+
select is(pg_temp.sees('H', 'D1'), '0', 'RT-VIS-H1: the delegate H cannot read A''s DM to B');
|
|
777
|
+
select is(pg_temp.sees('H', 'MY'), '0', 'RT-VIS-H2: the delegate H cannot read MY on Y, where it is not joined');
|
|
778
|
+
select is(pg_temp.sees('B', 'M1') || '/' || pg_temp.sees('B', 'D1'), '1/1', 'RT-VIS-P: B reads M1 and D1 (positive control)');
|
|
779
|
+
select is(pg_temp.sees('H', 'M1'), '1', 'RT-VIS-PH: the delegate H reads M1 (positive control)');
|
|
780
|
+
select is(pg_temp.sees('H', 'DH') || '/' || pg_temp.sees('N', 'M1'), '0/1',
|
|
781
|
+
'RT-VIS-DH: the delegate H cannot read DH (a private delivery only), and the late joiner N reads M1 (board-member branch)');
|
|
782
|
+
-- RT-B4-D: the writer agrees for the delegate: H's reply to DH is refused (the delegate predicate,
|
|
783
|
+
-- not the authenticated one, decides for a delegate).
|
|
784
|
+
select is(pg_temp.as_('H', pg_temp.pbm('T', 'X', 'RT-B4-D delegate reply to DH', 'DH')), '22023:reply_to_message_id not found in thread',
|
|
785
|
+
'RT-B4-D: the delegate H cannot reply to DH, which only a private delivery names it on');
|
|
786
|
+
|
|
787
|
+
-- RA-NULL (NIT-4): a NULL readability fails closed. The function is replaced to return NULL inside
|
|
788
|
+
-- an always-rolled-back subtransaction; the restored definition is checked after.
|
|
789
|
+
create function pg_temp.ra_null() returns text language plpgsql as $$
|
|
790
|
+
declare v text; v_before text := md5(coalesce(pg_get_functiondef(to_regprocedure('agent_board._actor_can_read_message(uuid,uuid)')), ''));
|
|
791
|
+
begin
|
|
792
|
+
begin
|
|
793
|
+
create or replace function agent_board._actor_can_read_message(p_actor_agent_id uuid, p_message_id uuid)
|
|
794
|
+
returns boolean language sql stable security definer set search_path = '' as $f$ select null::boolean $f$;
|
|
795
|
+
v := pg_temp.as_('B', pg_temp.pm('T', 'RA-NULL reply', 'M1'));
|
|
796
|
+
raise exception using errcode = 'P0655', message = 'ra_null rollback';
|
|
797
|
+
exception when sqlstate 'P0655' then null;
|
|
798
|
+
when others then v := 'ERR ' || sqlstate || ':' || sqlerrm; -- fail the row, never abort the file
|
|
799
|
+
end;
|
|
800
|
+
return v || ' restored=' || (md5(coalesce(pg_get_functiondef(to_regprocedure('agent_board._actor_can_read_message(uuid,uuid)')), '')) = v_before)::text;
|
|
801
|
+
end $$;
|
|
802
|
+
select is(pg_temp.ra_null(), '22023:reply_to_message_id not found in thread restored=true',
|
|
803
|
+
'RA-NULL: when readability is NULL the reply is refused with the single 22023 (fails closed)');
|
|
804
|
+
|
|
805
|
+
-- RT-B4-3: callable by no API role; ACL non-null, no PUBLIC grantee.
|
|
806
|
+
select is(
|
|
807
|
+
(select format('acl_null=%s public=%s api=%s', (p.proacl is null)::text,
|
|
808
|
+
(exists (select 1 from aclexplode(p.proacl) x where x.grantee = 0))::text,
|
|
809
|
+
coalesce((select string_agg(r, ',' order by r) from unnest(array['anon', 'authenticated', 'service_role', 'agent_board_delegate']) r
|
|
810
|
+
where has_function_privilege(r, p.oid, 'execute')), 'none'))
|
|
811
|
+
from pg_proc p where p.oid = to_regprocedure('agent_board._actor_can_read_message(uuid,uuid)')),
|
|
812
|
+
'acl_null=false public=false api=none',
|
|
813
|
+
'RT-B4-3: _actor_can_read_message has an explicit ACL, no PUBLIC grant, and no API role can execute it');
|
|
814
|
+
|
|
815
|
+
-- ===========================================================================
|
|
816
|
+
-- §8.8 ACL and COMMENT
|
|
817
|
+
-- ===========================================================================
|
|
818
|
+
create temp table _acl(sig text primary key, roles text);
|
|
819
|
+
insert into _acl values
|
|
820
|
+
('agent_board._actor_can_read_message(uuid,uuid)', 'none'),
|
|
821
|
+
('agent_board._insert_thread_message(uuid,uuid,text,text,uuid,jsonb,uuid)', 'none'),
|
|
822
|
+
('agent_board.post_message(uuid,uuid,text,text,uuid,jsonb,uuid)', 'agent_board_delegate,authenticated,service_role'),
|
|
823
|
+
('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid)', 'agent_board_delegate,authenticated,service_role'),
|
|
824
|
+
('agent_board.post_board_message(uuid,uuid,text,text,text,text,text,text,text,jsonb,uuid)', 'agent_board_delegate,authenticated,service_role'),
|
|
825
|
+
('agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid)', 'authenticated,service_role'),
|
|
826
|
+
('agent_board.send_direct_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb,uuid)', 'authenticated,service_role'),
|
|
827
|
+
('agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb,uuid)', 'authenticated,service_role'),
|
|
828
|
+
('agent_board.send_multicast_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb,uuid)', 'authenticated,service_role'),
|
|
829
|
+
('agent_board.signed_post_board_message(jsonb,jsonb)', 'service_role'),
|
|
830
|
+
('agent_board.signed_send_direct_message(jsonb,jsonb)', 'service_role'),
|
|
831
|
+
('agent_board.signed_send_multicast_message(jsonb,jsonb)', 'service_role');
|
|
832
|
+
select is(
|
|
833
|
+
(select string_agg(a.sig || ' want=' || a.roles || ' have=' || coalesce(h.roles, 'MISSING'), '; ' order by a.sig)
|
|
834
|
+
from _acl a
|
|
835
|
+
left join lateral (select coalesce((select string_agg(r, ',' order by r) from unnest(array['anon', 'authenticated', 'service_role', 'agent_board_delegate']) r
|
|
836
|
+
where has_function_privilege(r, p.oid, 'execute')), 'none') as roles
|
|
837
|
+
from pg_proc p where p.oid = to_regprocedure(a.sig)) h on true
|
|
838
|
+
where h.roles is distinct from a.roles),
|
|
839
|
+
null,
|
|
840
|
+
'RT-ACL-1: every function this migration created or replaced is executable by exactly its recorded API roles');
|
|
841
|
+
select is(
|
|
842
|
+
(select string_agg(a.sig || case when p.oid is null then ' MISSING' when p.proacl is null then ' ACL NULL (= PUBLIC)' else ' PUBLIC' end, '; ' order by a.sig)
|
|
843
|
+
from _acl a left join pg_proc p on p.oid = to_regprocedure(a.sig)
|
|
844
|
+
where p.oid is null or p.proacl is null or exists (select 1 from aclexplode(p.proacl) x where x.grantee = 0)),
|
|
845
|
+
null,
|
|
846
|
+
'RT-ACL-2: each has an explicit ACL (checked before aclexplode) with no PUBLIC grantee');
|
|
847
|
+
create temp table _cmt(sig text primary key, cmt text);
|
|
848
|
+
insert into _cmt values
|
|
849
|
+
('agent_board._actor_can_read_message(uuid, uuid)', '#655 (§6.2): the writer''s ONE copy of the two messages SELECT policies, for a named actor: the delegate policy when the actor is a hosted delegate, the authenticated policy otherwise. NULL for a missing message or actor. Called only by _insert_thread_message with the session-derived actor, which refuses unless the result is true. EXECUTE to no API role (it would be a cross-agent readability oracle). RT-B4-1 freezes the policies it copies; RT-B4-EQ pins that it agrees with them.'),
|
|
850
|
+
('agent_board._insert_thread_message(uuid, uuid, text, text, uuid, jsonb, uuid)', 'Internal authorized message insertion; preserves sequence locking, session author and hosted attribution. Delivery is the caller responsibility. #655: p_reply_to_message_id must name a message of the same thread that the session-derived actor can read (_actor_can_read_message is true); otherwise 22023 reply_to_message_id not found in thread.'),
|
|
851
|
+
('agent_board.post_message(uuid, uuid, text, text, uuid, jsonb, uuid)', 'Posts a thread reply; inherited board threads use canonical board delivery; non-board and restricted threads retain insertion-only behavior. Session-derived author and return shape are unchanged. #655: p_reply_to_message_id (optional) names the message this one replies to; it must be in the same thread and readable by the author, else 22023 reply_to_message_id not found in thread.'),
|
|
852
|
+
('agent_board.post_board_message(uuid, uuid, uuid, text, text, text, text, text, jsonb, uuid)', 'Creates or reuses a canonical board post after validating the session actor, board authority and locked thread association; delivery fan-out and downstream effects are transactional. Same-key calls serialize on (thread, actor, key), whatever the board. A keyed session send that inserts records the author-scoped receipt get_send_outcome reads (#670). #655: p_reply_to_message_id (optional) names a same-thread parent the author can read, else 22023 reply_to_message_id not found in thread; a same-key replay ignores it, as it ignores p_body.'),
|
|
853
|
+
('agent_board.post_board_message(uuid, uuid, text, text, text, text, text, text, text, jsonb, uuid)', 'By-slug board post (addressing_v1): resolves an active board by slug and forwards every argument by name to the board-id overload, including #655 p_reply_to_message_id.'),
|
|
854
|
+
('agent_board.send_direct_message(uuid, uuid, uuid, text, text, text, text, text, jsonb, uuid)', 'Sends privately to the explicit recipient, including on board threads; uses the common authorized insertion primitive. A same-key replay cannot widen the message''s audience: it is refused (42501) unless the recipient already holds a private direct delivery of the source message. A keyed session send that inserts records the author-scoped receipt get_send_outcome reads (#670). #655: p_reply_to_message_id (optional) names a same-thread parent the author can read, else 22023 reply_to_message_id not found in thread; a same-key replay ignores it.'),
|
|
855
|
+
('agent_board.send_direct_message(uuid, uuid, jsonb, text, text, text, text, text, text, jsonb, uuid)', 'Direct message by recipient descriptor (addressing_v1): resolves exactly one recipient and forwards every argument by name to the uuid overload, including #655 p_reply_to_message_id.'),
|
|
856
|
+
('agent_board.send_multicast_message(uuid, uuid, uuid[], text, text, text, text, text, jsonb, uuid)', 'Sends privately to explicit recipients, including on board threads; uses the common authorized insertion primitive. A same-key replay cannot widen the message''s audience: it is refused (42501) unless every requested recipient already holds a private multicast delivery of the source message. A keyed session send that inserts records the author-scoped receipt get_send_outcome reads (#670). #655: p_reply_to_message_id (optional) names a same-thread parent the author can read, else 22023 reply_to_message_id not found in thread; a same-key replay ignores it.'),
|
|
857
|
+
('agent_board.send_multicast_message(uuid, uuid, jsonb, text, text, text, text, text, text, jsonb, uuid)', 'Multicast by recipient descriptors (addressing_v1): resolves the recipients and forwards every argument by name to the uuid[] overload, including #655 p_reply_to_message_id.'),
|
|
858
|
+
('agent_board.signed_post_board_message(jsonb, jsonb)', 'Signed-write gateway wrapper (service_role only): post_board_message as the gateway-decided actor, recording signature evidence. #655: the payload key p_reply_to_message_id is forwarded by name.'),
|
|
859
|
+
('agent_board.signed_send_direct_message(jsonb, jsonb)', 'Signed-write gateway wrapper (service_role only): send_direct_message as the gateway-decided actor, recording signature evidence. #655: the payload key p_reply_to_message_id is forwarded by name.'),
|
|
860
|
+
('agent_board.signed_send_multicast_message(jsonb, jsonb)', 'Signed-write gateway wrapper (service_role only): send_multicast_message as the gateway-decided actor, by recipient ids or descriptors, recording signature evidence. #655: the payload key p_reply_to_message_id is forwarded by name on BOTH branches.');
|
|
861
|
+
select is(
|
|
862
|
+
(select string_agg(c.sig || case when p.oid is null then ' MISSING' else ' comment differs' end, '; ' order by c.sig)
|
|
863
|
+
from _cmt c left join pg_proc p on p.oid = to_regprocedure(c.sig)
|
|
864
|
+
where p.oid is null or obj_description(p.oid, 'pg_proc') is distinct from c.cmt),
|
|
865
|
+
null,
|
|
866
|
+
'RT-ACL-3: every created or replaced function carries exactly the migration''s COMMENT');
|
|
867
|
+
select is(
|
|
868
|
+
(select string_agg(signature, ', ' order by signature) from agent_board.delegate_allowed_operations
|
|
869
|
+
where kind = 'rpc' and name in ('post_message', 'post_board_message')),
|
|
870
|
+
'post_board_message(uuid, uuid, text, text, text, text, text, text, text, jsonb, uuid), post_board_message(uuid, uuid, uuid, text, text, text, text, text, jsonb, uuid), post_message(uuid, uuid, text, text, uuid, jsonb, uuid)',
|
|
871
|
+
'RT-ACL-4: the delegate allow set holds the three new post_* signatures and none of the old ones');
|
|
872
|
+
|
|
873
|
+
select * from finish();
|
|
874
|
+
rollback;
|