@consiliency/agent-board-schema 1.74.0 → 1.75.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fixtures/already-applied.json +4 -0
- package/migrations/20261108000100_agent_board_send_outcome.sql +708 -0
- package/package.json +1 -1
- package/schema-manifest.json +12 -3
- package/src/deployment-drift-main.test.mjs +20 -3
- package/src/deployment-drift-script.test.mjs +77 -2
- package/src/deployment-drift.test.mjs +60 -0
- package/src/tooling.test.mjs +3 -2
- package/tests/agent_board_667_history_class.test.sql +2 -0
- package/tests/agent_board_717_native_surface.test.sql +223 -0
- package/tests/agent_board_delegate_role_class.test.sql +2 -2
- package/tests/agent_board_delivery_writer_execute.test.sql +4 -2
- package/tests/agent_board_hosted_delegates.test.sql +8 -1
- package/tests/agent_board_send_outcome.test.sql +805 -0
- package/tests/agent_board_write_enforcement_class.test.sql +9 -5
- package/tests/catalog/function_execute_privileges.json +24 -0
- package/tests/catalog/p_metadata_parameters.json +8 -0
- package/tests/fixtures/isolation-substrate.sql +8 -0
- package/tests/fixtures/pgtap-unexposed.sql +61 -0
- package/tests/isolation/expected/send_outcome_cross_board.out +40 -0
- package/tests/isolation/expected/send_outcome_independence.out +295 -0
- package/tests/isolation/expected/send_outcome_membership.out +140 -0
- package/tests/isolation/expected/send_outcome_overlap.out +89 -0
- package/tests/isolation/expected/send_outcome_reader.out +87 -0
- package/tests/isolation/expected/send_outcome_recovery.out +107 -0
- package/tests/isolation/specs/send_outcome_cross_board.spec +66 -0
- package/tests/isolation/specs/send_outcome_independence.spec +95 -0
- package/tests/isolation/specs/send_outcome_membership.spec +73 -0
- package/tests/isolation/specs/send_outcome_overlap.spec +69 -0
- package/tests/isolation/specs/send_outcome_reader.spec +61 -0
- package/tests/isolation/specs/send_outcome_recovery.spec +68 -0
- package/tests/mutations/670/E1_helper_author_from_message.sql +7 -0
- package/tests/mutations/670/E2_helper_no_null_check.sql +13 -0
- package/tests/mutations/670/M10_lock_removed.sql +10 -0
- package/tests/mutations/670/M11_table_granted_to_authenticated.sql +2 -0
- package/tests/mutations/670/M12_helper_granted_to_service_role.sql +2 -0
- package/tests/mutations/670/M13_message_fk_no_cascade.sql +8 -0
- package/tests/mutations/670/M14_post_message_mints_from_metadata.sql +11 -0
- package/tests/mutations/670/M15_second_receipt_writer.sql +9 -0
- package/tests/mutations/670/M16_read_no_btrim.sql +7 -0
- package/tests/mutations/670/M17_read_volatile.sql +2 -0
- package/tests/mutations/670/M1_helper_no_service_role_guard.sql +13 -0
- package/tests/mutations/670/M3_helper_on_replay_branch.sql +10 -0
- package/tests/mutations/670/M4_read_checks_thread_existence.sql +10 -0
- package/tests/mutations/670/M5a_read_requires_thread_access.sql +10 -0
- package/tests/mutations/670/M5b_read_requires_board_membership.sql +11 -0
- package/tests/mutations/670/M6_read_admits_service_role.sql +14 -0
- package/tests/mutations/670/M7_confirmed_adds_message_sequence.sql +12 -0
- package/tests/mutations/670/M8_delivery_swallows_errors.sql +19 -0
- package/tests/mutations/670/M9_lock_key_includes_board.sql +10 -0
- package/tests/mutations/670/run.sh +84 -0
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
-- #670 SENDOUTCOME: a durable, private, author-scoped success receipt for a keyed session send,
|
|
2
|
+
-- and get_send_outcome(), the read that recovers it after a lost response.
|
|
3
|
+
--
|
|
4
|
+
-- Ruling: plans/SENDOUTCOME-670-plan.md (D1-D15). In short:
|
|
5
|
+
-- * agent_board.send_outcomes holds ONE row per message a keyed session send inserted: the
|
|
6
|
+
-- operation, the SESSION author, the thread, the trimmed key, the message id, the board (board
|
|
7
|
+
-- posts only) and the message's created_at. No body, subject, recipients, counts, metadata or
|
|
8
|
+
-- digest. RLS on, zero policies, no API-role privilege. Its lifetime is the message's: the row
|
|
9
|
+
-- cascades from agent_board.messages (and so from threads). No TTL, pruning or erasure path.
|
|
10
|
+
-- * agent_board._record_send_outcome() is its only writer. It takes no author argument (the
|
|
11
|
+
-- author is current_agent_id()), records nothing when _is_service_role(), and is a plain INSERT
|
|
12
|
+
-- so a duplicate surfaces as a 23505 defect. It is called only by the three UUID cores, only on
|
|
13
|
+
-- the branch that inserted the message in this call, only under a key, after delivery and
|
|
14
|
+
-- immediately before `return query`. Replays never record.
|
|
15
|
+
-- * agent_board.get_send_outcome() is STABLE, SECURITY DEFINER, and touches only send_outcomes:
|
|
16
|
+
-- no existence, membership or access check, so every miss is the same byte-identical
|
|
17
|
+
-- 'unknown'. A missing receipt never means "not sent".
|
|
18
|
+
--
|
|
19
|
+
-- #717 F2/F2b replay semantics are unchanged (D2): the three cores below are this tree's current
|
|
20
|
+
-- definitions -- post_board_message from 20261104065658_agent_board_reply_delivery.sql,
|
|
21
|
+
-- send_direct_message and send_multicast_message from 20261104190000_agent_board_message_replay_scope.sql
|
|
22
|
+
-- -- with only the receipt call (and its v_inserted flag) added. The one other writer change is
|
|
23
|
+
-- D6: post_board_message's idempotency lock no longer includes the board, so it matches its own
|
|
24
|
+
-- lookup (thread, author, key) and the DM / multicast / tasking lock domains. Without it two
|
|
25
|
+
-- concurrent same-key posts to different boards take different locks, both miss the uncommitted
|
|
26
|
+
-- first message, and the second hits the receipt's unique index as a raw 23505.
|
|
27
|
+
--
|
|
28
|
+
-- Limits, by design (D8): service_role, signed-gateway and Portal service-role sends never mint
|
|
29
|
+
-- (F1); keyed tasking is refused by the read (F2); recovery works only under the same agent id
|
|
30
|
+
-- (F5); coverage is prospective, so pre-upgrade sends and replays of legacy messages read 'unknown'.
|
|
31
|
+
|
|
32
|
+
-- =============================================================================
|
|
33
|
+
-- 1. Storage (D3)
|
|
34
|
+
-- =============================================================================
|
|
35
|
+
create table agent_board.send_outcomes (
|
|
36
|
+
id uuid primary key default gen_random_uuid(),
|
|
37
|
+
operation text not null
|
|
38
|
+
check (operation in ('post_board_message', 'send_direct_message', 'send_multicast_message')),
|
|
39
|
+
author_agent_id uuid not null references agent_board.agents(id),
|
|
40
|
+
thread_id uuid not null,
|
|
41
|
+
idempotency_key text not null
|
|
42
|
+
check (idempotency_key = btrim(idempotency_key) and idempotency_key <> ''),
|
|
43
|
+
message_id uuid not null unique references agent_board.messages(id) on delete cascade,
|
|
44
|
+
board_id uuid,
|
|
45
|
+
created_at timestamptz not null,
|
|
46
|
+
constraint send_outcomes_board_only_for_board_posts
|
|
47
|
+
check ((operation = 'post_board_message') = (board_id is not null)),
|
|
48
|
+
constraint send_outcomes_identity_key
|
|
49
|
+
unique (operation, author_agent_id, thread_id, idempotency_key)
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
alter table agent_board.send_outcomes enable row level security;
|
|
53
|
+
revoke all on table agent_board.send_outcomes from public, anon, authenticated, service_role, agent_board_delegate;
|
|
54
|
+
|
|
55
|
+
comment on table agent_board.send_outcomes is
|
|
56
|
+
'#670: private author-scoped send receipts. One row per message a keyed session send inserted (post_board_message, send_direct_message, send_multicast_message). Written only by _record_send_outcome, read only by get_send_outcome; RLS on with zero policies and no API-role privilege. Cascades from messages: a receipt lives exactly as long as its message.';
|
|
57
|
+
|
|
58
|
+
-- =============================================================================
|
|
59
|
+
-- 2. The only writer (D4)
|
|
60
|
+
-- =============================================================================
|
|
61
|
+
create or replace function agent_board._record_send_outcome(
|
|
62
|
+
p_operation text,
|
|
63
|
+
p_message_id uuid,
|
|
64
|
+
p_idempotency_key text,
|
|
65
|
+
p_board_id uuid
|
|
66
|
+
)
|
|
67
|
+
returns void
|
|
68
|
+
language plpgsql
|
|
69
|
+
security definer
|
|
70
|
+
set search_path = ''
|
|
71
|
+
as $$
|
|
72
|
+
declare
|
|
73
|
+
v_author uuid;
|
|
74
|
+
v_message record;
|
|
75
|
+
begin
|
|
76
|
+
-- service_role (and a direct postgres / supabase_admin session) acts for a caller-supplied
|
|
77
|
+
-- author the database cannot verify, so it never mints an author-readable receipt (D4, F1).
|
|
78
|
+
if agent_board._is_service_role() then
|
|
79
|
+
return;
|
|
80
|
+
end if;
|
|
81
|
+
|
|
82
|
+
v_author := agent_board.current_agent_id();
|
|
83
|
+
-- Defense in depth only: on the write path _resolved_actor_agent_id has already refused a
|
|
84
|
+
-- session without a principal, and bound the author to it.
|
|
85
|
+
if v_author is null then
|
|
86
|
+
raise exception 'send outcome requires a session principal' using errcode = '42501';
|
|
87
|
+
end if;
|
|
88
|
+
|
|
89
|
+
select m.author_agent_id, m.thread_id, m.created_at
|
|
90
|
+
into v_message
|
|
91
|
+
from agent_board.messages m
|
|
92
|
+
where m.id = p_message_id;
|
|
93
|
+
if not found or v_message.author_agent_id is distinct from v_author then
|
|
94
|
+
raise exception 'send outcome author must be the session principal' using errcode = '42501';
|
|
95
|
+
end if;
|
|
96
|
+
|
|
97
|
+
insert into agent_board.send_outcomes(
|
|
98
|
+
operation, author_agent_id, thread_id, idempotency_key, message_id, board_id, created_at
|
|
99
|
+
)
|
|
100
|
+
values (
|
|
101
|
+
p_operation,
|
|
102
|
+
v_author,
|
|
103
|
+
v_message.thread_id,
|
|
104
|
+
btrim(p_idempotency_key),
|
|
105
|
+
p_message_id,
|
|
106
|
+
case when p_operation = 'post_board_message' then p_board_id end,
|
|
107
|
+
v_message.created_at
|
|
108
|
+
);
|
|
109
|
+
end;
|
|
110
|
+
$$;
|
|
111
|
+
|
|
112
|
+
revoke all on function agent_board._record_send_outcome(text, uuid, text, uuid)
|
|
113
|
+
from public, anon, authenticated, service_role, agent_board_delegate;
|
|
114
|
+
comment on function agent_board._record_send_outcome(text, uuid, text, uuid) is
|
|
115
|
+
'#670: the only writer of send_outcomes. Author from current_agent_id(), never an argument; records nothing for service_role; plain INSERT (a duplicate is a 23505 defect). Called only by the three keyed UUID-core message writers on the branch that inserted the message. No API-role EXECUTE.';
|
|
116
|
+
|
|
117
|
+
-- =============================================================================
|
|
118
|
+
-- 3. The read (D5)
|
|
119
|
+
-- =============================================================================
|
|
120
|
+
create or replace function agent_board.get_send_outcome(
|
|
121
|
+
p_thread_id uuid,
|
|
122
|
+
p_idempotency_key text,
|
|
123
|
+
p_operation text default 'post_board_message'
|
|
124
|
+
)
|
|
125
|
+
returns table(
|
|
126
|
+
status text,
|
|
127
|
+
operation text,
|
|
128
|
+
thread_id uuid,
|
|
129
|
+
message_id uuid,
|
|
130
|
+
board_id uuid,
|
|
131
|
+
created_at timestamptz
|
|
132
|
+
)
|
|
133
|
+
language plpgsql
|
|
134
|
+
stable
|
|
135
|
+
security definer
|
|
136
|
+
set search_path = ''
|
|
137
|
+
as $$
|
|
138
|
+
declare
|
|
139
|
+
v_reader uuid;
|
|
140
|
+
v_key text;
|
|
141
|
+
v_row record;
|
|
142
|
+
begin
|
|
143
|
+
-- Step 1, the reader: an agent session, never service_role or a principal-less session.
|
|
144
|
+
if agent_board._is_service_role() then
|
|
145
|
+
raise exception 'get_send_outcome requires an agent session' using errcode = '42501';
|
|
146
|
+
end if;
|
|
147
|
+
v_reader := agent_board.current_agent_id();
|
|
148
|
+
if v_reader is null then
|
|
149
|
+
raise exception 'get_send_outcome requires an agent session' using errcode = '42501';
|
|
150
|
+
end if;
|
|
151
|
+
|
|
152
|
+
-- Step 2, the inputs. Keyed tasking is outside the receipt set and is refused, not 'unknown'.
|
|
153
|
+
if p_thread_id is null then
|
|
154
|
+
raise exception 'thread id is required' using errcode = '22023';
|
|
155
|
+
end if;
|
|
156
|
+
v_key := nullif(btrim(p_idempotency_key), '');
|
|
157
|
+
if v_key is null then
|
|
158
|
+
raise exception 'idempotency key is required' using errcode = '22023';
|
|
159
|
+
end if;
|
|
160
|
+
if p_operation is null
|
|
161
|
+
or p_operation not in ('post_board_message', 'send_direct_message', 'send_multicast_message') then
|
|
162
|
+
raise exception 'unsupported send outcome operation' using errcode = '22023';
|
|
163
|
+
end if;
|
|
164
|
+
|
|
165
|
+
-- Step 3, the lookup: the receipt table only. No thread, board or message existence check and no
|
|
166
|
+
-- access check, so a miss for any reason reads exactly like every other miss.
|
|
167
|
+
select so.operation, so.thread_id, so.message_id, so.board_id, so.created_at
|
|
168
|
+
into v_row
|
|
169
|
+
from agent_board.send_outcomes so
|
|
170
|
+
where so.operation = p_operation
|
|
171
|
+
and so.author_agent_id = v_reader
|
|
172
|
+
and so.thread_id = p_thread_id
|
|
173
|
+
and so.idempotency_key = v_key;
|
|
174
|
+
|
|
175
|
+
if not found then
|
|
176
|
+
return query select 'unknown'::text, null::text, null::uuid, null::uuid, null::uuid, null::timestamptz;
|
|
177
|
+
return;
|
|
178
|
+
end if;
|
|
179
|
+
|
|
180
|
+
return query select 'confirmed'::text, v_row.operation, v_row.thread_id, v_row.message_id, v_row.board_id, v_row.created_at;
|
|
181
|
+
end;
|
|
182
|
+
$$;
|
|
183
|
+
|
|
184
|
+
revoke all on function agent_board.get_send_outcome(uuid, text, text)
|
|
185
|
+
from public, anon, authenticated, service_role, agent_board_delegate;
|
|
186
|
+
grant execute on function agent_board.get_send_outcome(uuid, text, text) to authenticated, agent_board_delegate;
|
|
187
|
+
comment on function agent_board.get_send_outcome(uuid, text, text) is
|
|
188
|
+
'#670: recovers the outcome of the caller''s own keyed send after a lost response. confirmed = the send committed (message id, thread, board for board posts, created_at); unknown = no receipt for THIS session author, which never means not sent. Reads only send_outcomes. service_role and principal-less sessions are refused (42501); a null thread, blank key or an operation outside post_board_message/send_direct_message/send_multicast_message is 22023.';
|
|
189
|
+
|
|
190
|
+
-- The delegate allow set is the one list of what agent_board_delegate may execute, and its grant
|
|
191
|
+
-- loop (20261008000100) ran once, so the row and its grant are both issued here.
|
|
192
|
+
insert into agent_board.delegate_allowed_operations(kind, name, signature, reason) values
|
|
193
|
+
('rpc', 'get_send_outcome',
|
|
194
|
+
'get_send_outcome(uuid, text, text)',
|
|
195
|
+
'recover the outcome of the delegate''s own keyed board post (#670)');
|
|
196
|
+
|
|
197
|
+
-- =============================================================================
|
|
198
|
+
-- 4. The three keyed UUID cores: receipt call + D6 lock alignment
|
|
199
|
+
-- =============================================================================
|
|
200
|
+
CREATE OR REPLACE FUNCTION agent_board.post_board_message(p_thread_id uuid, p_author_agent_id uuid, p_board_id uuid, p_subject text, p_body text, p_message_type text DEFAULT 'comment'::text, p_priority text DEFAULT 'medium'::text, p_idempotency_key text DEFAULT NULL::text, p_metadata jsonb DEFAULT '{}'::jsonb)
|
|
201
|
+
RETURNS TABLE(message_id uuid, board_id uuid, delivered_recipient_count integer, callback_outbox_count integer, created_at timestamp with time zone)
|
|
202
|
+
LANGUAGE plpgsql
|
|
203
|
+
SECURITY DEFINER
|
|
204
|
+
SET search_path TO 'agent_board', 'public'
|
|
205
|
+
AS $function$
|
|
206
|
+
declare
|
|
207
|
+
v_message agent_board.messages%rowtype;
|
|
208
|
+
v_created_message record;
|
|
209
|
+
v_membership record;
|
|
210
|
+
v_recipient agent_board.delivery_recipients%rowtype;
|
|
211
|
+
v_inbox agent_board.inbox_items%rowtype;
|
|
212
|
+
v_delivered_count integer := 0;
|
|
213
|
+
v_callback_count integer := 0;
|
|
214
|
+
v_metadata jsonb;
|
|
215
|
+
v_preview text;
|
|
216
|
+
v_fetch_descriptor jsonb;
|
|
217
|
+
v_idempotency_key text := nullif(trim(p_idempotency_key), '');
|
|
218
|
+
v_actor_agent_id uuid;
|
|
219
|
+
v_inserted boolean := false;
|
|
220
|
+
v_is_author boolean;
|
|
221
|
+
begin
|
|
222
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_author_agent_id, 'p_author_agent_id');
|
|
223
|
+
|
|
224
|
+
if nullif(trim(p_subject), '') is null or nullif(trim(p_body), '') is null then
|
|
225
|
+
raise exception 'subject and body are required' using errcode = '22023';
|
|
226
|
+
end if;
|
|
227
|
+
if p_priority not in ('low', 'medium', 'high', 'urgent') then
|
|
228
|
+
raise exception 'invalid priority' using errcode = '22023';
|
|
229
|
+
end if;
|
|
230
|
+
if not exists (select 1 from agent_board.threads where id = p_thread_id) then
|
|
231
|
+
raise exception 'thread not found' using errcode = 'P0002';
|
|
232
|
+
end if;
|
|
233
|
+
if not exists (select 1 from agent_board.boards where id = p_board_id) then
|
|
234
|
+
raise exception 'board not found' using errcode = 'P0002';
|
|
235
|
+
end if;
|
|
236
|
+
if not agent_board._current_agent_can_access_thread(p_thread_id) then
|
|
237
|
+
raise exception 'thread not accessible to current agent' using errcode = '42501';
|
|
238
|
+
end if;
|
|
239
|
+
if not agent_board._current_agent_has_board_access(p_board_id) then
|
|
240
|
+
raise exception 'board not accessible to current agent' using errcode = '42501';
|
|
241
|
+
end if;
|
|
242
|
+
if not agent_board._current_agent_can_send_to_board(p_board_id) then
|
|
243
|
+
raise exception 'board send requires a non-observer board role' using errcode = '42501';
|
|
244
|
+
end if;
|
|
245
|
+
|
|
246
|
+
if agent_board._is_service_role() then
|
|
247
|
+
perform agent_board._record_send_authz_telemetry(
|
|
248
|
+
'service_role_bypass',
|
|
249
|
+
coalesce(nullif(p_metadata -> 'signed_write' ->> 'origin', ''), 'unknown'),
|
|
250
|
+
v_actor_agent_id,
|
|
251
|
+
p_board_id,
|
|
252
|
+
'post_board_message',
|
|
253
|
+
agent_board._resolve_send_fleet_scope(v_actor_agent_id),
|
|
254
|
+
null,
|
|
255
|
+
jsonb_build_object('redacted', true)
|
|
256
|
+
);
|
|
257
|
+
end if;
|
|
258
|
+
|
|
259
|
+
perform agent_board._lock_idempotency_key(
|
|
260
|
+
'post_board_message:' || p_thread_id::text || ':' || v_actor_agent_id::text,
|
|
261
|
+
v_idempotency_key
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
if p_board_id is not null then
|
|
265
|
+
perform agent_board._associate_empty_thread(p_thread_id, p_board_id);
|
|
266
|
+
-- Both checks are new statements after the thread-lock wait.
|
|
267
|
+
if not agent_board._current_agent_can_access_thread(p_thread_id) then
|
|
268
|
+
raise exception 'thread not accessible to current agent' using errcode = '42501';
|
|
269
|
+
end if;
|
|
270
|
+
if not agent_board._current_agent_has_board_access(p_board_id) then
|
|
271
|
+
raise exception 'board not accessible to current agent' using errcode = '42501';
|
|
272
|
+
end if;
|
|
273
|
+
if not agent_board._current_agent_can_send_to_board(p_board_id) then
|
|
274
|
+
raise exception 'board send requires a non-observer board role' using errcode = '42501';
|
|
275
|
+
end if;
|
|
276
|
+
end if;
|
|
277
|
+
|
|
278
|
+
if v_idempotency_key is not null then
|
|
279
|
+
select *
|
|
280
|
+
into v_message
|
|
281
|
+
from agent_board.messages
|
|
282
|
+
where thread_id = p_thread_id
|
|
283
|
+
and author_agent_id = v_actor_agent_id
|
|
284
|
+
and metadata ->> 'rpcName' = 'post_board_message'
|
|
285
|
+
and metadata ->> 'idempotencyKey' = v_idempotency_key
|
|
286
|
+
order by created_at desc, id desc
|
|
287
|
+
limit 1;
|
|
288
|
+
end if;
|
|
289
|
+
|
|
290
|
+
if v_message.id is null then
|
|
291
|
+
v_metadata := coalesce(p_metadata, '{}'::jsonb)
|
|
292
|
+
|| jsonb_build_object(
|
|
293
|
+
'rpcName', 'post_board_message',
|
|
294
|
+
'subject', trim(p_subject),
|
|
295
|
+
'priority', p_priority,
|
|
296
|
+
'boardId', p_board_id::text
|
|
297
|
+
);
|
|
298
|
+
if v_idempotency_key is not null then
|
|
299
|
+
v_metadata := v_metadata || jsonb_build_object('idempotencyKey', v_idempotency_key);
|
|
300
|
+
end if;
|
|
301
|
+
|
|
302
|
+
select *
|
|
303
|
+
into v_created_message
|
|
304
|
+
from agent_board._insert_thread_message(
|
|
305
|
+
p_thread_id,
|
|
306
|
+
v_actor_agent_id,
|
|
307
|
+
p_body,
|
|
308
|
+
p_message_type,
|
|
309
|
+
null,
|
|
310
|
+
v_metadata
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
select *
|
|
314
|
+
into v_message
|
|
315
|
+
from agent_board.messages
|
|
316
|
+
where id = v_created_message.message_id;
|
|
317
|
+
v_inserted := true;
|
|
318
|
+
end if;
|
|
319
|
+
|
|
320
|
+
select d.delivered_recipient_count, d.callback_outbox_count
|
|
321
|
+
into v_delivered_count, v_callback_count
|
|
322
|
+
from agent_board._deliver_board_message(v_message.id, trim(p_subject), p_body, p_priority) d;
|
|
323
|
+
|
|
324
|
+
-- #670: the author-scoped send receipt. Only a message inserted by THIS call, under a key, after
|
|
325
|
+
-- delivery and immediately before returning; replays never record. The helper derives the author
|
|
326
|
+
-- from the session and records nothing for service_role.
|
|
327
|
+
if v_inserted and v_idempotency_key is not null then
|
|
328
|
+
perform agent_board._record_send_outcome('post_board_message', v_message.id, v_idempotency_key, p_board_id);
|
|
329
|
+
end if;
|
|
330
|
+
|
|
331
|
+
return query
|
|
332
|
+
select v_message.id, p_board_id, v_delivered_count, v_callback_count, v_message.created_at;
|
|
333
|
+
end;
|
|
334
|
+
$function$;
|
|
335
|
+
|
|
336
|
+
CREATE OR REPLACE FUNCTION agent_board.send_direct_message(p_thread_id uuid, p_author_agent_id uuid, p_recipient_agent_id uuid, p_subject text, p_body text, p_message_type text DEFAULT 'comment'::text, p_priority text DEFAULT 'medium'::text, p_idempotency_key text DEFAULT NULL::text, p_metadata jsonb DEFAULT '{}'::jsonb)
|
|
337
|
+
RETURNS TABLE(message_id uuid, delivery_recipient_id uuid, inbox_item_id uuid, callback_outbox_count integer, created_at timestamp with time zone)
|
|
338
|
+
LANGUAGE plpgsql
|
|
339
|
+
SECURITY DEFINER
|
|
340
|
+
SET search_path TO 'agent_board', 'public'
|
|
341
|
+
AS $function$
|
|
342
|
+
declare
|
|
343
|
+
v_message agent_board.messages%rowtype;
|
|
344
|
+
v_created_message record;
|
|
345
|
+
v_recipient agent_board.delivery_recipients%rowtype;
|
|
346
|
+
v_inbox agent_board.inbox_items%rowtype;
|
|
347
|
+
v_callback_count integer := 0;
|
|
348
|
+
v_metadata jsonb;
|
|
349
|
+
v_preview text;
|
|
350
|
+
v_fetch_descriptor jsonb;
|
|
351
|
+
v_idempotency_key text := nullif(trim(p_idempotency_key), '');
|
|
352
|
+
v_actor_agent_id uuid;
|
|
353
|
+
v_inserted boolean := false;
|
|
354
|
+
begin
|
|
355
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_author_agent_id, 'p_author_agent_id');
|
|
356
|
+
|
|
357
|
+
if nullif(trim(p_subject), '') is null or nullif(trim(p_body), '') is null then
|
|
358
|
+
raise exception 'subject and body are required' using errcode = '22023';
|
|
359
|
+
end if;
|
|
360
|
+
if p_priority not in ('low', 'medium', 'high', 'urgent') then
|
|
361
|
+
raise exception 'invalid priority' using errcode = '22023';
|
|
362
|
+
end if;
|
|
363
|
+
if not exists (select 1 from agent_board.threads where id = p_thread_id) then
|
|
364
|
+
raise exception 'thread not found' using errcode = 'P0002';
|
|
365
|
+
end if;
|
|
366
|
+
if not agent_board._current_agent_can_access_thread(p_thread_id) then
|
|
367
|
+
raise exception 'thread not accessible to current agent' using errcode = '42501';
|
|
368
|
+
end if;
|
|
369
|
+
if not exists (select 1 from agent_board.agents where id = p_recipient_agent_id) then
|
|
370
|
+
raise exception 'recipient agent not found' using errcode = 'P0002';
|
|
371
|
+
end if;
|
|
372
|
+
|
|
373
|
+
if agent_board._is_service_role() then
|
|
374
|
+
perform agent_board._record_send_authz_telemetry(
|
|
375
|
+
'service_role_bypass',
|
|
376
|
+
coalesce(nullif(p_metadata -> 'signed_write' ->> 'origin', ''), 'unknown'),
|
|
377
|
+
v_actor_agent_id,
|
|
378
|
+
null,
|
|
379
|
+
'send_direct_message',
|
|
380
|
+
agent_board._resolve_send_fleet_scope(v_actor_agent_id),
|
|
381
|
+
null,
|
|
382
|
+
jsonb_build_object('redacted', true)
|
|
383
|
+
);
|
|
384
|
+
end if;
|
|
385
|
+
|
|
386
|
+
perform agent_board._lock_idempotency_key(
|
|
387
|
+
'send_direct_message:' || p_thread_id::text || ':' || v_actor_agent_id::text,
|
|
388
|
+
v_idempotency_key
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
if v_idempotency_key is not null then
|
|
392
|
+
select *
|
|
393
|
+
into v_message
|
|
394
|
+
from agent_board.messages
|
|
395
|
+
where thread_id = p_thread_id
|
|
396
|
+
and author_agent_id = v_actor_agent_id
|
|
397
|
+
and metadata ->> 'rpcName' = 'send_direct_message'
|
|
398
|
+
and metadata ->> 'idempotencyKey' = v_idempotency_key
|
|
399
|
+
order by created_at desc
|
|
400
|
+
limit 1;
|
|
401
|
+
end if;
|
|
402
|
+
|
|
403
|
+
-- #717 F2b: a same-key replay may only re-deliver the source message to the recipient it was
|
|
404
|
+
-- already PRIVATELY delivered to. The proof is the persisted delivery row of THIS source
|
|
405
|
+
-- message in THIS function's mode, for THIS recipient, with no board scope. Metadata, the
|
|
406
|
+
-- thread's board stamp and any board-mode delivery are not evidence. Refused before any
|
|
407
|
+
-- replay effect, so the whole call (telemetry, signed evidence) rolls back. A new recipient
|
|
408
|
+
-- needs a new key, which creates a new message without the source's artifacts.
|
|
409
|
+
if v_message.id is not null and not exists (
|
|
410
|
+
select 1
|
|
411
|
+
from agent_board.delivery_recipients dr
|
|
412
|
+
where dr.source_kind = 'message'
|
|
413
|
+
and dr.source_message_id = v_message.id
|
|
414
|
+
and dr.delivery_mode = 'direct'
|
|
415
|
+
and dr.recipient_agent_id = p_recipient_agent_id
|
|
416
|
+
and dr.board_id is null
|
|
417
|
+
) then
|
|
418
|
+
raise exception 'message replay cannot widen delivery scope' using errcode = '42501';
|
|
419
|
+
end if;
|
|
420
|
+
|
|
421
|
+
if v_message.id is null then
|
|
422
|
+
v_metadata := coalesce(p_metadata, '{}'::jsonb)
|
|
423
|
+
|| jsonb_build_object(
|
|
424
|
+
'rpcName', 'send_direct_message',
|
|
425
|
+
'subject', trim(p_subject),
|
|
426
|
+
'priority', p_priority
|
|
427
|
+
);
|
|
428
|
+
if v_idempotency_key is not null then
|
|
429
|
+
v_metadata := v_metadata || jsonb_build_object('idempotencyKey', v_idempotency_key);
|
|
430
|
+
end if;
|
|
431
|
+
|
|
432
|
+
select *
|
|
433
|
+
into v_created_message
|
|
434
|
+
from agent_board._insert_thread_message(
|
|
435
|
+
p_thread_id,
|
|
436
|
+
v_actor_agent_id,
|
|
437
|
+
p_body,
|
|
438
|
+
p_message_type,
|
|
439
|
+
null,
|
|
440
|
+
v_metadata
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
select *
|
|
444
|
+
into v_message
|
|
445
|
+
from agent_board.messages
|
|
446
|
+
where id = v_created_message.message_id;
|
|
447
|
+
v_inserted := true;
|
|
448
|
+
end if;
|
|
449
|
+
|
|
450
|
+
v_preview := agent_board._trim_inbox_text(p_body, 280);
|
|
451
|
+
v_fetch_descriptor := agent_board._build_fetch_descriptor(
|
|
452
|
+
'thread',
|
|
453
|
+
p_thread_id,
|
|
454
|
+
'agent_board.v_thread_feed',
|
|
455
|
+
'direct delivery',
|
|
456
|
+
jsonb_build_object('messageId', v_message.id::text, 'recipientAgentId', p_recipient_agent_id::text)
|
|
457
|
+
);
|
|
458
|
+
|
|
459
|
+
v_recipient := agent_board._upsert_delivery_recipient(
|
|
460
|
+
'direct',
|
|
461
|
+
p_recipient_agent_id,
|
|
462
|
+
'message',
|
|
463
|
+
v_message.id,
|
|
464
|
+
null,
|
|
465
|
+
null,
|
|
466
|
+
null,
|
|
467
|
+
null,
|
|
468
|
+
jsonb_build_object('priority', p_priority)
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
v_inbox := agent_board._upsert_inbox_item(
|
|
472
|
+
v_recipient.id,
|
|
473
|
+
p_recipient_agent_id,
|
|
474
|
+
null,
|
|
475
|
+
v_recipient.delivery_mode,
|
|
476
|
+
'message',
|
|
477
|
+
v_message.id,
|
|
478
|
+
null,
|
|
479
|
+
null,
|
|
480
|
+
trim(p_subject),
|
|
481
|
+
v_preview,
|
|
482
|
+
p_priority,
|
|
483
|
+
true,
|
|
484
|
+
false,
|
|
485
|
+
v_fetch_descriptor,
|
|
486
|
+
jsonb_build_object('threadId', p_thread_id::text)
|
|
487
|
+
);
|
|
488
|
+
|
|
489
|
+
v_callback_count := agent_board._queue_inbox_callbacks(v_inbox.id, 'inbox.item.delivered');
|
|
490
|
+
|
|
491
|
+
-- #670: the author-scoped send receipt. Only a message inserted by THIS call, under a key, after
|
|
492
|
+
-- delivery and immediately before returning; replays never record. The helper derives the author
|
|
493
|
+
-- from the session and records nothing for service_role.
|
|
494
|
+
if v_inserted and v_idempotency_key is not null then
|
|
495
|
+
perform agent_board._record_send_outcome('send_direct_message', v_message.id, v_idempotency_key, null);
|
|
496
|
+
end if;
|
|
497
|
+
|
|
498
|
+
return query
|
|
499
|
+
select v_message.id, v_recipient.id, v_inbox.id, v_callback_count, v_message.created_at;
|
|
500
|
+
end;
|
|
501
|
+
$function$;
|
|
502
|
+
|
|
503
|
+
CREATE OR REPLACE FUNCTION agent_board.send_multicast_message(p_thread_id uuid, p_author_agent_id uuid, p_recipient_agent_ids uuid[], p_subject text, p_body text, p_message_type text DEFAULT 'comment'::text, p_priority text DEFAULT 'medium'::text, p_idempotency_key text DEFAULT NULL::text, p_metadata jsonb DEFAULT '{}'::jsonb)
|
|
504
|
+
RETURNS TABLE(message_id uuid, delivered_recipient_count integer, inbox_item_count integer, callback_outbox_count integer, created_at timestamp with time zone)
|
|
505
|
+
LANGUAGE plpgsql
|
|
506
|
+
SECURITY DEFINER
|
|
507
|
+
SET search_path TO 'agent_board', 'public'
|
|
508
|
+
AS $function$
|
|
509
|
+
declare
|
|
510
|
+
v_message agent_board.messages%rowtype;
|
|
511
|
+
v_created_message record;
|
|
512
|
+
v_recipient_ids uuid[];
|
|
513
|
+
v_recipient_id uuid;
|
|
514
|
+
v_recipient agent_board.delivery_recipients%rowtype;
|
|
515
|
+
v_inbox agent_board.inbox_items%rowtype;
|
|
516
|
+
v_delivered_count integer := 0;
|
|
517
|
+
v_inbox_count integer := 0;
|
|
518
|
+
v_callback_count integer := 0;
|
|
519
|
+
v_metadata jsonb;
|
|
520
|
+
v_preview text;
|
|
521
|
+
v_fetch_descriptor jsonb;
|
|
522
|
+
v_idempotency_key text := nullif(trim(p_idempotency_key), '');
|
|
523
|
+
v_actor_agent_id uuid;
|
|
524
|
+
v_inserted boolean := false;
|
|
525
|
+
begin
|
|
526
|
+
v_actor_agent_id := agent_board._resolved_actor_agent_id(p_author_agent_id, 'p_author_agent_id');
|
|
527
|
+
|
|
528
|
+
if nullif(trim(p_subject), '') is null or nullif(trim(p_body), '') is null then
|
|
529
|
+
raise exception 'subject and body are required' using errcode = '22023';
|
|
530
|
+
end if;
|
|
531
|
+
if p_priority not in ('low', 'medium', 'high', 'urgent') then
|
|
532
|
+
raise exception 'invalid priority' using errcode = '22023';
|
|
533
|
+
end if;
|
|
534
|
+
if p_recipient_agent_ids is null or cardinality(p_recipient_agent_ids) = 0 then
|
|
535
|
+
raise exception 'recipient_agent_ids must not be empty' using errcode = '22023';
|
|
536
|
+
end if;
|
|
537
|
+
if array_position(p_recipient_agent_ids, null) is not null then
|
|
538
|
+
raise exception 'recipient_agent_ids must not contain nulls' using errcode = '22023';
|
|
539
|
+
end if;
|
|
540
|
+
if not exists (select 1 from agent_board.threads where id = p_thread_id) then
|
|
541
|
+
raise exception 'thread not found' using errcode = 'P0002';
|
|
542
|
+
end if;
|
|
543
|
+
if not agent_board._current_agent_can_access_thread(p_thread_id) then
|
|
544
|
+
raise exception 'thread not accessible to current agent' using errcode = '42501';
|
|
545
|
+
end if;
|
|
546
|
+
|
|
547
|
+
select array_agg(distinct recipient_id)
|
|
548
|
+
into v_recipient_ids
|
|
549
|
+
from unnest(p_recipient_agent_ids) as recipient_id;
|
|
550
|
+
|
|
551
|
+
if (
|
|
552
|
+
select count(*)
|
|
553
|
+
from agent_board.agents
|
|
554
|
+
where id = any(v_recipient_ids)
|
|
555
|
+
) <> cardinality(v_recipient_ids) then
|
|
556
|
+
raise exception 'recipient agent not found' using errcode = 'P0002';
|
|
557
|
+
end if;
|
|
558
|
+
|
|
559
|
+
if agent_board._is_service_role() then
|
|
560
|
+
perform agent_board._record_send_authz_telemetry(
|
|
561
|
+
'service_role_bypass',
|
|
562
|
+
coalesce(nullif(p_metadata -> 'signed_write' ->> 'origin', ''), 'unknown'),
|
|
563
|
+
v_actor_agent_id,
|
|
564
|
+
null,
|
|
565
|
+
'send_multicast_message',
|
|
566
|
+
agent_board._resolve_send_fleet_scope(v_actor_agent_id),
|
|
567
|
+
null,
|
|
568
|
+
jsonb_build_object('redacted', true)
|
|
569
|
+
);
|
|
570
|
+
end if;
|
|
571
|
+
|
|
572
|
+
perform agent_board._lock_idempotency_key(
|
|
573
|
+
'send_multicast_message:' || p_thread_id::text || ':' || v_actor_agent_id::text,
|
|
574
|
+
v_idempotency_key
|
|
575
|
+
);
|
|
576
|
+
|
|
577
|
+
if v_idempotency_key is not null then
|
|
578
|
+
select *
|
|
579
|
+
into v_message
|
|
580
|
+
from agent_board.messages
|
|
581
|
+
where thread_id = p_thread_id
|
|
582
|
+
and author_agent_id = v_actor_agent_id
|
|
583
|
+
and metadata ->> 'rpcName' = 'send_multicast_message'
|
|
584
|
+
and metadata ->> 'idempotencyKey' = v_idempotency_key
|
|
585
|
+
order by created_at desc
|
|
586
|
+
limit 1;
|
|
587
|
+
end if;
|
|
588
|
+
|
|
589
|
+
-- #717 F2b: a same-key replay may name the same normalized recipient set or a nonempty subset
|
|
590
|
+
-- of it, never a new recipient. Each requested recipient must already hold a persisted
|
|
591
|
+
-- multicast delivery of THIS source message with no board scope; an omitted recipient's
|
|
592
|
+
-- delivery is kept, not revoked. Metadata, the thread's board stamp and board-mode deliveries
|
|
593
|
+
-- are not evidence. Refused before any replay effect, so the whole call rolls back. A new
|
|
594
|
+
-- recipient needs a new key, which creates a new message without the source's artifacts.
|
|
595
|
+
if v_message.id is not null and exists (
|
|
596
|
+
select 1
|
|
597
|
+
from unnest(v_recipient_ids) as requested(recipient_id)
|
|
598
|
+
where not exists (
|
|
599
|
+
select 1
|
|
600
|
+
from agent_board.delivery_recipients dr
|
|
601
|
+
where dr.source_kind = 'message'
|
|
602
|
+
and dr.source_message_id = v_message.id
|
|
603
|
+
and dr.delivery_mode = 'multicast'
|
|
604
|
+
and dr.recipient_agent_id = requested.recipient_id
|
|
605
|
+
and dr.board_id is null
|
|
606
|
+
)
|
|
607
|
+
) then
|
|
608
|
+
raise exception 'message replay cannot widen delivery scope' using errcode = '42501';
|
|
609
|
+
end if;
|
|
610
|
+
|
|
611
|
+
if v_message.id is null then
|
|
612
|
+
v_metadata := coalesce(p_metadata, '{}'::jsonb)
|
|
613
|
+
|| jsonb_build_object(
|
|
614
|
+
'rpcName', 'send_multicast_message',
|
|
615
|
+
'subject', trim(p_subject),
|
|
616
|
+
'priority', p_priority
|
|
617
|
+
);
|
|
618
|
+
if v_idempotency_key is not null then
|
|
619
|
+
v_metadata := v_metadata || jsonb_build_object('idempotencyKey', v_idempotency_key);
|
|
620
|
+
end if;
|
|
621
|
+
|
|
622
|
+
select *
|
|
623
|
+
into v_created_message
|
|
624
|
+
from agent_board._insert_thread_message(
|
|
625
|
+
p_thread_id,
|
|
626
|
+
v_actor_agent_id,
|
|
627
|
+
p_body,
|
|
628
|
+
p_message_type,
|
|
629
|
+
null,
|
|
630
|
+
v_metadata
|
|
631
|
+
);
|
|
632
|
+
|
|
633
|
+
select *
|
|
634
|
+
into v_message
|
|
635
|
+
from agent_board.messages
|
|
636
|
+
where id = v_created_message.message_id;
|
|
637
|
+
v_inserted := true;
|
|
638
|
+
end if;
|
|
639
|
+
|
|
640
|
+
v_preview := agent_board._trim_inbox_text(p_body, 280);
|
|
641
|
+
|
|
642
|
+
foreach v_recipient_id in array v_recipient_ids loop
|
|
643
|
+
v_fetch_descriptor := agent_board._build_fetch_descriptor(
|
|
644
|
+
'thread',
|
|
645
|
+
p_thread_id,
|
|
646
|
+
'agent_board.v_thread_feed',
|
|
647
|
+
'multicast delivery',
|
|
648
|
+
jsonb_build_object('messageId', v_message.id::text, 'recipientAgentId', v_recipient_id::text)
|
|
649
|
+
);
|
|
650
|
+
|
|
651
|
+
v_recipient := agent_board._upsert_delivery_recipient(
|
|
652
|
+
'multicast',
|
|
653
|
+
v_recipient_id,
|
|
654
|
+
'message',
|
|
655
|
+
v_message.id,
|
|
656
|
+
null,
|
|
657
|
+
null,
|
|
658
|
+
null,
|
|
659
|
+
null,
|
|
660
|
+
jsonb_build_object('priority', p_priority)
|
|
661
|
+
);
|
|
662
|
+
|
|
663
|
+
v_inbox := agent_board._upsert_inbox_item(
|
|
664
|
+
v_recipient.id,
|
|
665
|
+
v_recipient_id,
|
|
666
|
+
null,
|
|
667
|
+
v_recipient.delivery_mode,
|
|
668
|
+
'message',
|
|
669
|
+
v_message.id,
|
|
670
|
+
null,
|
|
671
|
+
null,
|
|
672
|
+
trim(p_subject),
|
|
673
|
+
v_preview,
|
|
674
|
+
p_priority,
|
|
675
|
+
true,
|
|
676
|
+
false,
|
|
677
|
+
v_fetch_descriptor,
|
|
678
|
+
jsonb_build_object('threadId', p_thread_id::text)
|
|
679
|
+
);
|
|
680
|
+
|
|
681
|
+
v_delivered_count := v_delivered_count + 1;
|
|
682
|
+
v_inbox_count := v_inbox_count + 1;
|
|
683
|
+
v_callback_count := v_callback_count + agent_board._queue_inbox_callbacks(v_inbox.id, 'inbox.item.delivered');
|
|
684
|
+
end loop;
|
|
685
|
+
|
|
686
|
+
-- #670: the author-scoped send receipt. Only a message inserted by THIS call, under a key, after
|
|
687
|
+
-- delivery and immediately before returning; replays never record. The helper derives the author
|
|
688
|
+
-- from the session and records nothing for service_role.
|
|
689
|
+
if v_inserted and v_idempotency_key is not null then
|
|
690
|
+
perform agent_board._record_send_outcome('send_multicast_message', v_message.id, v_idempotency_key, null);
|
|
691
|
+
end if;
|
|
692
|
+
|
|
693
|
+
return query
|
|
694
|
+
select v_message.id, v_delivered_count, v_inbox_count, v_callback_count, v_message.created_at;
|
|
695
|
+
end;
|
|
696
|
+
$function$;
|
|
697
|
+
|
|
698
|
+
revoke all on function agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb) from public,anon,authenticated,service_role,agent_board_delegate;
|
|
699
|
+
grant execute on function agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb) to authenticated,service_role,agent_board_delegate;
|
|
700
|
+
comment on function agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb) is '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).';
|
|
701
|
+
|
|
702
|
+
revoke all on function agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb) from public,anon,authenticated,service_role,agent_board_delegate;
|
|
703
|
+
grant execute on function agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb) to authenticated,service_role;
|
|
704
|
+
comment on function agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb) is '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).';
|
|
705
|
+
|
|
706
|
+
revoke all on function agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb) from public,anon,authenticated,service_role,agent_board_delegate;
|
|
707
|
+
grant execute on function agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb) to authenticated,service_role;
|
|
708
|
+
comment on function agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb) is '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).';
|