@consiliency/agent-board-schema 1.68.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 +32 -4
- package/migrations/20261026000100_agent_board_thread_association.sql +1287 -0
- package/migrations/20261101000100_agent_board_session_measured_mint.sql +145 -0
- package/migrations/20261104065658_agent_board_reply_delivery.sql +659 -0
- package/migrations/20261104170000_agent_board_tasking_replay_scope.sql +320 -0
- package/migrations/20261104180000_agent_board_delivery_writer_execute.sql +51 -0
- package/migrations/20261104190000_agent_board_message_replay_scope.sql +373 -0
- package/migrations/20261108000100_agent_board_send_outcome.sql +708 -0
- package/package.json +1 -1
- package/schema-manifest.json +66 -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 +9 -2
- package/tests/agent_board_661_reply_delivery.test.sql +185 -0
- package/tests/agent_board_667_boundary.test.sql +162 -0
- package/tests/agent_board_667_conversion_branches.test.sql +91 -0
- package/tests/agent_board_667_conversion_profile.test.sql +112 -0
- package/tests/agent_board_667_history_class.test.sql +787 -0
- package/tests/agent_board_667_native_cast_effect.test.sql +88 -0
- package/tests/agent_board_667_realtime_apply_behavior.test.sql +117 -0
- package/tests/agent_board_667_realtime_apply_controls.test.sql +143 -0
- package/tests/agent_board_667_realtime_apply_profile.test.sql +61 -0
- package/tests/agent_board_667_retention.test.sql +201 -0
- package/tests/agent_board_667_routing.test.sql +294 -0
- package/tests/agent_board_667_sender_effects.test.sql +100 -0
- package/tests/agent_board_667_sender_profile_controls.test.sql +29 -0
- package/tests/agent_board_667_storage_delimiter_behavior.test.sql +84 -0
- package/tests/agent_board_667_storage_delimiter_controls.test.sql +23 -0
- package/tests/agent_board_667_storage_delimiter_profile.test.sql +61 -0
- package/tests/agent_board_667_storage_search_behavior.test.sql +93 -0
- package/tests/agent_board_667_storage_search_controls.test.sql +22 -0
- package/tests/agent_board_667_storage_search_profile.test.sql +60 -0
- package/tests/agent_board_667_storage_timestamp_behavior.test.sql +58 -0
- package/tests/agent_board_667_storage_timestamp_controls.test.sql +22 -0
- package/tests/agent_board_667_storage_timestamp_profile.test.sql +54 -0
- package/tests/agent_board_667_writers.test.sql +281 -0
- package/tests/agent_board_717_message_replay_scope.test.sql +828 -0
- package/tests/agent_board_717_native_surface.test.sql +223 -0
- package/tests/agent_board_717_tasking_replay_scope.test.sql +661 -0
- package/tests/agent_board_addressing.test.sql +8 -5
- package/tests/agent_board_claim_subject_gate.test.sql +7 -3
- package/tests/agent_board_claimpolicy_race.test.sql +39 -11
- package/tests/agent_board_delegate_role_class.test.sql +35 -17
- package/tests/agent_board_delivery_writer_execute.test.sql +373 -0
- package/tests/agent_board_fk_lock_edges.test.sql +3 -0
- package/tests/agent_board_hosted_delegates.test.sql +13 -3
- package/tests/agent_board_inbox_rpcs.test.sql +5 -3
- package/tests/agent_board_obsgate.test.sql +8 -4
- package/tests/agent_board_obsgate4.test.sql +2 -2
- package/tests/agent_board_outbox_wake_ordering.test.sql +25 -2
- package/tests/agent_board_own_post_not_delivered.test.sql +11 -9
- package/tests/agent_board_send_outcome.test.sql +805 -0
- package/tests/agent_board_session_identity_behavior_classes.test.sql +2 -2
- package/tests/agent_board_session_identity_limits.test.sql +1 -1
- package/tests/agent_board_session_identity_measured_mint.test.sql +342 -0
- package/tests/agent_board_session_identity_predicate_isolation.test.sql +146 -0
- package/tests/agent_board_sidserver_authority.test.sql +69 -0
- package/tests/agent_board_sidserver_catalog.test.sql +47 -0
- package/tests/agent_board_signed_write_gateway.test.sql +4 -2
- package/tests/agent_board_task_thread_lock_order.test.sql +67 -42
- package/tests/agent_board_v2_invariants.test.sql +5 -2
- package/tests/agent_board_write_enforcement_class.test.sql +12 -8
- package/tests/catalog/function_execute_privileges.json +65 -5
- package/tests/catalog/p_metadata_parameters.json +24 -0
- package/tests/fixtures/agent_board_667_apply_rls_guard.sql +195 -0
- package/tests/fixtures/agent_board_667_apply_rls_provenance.md +125 -0
- package/tests/fixtures/agent_board_667_apply_rls_reference.sql +1903 -0
- package/tests/fixtures/agent_board_667_conversion_guard.sql +162 -0
- package/tests/fixtures/agent_board_667_conversion_reference.md +23 -0
- package/tests/fixtures/agent_board_667_conversion_reference.sql +5325 -0
- package/tests/fixtures/agent_board_667_delimiter_guard.sql +92 -0
- package/tests/fixtures/agent_board_667_delimiter_provenance.md +116 -0
- package/tests/fixtures/agent_board_667_delimiter_reference.sql +10150 -0
- package/tests/fixtures/agent_board_667_search_guard.sql +92 -0
- package/tests/fixtures/agent_board_667_search_provenance.md +141 -0
- package/tests/fixtures/agent_board_667_search_reference.sql +4779 -0
- package/tests/fixtures/agent_board_667_sender_guard.sql +102 -0
- package/tests/fixtures/agent_board_667_sender_reference.md +20 -0
- package/tests/fixtures/agent_board_667_sender_reference.sql +875 -0
- package/tests/fixtures/agent_board_667_timestamp_graph.sql +84 -0
- package/tests/fixtures/agent_board_667_timestamp_guard.sql +94 -0
- package/tests/fixtures/agent_board_667_timestamp_provenance.md +99 -0
- package/tests/fixtures/agent_board_667_timestamp_reference.sql +2938 -0
- package/tests/fixtures/isolation-substrate.sql +8 -0
- package/tests/fixtures/pgtap-unexposed.sql +61 -0
- package/tests/isolation/expected/association_artifact_anchor_order.out +108 -0
- package/tests/isolation/expected/association_board_vs_board.out +71 -0
- package/tests/isolation/expected/association_post_wait_authority.out +77 -0
- package/tests/isolation/expected/association_strong_history_writers.out +203 -0
- package/tests/isolation/expected/association_strong_isolation.out +185 -0
- package/tests/isolation/expected/association_vs_artifact_claim_escalation.out +199 -0
- package/tests/isolation/expected/association_vs_heartbeat.out +78 -0
- package/tests/isolation/expected/association_vs_multi_anchor_artifact.out +67 -0
- package/tests/isolation/expected/association_vs_private_message.out +67 -0
- package/tests/isolation/expected/association_vs_task_and_conflict.out +133 -0
- package/tests/isolation/expected/association_vs_taskful_cross_thread.out +133 -0
- package/tests/isolation/expected/message_replay_scope.out +209 -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/expected/tasking_replay_scope.out +125 -0
- package/tests/isolation/specs/association_artifact_anchor_order.spec +46 -0
- package/tests/isolation/specs/association_board_vs_board.spec +37 -0
- package/tests/isolation/specs/association_post_wait_authority.spec +48 -0
- package/tests/isolation/specs/association_strong_history_writers.spec +61 -0
- package/tests/isolation/specs/association_strong_isolation.spec +50 -0
- package/tests/isolation/specs/association_vs_artifact_claim_escalation.spec +41 -0
- package/tests/isolation/specs/association_vs_heartbeat.spec +47 -0
- package/tests/isolation/specs/association_vs_multi_anchor_artifact.spec +38 -0
- package/tests/isolation/specs/association_vs_private_message.spec +33 -0
- package/tests/isolation/specs/association_vs_task_and_conflict.spec +38 -0
- package/tests/isolation/specs/association_vs_taskful_cross_thread.spec +42 -0
- package/tests/isolation/specs/message_replay_scope.spec +77 -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/isolation/specs/tasking_replay_scope.spec +72 -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
- package/tests/mutations/717-f1/M0_prefix_main_acl_and_comments.sql +11 -0
- package/tests/mutations/717-f1/M10_wrapper_stops_delegating.sql +7 -0
- package/tests/mutations/717-f1/M11_transitive_recipient_content.sql +8 -0
- package/tests/mutations/717-f1/M12_transitive_recipient_visibility.sql +8 -0
- package/tests/mutations/717-f1/M13_transitive_worker_content.sql +15 -0
- package/tests/mutations/717-f1/M1a_regrant_upsert_delivery_recipient.sql +1 -0
- package/tests/mutations/717-f1/M1b_regrant_upsert_inbox_item.sql +1 -0
- package/tests/mutations/717-f1/M1c_regrant_queue_inbox_callbacks.sql +1 -0
- package/tests/mutations/717-f1/M1d_regrant_queue_board_post_callbacks.sql +1 -0
- package/tests/mutations/717-f1/M1e_regrant_next_thread_message_sequence.sql +1 -0
- package/tests/mutations/717-f1/M2_blanket_schema_grant.sql +1 -0
- package/tests/mutations/717-f1/M3_new_overload_of_sink.sql +10 -0
- package/tests/mutations/717-f1/M4_new_route_calling_sink.sql +7 -0
- package/tests/mutations/717-f1/M5_actor_trusts_supplied_id.sql +5 -0
- package/tests/mutations/717-f1/M6_signed_route_granted_to_authenticated.sql +1 -0
- package/tests/mutations/717-f1/M7_worker_granted_to_authenticated.sql +1 -0
- package/tests/mutations/717-f1/M8_recipient_state_rewrites_visibility.sql +5 -0
- package/tests/mutations/717-f1/M9_public_route_revoked_from_service_role.sql +1 -0
- package/tests/mutations/717-f1/outbox_wake_ordering/W1_regrant_queue_inbox_callbacks.sql +1 -0
- package/tests/mutations/717-f1/outbox_wake_ordering/W2_revoke_worker_from_service_role.sql +1 -0
- package/tests/mutations/717-f1/outbox_wake_ordering/W3_invoker_caller_of_sink.sql +1 -0
- package/tests/mutations/717-f1/outbox_wake_ordering/W4_public_regrant_board_post_sink.sql +1 -0
- package/tests/mutations/717-f1/run.sh +17 -0
- package/tests/mutations/717-f2b/C1_new_delivery_route.sql +4 -0
- package/tests/mutations/717-f2b/C2_new_replay_lookup.sql +3 -0
- package/tests/mutations/717-f2b/C3_internal_regranted.sql +2 -0
- package/tests/mutations/717-f2b/C4_wrapper_writes_delivery.sql +9 -0
- package/tests/mutations/717-f2b/C5_class_member_dynamic_sql.sql +9 -0
- package/tests/mutations/717-f2b/C6_drop_board_scope_constraint.sql +2 -0
- package/tests/mutations/717-f2b/C7_state_private_helper_content.sql +14 -0
- package/tests/mutations/717-f2b/L1_dm_lock_split_by_recipient.sql +8 -0
- package/tests/mutations/717-f2b/L2_dm_lock_removed.sql +12 -0
- package/tests/mutations/717-f2b/L3_mc_lock_split_by_recipients.sql +8 -0
- package/tests/mutations/717-f2b/L4_mc_lock_removed.sql +12 -0
- package/tests/mutations/717-f2b/M0_prefix_definitions.sql +17 -0
- package/tests/mutations/717-f2b/M10_mc_lookup_any_author.sql +15 -0
- package/tests/mutations/717-f2b/M11_dm_swallow_refusal.sql +8 -0
- package/tests/mutations/717-f2b/M12_mc_swallow_refusal.sql +8 -0
- package/tests/mutations/717-f2b/M13_signed_dm_swallows_refusal.sql +32 -0
- package/tests/mutations/717-f2b/M14_signed_mc_swallows_refusal.sql +20 -0
- package/tests/mutations/717-f2b/M15_mc_any_source.sql +7 -0
- package/tests/mutations/717-f2b/M1_dm_drop_recipient_proof.sql +16 -0
- package/tests/mutations/717-f2b/M2_mc_drop_recipient_proof.sql +16 -0
- package/tests/mutations/717-f2b/M3_dm_metadata_as_proof.sql +12 -0
- package/tests/mutations/717-f2b/M4_mc_metadata_as_proof.sql +16 -0
- package/tests/mutations/717-f2b/M5_dm_any_mode.sql +11 -0
- package/tests/mutations/717-f2b/M6_dm_board_delivery_counts.sql +11 -0
- package/tests/mutations/717-f2b/M7_mc_any_mode.sql +11 -0
- package/tests/mutations/717-f2b/M8_dm_thread_stamp_as_proof.sql +12 -0
- package/tests/mutations/717-f2b/M9_dm_lookup_any_author.sql +15 -0
- package/tests/mutations/717-f2b/run.sh +82 -0
|
@@ -0,0 +1,805 @@
|
|
|
1
|
+
-- SENDOUTCOME pgTAP (#670, migration 20261108000100_agent_board_send_outcome.sql).
|
|
2
|
+
--
|
|
3
|
+
-- THE CONTRACT (plans/SENDOUTCOME-670-plan.md D3-D10). A keyed SESSION send by one of the three
|
|
4
|
+
-- UUID cores (post_board_message, send_direct_message, send_multicast_message) that INSERTS a
|
|
5
|
+
-- message records one private receipt in agent_board.send_outcomes, atomically with the send.
|
|
6
|
+
-- get_send_outcome(thread, key, operation) returns 'confirmed' with (operation, thread, message,
|
|
7
|
+
-- board for board posts, created_at) to THAT author only, and an all-NULL 'unknown' row for every
|
|
8
|
+
-- other case. It reads only send_outcomes: no existence, membership or access check. service_role
|
|
9
|
+
-- sends, signed wrappers and replays never mint. service_role and principal-less sessions cannot
|
|
10
|
+
-- read (42501); bad inputs and keyed tasking are 22023.
|
|
11
|
+
--
|
|
12
|
+
-- METHOD (AGENTS.md rules 1-3).
|
|
13
|
+
-- * Every actor runs under its OWN role AND claims (pg_temp.ident); the flattened
|
|
14
|
+
-- request.jwt.claim.* GUCs are cleared on every switch. A direct postgres session with no JWT
|
|
15
|
+
-- is its own actor (role 'postgres').
|
|
16
|
+
-- * Allow-side calls go through lives_ok (or a helper that reports 'ok') and a SEPARATE state
|
|
17
|
+
-- read; refusals use throws_ok with the SQLSTATE, then a separate zero-row check. No bare
|
|
18
|
+
-- `select` of a raising RPC: a helper with its own exception block reports the error instead
|
|
19
|
+
-- of aborting the file.
|
|
20
|
+
-- * Mutation labels: SO-M<n> is the assertion the D11 mutant M<n> must turn red (M9 and M10 are
|
|
21
|
+
-- isolation controls, tests/isolation/specs/send_outcome_*.spec). Receipts are in
|
|
22
|
+
-- plans/evidence/670/.
|
|
23
|
+
create extension if not exists pgtap with schema extensions;
|
|
24
|
+
set search_path = extensions, public, agent_board;
|
|
25
|
+
|
|
26
|
+
begin;
|
|
27
|
+
|
|
28
|
+
select plan(100);
|
|
29
|
+
|
|
30
|
+
grant usage on schema extensions to authenticated, service_role, anon;
|
|
31
|
+
do $$
|
|
32
|
+
begin
|
|
33
|
+
if not (select rolsuper from pg_roles where rolname = current_user) then
|
|
34
|
+
execute format('grant agent_board_delegate to %I with set true', current_user);
|
|
35
|
+
end if;
|
|
36
|
+
end $$;
|
|
37
|
+
|
|
38
|
+
create temp table _h(k text primary key, v text);
|
|
39
|
+
create function pg_temp.v(p_k text) returns text language sql as $$ select v from _h where k = p_k $$;
|
|
40
|
+
-- Never raises: a value that is not a uuid reads as NULL, so a broken implementation fails an
|
|
41
|
+
-- assertion instead of aborting the file.
|
|
42
|
+
create function pg_temp.u(p_k text) returns uuid language sql as $$
|
|
43
|
+
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 $$;
|
|
44
|
+
create function pg_temp.put(p_k text, p_v text) returns void language sql as $$
|
|
45
|
+
insert into _h values (p_k, p_v) on conflict (k) do update set v = excluded.v $$;
|
|
46
|
+
|
|
47
|
+
-- Identity switch: ONE JSON claim set, every flattened claim GUC cleared.
|
|
48
|
+
create function pg_temp.ident(p_role text, p_sub uuid, p_cred uuid default null, p_epoch text default null)
|
|
49
|
+
returns void language plpgsql as $$
|
|
50
|
+
begin
|
|
51
|
+
perform set_config('request.jwt.claim.sub', '', true);
|
|
52
|
+
perform set_config('request.jwt.claim.role', '', true);
|
|
53
|
+
perform set_config('request.jwt.claim.agent_credential_id', '', true);
|
|
54
|
+
perform set_config('request.jwt.claim.identity_lease_epoch', '', true);
|
|
55
|
+
perform set_config('request.jwt.claims',
|
|
56
|
+
case when p_role is null then ''
|
|
57
|
+
else jsonb_strip_nulls(jsonb_build_object('role', p_role, 'sub', p_sub,
|
|
58
|
+
'agent_credential_id', p_cred, 'identity_lease_epoch', p_epoch))::text end,
|
|
59
|
+
true);
|
|
60
|
+
end $$;
|
|
61
|
+
|
|
62
|
+
-- Become actor p_who. Everything is read from _h BEFORE the role switch (API roles cannot read it).
|
|
63
|
+
create function pg_temp.enter(p_who text) returns void language plpgsql as $$
|
|
64
|
+
declare
|
|
65
|
+
v_role text := pg_temp.v(p_who || '.role');
|
|
66
|
+
v_sub uuid := pg_temp.u(p_who || '.auth');
|
|
67
|
+
v_cred uuid := pg_temp.u(p_who || '.cred');
|
|
68
|
+
v_epoch text := pg_temp.v(p_who || '.epoch');
|
|
69
|
+
begin
|
|
70
|
+
if v_role is null then
|
|
71
|
+
raise exception 'fixture: actor % has no role', p_who;
|
|
72
|
+
end if;
|
|
73
|
+
if v_role = 'postgres' then
|
|
74
|
+
perform pg_temp.ident(null, null);
|
|
75
|
+
return;
|
|
76
|
+
end if;
|
|
77
|
+
perform pg_temp.ident(v_role, v_sub, v_cred, v_epoch);
|
|
78
|
+
execute format('set local role %I', v_role);
|
|
79
|
+
end $$;
|
|
80
|
+
create function pg_temp.leave() returns void language plpgsql as $$
|
|
81
|
+
begin
|
|
82
|
+
reset role;
|
|
83
|
+
perform pg_temp.ident(null, null);
|
|
84
|
+
end $$;
|
|
85
|
+
|
|
86
|
+
-- Run p_sql AS p_who: 'ok' or '<sqlstate>:<message>'.
|
|
87
|
+
create function pg_temp.as_(p_who text, p_sql text) returns text language plpgsql as $$
|
|
88
|
+
declare v text;
|
|
89
|
+
begin
|
|
90
|
+
perform pg_temp.enter(p_who);
|
|
91
|
+
begin
|
|
92
|
+
execute p_sql;
|
|
93
|
+
v := 'ok';
|
|
94
|
+
exception when others then
|
|
95
|
+
v := sqlstate || ':' || sqlerrm;
|
|
96
|
+
end;
|
|
97
|
+
perform pg_temp.leave();
|
|
98
|
+
return v;
|
|
99
|
+
end $$;
|
|
100
|
+
|
|
101
|
+
-- Run p_sql AS p_who and return its single scalar ('(null)' for NULL, 'ERR <state>:<msg>' on error).
|
|
102
|
+
create function pg_temp.val(p_who text, p_sql text) returns text language plpgsql as $$
|
|
103
|
+
declare v text;
|
|
104
|
+
begin
|
|
105
|
+
perform pg_temp.enter(p_who);
|
|
106
|
+
begin
|
|
107
|
+
execute p_sql into v;
|
|
108
|
+
exception when others then
|
|
109
|
+
v := 'ERR ' || sqlstate || ':' || sqlerrm;
|
|
110
|
+
end;
|
|
111
|
+
perform pg_temp.leave();
|
|
112
|
+
return coalesce(v, '(null)');
|
|
113
|
+
end $$;
|
|
114
|
+
|
|
115
|
+
-- Run p_sql AS p_who and RE-RAISE its error with the original SQLSTATE: the throws_ok target.
|
|
116
|
+
create function pg_temp.raise_as(p_who text, p_sql text) returns void language plpgsql as $$
|
|
117
|
+
declare v_state text; v_msg text;
|
|
118
|
+
begin
|
|
119
|
+
perform pg_temp.enter(p_who);
|
|
120
|
+
begin
|
|
121
|
+
execute p_sql;
|
|
122
|
+
exception when others then
|
|
123
|
+
get stacked diagnostics v_state = returned_sqlstate, v_msg = message_text;
|
|
124
|
+
perform pg_temp.leave();
|
|
125
|
+
raise exception '%', v_msg using errcode = v_state;
|
|
126
|
+
end;
|
|
127
|
+
perform pg_temp.leave();
|
|
128
|
+
end $$;
|
|
129
|
+
|
|
130
|
+
-- How many rows p_sql returned AS p_who (0 when it raised): the zero-row state check.
|
|
131
|
+
create function pg_temp.nrows(p_who text, p_sql text) returns int language plpgsql as $$
|
|
132
|
+
declare v int := 0;
|
|
133
|
+
begin
|
|
134
|
+
perform pg_temp.enter(p_who);
|
|
135
|
+
begin
|
|
136
|
+
execute 'select count(*)::int from (' || p_sql || ') q' into v;
|
|
137
|
+
exception when others then
|
|
138
|
+
v := 0;
|
|
139
|
+
end;
|
|
140
|
+
perform pg_temp.leave();
|
|
141
|
+
return v;
|
|
142
|
+
end $$;
|
|
143
|
+
|
|
144
|
+
-- As the table owner, 'ok' or the SQLSTATE.
|
|
145
|
+
create function pg_temp.try_owner(p_sql text) returns text language plpgsql as $$
|
|
146
|
+
begin execute p_sql; return 'ok'; exception when others then return sqlstate; end $$;
|
|
147
|
+
|
|
148
|
+
-- The read, as SQL text: the whole returned row, as text.
|
|
149
|
+
create function pg_temp.so(p_thread text, p_key text, p_op text default 'post_board_message') returns text language sql as $$
|
|
150
|
+
select format('select r::text from agent_board.get_send_outcome(%L::uuid, %L::text, %L::text) r', pg_temp.v(p_thread), p_key, p_op)
|
|
151
|
+
$$;
|
|
152
|
+
create function pg_temp.so_raw(p_thread uuid, p_key text, p_op text) returns text language sql as $$
|
|
153
|
+
select format('select * from agent_board.get_send_outcome(%L::uuid, %L::text, %L::text)', p_thread, p_key, p_op)
|
|
154
|
+
$$;
|
|
155
|
+
-- The read as p_who.
|
|
156
|
+
create function pg_temp.read(p_who text, p_thread text, p_key text, p_op text default 'post_board_message') returns text language sql as $$
|
|
157
|
+
select pg_temp.val(p_who, pg_temp.so(p_thread, p_key, p_op))
|
|
158
|
+
$$;
|
|
159
|
+
-- The expected 'confirmed' row for message p_msg, built from the message row itself.
|
|
160
|
+
create function pg_temp.confirmed(p_msg text, p_op text, p_board text) returns text language sql as $$
|
|
161
|
+
select row('confirmed'::text, p_op, m.thread_id, m.id, pg_temp.u(p_board), m.created_at)::text
|
|
162
|
+
from agent_board.messages m where m.id = pg_temp.u(p_msg)
|
|
163
|
+
$$;
|
|
164
|
+
-- Receipts for a message, and for an (author, thread, key), counted as the owner.
|
|
165
|
+
create function pg_temp.receipts_for(p_msg text) returns int language sql as $$
|
|
166
|
+
select count(*)::int from agent_board.send_outcomes where message_id = pg_temp.u(p_msg) $$;
|
|
167
|
+
create function pg_temp.receipts_key(p_author text, p_key text) returns int language sql as $$
|
|
168
|
+
select count(*)::int from agent_board.send_outcomes where author_agent_id = pg_temp.u(p_author) and idempotency_key = p_key $$;
|
|
169
|
+
|
|
170
|
+
-- Sends, as SQL text returning message_id::text. p_author NULL = the session's own (resolved) id.
|
|
171
|
+
create function pg_temp.pbm(p_author text, p_thread text, p_board text, p_key text, p_body text default 'so670 body',
|
|
172
|
+
p_metadata jsonb default '{}'::jsonb) returns text language sql as $$
|
|
173
|
+
select format('select message_id::text from agent_board.post_board_message(%L::uuid, %L::uuid, %L::uuid, %L, %L, ''comment'', ''medium'', %L, %L::jsonb)',
|
|
174
|
+
pg_temp.v(p_thread), pg_temp.v(p_author), pg_temp.v(p_board), 'so670 ' || p_body, p_body, p_key, p_metadata::text)
|
|
175
|
+
$$;
|
|
176
|
+
create function pg_temp.dm(p_author text, p_thread text, p_to text, p_key text) returns text language sql as $$
|
|
177
|
+
select format('select message_id::text from agent_board.send_direct_message(%L::uuid, %L::uuid, %L::uuid, ''so670 dm'', ''so670 dm body'', ''comment'', ''medium'', %L, ''{}''::jsonb)',
|
|
178
|
+
pg_temp.v(p_thread), pg_temp.v(p_author), pg_temp.v(p_to), p_key)
|
|
179
|
+
$$;
|
|
180
|
+
create function pg_temp.mc(p_author text, p_thread text, p_key text, variadic p_to text[]) returns text language sql as $$
|
|
181
|
+
select format('select message_id::text from agent_board.send_multicast_message(%L::uuid, %L::uuid, %L::uuid[], ''so670 mc'', ''so670 mc body'', ''comment'', ''medium'', %L, ''{}''::jsonb)',
|
|
182
|
+
pg_temp.v(p_thread), pg_temp.v(p_author), (select array_agg(pg_temp.v(t) order by o) from unnest(p_to) with ordinality x(t, o)), p_key)
|
|
183
|
+
$$;
|
|
184
|
+
-- A gateway decision for p_actor with the given verification status (a SQL fixture: this pins
|
|
185
|
+
-- what the wrapper records, not signature verification).
|
|
186
|
+
create function pg_temp.gw(p_actor text, p_status text default 'verified') returns text language sql as $$
|
|
187
|
+
select quote_literal(jsonb_build_object('agent_id', pg_temp.v(p_actor), 'verification_status', p_status,
|
|
188
|
+
'payload_digest', 'sha256:' || repeat('6', 64), 'enforcement_mode', 'warn')::text) || '::jsonb'
|
|
189
|
+
$$;
|
|
190
|
+
create function pg_temp.signed(p_fn text, p_payload jsonb, p_actor text) returns text language sql as $$
|
|
191
|
+
select format('select message_id::text from agent_board.%I(%L::jsonb, %s)', p_fn, p_payload::text, pg_temp.gw(p_actor))
|
|
192
|
+
$$;
|
|
193
|
+
|
|
194
|
+
-- ===========================================================================
|
|
195
|
+
-- Fixtures
|
|
196
|
+
-- ===========================================================================
|
|
197
|
+
-- Keyed agents (authenticated, no credential unless stated):
|
|
198
|
+
-- A author, owner of X and XA B2 second author on X O outsider, owner of Y
|
|
199
|
+
-- R / R2 private recipients NA non-member named by service_role
|
|
200
|
+
-- LM member who leaves DE member demoted to observer SU member later suspended
|
|
201
|
+
-- CR member with a credential that is later revoked CE ... later expired
|
|
202
|
+
-- SE session identity whose epoch moves ST session identity later tombstoned
|
|
203
|
+
-- Z sentinel member of XA (atomicity)
|
|
204
|
+
-- NOAGENT is an auth user with no agent row.
|
|
205
|
+
insert into auth.users(id, instance_id, aud, role, email)
|
|
206
|
+
select ('670a0000-0000-4000-8000-' || lpad(n::text, 12, '0'))::uuid,
|
|
207
|
+
'00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'so670-' || n || '@t.test'
|
|
208
|
+
from generate_series(1, 16) n;
|
|
209
|
+
insert into agent_board.agents(id, agent_type, kind, handle, display_name, status, auth_user_id)
|
|
210
|
+
select ('670a1000-0000-4000-8000-' || lpad(n::text, 12, '0'))::uuid, 'agent', 'agent',
|
|
211
|
+
'so670-' || lower(h), 'SO670 ' || h, 'active', ('670a0000-0000-4000-8000-' || lpad(n::text, 12, '0'))::uuid
|
|
212
|
+
from unnest(array['A', 'B2', 'O', 'R', 'R2', 'NA', 'LM', 'DE', 'SU', 'CR', 'CE', 'SE', 'ST', 'Z']) with ordinality as x(h, n);
|
|
213
|
+
do $$
|
|
214
|
+
declare h text; n int := 0;
|
|
215
|
+
begin
|
|
216
|
+
foreach h in array array['A', 'B2', 'O', 'R', 'R2', 'NA', 'LM', 'DE', 'SU', 'CR', 'CE', 'SE', 'ST', 'Z'] loop
|
|
217
|
+
n := n + 1;
|
|
218
|
+
perform pg_temp.put(h, '670a1000-0000-4000-8000-' || lpad(n::text, 12, '0'));
|
|
219
|
+
perform pg_temp.put(h || '.auth', '670a0000-0000-4000-8000-' || lpad(n::text, 12, '0'));
|
|
220
|
+
perform pg_temp.put(h || '.role', 'authenticated');
|
|
221
|
+
end loop;
|
|
222
|
+
end $$;
|
|
223
|
+
select pg_temp.put('NOAGENT.auth', '670a0000-0000-4000-8000-000000000016'), pg_temp.put('NOAGENT.role', 'authenticated');
|
|
224
|
+
select pg_temp.put('ANON.role', 'anon');
|
|
225
|
+
select pg_temp.put('SVC.role', 'service_role');
|
|
226
|
+
-- service_role JWT carrying A's auth user: current_agent_id() resolves A (it has no role predicate),
|
|
227
|
+
-- so only _is_service_role() stands between it and A's receipts.
|
|
228
|
+
select pg_temp.put('SVCA.role', 'service_role'), pg_temp.put('SVCA.auth', pg_temp.v('A.auth'));
|
|
229
|
+
select pg_temp.put('PG.role', 'postgres');
|
|
230
|
+
|
|
231
|
+
-- Credentials for CR and CE (valid now; revoked / expired later).
|
|
232
|
+
insert into agent_board.agent_credentials(id, agent_id, refresh_token_hash, hash_version, credential_label, expires_at)
|
|
233
|
+
values ('670a3000-0000-4000-8000-000000000001', pg_temp.u('CR'), decode(md5('so670-cr-refresh'), 'hex'), 1, 'so670-cr', now() + interval '1 day'),
|
|
234
|
+
('670a3000-0000-4000-8000-000000000002', pg_temp.u('CE'), decode(md5('so670-ce-refresh'), 'hex'), 1, 'so670-ce', now() + interval '1 day');
|
|
235
|
+
select pg_temp.put('CR.cred', '670a3000-0000-4000-8000-000000000001'), pg_temp.put('CE.cred', '670a3000-0000-4000-8000-000000000002');
|
|
236
|
+
-- Session identities for SE and ST at epoch 0. A session identity is always a child
|
|
237
|
+
-- (identity_kind <> 'session' or parent_agent_id is not null), so both hang off root RT.
|
|
238
|
+
insert into agent_board.agents(id, agent_type, kind, handle, display_name, status)
|
|
239
|
+
values ('670a1000-0000-4000-8000-000000000020', 'agent', 'agent', 'so670-rt', 'SO670 RT', 'active');
|
|
240
|
+
select pg_temp.put('RT', '670a1000-0000-4000-8000-000000000020');
|
|
241
|
+
insert into agent_board.session_identities(agent_id, parent_agent_id, root_agent_id, depth, identity_kind, harness, credential_epoch)
|
|
242
|
+
values (pg_temp.u('RT'), null, pg_temp.u('RT'), 0, 'legacy', null, 0),
|
|
243
|
+
(pg_temp.u('SE'), pg_temp.u('RT'), pg_temp.u('RT'), 1, 'session', 'claude', 0),
|
|
244
|
+
(pg_temp.u('ST'), pg_temp.u('RT'), pg_temp.u('RT'), 1, 'session', 'claude', 0);
|
|
245
|
+
update agent_board.agents set parent_agent_id = pg_temp.u('RT'), root_agent_id = pg_temp.u('RT') where id in (pg_temp.u('SE'), pg_temp.u('ST'));
|
|
246
|
+
select pg_temp.put('SE.epoch', '0'), pg_temp.put('ST.epoch', '0');
|
|
247
|
+
|
|
248
|
+
insert into agent_board.boards(id, slug, name, created_by_agent_id, visibility, status)
|
|
249
|
+
values ('670a2000-0000-4000-8000-000000000001', 'so670-x', 'SO670 X', pg_temp.u('A'), 'private', 'active'),
|
|
250
|
+
('670a2000-0000-4000-8000-000000000002', 'so670-y', 'SO670 Y', pg_temp.u('O'), 'private', 'active'),
|
|
251
|
+
('670a2000-0000-4000-8000-000000000003', 'so670-xa', 'SO670 XA', pg_temp.u('A'), 'private', 'active');
|
|
252
|
+
select pg_temp.put('X', '670a2000-0000-4000-8000-000000000001'), pg_temp.put('Y', '670a2000-0000-4000-8000-000000000002'),
|
|
253
|
+
pg_temp.put('XA', '670a2000-0000-4000-8000-000000000003');
|
|
254
|
+
insert into agent_board.board_memberships(board_id, agent_id, role, membership_state, created_by_agent_id)
|
|
255
|
+
select pg_temp.u('X'), pg_temp.u(h), case when h = 'A' then 'owner' else 'member' end, 'joined', pg_temp.u('A')
|
|
256
|
+
from unnest(array['A', 'B2', 'LM', 'DE', 'SU', 'CR', 'CE', 'SE', 'ST']) h;
|
|
257
|
+
insert into agent_board.board_memberships(board_id, agent_id, role, membership_state, created_by_agent_id)
|
|
258
|
+
values (pg_temp.u('Y'), pg_temp.u('O'), 'owner', 'joined', pg_temp.u('O')),
|
|
259
|
+
(pg_temp.u('XA'), pg_temp.u('A'), 'owner', 'joined', pg_temp.u('A')),
|
|
260
|
+
(pg_temp.u('XA'), pg_temp.u('Z'), 'member', 'joined', pg_temp.u('A'));
|
|
261
|
+
|
|
262
|
+
-- Threads. TX: A's thread, stamped to X by A's first post below. TD: A's private thread (DM and
|
|
263
|
+
-- multicast). TO: O's plain thread (A never had it). TY: O's thread on board Y. TAT: A's thread for
|
|
264
|
+
-- the atomicity fault on XA. TR: A's thread for the addressing routes.
|
|
265
|
+
select pg_temp.put('TX', pg_temp.val('A', $$ select thread_id::text from agent_board.create_or_find_thread(null, 'SO670 TX', 'so670-tx') $$));
|
|
266
|
+
select pg_temp.put('TD', pg_temp.val('A', $$ select thread_id::text from agent_board.create_or_find_thread(null, 'SO670 TD', 'so670-td') $$));
|
|
267
|
+
select pg_temp.put('TO', pg_temp.val('O', $$ select thread_id::text from agent_board.create_or_find_thread(null, 'SO670 TO', 'so670-to') $$));
|
|
268
|
+
select pg_temp.put('TY', pg_temp.val('O', $$ select thread_id::text from agent_board.create_or_find_thread(null, 'SO670 TY', 'so670-ty') $$));
|
|
269
|
+
select pg_temp.put('TAT', pg_temp.val('A', $$ select thread_id::text from agent_board.create_or_find_thread(null, 'SO670 TAT', 'so670-tat') $$));
|
|
270
|
+
select pg_temp.put('TR', pg_temp.val('A', $$ select thread_id::text from agent_board.create_or_find_thread(null, 'SO670 TR', 'so670-tr') $$));
|
|
271
|
+
|
|
272
|
+
-- The sends every later section reads back.
|
|
273
|
+
select pg_temp.put('M.K1', pg_temp.val('A', pg_temp.pbm('A', 'TX', 'X', 'K1')));
|
|
274
|
+
select pg_temp.put('M.O.TY', pg_temp.val('O', pg_temp.pbm('O', 'TY', 'Y', 'K1')));
|
|
275
|
+
select pg_temp.put('M.B2', pg_temp.val('B2', pg_temp.pbm('B2', 'TX', 'X', 'KB')));
|
|
276
|
+
select pg_temp.put('M.KDEL', pg_temp.val('A', pg_temp.pbm('A', 'TX', 'X', 'KDEL')));
|
|
277
|
+
select pg_temp.put('M.KP', pg_temp.val('A', pg_temp.pbm('A', 'TX', 'X', ' KP ')));
|
|
278
|
+
select pg_temp.put('M.KDM', pg_temp.val('A', pg_temp.dm('A', 'TD', 'R', 'KDM')));
|
|
279
|
+
select pg_temp.put('M.KMC', pg_temp.val('A', pg_temp.mc('A', 'TD', 'KMC', 'R', 'R2')));
|
|
280
|
+
select is(
|
|
281
|
+
(select count(*)::int from _h where k like 'M.%' and v ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$')
|
|
282
|
+
|| ' ' || (select coalesce(board_id::text, 'null') from agent_board.threads where id = pg_temp.u('TX')),
|
|
283
|
+
'7 ' || pg_temp.v('X'),
|
|
284
|
+
'SO-FIX-1: seven fixture sends returned message ids; TX is stamped to X');
|
|
285
|
+
|
|
286
|
+
-- ===========================================================================
|
|
287
|
+
-- D3. Storage: a private table
|
|
288
|
+
-- ===========================================================================
|
|
289
|
+
create temp table _so_roles as
|
|
290
|
+
select rolname::text as rolname from pg_roles
|
|
291
|
+
where rolname in ('anon', 'authenticated', 'service_role', 'agent_board_delegate');
|
|
292
|
+
select is((select count(*)::int from _so_roles), 4, 'SO-D3-0: the four API roles exist in pg_roles (the privilege rows are not vacuous)');
|
|
293
|
+
select is(
|
|
294
|
+
(select coalesce(string_agg(r.rolname || ':' || p, ' ' order by r.rolname, p), '(none)')
|
|
295
|
+
from _so_roles r
|
|
296
|
+
cross join unnest(array['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER']) p
|
|
297
|
+
where has_table_privilege(r.rolname, 'agent_board.send_outcomes', p))
|
|
298
|
+
|| ' public:' || (select count(*)::text from pg_class c, aclexplode(coalesce(c.relacl, acldefault('r', c.relowner))) a
|
|
299
|
+
where c.oid = 'agent_board.send_outcomes'::regclass and a.grantee = 0),
|
|
300
|
+
'(none) public:0',
|
|
301
|
+
'SO-M11: no API role and not PUBLIC holds any table privilege on send_outcomes');
|
|
302
|
+
select is(
|
|
303
|
+
(select relrowsecurity::text || '/' || (select count(*)::text from pg_policy where polrelid = 'agent_board.send_outcomes'::regclass)
|
|
304
|
+
from pg_class where oid = 'agent_board.send_outcomes'::regclass),
|
|
305
|
+
'true/0',
|
|
306
|
+
'SO-D3-2: RLS is enabled on send_outcomes with zero policies');
|
|
307
|
+
select is(
|
|
308
|
+
(select string_agg(a.attname || ':' || format_type(a.atttypid, a.atttypmod) || ':' || a.attnotnull::text, ' ' order by a.attnum)
|
|
309
|
+
from pg_attribute a where a.attrelid = 'agent_board.send_outcomes'::regclass and a.attnum > 0 and not a.attisdropped),
|
|
310
|
+
'id:uuid:true operation:text:true author_agent_id:uuid:true thread_id:uuid:true idempotency_key:text:true message_id:uuid:true board_id:uuid:false created_at:timestamp with time zone:true',
|
|
311
|
+
'SO-D3-3: send_outcomes holds exactly the D3 columns (no body, subject, recipients, counts, metadata or digest)');
|
|
312
|
+
-- foreign keys: author NO ACTION (mirrors messages.author_agent_id), message ON DELETE CASCADE
|
|
313
|
+
select is(
|
|
314
|
+
(select string_agg(a.attname || '->' || c.confrelid::regclass::text || ':' || c.confdeltype::text, ' ' order by a.attname)
|
|
315
|
+
from pg_constraint c join pg_attribute a on a.attrelid = c.conrelid and a.attnum = c.conkey[1]
|
|
316
|
+
where c.conrelid = 'agent_board.send_outcomes'::regclass and c.contype = 'f'),
|
|
317
|
+
'author_agent_id->agents:a message_id->messages:c',
|
|
318
|
+
'SO-D3-4: the author FK is NO ACTION and the message FK cascades');
|
|
319
|
+
-- the identity key and the message are each unique
|
|
320
|
+
select is(
|
|
321
|
+
(select string_agg(pg_get_constraintdef(c.oid), ' | ' order by pg_get_constraintdef(c.oid))
|
|
322
|
+
from pg_constraint c where c.conrelid = 'agent_board.send_outcomes'::regclass and c.contype = 'u'),
|
|
323
|
+
'UNIQUE (message_id) | UNIQUE (operation, author_agent_id, thread_id, idempotency_key)',
|
|
324
|
+
'SO-D3-5: unique(message_id) and unique(operation, author, thread, key)');
|
|
325
|
+
|
|
326
|
+
-- ===========================================================================
|
|
327
|
+
-- D4/D5. Function definitions and ACLs
|
|
328
|
+
-- ===========================================================================
|
|
329
|
+
select is(
|
|
330
|
+
(select coalesce(string_agg(r.rolname, ',' order by r.rolname), '-')
|
|
331
|
+
from _so_roles r where has_function_privilege(r.rolname, 'agent_board._record_send_outcome(text,uuid,text,uuid)', 'EXECUTE'))
|
|
332
|
+
|| ' public:' || (select count(*)::text from pg_proc p, aclexplode(coalesce(p.proacl, acldefault('f', p.proowner))) a
|
|
333
|
+
where p.oid = 'agent_board._record_send_outcome(text,uuid,text,uuid)'::regprocedure and a.grantee = 0),
|
|
334
|
+
'- public:0',
|
|
335
|
+
'SO-M12: no API role and not PUBLIC can EXECUTE _record_send_outcome');
|
|
336
|
+
select is(
|
|
337
|
+
(select coalesce(string_agg(r.rolname, ',' order by r.rolname), '-')
|
|
338
|
+
from _so_roles r where has_function_privilege(r.rolname, 'agent_board.get_send_outcome(uuid,text,text)', 'EXECUTE'))
|
|
339
|
+
|| ' public:' || (select count(*)::text from pg_proc p, aclexplode(coalesce(p.proacl, acldefault('f', p.proowner))) a
|
|
340
|
+
where p.oid = 'agent_board.get_send_outcome(uuid,text,text)'::regprocedure and a.grantee = 0),
|
|
341
|
+
'agent_board_delegate,authenticated public:0',
|
|
342
|
+
'SO-D5-ACL: get_send_outcome is EXECUTE-able by authenticated and agent_board_delegate only (not anon, service_role or PUBLIC)');
|
|
343
|
+
select is(
|
|
344
|
+
(select string_agg(p.proname || ':' || p.prosecdef::text || ':' || array_to_string(p.proconfig, ','), ' ' order by p.proname)
|
|
345
|
+
from pg_proc p where p.oid in ('agent_board._record_send_outcome(text,uuid,text,uuid)'::regprocedure,
|
|
346
|
+
'agent_board.get_send_outcome(uuid,text,text)'::regprocedure)),
|
|
347
|
+
'_record_send_outcome:true:search_path="" get_send_outcome:true:search_path=""',
|
|
348
|
+
'SO-D4D5-DEF: both functions are SECURITY DEFINER with an empty search_path');
|
|
349
|
+
select is(pg_get_function_result('agent_board.get_send_outcome(uuid,text,text)'::regprocedure),
|
|
350
|
+
'TABLE(status text, operation text, thread_id uuid, message_id uuid, board_id uuid, created_at timestamp with time zone)',
|
|
351
|
+
'SO-M7: the read returns exactly (status, operation, thread_id, message_id, board_id, created_at)');
|
|
352
|
+
select is((select provolatile::text from pg_proc where oid = 'agent_board.get_send_outcome(uuid,text,text)'::regprocedure),
|
|
353
|
+
's', 'SO-M17: the read is STABLE');
|
|
354
|
+
select is(
|
|
355
|
+
(select count(*)::int from agent_board.delegate_allowed_operations
|
|
356
|
+
where kind = 'rpc' and name = 'get_send_outcome' and signature = 'get_send_outcome(uuid, text, text)'),
|
|
357
|
+
1, 'SO-D5-DELEG: the read is in the delegate allow set (the grant and the row ship together)');
|
|
358
|
+
select ok(
|
|
359
|
+
obj_description('agent_board.send_outcomes'::regclass, 'pg_class') ~ '#670'
|
|
360
|
+
and obj_description('agent_board._record_send_outcome(text,uuid,text,uuid)'::regprocedure, 'pg_proc') ~ '#670'
|
|
361
|
+
and obj_description('agent_board.get_send_outcome(uuid,text,text)'::regprocedure, 'pg_proc') ~ 'never means not sent',
|
|
362
|
+
'SO-D4D5-COMMENT: the table, the helper and the read carry their comments');
|
|
363
|
+
|
|
364
|
+
-- ===========================================================================
|
|
365
|
+
-- Allow: the author recovers its own committed send
|
|
366
|
+
-- ===========================================================================
|
|
367
|
+
select is(pg_temp.read('A', 'TX', 'K1'), pg_temp.confirmed('M.K1', 'post_board_message', 'X'),
|
|
368
|
+
'SO-ALLOW-1: a session board post reads confirmed with exactly (operation, thread, message, board, created_at)');
|
|
369
|
+
select is(
|
|
370
|
+
(select author_agent_id::text || ' ' || thread_id::text || ' ' || idempotency_key || ' ' || board_id::text || ' ' ||
|
|
371
|
+
(created_at = (select m.created_at from agent_board.messages m where m.id = pg_temp.u('M.K1')))::text
|
|
372
|
+
from agent_board.send_outcomes where message_id = pg_temp.u('M.K1')),
|
|
373
|
+
pg_temp.v('A') || ' ' || pg_temp.v('TX') || ' K1 ' || pg_temp.v('X') || ' true',
|
|
374
|
+
'SO-ALLOW-2: the stored receipt is the session author, the thread, the key, the board and the message''s created_at');
|
|
375
|
+
select is(pg_temp.read('A', 'TD', 'KDM', 'send_direct_message') || ' ' || pg_temp.read('A', 'TD', 'KMC', 'send_multicast_message'),
|
|
376
|
+
pg_temp.confirmed('M.KDM', 'send_direct_message', null) || ' ' || pg_temp.confirmed('M.KMC', 'send_multicast_message', null),
|
|
377
|
+
'SO-ALLOW-3: direct and multicast sends read confirmed, with no board');
|
|
378
|
+
select is(pg_temp.read('A', 'TX', ' KP '), pg_temp.confirmed('M.KP', 'post_board_message', 'X'),
|
|
379
|
+
'SO-M16: a padded key reads confirmed (the read trims the key)');
|
|
380
|
+
select is((select idempotency_key from agent_board.send_outcomes where message_id = pg_temp.u('M.KP')), 'KP',
|
|
381
|
+
'SO-ALLOW-4: a padded send key is stored trimmed');
|
|
382
|
+
-- a signing client retry: same key, a fresh per-attempt signed_write envelope in p_metadata
|
|
383
|
+
select is(
|
|
384
|
+
pg_temp.val('A', pg_temp.pbm('A', 'TX', 'X', 'KSR', 'signed retry', '{"signed_write": {"nonce": "attempt-1"}}'))
|
|
385
|
+
= pg_temp.val('A', pg_temp.pbm('A', 'TX', 'X', 'KSR', 'signed retry', '{"signed_write": {"nonce": "attempt-2"}}')),
|
|
386
|
+
true,
|
|
387
|
+
'SO-ALLOW-5: a signing-client retry with a fresh envelope replays the same message id');
|
|
388
|
+
select is(pg_temp.receipts_key('A', 'KSR'), 1, 'SO-ALLOW-6: ...and the key still has exactly one receipt');
|
|
389
|
+
|
|
390
|
+
-- Membership removed after the send: the receipt stays readable, a fresh send is refused.
|
|
391
|
+
select pg_temp.put('M.KLM', pg_temp.val('LM', pg_temp.pbm('LM', 'TX', 'X', 'KLM')));
|
|
392
|
+
select is(pg_temp.as_('LM', format($$ select agent_board.leave_board(%L::uuid, %L::uuid, '{}'::jsonb) $$, pg_temp.v('X'), pg_temp.v('LM'))), -- pgtap-allow-raise-checked: as_ reports any error as text
|
|
393
|
+
'ok', 'SO-ALLOW-7: fixture: LM leaves X after its send');
|
|
394
|
+
select is(pg_temp.read('LM', 'TX', 'KLM'), pg_temp.confirmed('M.KLM', 'post_board_message', 'X'),
|
|
395
|
+
'SO-M5: a member removed after its send still reads confirmed (the read has no access check)');
|
|
396
|
+
select is(left(pg_temp.val('LM', pg_temp.pbm('LM', 'TX', 'X', 'KLM2')), 9), 'ERR 42501',
|
|
397
|
+
'SO-ALLOW-8: ...while a fresh send with a new key is refused 42501');
|
|
398
|
+
-- Demoted to observer after the send.
|
|
399
|
+
select pg_temp.put('M.KDE', pg_temp.val('DE', pg_temp.pbm('DE', 'TX', 'X', 'KDE')));
|
|
400
|
+
update agent_board.board_memberships set role = 'observer'
|
|
401
|
+
where board_id = pg_temp.u('X') and agent_id = pg_temp.u('DE') and membership_state = 'joined';
|
|
402
|
+
select is((select coalesce(string_agg(role, ',' order by role), '(none)') from agent_board.board_memberships
|
|
403
|
+
where board_id = pg_temp.u('X') and agent_id = pg_temp.u('DE') and membership_state = 'joined')
|
|
404
|
+
|| ' ' || left(pg_temp.val('DE', pg_temp.pbm('DE', 'TX', 'X', 'KDE2')), 9),
|
|
405
|
+
'observer ERR 42501', 'SO-ALLOW-9: fixture: DE is demoted to observer after its send (a new send is refused)');
|
|
406
|
+
select is(pg_temp.read('DE', 'TX', 'KDE'), pg_temp.confirmed('M.KDE', 'post_board_message', 'X'),
|
|
407
|
+
'SO-ALLOW-10: a member demoted to observer still reads confirmed');
|
|
408
|
+
|
|
409
|
+
-- ===========================================================================
|
|
410
|
+
-- Byte-identical 'unknown'
|
|
411
|
+
-- ===========================================================================
|
|
412
|
+
select is(pg_temp.val('A', format('select r::text from agent_board.get_send_outcome(%L::uuid, %L, %L) r', '670a9999-0000-4000-8000-000000000001', 'K1', 'post_board_message')),
|
|
413
|
+
'(unknown,,,,,)', 'SO-M4: a nonexistent thread reads the all-NULL unknown row (no existence check)');
|
|
414
|
+
select is(pg_temp.read('A', 'TO', 'K1'), '(unknown,,,,,)', 'SO-UNK-2: an existing thread the reader never had reads unknown');
|
|
415
|
+
select is(pg_temp.read('A', 'TY', 'K1'), '(unknown,,,,,)', 'SO-UNK-3: another board''s thread (its author used the same key) reads unknown');
|
|
416
|
+
select is(pg_temp.read('A', 'TX', 'K-NEVER'), '(unknown,,,,,)', 'SO-UNK-4: the reader''s own thread with a wrong key reads unknown');
|
|
417
|
+
select is(pg_temp.read('A', 'TX', 'K1', 'send_direct_message'), '(unknown,,,,,)', 'SO-UNK-5: the right key under the wrong operation reads unknown');
|
|
418
|
+
select is(pg_temp.read('A', 'TX', 'KB'), '(unknown,,,,,)', 'SO-UNK-6: another author''s matching key reads unknown');
|
|
419
|
+
-- the owner of the deleted message reads confirmed first
|
|
420
|
+
select is(pg_temp.read('A', 'TX', 'KDEL'), pg_temp.confirmed('M.KDEL', 'post_board_message', 'X'),
|
|
421
|
+
'SO-UNK-7a: control: before deletion the message reads confirmed');
|
|
422
|
+
select is(pg_temp.try_owner(format('delete from agent_board.messages where id = %L', pg_temp.v('M.KDEL'))), 'ok',
|
|
423
|
+
'SO-UNK-7b: fixture: the message is deleted (the receipt does not block it)');
|
|
424
|
+
select is(pg_temp.read('A', 'TX', 'KDEL'), '(unknown,,,,,)',
|
|
425
|
+
'SO-M13: a deleted message reads unknown (its receipt cascaded with it)');
|
|
426
|
+
select is(pg_temp.receipts_key('A', 'KDEL'), 0, 'SO-UNK-7c: no receipt survives its message');
|
|
427
|
+
|
|
428
|
+
-- ===========================================================================
|
|
429
|
+
-- REJECT: minting
|
|
430
|
+
-- ===========================================================================
|
|
431
|
+
-- service_role names a non-member author
|
|
432
|
+
select pg_temp.put('M.SVC', pg_temp.val('SVC', pg_temp.pbm('NA', 'TX', 'X', 'KSVC')));
|
|
433
|
+
select is(pg_temp.receipts_for('M.SVC') || ' ' || pg_temp.read('NA', 'TX', 'KSVC')
|
|
434
|
+
|| ' ' || (select author_agent_id::text from agent_board.messages where id = pg_temp.u('M.SVC')),
|
|
435
|
+
'0 (unknown,,,,,) ' || pg_temp.v('NA'),
|
|
436
|
+
'SO-MINT-1: a service_role post naming non-member NA lands as NA but mints no receipt; NA reads unknown');
|
|
437
|
+
-- service_role JWT carrying A's sub (current_agent_id() resolves A)
|
|
438
|
+
select is(pg_temp.val('SVCA', 'select agent_board.current_agent_id()::text'), pg_temp.v('A'),
|
|
439
|
+
'SO-MINT-2a: control: a service_role JWT carrying A''s sub resolves current_agent_id() to A');
|
|
440
|
+
select pg_temp.put('M.SVCA', pg_temp.val('SVCA', pg_temp.pbm('A', 'TX', 'X', 'KSA')));
|
|
441
|
+
select is(pg_temp.receipts_for('M.SVCA') || ' ' || pg_temp.read('A', 'TX', 'KSA')
|
|
442
|
+
|| ' ' || (select author_agent_id::text from agent_board.messages where id = pg_temp.u('M.SVCA')),
|
|
443
|
+
'0 (unknown,,,,,) ' || pg_temp.v('A'),
|
|
444
|
+
'SO-M1: a service_role JWT carrying A''s sub posts as A but mints no receipt');
|
|
445
|
+
-- the signed wrappers with a VERIFIED decision for A
|
|
446
|
+
select pg_temp.put('M.SIG.P', pg_temp.val('SVC', pg_temp.signed('signed_post_board_message', jsonb_build_object(
|
|
447
|
+
'p_thread_id', pg_temp.v('TX'), 'p_author_agent_id', pg_temp.v('A'), 'p_board_id', pg_temp.v('X'),
|
|
448
|
+
'p_subject', 's', 'p_body', 'signed post', 'p_idempotency_key', 'KSIGP'), 'A')));
|
|
449
|
+
select pg_temp.put('M.SIG.D', pg_temp.val('SVC', pg_temp.signed('signed_send_direct_message', jsonb_build_object(
|
|
450
|
+
'p_thread_id', pg_temp.v('TD'), 'p_author_agent_id', pg_temp.v('A'), 'p_recipient_agent_id', pg_temp.v('R'),
|
|
451
|
+
'p_subject', 's', 'p_body', 'signed dm', 'p_idempotency_key', 'KSIGD'), 'A')));
|
|
452
|
+
select pg_temp.put('M.SIG.M', pg_temp.val('SVC', pg_temp.signed('signed_send_multicast_message', jsonb_build_object(
|
|
453
|
+
'p_thread_id', pg_temp.v('TD'), 'p_author_agent_id', pg_temp.v('A'), 'p_recipient_agent_ids', jsonb_build_array(pg_temp.v('R'), pg_temp.v('R2')),
|
|
454
|
+
'p_subject', 's', 'p_body', 'signed mc', 'p_idempotency_key', 'KSIGM'), 'A')));
|
|
455
|
+
select is(
|
|
456
|
+
(select count(*)::int from _h where k like 'M.SIG.%' and v ~* '^[0-9a-f]{8}-') || ' '
|
|
457
|
+
|| (pg_temp.receipts_for('M.SIG.P') + pg_temp.receipts_for('M.SIG.D') + pg_temp.receipts_for('M.SIG.M')) || ' '
|
|
458
|
+
|| pg_temp.read('A', 'TX', 'KSIGP') || ' ' || pg_temp.read('A', 'TD', 'KSIGD', 'send_direct_message')
|
|
459
|
+
|| ' ' || pg_temp.read('A', 'TD', 'KSIGM', 'send_multicast_message'),
|
|
460
|
+
'3 0 (unknown,,,,,) (unknown,,,,,) (unknown,,,,,)',
|
|
461
|
+
'SO-MINT-3: the three signed wrappers with a verified decision naming A all land and mint nothing; A reads unknown');
|
|
462
|
+
-- a session replay of a message a service_role send created
|
|
463
|
+
select pg_temp.put('M.RP.svc', pg_temp.val('SVC', pg_temp.pbm('A', 'TX', 'X', 'KRP')));
|
|
464
|
+
select pg_temp.put('M.RP.session', pg_temp.val('A', pg_temp.pbm('A', 'TX', 'X', 'KRP', 'a changed body')));
|
|
465
|
+
select is(
|
|
466
|
+
(pg_temp.v('M.RP.session') = pg_temp.v('M.RP.svc'))::text || ' ' || pg_temp.receipts_key('A', 'KRP') || ' ' || pg_temp.read('A', 'TX', 'KRP'),
|
|
467
|
+
'true 0 (unknown,,,,,)',
|
|
468
|
+
'SO-M3: a session replay of a service_role-created message returns the original id and still reads unknown');
|
|
469
|
+
-- post_message carrying forged replay metadata
|
|
470
|
+
select is(
|
|
471
|
+
pg_temp.as_('A', format($$ select * from agent_board.post_message(%L::uuid, %L::uuid, 'forged reply', 'comment', null,
|
|
472
|
+
'{"rpcName": "post_board_message", "idempotencyKey": "KF"}'::jsonb) $$, pg_temp.v('TX'), pg_temp.v('A'))),
|
|
473
|
+
'ok', 'SO-MINT-5a: fixture: post_message accepts caller metadata naming rpcName/idempotencyKey');
|
|
474
|
+
select is(
|
|
475
|
+
(select count(*)::int from agent_board.messages where thread_id = pg_temp.u('TX') and metadata ->> 'idempotencyKey' = 'KF') || ' '
|
|
476
|
+
|| pg_temp.receipts_key('A', 'KF') || ' ' || pg_temp.read('A', 'TX', 'KF'),
|
|
477
|
+
'1 0 (unknown,,,,,)',
|
|
478
|
+
'SO-M14: post_message with forged {rpcName, idempotencyKey} metadata mints no receipt and reads unknown');
|
|
479
|
+
|
|
480
|
+
-- ===========================================================================
|
|
481
|
+
-- REJECT: the read's inputs (22023)
|
|
482
|
+
-- ===========================================================================
|
|
483
|
+
select throws_ok(format('select pg_temp.raise_as(%L, %L)', 'A', pg_temp.so_raw(null, 'K1', 'post_board_message')), '22023', null,
|
|
484
|
+
'SO-IN-1: a null thread is 22023');
|
|
485
|
+
select throws_ok(format('select pg_temp.raise_as(%L, %L)', 'A', pg_temp.so_raw(pg_temp.u('TX'), null, 'post_board_message')), '22023', null,
|
|
486
|
+
'SO-IN-2: a null key is 22023');
|
|
487
|
+
select throws_ok(format('select pg_temp.raise_as(%L, %L)', 'A', pg_temp.so_raw(pg_temp.u('TX'), ' ', 'post_board_message')), '22023', null,
|
|
488
|
+
'SO-IN-3: a blank key is 22023');
|
|
489
|
+
select throws_ok(format('select pg_temp.raise_as(%L, %L)', 'A', pg_temp.so_raw(pg_temp.u('TX'), 'K1', 'post_message')), '22023', null,
|
|
490
|
+
'SO-IN-4: an operation outside the closed set is 22023');
|
|
491
|
+
select throws_ok(format('select pg_temp.raise_as(%L, %L)', 'A', pg_temp.so_raw(pg_temp.u('TX'), 'K1', 'create_tasking_delivery')), '22023', null,
|
|
492
|
+
'SO-IN-5: keyed tasking is refused 22023, not answered unknown');
|
|
493
|
+
|
|
494
|
+
-- ===========================================================================
|
|
495
|
+
-- Delegates: own receipt confirmed, the owner's read is unknown
|
|
496
|
+
-- ===========================================================================
|
|
497
|
+
insert into agent_board.identity_providers
|
|
498
|
+
(issuer, audience, supported, policy_approved, allowed_scopes, allowed_board_scopes, allowed_agent_kinds)
|
|
499
|
+
values ('https://portal.so670.test', 'message-board', true, true, '{}', '{}', '{agent,human}');
|
|
500
|
+
do $$
|
|
501
|
+
declare r record;
|
|
502
|
+
begin
|
|
503
|
+
perform set_config('request.jwt.claims', '', true);
|
|
504
|
+
select * into r from agent_board.enroll_federated_human(
|
|
505
|
+
'https://portal.so670.test', 'message-board', 'so670-owner',
|
|
506
|
+
jsonb_build_object('sub', 'so670-owner', 'email', 'so670-owner@hosted.test', 'email_verified', true), 'SO670 Owner');
|
|
507
|
+
perform pg_temp.put('OW', r.agent_id::text);
|
|
508
|
+
perform pg_temp.put('OW.auth', r.auth_user_id::text);
|
|
509
|
+
perform pg_temp.put('OW.role', 'authenticated');
|
|
510
|
+
select * into r from agent_board.exchange_delegate_credential(jsonb_build_object(
|
|
511
|
+
'iss', 'https://portal.so670.test', 'sub', 'so670-owner', 'slot_resource', null,
|
|
512
|
+
'agent_board_delegate_instance', jsonb_build_object(
|
|
513
|
+
'instance_id', 'so670_instance_aaaaaaaa', 'grant_id', 'so670_grant_aaaaaaaaa_1', 'generation', 1,
|
|
514
|
+
'owner_sub', 'so670-owner', 'client_id', 'so670-client', 'status', 'active', 'client_change', null)));
|
|
515
|
+
perform pg_temp.put('DG', r.agent_id::text);
|
|
516
|
+
perform pg_temp.put('DG.auth', r.auth_user_id::text);
|
|
517
|
+
perform pg_temp.put('DG.cred', r.credential_id::text);
|
|
518
|
+
perform pg_temp.put('DG.role', 'agent_board_delegate');
|
|
519
|
+
end $$;
|
|
520
|
+
insert into agent_board.board_memberships(board_id, agent_id, role, membership_state, created_by_agent_id)
|
|
521
|
+
values (pg_temp.u('X'), pg_temp.u('OW'), 'member', 'joined', pg_temp.u('A'));
|
|
522
|
+
select pg_temp.put('DG.placed', pg_temp.as_('OW', format($$ select agent_board.add_delegate_to_board(%L::uuid, %L::uuid) $$, pg_temp.v('DG'), pg_temp.v('X'))));
|
|
523
|
+
select pg_temp.put('TDG', pg_temp.val('DG', $$ select thread_id::text from agent_board.create_or_find_thread(null, 'SO670 delegate', 'so670-dg') $$));
|
|
524
|
+
select pg_temp.put('M.KDG', pg_temp.val('DG', format($$ select message_id::text from agent_board.post_board_message(%L::uuid, null, %L::uuid, 's', 'delegate post', 'comment', 'medium', 'KDG', '{}'::jsonb) $$,
|
|
525
|
+
pg_temp.v('TDG'), pg_temp.v('X'))));
|
|
526
|
+
select is(pg_temp.v('DG.placed') || ' ' || pg_temp.read('DG', 'TDG', 'KDG') || ' ' || (select author_agent_id::text from agent_board.messages where id = pg_temp.u('M.KDG')),
|
|
527
|
+
'ok ' || pg_temp.confirmed('M.KDG', 'post_board_message', 'X') || ' ' || pg_temp.v('DG'),
|
|
528
|
+
'SO-ALLOW-11: a hosted delegate reads its own receipt confirmed');
|
|
529
|
+
select is(pg_temp.read('OW', 'TDG', 'KDG'), '(unknown,,,,,)', 'SO-ALLOW-12: the delegate''s owner reads the same (thread, key) unknown');
|
|
530
|
+
|
|
531
|
+
-- ===========================================================================
|
|
532
|
+
-- REJECT: the read's identity (42501), each followed by a zero-row state check
|
|
533
|
+
-- ===========================================================================
|
|
534
|
+
-- Every identity below first sends (so a receipt exists that a broken refusal would disclose),
|
|
535
|
+
-- then loses its principal through the operation named.
|
|
536
|
+
select pg_temp.put('M.SU', pg_temp.val('SU', pg_temp.pbm('SU', 'TX', 'X', 'KSU')));
|
|
537
|
+
select pg_temp.put('M.CR', pg_temp.val('CR', pg_temp.pbm('CR', 'TX', 'X', 'KCR')));
|
|
538
|
+
select pg_temp.put('M.CE', pg_temp.val('CE', pg_temp.pbm('CE', 'TX', 'X', 'KCE')));
|
|
539
|
+
select pg_temp.put('M.SE', pg_temp.val('SE', pg_temp.pbm('SE', 'TX', 'X', 'KSE')));
|
|
540
|
+
select pg_temp.put('M.ST', pg_temp.val('ST', pg_temp.pbm('ST', 'TX', 'X', 'KST')));
|
|
541
|
+
select is(
|
|
542
|
+
(select string_agg(h || '=' || pg_temp.receipts_for('M.' || h), ' ' order by h) from unnest(array['CE', 'CR', 'SE', 'ST', 'SU']) h)
|
|
543
|
+
|| ' ' || pg_temp.read('ST', 'TX', 'KST'),
|
|
544
|
+
'CE=1 CR=1 SE=1 ST=1 SU=1 ' || pg_temp.confirmed('M.ST', 'post_board_message', 'X'),
|
|
545
|
+
'SO-ID-0: control: the five identities each hold a receipt and can read it before losing their principal');
|
|
546
|
+
update agent_board.agents set status = 'suspended' where id = pg_temp.u('SU');
|
|
547
|
+
update agent_board.agent_credentials set revoked_at = now() where id = pg_temp.u('CR.cred');
|
|
548
|
+
update agent_board.agent_credentials set expires_at = now() - interval '1 second' where id = pg_temp.u('CE.cred');
|
|
549
|
+
update agent_board.session_identities set credential_epoch = 1 where agent_id = pg_temp.u('SE');
|
|
550
|
+
-- The two tombstones run through their real operations, as a direct postgres session.
|
|
551
|
+
select pg_temp.ident(null, null);
|
|
552
|
+
select lives_ok(format('select agent_board._revoke_agent_principal(%L::uuid, %L::uuid, %L)', pg_temp.v('ST'), pg_temp.v('A'), 'so670 tombstone'),
|
|
553
|
+
'SO-ID-TOMB-1: the session identity is revoked through _revoke_agent_principal');
|
|
554
|
+
select lives_ok($$ select * from agent_board.tombstone_delegate_instance('so670_instance_aaaaaaaa', 'https://portal.so670.test', 'so670-owner', 'owner_revoked') $$,
|
|
555
|
+
'SO-ID-TOMB-2: the delegate instance is tombstoned through tombstone_delegate_instance');
|
|
556
|
+
-- D10: measure, do not assume, what current_agent_id() returns after each tombstone
|
|
557
|
+
select is(
|
|
558
|
+
(select tombstoned_at is not null from agent_board.session_identities where agent_id = pg_temp.u('ST'))::text || ' '
|
|
559
|
+
|| (select tombstoned_at is not null from agent_board.delegate_instances where instance_id = 'so670_instance_aaaaaaaa')::text || ' '
|
|
560
|
+
|| pg_temp.val('ST', 'select agent_board.current_agent_id()::text') || ' '
|
|
561
|
+
|| pg_temp.val('DG', 'select agent_board.current_agent_id()::text'),
|
|
562
|
+
'true true (null) (null)',
|
|
563
|
+
'SO-ID-TOMB-3: both tombstones are recorded, and current_agent_id() resolves NULL for each');
|
|
564
|
+
select pg_temp.put('ST.epoch', '1');
|
|
565
|
+
select is(pg_temp.val('ST', 'select agent_board.current_agent_id()::text'), '(null)',
|
|
566
|
+
'SO-ID-TOMB-4: the tombstoned session identity also resolves NULL at its bumped epoch');
|
|
567
|
+
select pg_temp.put('ST.epoch', '0');
|
|
568
|
+
|
|
569
|
+
create temp table _so_reject(ord int primary key, who text, thread text, key text, label text);
|
|
570
|
+
insert into _so_reject values
|
|
571
|
+
(1, 'ANON', 'TX', 'K1', 'anon (no EXECUTE)'),
|
|
572
|
+
(2, 'NOAGENT', 'TX', 'K1', 'authenticated with no agent'),
|
|
573
|
+
(3, 'SU', 'TX', 'KSU', 'a suspended agent'),
|
|
574
|
+
(4, 'CR', 'TX', 'KCR', 'a revoked credential'),
|
|
575
|
+
(5, 'CE', 'TX', 'KCE', 'an expired credential'),
|
|
576
|
+
(6, 'SE', 'TX', 'KSE', 'a stale identity epoch'),
|
|
577
|
+
(7, 'ST', 'TX', 'KST', 'a tombstoned session identity'),
|
|
578
|
+
(8, 'DG', 'TDG', 'KDG', 'a tombstoned delegate'),
|
|
579
|
+
(9, 'SVC', 'TX', 'K1', 'a service_role JWT'),
|
|
580
|
+
(10, 'PG', 'TX', 'K1', 'a direct postgres session with no JWT role');
|
|
581
|
+
-- ten throws_ok, then ten zero-row checks
|
|
582
|
+
select throws_ok(format('select pg_temp.raise_as(%L, %L)', who, pg_temp.so(thread, key)), '42501', null,
|
|
583
|
+
'SO-ID-' || ord || 'a: ' || label || ' is refused 42501')
|
|
584
|
+
from _so_reject order by ord;
|
|
585
|
+
select is(pg_temp.nrows(who, pg_temp.so(thread, key)), 0, 'SO-ID-' || ord || 'b: ' || label || ' gets zero rows')
|
|
586
|
+
from _so_reject order by ord;
|
|
587
|
+
-- service_role carrying A's sub: current_agent_id() = A, so only _is_service_role() refuses (M6)
|
|
588
|
+
select throws_ok(format('select pg_temp.raise_as(%L, %L)', 'SVCA', pg_temp.so('TX', 'K1')), '42501', null,
|
|
589
|
+
'SO-M6: a service_role JWT carrying A''s sub (current_agent_id() = A) is refused 42501');
|
|
590
|
+
select is(pg_temp.nrows('SVCA', pg_temp.so('TX', 'K1')), 0, 'SO-ID-11b: a service_role JWT carrying A''s sub gets zero rows');
|
|
591
|
+
|
|
592
|
+
-- ===========================================================================
|
|
593
|
+
-- Atomicity: a delivery fault rolls back the message AND its receipt
|
|
594
|
+
-- ===========================================================================
|
|
595
|
+
create function agent_board_test.zz670_refuse_delivery() returns trigger language plpgsql as $f$
|
|
596
|
+
begin
|
|
597
|
+
if new.recipient_agent_id = '670a1000-0000-4000-8000-000000000014'::uuid then
|
|
598
|
+
raise exception 'so670 injected delivery fault';
|
|
599
|
+
end if;
|
|
600
|
+
return new;
|
|
601
|
+
end $f$;
|
|
602
|
+
create trigger zz670_refuse_delivery before insert or update on agent_board.delivery_recipients
|
|
603
|
+
for each row execute function agent_board_test.zz670_refuse_delivery();
|
|
604
|
+
select is(pg_temp.as_('A', pg_temp.pbm('A', 'TAT', 'XA', 'KAT')), 'P0001:so670 injected delivery fault',
|
|
605
|
+
'SO-ATOM-1: the send to XA raises on the injected delivery fault for its sentinel member');
|
|
606
|
+
select is(
|
|
607
|
+
(select count(*)::int from agent_board.messages where metadata ->> 'idempotencyKey' = 'KAT') || ' ' || pg_temp.receipts_key('A', 'KAT'),
|
|
608
|
+
'0 0',
|
|
609
|
+
'SO-M8: the failed send leaves zero messages and zero receipts for its key');
|
|
610
|
+
drop trigger zz670_refuse_delivery on agent_board.delivery_recipients;
|
|
611
|
+
drop function agent_board_test.zz670_refuse_delivery();
|
|
612
|
+
|
|
613
|
+
-- ===========================================================================
|
|
614
|
+
-- D9. The class, from the catalog (AGENTS.md rule 1)
|
|
615
|
+
-- ===========================================================================
|
|
616
|
+
-- PREDICATE: every agent_board routine, by full signature, that (i) reaches an INSERT/MERGE into
|
|
617
|
+
-- agent_board.messages through the name-resolved call closure (fn_call_closure over
|
|
618
|
+
-- stripped_body; call_re / stmt_re, case-insensitive), and (ii) accepts an idempotency key: an
|
|
619
|
+
-- argument named p_idempotency_key, or a literal 'p_idempotency_key' it extracts from a jsonb
|
|
620
|
+
-- argument. Nonce/key helpers with no messages sink fall outside by (i), not by a list.
|
|
621
|
+
-- BOUNDARY: calls are matched by name, so dynamic SQL is not followed; SO-D9-6 fails if any routine
|
|
622
|
+
-- in (i) or (ii) contains EXECUTE at all.
|
|
623
|
+
create function pg_temp.so_fns() returns table(oid oid, signature text, body text, lit_body text, takes_key boolean)
|
|
624
|
+
language sql as $d$
|
|
625
|
+
select p.oid,
|
|
626
|
+
p.proname || '(' || replace(oidvectortypes(p.proargtypes), ' ', '') || ')' as signature,
|
|
627
|
+
agent_board_test.stripped_body(p.oid) as body,
|
|
628
|
+
agent_board_test.stripped_body(p.oid, true) as lit_body,
|
|
629
|
+
('p_idempotency_key' = any(coalesce(p.proargnames, '{}'))
|
|
630
|
+
or agent_board_test.stripped_body(p.oid, true) ~ '''p_idempotency_key''') as takes_key
|
|
631
|
+
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
|
632
|
+
where n.nspname = 'agent_board' and p.prokind in ('f', 'p')
|
|
633
|
+
and p.prolang <> (select oid from pg_language where lanname = 'internal')
|
|
634
|
+
$d$;
|
|
635
|
+
create function pg_temp.so_reach() returns table(oid oid) language sql as $d$
|
|
636
|
+
select c.root_oid from agent_board_test.fn_call_closure() c
|
|
637
|
+
where c.reached_oid in (select f.oid from pg_temp.so_fns() f where f.body ~ agent_board_test.stmt_re('insert into|merge into', 'messages'))
|
|
638
|
+
$d$;
|
|
639
|
+
create function pg_temp.so_class() returns table(signature text) language sql as $d$
|
|
640
|
+
select f.signature from pg_temp.so_fns() f where f.takes_key and f.oid in (select oid from pg_temp.so_reach())
|
|
641
|
+
$d$;
|
|
642
|
+
create temp table _so_fns as select * from pg_temp.so_fns();
|
|
643
|
+
create temp table _so_class as select * from pg_temp.so_class();
|
|
644
|
+
create temp table _so_disp(signature text primary key, disposition text not null);
|
|
645
|
+
insert into _so_disp values
|
|
646
|
+
('post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)', 'MINTS'),
|
|
647
|
+
('send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)', 'MINTS'),
|
|
648
|
+
('send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb)', 'MINTS'),
|
|
649
|
+
('post_board_message(uuid,uuid,text,text,text,text,text,text,text,jsonb)', 'ROUTES'),
|
|
650
|
+
('send_direct_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb)', 'ROUTES'),
|
|
651
|
+
('send_multicast_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb)', 'ROUTES'),
|
|
652
|
+
('signed_post_board_message(jsonb,jsonb)', 'SERVICE-ONLY'),
|
|
653
|
+
('signed_send_direct_message(jsonb,jsonb)', 'SERVICE-ONLY'),
|
|
654
|
+
('signed_send_multicast_message(jsonb,jsonb)', 'SERVICE-ONLY');
|
|
655
|
+
select is(
|
|
656
|
+
(select coalesce(string_agg(signature, ' ' order by signature), '(none)') from (select signature from _so_class except select signature from _so_disp) d)
|
|
657
|
+
|| ' || stale: ' ||
|
|
658
|
+
(select coalesce(string_agg(signature, ' ' order by signature), '(none)') from (select signature from _so_disp except select signature from _so_class) d),
|
|
659
|
+
'(none) || stale: (none)',
|
|
660
|
+
'SO-D9-1: the keyed messages-writer class equals the dispositioned set exactly (a new site, or a stale row, names itself)');
|
|
661
|
+
-- MINTS: each core calls the helper
|
|
662
|
+
select is(
|
|
663
|
+
(select string_agg(d.signature || '=' || (f.body ~ agent_board_test.call_re('_record_send_outcome'))::text, ' ' order by d.signature)
|
|
664
|
+
from _so_disp d join _so_fns f on f.signature = d.signature where d.disposition = 'MINTS'),
|
|
665
|
+
'post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)=true send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)=true send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb)=true',
|
|
666
|
+
'SO-D9-2: MINTS: each UUID core calls _record_send_outcome');
|
|
667
|
+
-- MINTS, behaviourally: exactly one receipt per covered core send, with its operation
|
|
668
|
+
select is(
|
|
669
|
+
(select string_agg(operation || ':' || count, ' ' order by operation) from (
|
|
670
|
+
select operation, count(*)::text as count from agent_board.send_outcomes
|
|
671
|
+
where message_id in (pg_temp.u('M.K1'), pg_temp.u('M.KDM'), pg_temp.u('M.KMC')) group by operation) s),
|
|
672
|
+
'post_board_message:1 send_direct_message:1 send_multicast_message:1',
|
|
673
|
+
'SO-D9-3: MINTS: a session send through each core yields exactly one receipt of its operation');
|
|
674
|
+
-- ROUTES: calls its same-named core and writes no receipt itself
|
|
675
|
+
select is(
|
|
676
|
+
(select string_agg(d.signature || '=' || (f.body ~ agent_board_test.call_re(split_part(d.signature, '(', 1))
|
|
677
|
+
and f.body !~ agent_board_test.call_re('_record_send_outcome')
|
|
678
|
+
and f.body !~ agent_board_test.writes_any_re(array['send_outcomes']))::text, ' ' order by d.signature)
|
|
679
|
+
from _so_disp d join _so_fns f on f.signature = d.signature where d.disposition = 'ROUTES'),
|
|
680
|
+
'post_board_message(uuid,uuid,text,text,text,text,text,text,text,jsonb)=true send_direct_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb)=true send_multicast_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb)=true',
|
|
681
|
+
'SO-D9-4: ROUTES: each addressing overload calls its core and records nothing itself');
|
|
682
|
+
-- ROUTES, behaviourally: a session send through each overload yields exactly one receipt
|
|
683
|
+
select pg_temp.put('M.R1', pg_temp.val('A', format($$ select message_id::text from agent_board.post_board_message(%L::uuid, %L::uuid, 'so670-x', 'addressing_v1', 's', 'by slug', 'comment', 'medium', 'KR1', '{}'::jsonb) $$,
|
|
684
|
+
pg_temp.v('TR'), pg_temp.v('A'))));
|
|
685
|
+
select pg_temp.put('M.R2', pg_temp.val('A', format($$ select message_id::text from agent_board.send_direct_message(%L::uuid, %L::uuid, %L::jsonb, 'addressing_v1', 's', 'by descriptor', 'comment', 'medium', 'KR2', '{}'::jsonb) $$,
|
|
686
|
+
pg_temp.v('TD'), pg_temp.v('A'), jsonb_build_object('kind', 'agent_id', 'agentId', pg_temp.v('R'))::text)));
|
|
687
|
+
select pg_temp.put('M.R3', pg_temp.val('A', format($$ select message_id::text from agent_board.send_multicast_message(%L::uuid, %L::uuid, %L::jsonb, 'addressing_v1', 's', 'by descriptors', 'comment', 'medium', 'KR3', '{}'::jsonb) $$,
|
|
688
|
+
pg_temp.v('TD'), pg_temp.v('A'), jsonb_build_array(jsonb_build_object('kind', 'agent_id', 'agentId', pg_temp.v('R')),
|
|
689
|
+
jsonb_build_object('kind', 'agent_id', 'agentId', pg_temp.v('R2')))::text)));
|
|
690
|
+
select is(
|
|
691
|
+
pg_temp.receipts_for('M.R1')::text || pg_temp.receipts_for('M.R2')::text || pg_temp.receipts_for('M.R3')::text || ' '
|
|
692
|
+
|| (pg_temp.read('A', 'TR', 'KR1') = pg_temp.confirmed('M.R1', 'post_board_message', 'X'))::text
|
|
693
|
+
|| (pg_temp.read('A', 'TD', 'KR2', 'send_direct_message') = pg_temp.confirmed('M.R2', 'send_direct_message', null))::text
|
|
694
|
+
|| (pg_temp.read('A', 'TD', 'KR3', 'send_multicast_message') = pg_temp.confirmed('M.R3', 'send_multicast_message', null))::text,
|
|
695
|
+
'111 truetruetrue',
|
|
696
|
+
'SO-D9-5: ROUTES: a session send through each addressing overload yields one receipt that reads confirmed');
|
|
697
|
+
-- SERVICE-ONLY: no end-user API role can execute a signed wrapper (behaviour: SO-MINT-3)
|
|
698
|
+
select is(
|
|
699
|
+
(select coalesce(string_agg(d.signature || ':' || r, ' ' order by d.signature, r), '(none)')
|
|
700
|
+
from _so_disp d cross join unnest(array['anon', 'authenticated', 'agent_board_delegate']) r
|
|
701
|
+
where d.disposition = 'SERVICE-ONLY' and has_function_privilege(r, ('agent_board.' || d.signature)::regprocedure, 'EXECUTE')),
|
|
702
|
+
'(none)',
|
|
703
|
+
'SO-D9-7: SERVICE-ONLY: anon, authenticated and agent_board_delegate cannot execute any signed wrapper');
|
|
704
|
+
-- the boundary: no routine in (i) or (ii) contains EXECUTE
|
|
705
|
+
select is(
|
|
706
|
+
(select coalesce(string_agg(signature, ' ' order by signature), '(none)') from _so_fns
|
|
707
|
+
where (oid in (select oid from pg_temp.so_reach()) or takes_key)
|
|
708
|
+
and body ~ agent_board_test.re(agent_board_test.lead_guard() || agent_board_test.ci_re('execute') || agent_board_test.trail_guard())),
|
|
709
|
+
'(none)',
|
|
710
|
+
'SO-D9-6: BOUNDARY: no messages-reaching or key-accepting routine contains EXECUTE (the name-matched closure is complete for them)');
|
|
711
|
+
-- the receipt table's writers, callers and readers, from the catalog
|
|
712
|
+
select is(
|
|
713
|
+
(select coalesce(string_agg(signature, ' ' order by signature), '(none)') from _so_fns where body ~ agent_board_test.writes_any_re(array['send_outcomes'])),
|
|
714
|
+
'_record_send_outcome(text,uuid,text,uuid)',
|
|
715
|
+
'SO-M15: the only writer of send_outcomes is _record_send_outcome');
|
|
716
|
+
select is(
|
|
717
|
+
(select coalesce(string_agg(signature, ' ' order by signature), '(none)') from _so_fns where body ~ agent_board_test.call_re('_record_send_outcome')),
|
|
718
|
+
'post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb) send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb) send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb)',
|
|
719
|
+
'SO-D9-8: the only callers of _record_send_outcome are the three UUID cores');
|
|
720
|
+
select is(
|
|
721
|
+
(select coalesce(string_agg(signature, ' ' order by signature), '(none)') from _so_fns where body ~ agent_board_test.qname_re('send_outcomes')),
|
|
722
|
+
'_record_send_outcome(text,uuid,text,uuid) get_send_outcome(uuid,text,text)',
|
|
723
|
+
'SO-D9-9: the only routines that name send_outcomes are get_send_outcome and _record_send_outcome');
|
|
724
|
+
-- the read touches no other agent_board relation
|
|
725
|
+
select is(
|
|
726
|
+
(select coalesce(string_agg(c.relname, ' ' order by c.relname), '(none)')
|
|
727
|
+
from pg_class c join pg_namespace n on n.oid = c.relnamespace
|
|
728
|
+
where n.nspname = 'agent_board' and c.relkind in ('r', 'v', 'm', 'p', 'f') and c.relname <> 'send_outcomes'
|
|
729
|
+
and (select body from _so_fns where signature = 'get_send_outcome(uuid,text,text)') ~ agent_board_test.qname_re(c.relname)),
|
|
730
|
+
'(none)',
|
|
731
|
+
'SO-D9-10: get_send_outcome names no relation but send_outcomes (no thread, board, message or membership read)');
|
|
732
|
+
-- discriminance: the derivations name planted sites of each kind (rolled back with the file)
|
|
733
|
+
create function agent_board.zz670_probe_route(p_idempotency_key text) returns void language plpgsql as $f$
|
|
734
|
+
begin perform agent_board._insert_thread_message(null, null, 'probe'); end $f$;
|
|
735
|
+
create function agent_board.zz670_probe_writer() returns void language plpgsql as $f$
|
|
736
|
+
begin insert into agent_board.send_outcomes(operation) values ('post_board_message'); end $f$;
|
|
737
|
+
select is(
|
|
738
|
+
(select string_agg(signature, ' ' order by signature) from pg_temp.so_class() where signature like 'zz670\_%') || ' | '
|
|
739
|
+
|| (select string_agg(signature, ' ' order by signature) from pg_temp.so_fns() where signature like 'zz670\_%'
|
|
740
|
+
and body ~ agent_board_test.writes_any_re(array['send_outcomes'])),
|
|
741
|
+
'zz670_probe_route(text) | zz670_probe_writer()',
|
|
742
|
+
'SO-D9-11: DISCRIMINANCE: the class names a planted keyed messages route, and the writer set a planted receipt writer');
|
|
743
|
+
drop function agent_board.zz670_probe_route(text);
|
|
744
|
+
drop function agent_board.zz670_probe_writer();
|
|
745
|
+
|
|
746
|
+
-- ===========================================================================
|
|
747
|
+
-- D2: replay semantics are unchanged (a replay still re-delivers; the key still binds the thread)
|
|
748
|
+
-- ===========================================================================
|
|
749
|
+
-- same key, same thread, different board: refused by the thread's board binding, no receipt
|
|
750
|
+
select is(left(pg_temp.val('A', pg_temp.pbm('A', 'TX', 'XA', 'K1')), 9) || ' ' || pg_temp.receipts_key('A', 'K1'),
|
|
751
|
+
'ERR 42501 1',
|
|
752
|
+
'SO-D2-1: a same-key post of TX to another board is refused (TX belongs to X); K1 still has one receipt');
|
|
753
|
+
-- a same-key multicast replay naming a subset with a changed body is allowed and records nothing new
|
|
754
|
+
select is(pg_temp.val('A', format($$ select message_id::text from agent_board.send_multicast_message(%L::uuid, %L::uuid, %L::uuid[], 's', 'changed body', 'comment', 'medium', 'KMC', '{}'::jsonb) $$,
|
|
755
|
+
pg_temp.v('TD'), pg_temp.v('A'), array[pg_temp.v('R')]))
|
|
756
|
+
|| ' ' || pg_temp.receipts_key('A', 'KMC'),
|
|
757
|
+
pg_temp.v('M.KMC') || ' 1',
|
|
758
|
+
'SO-D2-2: a #717-permitted subset replay with a changed body returns the original message and mints no second receipt');
|
|
759
|
+
-- the replay still re-fans-out to current members (a new member of X gets the K1 post)
|
|
760
|
+
insert into agent_board.board_memberships(board_id, agent_id, role, membership_state, created_by_agent_id)
|
|
761
|
+
values (pg_temp.u('X'), pg_temp.u('R2'), 'member', 'joined', pg_temp.u('A'));
|
|
762
|
+
-- The call and the state read are SEPARATE statements: an inline subquery in the calling
|
|
763
|
+
-- statement reads that statement's snapshot, i.e. the state before the call.
|
|
764
|
+
select pg_temp.put('M.K1.replay', pg_temp.val('A', pg_temp.pbm('A', 'TX', 'X', 'K1')));
|
|
765
|
+
select is(
|
|
766
|
+
pg_temp.v('M.K1.replay') || ' '
|
|
767
|
+
|| (select count(*)::int from agent_board.delivery_recipients where source_message_id = pg_temp.u('M.K1') and recipient_agent_id = pg_temp.u('R2'))
|
|
768
|
+
|| ' ' || pg_temp.receipts_key('A', 'K1'),
|
|
769
|
+
pg_temp.v('M.K1') || ' 1 1',
|
|
770
|
+
'SO-D2-3: a same-board replay returns the original id, re-fans out to a new member, and mints nothing');
|
|
771
|
+
-- an unkeyed send mints nothing (N2: the helper is gated on a key)
|
|
772
|
+
select pg_temp.put('UNKEYED', pg_temp.as_('A', format($$ select * from agent_board.post_board_message(%L::uuid, %L::uuid, %L::uuid, 's', 'unkeyed', 'comment', 'medium', null, '{}'::jsonb) $$,
|
|
773
|
+
pg_temp.v('TX'), pg_temp.v('A'), pg_temp.v('X'))));
|
|
774
|
+
select is(
|
|
775
|
+
pg_temp.v('UNKEYED')
|
|
776
|
+
|| ' ' || (select count(*)::int from agent_board.messages m where m.thread_id = pg_temp.u('TX') and m.body = 'unkeyed')
|
|
777
|
+
|| ' ' || (select count(*)::int from agent_board.send_outcomes so join agent_board.messages m on m.id = so.message_id where m.body = 'unkeyed'),
|
|
778
|
+
'ok 1 0',
|
|
779
|
+
'SO-D4-N2: an unkeyed send lives and records no receipt');
|
|
780
|
+
-- D6: the lock domain no longer includes the board
|
|
781
|
+
-- source-scan: loud-literal a presence over ONE body: a spelling miss turns this red, never green
|
|
782
|
+
select ok(
|
|
783
|
+
agent_board_test.stripped_body('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure, true)
|
|
784
|
+
~ $re$_lock_idempotency_key\(\s*'post_board_message:'\s*\|\|\s*p_thread_id::text\s*\|\|\s*':'\s*\|\|\s*v_actor_agent_id::text\s*,$re$,
|
|
785
|
+
'SO-D6: post_board_message locks (thread, actor, key), with no board component');
|
|
786
|
+
-- every receipt names its own message's author, thread and created_at
|
|
787
|
+
select is(
|
|
788
|
+
(select count(*)::int from agent_board.send_outcomes so join agent_board.messages m on m.id = so.message_id
|
|
789
|
+
where so.author_agent_id <> m.author_agent_id or so.thread_id <> m.thread_id or so.created_at <> m.created_at),
|
|
790
|
+
0,
|
|
791
|
+
'SO-D4-1: every receipt matches its message''s author, thread and created_at');
|
|
792
|
+
-- every receipt is for a keyed message the session sent under that key and operation
|
|
793
|
+
select is(
|
|
794
|
+
(select count(*)::int from agent_board.send_outcomes so join agent_board.messages m on m.id = so.message_id
|
|
795
|
+
where m.metadata ->> 'idempotencyKey' is distinct from so.idempotency_key or m.metadata ->> 'rpcName' is distinct from so.operation),
|
|
796
|
+
0,
|
|
797
|
+
'SO-D4-2: every receipt''s key and operation equal the replay metadata of its message');
|
|
798
|
+
-- the read writes nothing (STABLE): no receipt count change from any read above
|
|
799
|
+
select is((select count(*)::int from agent_board.send_outcomes where author_agent_id in (pg_temp.u('O'), pg_temp.u('NA'))),
|
|
800
|
+
1, 'SO-D5-1: the reads created no receipts (O''s only receipt is its own TY post; NA has none)');
|
|
801
|
+
select is(pg_temp.read('O', 'TY', 'K1'), pg_temp.confirmed('M.O.TY', 'post_board_message', 'Y'),
|
|
802
|
+
'SO-D5-2: the other author reads its own same-key receipt confirmed');
|
|
803
|
+
|
|
804
|
+
select * from finish();
|
|
805
|
+
rollback;
|