@consiliency/agent-board-schema 1.81.0 → 1.83.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/README.md +6 -0
- package/fixtures/already-applied.json +8 -0
- package/migrations/20261202000100_agent_board_archive_board.sql +1238 -0
- package/migrations/20261203000100_agent_board_archive_board_reads.sql +391 -0
- package/package.json +1 -1
- package/schema-manifest.json +21 -3
- package/src/tooling.test.mjs +6 -2
- package/tests/agent_board_667_history_class.test.sql +20 -3
- package/tests/agent_board_archive_board.test.sql +956 -0
- package/tests/agent_board_archive_board_reads.test.sql +785 -0
- package/tests/agent_board_archive_board_reads_plan.test.sql +56 -0
- package/tests/agent_board_atomic_fleet_onboarding.test.sql +2 -0
- package/tests/agent_board_board_claim_policy.test.sql +5 -3
- package/tests/agent_board_claimpolicy_race.test.sql +8 -6
- package/tests/agent_board_fk_lock_edges.test.sql +27 -3
- package/tests/agent_board_gatenull_membstate.test.sql +3 -1
- package/tests/agent_board_invite_no_downgrade.test.sql +2 -2
- package/tests/agent_board_ledger_authority.test.sql +3 -1
- package/tests/agent_board_role_guard.test.sql +4 -1
- package/tests/agent_board_rolereset.test.sql +5 -4
- package/tests/agent_board_room_roster.test.sql +2 -2
- package/tests/agent_board_step_up.test.sql +12 -7
- package/tests/agent_board_step_up_helper.test.sql +20 -17
- package/tests/agent_board_write_enforcement_class.test.sql +2 -1
- package/tests/catalog/function_execute_privileges.json +36 -0
- package/tests/catalog/p_metadata_parameters.json +16 -0
- package/tests/catalog/step_up_gated_closure.json +2 -0
- package/tests/isolation/expected/archive_vs_content.out +800 -0
- package/tests/isolation/specs/archive_vs_content.spec +128 -0
- package/tests/mutations/918/M01_any_role.sql +13 -0
- package/tests/mutations/918/M02_departed_owner.sql +13 -0
- package/tests/mutations/918/M03_no_fleet_recheck.sql +13 -0
- package/tests/mutations/918/M04_no_step_up.sql +13 -0
- package/tests/mutations/918/M04b_step_up_archive_only.sql +14 -0
- package/tests/mutations/918/M04c_step_up_change_only.sql +17 -0
- package/tests/mutations/918/M05_null_flag.sql +12 -0
- package/tests/mutations/918/M05b_null_board.sql +12 -0
- package/tests/mutations/918/M06_not_idempotent.sql +12 -0
- package/tests/mutations/918/M06b_wrong_flag_returned.sql +12 -0
- package/tests/mutations/918/M07_create_board_unarchives.sql +15 -0
- package/tests/mutations/918/M08_no_moderation.sql +12 -0
- package/tests/mutations/918/M08b_any_change.sql +12 -0
- package/tests/mutations/918/M09_bootstrap_archived.sql +12 -0
- package/tests/mutations/918/M10_drop_artifacts.sql +2 -0
- package/tests/mutations/918/M10_drop_board_authority_ceilings.sql +2 -0
- package/tests/mutations/918/M10_drop_board_invite_blocks.sql +2 -0
- package/tests/mutations/918/M10_drop_board_invite_tokens.sql +3 -0
- package/tests/mutations/918/M10_drop_board_memberships.sql +3 -0
- package/tests/mutations/918/M10_drop_boards.sql +2 -0
- package/tests/mutations/918/M10_drop_callback_subscriptions.sql +3 -0
- package/tests/mutations/918/M10_drop_claims.sql +3 -0
- package/tests/mutations/918/M10_drop_conflicts.sql +2 -0
- package/tests/mutations/918/M10_drop_delegations.sql +2 -0
- package/tests/mutations/918/M10_drop_delivery_recipients.sql +2 -0
- package/tests/mutations/918/M10_drop_escalations.sql +2 -0
- package/tests/mutations/918/M10_drop_fleet_receive_policies.sql +2 -0
- package/tests/mutations/918/M10_drop_messages.sql +2 -0
- package/tests/mutations/918/M10_drop_task_events.sql +2 -0
- package/tests/mutations/918/M10_drop_tasks.sql +2 -0
- package/tests/mutations/918/M10_drop_thread_memberships.sql +3 -0
- package/tests/mutations/918/M10_drop_threads.sql +2 -0
- package/tests/mutations/918/M10i_drop_insert_board_invite_tokens.sql +2 -0
- package/tests/mutations/918/M10i_drop_insert_board_memberships.sql +2 -0
- package/tests/mutations/918/M10i_drop_insert_callback_subscriptions.sql +2 -0
- package/tests/mutations/918/M10i_drop_insert_claims.sql +2 -0
- package/tests/mutations/918/M10i_drop_insert_thread_memberships.sql +2 -0
- package/tests/mutations/918/M10u_drop_update_board_invite_tokens.sql +2 -0
- package/tests/mutations/918/M10u_drop_update_board_memberships.sql +2 -0
- package/tests/mutations/918/M10u_drop_update_callback_subscriptions.sql +2 -0
- package/tests/mutations/918/M10u_drop_update_claims.sql +2 -0
- package/tests/mutations/918/M10u_drop_update_thread_memberships.sql +2 -0
- package/tests/mutations/918/M11_artifacts_no_message.sql +10 -0
- package/tests/mutations/918/M11_artifacts_no_task.sql +10 -0
- package/tests/mutations/918/M11_artifacts_no_task_event.sql +10 -0
- package/tests/mutations/918/M11_claims_no_resource_key.sql +10 -0
- package/tests/mutations/918/M11_claims_no_thread.sql +10 -0
- package/tests/mutations/918/M11_delivery_no_source_message.sql +10 -0
- package/tests/mutations/918/M11_delivery_no_source_task.sql +10 -0
- package/tests/mutations/918/M12_claims_all.sql +9 -0
- package/tests/mutations/918/M13_memberships_all.sql +9 -0
- package/tests/mutations/918/M14_tokens_all.sql +9 -0
- package/tests/mutations/918/M15_subscriptions_all.sql +9 -0
- package/tests/mutations/918/M15b_thread_memberships_all.sql +9 -0
- package/tests/mutations/918/M16_messages_update.sql +10 -0
- package/tests/mutations/918/M16b_task_events_update.sql +10 -0
- package/tests/mutations/918/M17_boards_old_only.sql +10 -0
- package/tests/mutations/918/M18_threads_insert_only.sql +10 -0
- package/tests/mutations/918/M18b_guard_disabled.sql +2 -0
- package/tests/mutations/918/M20_no_clamp_exemption.sql +14 -0
- package/tests/mutations/918/M20b_clamp_any_session.sql +14 -0
- package/tests/mutations/918/M20c_clamp_raise.sql +13 -0
- package/tests/mutations/918/M20d_clamp_other_columns.sql +13 -0
- package/tests/mutations/918/M20e_clamp_any_principal.sql +15 -0
- package/tests/mutations/918/M20f_clamp_any_identity_row.sql +13 -0
- package/tests/mutations/918/M21_ignore_old_row.sql +14 -0
- package/tests/mutations/918/M22_never_refuses.sql +12 -0
- package/tests/mutations/918/M22b_wrong_sqlstate.sql +12 -0
- package/tests/mutations/918/M23_delegate_execute.sql +2 -0
- package/tests/mutations/918/M23b_status_oracle.sql +2 -0
- package/tests/mutations/918/M24_new_writer.sql +2 -0
- package/tests/mutations/918/M24b_new_dynamic_writer.sql +2 -0
- package/tests/mutations/918/M24c_second_status_writer.sql +2 -0
- package/tests/mutations/918/M24d_new_relation.sql +2 -0
- package/tests/mutations/918/M24e_direct_grant.sql +2 -0
- package/tests/mutations/918/M25_memberships_dropped_on_archive.sql +10 -0
- package/tests/mutations/918/M27_departure_result_only.sql +10 -0
- package/tests/mutations/918/M27b_departure_any_role.sql +10 -0
- package/tests/mutations/918/M27c_claim_result_only.sql +10 -0
- package/tests/mutations/918/M27d_claim_any_thread.sql +10 -0
- package/tests/mutations/918/M27e_subscription_result_only.sql +10 -0
- package/tests/mutations/918/M28_token_update_not_expiry.sql +8 -0
- package/tests/mutations/918/M28_token_update_not_reanchor.sql +8 -0
- package/tests/mutations/918/M28_token_update_not_role.sql +8 -0
- package/tests/mutations/918/M28_token_update_not_uses.sql +8 -0
- package/tests/mutations/918/M28b_revocation_any_edit.sql +10 -0
- package/tests/mutations/918/M28c_unrevoke.sql +10 -0
- package/tests/mutations/918/M28d_thread_access_agent.sql +10 -0
- package/tests/mutations/918/M28e_thread_access_thread.sql +10 -0
- package/tests/mutations/918/M28f_thread_access_expiry.sql +10 -0
- package/tests/mutations/918/M28g_thread_access_unrevoke.sql +10 -0
- package/tests/mutations/918/M29_grant_no_refusal.sql +13 -0
- package/tests/mutations/918/M29b_grant_refuses_first.sql +18 -0
- package/tests/mutations/918/M30_redeem_no_status.sql +12 -0
- package/tests/mutations/918/M30b_redeem_distinct_text.sql +16 -0
- package/tests/mutations/918/M31_display_any_status.sql +7 -0
- package/tests/mutations/918/M31b_display_not_service.sql +2 -0
- package/tests/mutations/918/M32_status_before_authority.sql +15 -0
- package/tests/mutations/918/M33_retention_refused.sql +14 -0
- package/tests/mutations/918/M33b_retention_any_metadata.sql +14 -0
- package/tests/mutations/918/M34_guard_rewrites_board_id.sql +13 -0
- package/tests/mutations/918/M34_guard_rewrites_claim_policy.sql +13 -0
- package/tests/mutations/918/M34_guard_rewrites_status.sql +13 -0
- package/tests/mutations/918/M34d_guard_returns_null.sql +16 -0
- package/tests/mutations/918/M34e_guard_rewrites_uppercase.sql +13 -0
- package/tests/mutations/918/M34f_guard_select_into_uppercase.sql +13 -0
- package/tests/mutations/918/M35_new_resource_kind.sql +3 -0
- package/tests/mutations/918/M35b_new_fk_anchor.sql +2 -0
- package/tests/mutations/918/M35c_exempt_gains_writer.sql +2 -0
- package/tests/mutations/918/M35d_service_role_gains_update.sql +2 -0
- package/tests/mutations/918/M36_token_created_at.sql +10 -0
- package/tests/mutations/918/M36b_thread_access_created_at.sql +10 -0
- package/tests/mutations/918/M37_non_object_refused.sql +14 -0
- package/tests/mutations/918/M37b_non_object_any_edit.sql +13 -0
- package/tests/mutations/918/M37c_non_object_any_key.sql +13 -0
- package/tests/mutations/918/M37d_non_object_partial_mark.sql +13 -0
- package/tests/mutations/918/M37e_non_object_result_any_type.sql +13 -0
- package/tests/mutations/918/M37f_non_object_any_last_element.sql +13 -0
- package/tests/mutations/918/M38_claim_key_free.sql +10 -0
- package/tests/mutations/918/M38b_claim_key_verbatim.sql +10 -0
- package/tests/mutations/918/M38c_claim_key_canonical_only.sql +10 -0
- package/tests/mutations/918/equivalents/M37ef_grid.sql +42 -0
- package/tests/mutations/918/isolation/I1_guard_no_lock.sql +13 -0
- package/tests/mutations/918/isolation/I2_archive_no_key_update.sql +12 -0
- package/tests/mutations/918/isolation/I3_guard_for_update.sql +12 -0
- package/tests/mutations/918/isolation/I4_archive_any_isolation.sql +12 -0
- package/tests/mutations/918/isolation/I5_redeem_unlocked.sql +12 -0
- package/tests/mutations/918/isolation/I6_thread_membership_unlocked.sql +12 -0
- package/tests/mutations/918/isolation/I7_thread_membership_for_share.sql +12 -0
- package/tests/mutations/918/pr2/R01_inbox_no_filter.sql +9 -0
- package/tests/mutations/918/pr2/R01b_inbox_hides_boardless.sql +9 -0
- package/tests/mutations/918/pr2/R01d_inbox_requires_readable.sql +9 -0
- package/tests/mutations/918/pr2/R01e_inbox_active_too.sql +9 -0
- package/tests/mutations/918/pr2/R02_counts_no_filter.sql +9 -0
- package/tests/mutations/918/pr2/R02b_counts_hide_boardless.sql +9 -0
- package/tests/mutations/918/pr2/R03_dashboard_no_filter.sql +9 -0
- package/tests/mutations/918/pr2/R03b_dashboard_hides_boardless.sql +9 -0
- package/tests/mutations/918/pr2/R03c_dashboard_tasks.sql +9 -0
- package/tests/mutations/918/pr2/R03d_dashboard_calls_helper.sql +9 -0
- package/tests/mutations/918/pr2/R04_open_esc_no_filter.sql +9 -0
- package/tests/mutations/918/pr2/R04b_open_esc_no_task.sql +9 -0
- package/tests/mutations/918/pr2/R04c_open_esc_no_thread.sql +9 -0
- package/tests/mutations/918/pr2/R05_ops_esc_no_filter.sql +9 -0
- package/tests/mutations/918/pr2/R05b_ops_esc_no_task.sql +9 -0
- package/tests/mutations/918/pr2/R05c_ops_esc_no_thread.sql +9 -0
- package/tests/mutations/918/pr2/R06_stale_no_filter.sql +9 -0
- package/tests/mutations/918/pr2/R06b_stale_no_task.sql +9 -0
- package/tests/mutations/918/pr2/R06c_stale_no_thread.sql +9 -0
- package/tests/mutations/918/pr2/R06d_stale_no_key.sql +9 -0
- package/tests/mutations/918/pr2/R06e_stale_unguarded_cast.sql +9 -0
- package/tests/mutations/918/pr2/R07_memberships_filtered.sql +9 -0
- package/tests/mutations/918/pr2/R07b_memberships_constant.sql +9 -0
- package/tests/mutations/918/pr2/R08a_v_active_claims_filtered.sql +9 -0
- package/tests/mutations/918/pr2/R08b_v_board_feed_filtered.sql +9 -0
- package/tests/mutations/918/pr2/R08c_task_tree_filtered.sql +5 -0
- package/tests/mutations/918/pr2/R08d_thread_feed_filtered.sql +5 -0
- package/tests/mutations/918/pr2/R08e_callback_subs_filtered.sql +5 -0
- package/tests/mutations/918/pr2/R09_inbox_definer_view.sql +9 -0
- package/tests/mutations/918/pr2/R09b_view_grant.sql +2 -0
- package/tests/mutations/918/pr2/R09c_view_revoke.sql +2 -0
- package/tests/mutations/918/pr2/R09d_column_renamed.sql +2 -0
- package/tests/mutations/918/pr2/R10_new_view.sql +2 -0
- package/tests/mutations/918/pr2/R10b_new_routine.sql +2 -0
- package/tests/mutations/918/pr2/R10c_new_indirect_routine.sql +2 -0
- package/tests/mutations/918/pr2/R10d_predicate_returns_rows.sql +2 -0
- package/tests/mutations/918/pr2/R10e_overload.sql +3 -0
- package/tests/mutations/918/pr2/R10f_execute_root.sql +2 -0
- package/tests/mutations/918/pr2/R10g_predicate_array.sql +2 -0
- package/tests/mutations/918/pr2/R10h_predicate_domain.sql +2 -0
- package/tests/mutations/918/pr2/R10i_polymorphic.sql +2 -0
- package/tests/mutations/918/pr2/R10j_predicate_json.sql +2 -0
- package/tests/mutations/918/pr2/R10k_foreign_view.sql +2 -0
- package/tests/mutations/918/pr2/R10l_foreign_view_transitive.sql +2 -0
- package/tests/mutations/918/pr2/R10m_foreign_view_function.sql +2 -0
- package/tests/mutations/918/pr2/R10n_foreign_matview.sql +2 -0
- package/tests/mutations/918/pr2/R10o_ambiguous_overload.sql +6 -0
- package/tests/mutations/918/pr2/R10p_resolved_overload.sql +6 -0
- package/tests/mutations/918/pr2/R11_requests_no_filter.sql +9 -0
- package/tests/mutations/918/pr2/R11b_requests_hide_all.sql +9 -0
- package/tests/mutations/918/pr2/R11c_requests_no_creator.sql +11 -0
- package/tests/mutations/918/pr2/R11d_requests_no_pending.sql +9 -0
- package/tests/mutations/918/pr2/R11e_requests_any_state.sql +9 -0
- package/tests/mutations/918/pr2/R11f_requests_active_too.sql +9 -0
- package/tests/mutations/918/pr2/R12_helper_definer.sql +2 -0
- package/tests/mutations/918/pr2/R12b_helper_no_thread.sql +11 -0
- package/tests/mutations/918/pr2/R12c_helper_no_task.sql +11 -0
- package/tests/mutations/918/pr2/R12d_helper_any_board.sql +10 -0
- package/tests/mutations/918/pr2/R12e_helper_task_any_board.sql +10 -0
- package/tests/mutations/918/pr2/R12f_helper_null.sql +11 -0
- package/tests/mutations/918/pr2/R12g_helper_delegate.sql +2 -0
- package/tests/mutations/918/pr2/R12h_helper_public.sql +2 -0
- package/tests/mutations/918/pr2/R12i_no_generic_plan.sql +2 -0
- package/tests/mutations/918/pr2/R13_archive_columns.sql +2 -0
- package/tests/mutations/918/pr2/R13b_second_agents_fk.sql +2 -0
- package/tests/mutations/918/pr2/R13c_archive_column.sql +2 -0
- package/tests/mutations/918/pr2/R15_rls_narrowed.sql +2 -0
- package/tests/mutations/918/pr2/R15b_rls_widened.sql +2 -0
- package/tests/mutations/918/pr2/R15c_threads_rls_narrowed.sql +2 -0
- package/tests/mutations/918/pr2/R16_archive_deletes.sql +10 -0
- package/tests/mutations/918/pr2/R16b_archive_marks_read.sql +10 -0
- package/tests/mutations/918/pr2/R17_wake_bypasses_inbox.sql +8 -0
- package/tests/mutations/918/pr2/R18_directory_archived.sql +7 -0
- package/tests/mutations/918/pr2/R19_delegate_ids_active_only.sql +7 -0
- package/tests/mutations/918/run.sh +95 -0
- package/tests/seeds/20261202000100_agent_board_archive_board.sql.pre.sql +24 -0
- package/tests/seeds/20261203000100_agent_board_archive_board_reads.sql.pre.sql +38 -0
|
@@ -0,0 +1,785 @@
|
|
|
1
|
+
-- #918 ARCHIVEBOARD PR-2 pgTAP (migration 20261203000100_agent_board_archive_board_reads.sql).
|
|
2
|
+
--
|
|
3
|
+
-- THE CONTRACT
|
|
4
|
+
-- * A filter never tells a caller anything it cannot already select. An inbox row on an archived board
|
|
5
|
+
-- leaves the inbox and the counts for a caller who can read that board's STATUS. An escalation or a
|
|
6
|
+
-- stale claim leaves its list for a caller who can read both the status AND the thread it names (or
|
|
7
|
+
-- its task and that task's thread). Rows come back when the board is un-archived.
|
|
8
|
+
-- * Who can read what does not change: every base relation reads the same before and after an
|
|
9
|
+
-- archive, for every caller, but for the boards row's own status.
|
|
10
|
+
-- * v_board_memberships exposes board_status.
|
|
11
|
+
--
|
|
12
|
+
-- METHOD (AGENTS.md rules 1-3)
|
|
13
|
+
-- * The classes are derived from pg_class / pg_rewrite / pg_proc and compared BY NAME, so a new view or
|
|
14
|
+
-- read routine names itself.
|
|
15
|
+
-- * Behaviour is a DIFF over the derived class, not a list of queries: for each caller, under its
|
|
16
|
+
-- effective role, every view and every base relation is read in full before the archive, while
|
|
17
|
+
-- archived, and after the un-archive. UNCHANGED means the three row sets are equal AND hold rows of
|
|
18
|
+
-- the archived board; EXCLUDE means exactly that board's rows are missing in the middle one.
|
|
19
|
+
-- Counts are asserted, never an absence.
|
|
20
|
+
-- * The fixture looks like production: `set constraints all immediate` fires the deferred lineage
|
|
21
|
+
-- trigger, so ordinary agents are `legacy`.
|
|
22
|
+
-- * WHAT THE CLASSES SEE. Views: every view or materialized view of agent_board, and of any other
|
|
23
|
+
-- non-system schema that depends (pg_depend, transitively) on an agent_board relation or function.
|
|
24
|
+
-- Routines: agent_board routines an API role can execute, whose call closure (fn_call_closure) writes
|
|
25
|
+
-- no board-scoped relation and reads one, or an agent_board view; plus every one whose closure runs
|
|
26
|
+
-- EXECUTE, listed apart because what it reads cannot be read from its text.
|
|
27
|
+
-- A call to an overloaded name is resolved where the catalog resolves it (a SQL-standard body); otherwise the
|
|
28
|
+
-- caller is kept and listed as ambiguous, never dropped.
|
|
29
|
+
-- * STILL INVISIBLE: a routine that writes a board-scoped relation and also returns rows (PR-1's class,
|
|
30
|
+
-- agent_board_archive_board.test.sql AB-C5); a routine in another schema; a scalar (text) that carries
|
|
31
|
+
-- serialized rows; a read behind EXECUTE (the routine is listed, its reads are not); a rewrite rule on
|
|
32
|
+
-- a table; a view whose only reference is inside a function body in another schema.
|
|
33
|
+
create extension if not exists pgtap with schema extensions;
|
|
34
|
+
set search_path = extensions, public, agent_board;
|
|
35
|
+
|
|
36
|
+
begin;
|
|
37
|
+
|
|
38
|
+
select plan(71);
|
|
39
|
+
|
|
40
|
+
create temp table _h(k text primary key, v text);
|
|
41
|
+
create function pg_temp.v(p_k text) returns text language sql as $$ select v from _h where k = p_k $$;
|
|
42
|
+
create function pg_temp.u(p_k text) returns uuid language sql as $$ select v::uuid from _h where k = p_k $$;
|
|
43
|
+
create function pg_temp.put(p_k text, p_v text) returns void language sql as $$
|
|
44
|
+
insert into _h values (p_k, p_v) on conflict (k) do update set v = excluded.v $$;
|
|
45
|
+
create function pg_temp.c(p_who text) returns text language sql as $$
|
|
46
|
+
select case p_who
|
|
47
|
+
when 'SVC' then '{"role":"service_role"}'
|
|
48
|
+
when 'D' then json_build_object('role', 'agent_board_delegate', 'sub', pg_temp.v('D.auth'), 'agent_credential_id', pg_temp.v('D.cred'))::text
|
|
49
|
+
else json_build_object('role', 'authenticated', 'sub', pg_temp.v(p_who || '.auth'), 'aud', 'authenticated')::text end $$;
|
|
50
|
+
create function pg_temp.role_of(p_who text) returns text language sql as $$
|
|
51
|
+
select case p_who when 'SVC' then 'service_role' when 'D' then 'agent_board_delegate' else 'authenticated' end $$;
|
|
52
|
+
create function pg_temp.claims(p_claims text) returns void language plpgsql as $$
|
|
53
|
+
begin
|
|
54
|
+
perform set_config('request.jwt.claim.sub', '', true);
|
|
55
|
+
perform set_config('request.jwt.claim.role', '', true);
|
|
56
|
+
perform set_config('request.jwt.claims', coalesce(p_claims, ''), true);
|
|
57
|
+
end $$;
|
|
58
|
+
create function pg_temp.fmt(p_sql text) returns text language plpgsql as $$
|
|
59
|
+
declare r record;
|
|
60
|
+
begin
|
|
61
|
+
for r in select k, v from _h loop p_sql := replace(p_sql, '{' || r.k || '}', coalesce(r.v, 'NULL')); end loop;
|
|
62
|
+
if p_sql ~ '\{[A-Z][A-Za-z0-9_.]*\}' then raise exception 'unresolved placeholder in %', p_sql; end if;
|
|
63
|
+
return p_sql;
|
|
64
|
+
end $$;
|
|
65
|
+
-- Run as p_who (claims only) and KEEP the effects: 'OK' / 'SQLSTATE message'.
|
|
66
|
+
create function pg_temp.run(p_who text, p_sql text) returns text language plpgsql as $$
|
|
67
|
+
begin
|
|
68
|
+
begin
|
|
69
|
+
perform pg_temp.claims(pg_temp.c(p_who));
|
|
70
|
+
execute pg_temp.fmt(p_sql);
|
|
71
|
+
perform pg_temp.claims(null);
|
|
72
|
+
return 'OK';
|
|
73
|
+
exception when others then
|
|
74
|
+
return sqlstate || ' ' || sqlerrm;
|
|
75
|
+
end;
|
|
76
|
+
end $$;
|
|
77
|
+
-- Read one value as p_who, under its EFFECTIVE role.
|
|
78
|
+
create function pg_temp.val(p_who text, p_sql text) returns text language plpgsql as $$
|
|
79
|
+
declare r text; q text := pg_temp.fmt(p_sql);
|
|
80
|
+
begin
|
|
81
|
+
begin
|
|
82
|
+
perform pg_temp.claims(pg_temp.c(p_who));
|
|
83
|
+
execute format('set local role %I', pg_temp.role_of(p_who));
|
|
84
|
+
execute q into r;
|
|
85
|
+
reset role;
|
|
86
|
+
perform pg_temp.claims(null);
|
|
87
|
+
return r;
|
|
88
|
+
exception when others then
|
|
89
|
+
return 'ERR ' || sqlstate || ' ' || sqlerrm;
|
|
90
|
+
end;
|
|
91
|
+
end $$;
|
|
92
|
+
-- Run p_do as p_who, read p_sql as p_reader, then ALWAYS roll back.
|
|
93
|
+
create function pg_temp.probe(p_who text, p_do text, p_reader text, p_sql text) returns text language plpgsql as $$
|
|
94
|
+
declare r text;
|
|
95
|
+
begin
|
|
96
|
+
begin
|
|
97
|
+
r := pg_temp.run(p_who, p_do);
|
|
98
|
+
if r <> 'OK' then raise exception using errcode = 'ZZ999', message = r; end if;
|
|
99
|
+
r := pg_temp.val(p_reader, p_sql);
|
|
100
|
+
raise exception using errcode = 'ZZ999', message = coalesce(r, '<null>');
|
|
101
|
+
exception when sqlstate 'ZZ999' then return sqlerrm;
|
|
102
|
+
end;
|
|
103
|
+
end $$;
|
|
104
|
+
|
|
105
|
+
-- ===========================================================================
|
|
106
|
+
-- A. The classes, from the catalog
|
|
107
|
+
-- ===========================================================================
|
|
108
|
+
-- Board-scoped relation: PR-1's derivation (FK path to boards, or a board_id/thread_id column).
|
|
109
|
+
create temp table _rel as
|
|
110
|
+
with recursive scoped(oid) as (
|
|
111
|
+
select 'agent_board.boards'::regclass::oid
|
|
112
|
+
union
|
|
113
|
+
select c.conrelid
|
|
114
|
+
from pg_constraint c
|
|
115
|
+
join scoped s on c.confrelid = s.oid
|
|
116
|
+
join pg_class r on r.oid = c.conrelid
|
|
117
|
+
where c.contype = 'f' and r.relnamespace = 'agent_board'::regnamespace and c.conrelid <> c.confrelid)
|
|
118
|
+
select c.oid, c.relname::text as name
|
|
119
|
+
from pg_class c
|
|
120
|
+
where c.oid in (
|
|
121
|
+
select oid from scoped
|
|
122
|
+
union
|
|
123
|
+
select a.attrelid from pg_attribute a join pg_class r on r.oid = a.attrelid
|
|
124
|
+
where r.relnamespace = 'agent_board'::regnamespace and r.relkind in ('r', 'p')
|
|
125
|
+
and a.attname in ('board_id', 'thread_id') and not a.attisdropped);
|
|
126
|
+
-- Every agent_board view, and every view of another non-system schema that depends on agent_board, with
|
|
127
|
+
-- the board-scoped relations it reads, directly or through another view.
|
|
128
|
+
create temp table _v as
|
|
129
|
+
with recursive vw as (
|
|
130
|
+
select c.oid, c.relname, c.relnamespace, n.nspname
|
|
131
|
+
from pg_class c join pg_namespace n on n.oid = c.relnamespace
|
|
132
|
+
where c.relkind in ('v', 'm') and n.nspname not in ('pg_catalog', 'information_schema') and n.nspname !~ '^pg_(toast|temp)'),
|
|
133
|
+
edge as (
|
|
134
|
+
select r.ev_class as view_oid, d.refclassid, d.refobjid
|
|
135
|
+
from pg_rewrite r
|
|
136
|
+
join pg_depend d on d.classid = 'pg_rewrite'::regclass and d.objid = r.oid
|
|
137
|
+
and d.refclassid in ('pg_class'::regclass, 'pg_proc'::regclass) and d.refobjid <> r.ev_class),
|
|
138
|
+
dep as (
|
|
139
|
+
select vw.oid as view_oid, e.refclassid, e.refobjid from vw join edge e on e.view_oid = vw.oid
|
|
140
|
+
union
|
|
141
|
+
select dep.view_oid, e.refclassid, e.refobjid from dep join edge e on dep.refclassid = 'pg_class'::regclass and e.view_oid = dep.refobjid)
|
|
142
|
+
select vw.oid, case when vw.nspname = 'agent_board' then '' else vw.nspname || '.' end || vw.relname as name,
|
|
143
|
+
coalesce((select array_agg(distinct x.name order by x.name) from dep join _rel x on x.oid = dep.refobjid and dep.refclassid = 'pg_class'::regclass where dep.view_oid = vw.oid), '{}') as rels
|
|
144
|
+
from vw
|
|
145
|
+
where vw.nspname = 'agent_board'
|
|
146
|
+
or exists (select 1 from dep join pg_class c on c.oid = dep.refobjid and dep.refclassid = 'pg_class'::regclass
|
|
147
|
+
where dep.view_oid = vw.oid and c.relnamespace = 'agent_board'::regnamespace)
|
|
148
|
+
or exists (select 1 from dep join pg_proc p on p.oid = dep.refobjid and dep.refclassid = 'pg_proc'::regclass
|
|
149
|
+
where dep.view_oid = vw.oid and p.pronamespace = 'agent_board'::regnamespace);
|
|
150
|
+
|
|
151
|
+
-- Dispositions.
|
|
152
|
+
-- EXPOSE gains the board's status; no row is left out
|
|
153
|
+
-- EXCLUDE this migration leaves rows out
|
|
154
|
+
-- INHERIT selects from an EXCLUDE view
|
|
155
|
+
-- ALREADY read active boards only before #918
|
|
156
|
+
-- COUNT EXCLUDE, but the rows are per agent: the counts move, pinned in section D
|
|
157
|
+
-- UNCHANGED for the reason given
|
|
158
|
+
create temp table _vdecl(name text primary key, cls text not null, why text not null, tie text);
|
|
159
|
+
insert into _vdecl values
|
|
160
|
+
('v_board_memberships', 'EXPOSE', 'the caller''s own rooms: they stay, with their status', null),
|
|
161
|
+
('v_agent_inbox', 'EXCLUDE', 'triage list', $$j->>'board_id' = '{AR.B}'$$),
|
|
162
|
+
('v_agent_wake_recovery', 'INHERIT', 'v_agent_inbox', $$j->>'board_id' = '{AR.B}'$$),
|
|
163
|
+
('v_agent_inbox_counts', 'COUNT', 'triage counts', null),
|
|
164
|
+
('v_ops_inbox_backlog', 'COUNT', 'v_agent_inbox_counts', null),
|
|
165
|
+
('v_agent_dashboard', 'COUNT', 'its two inbox counts; task and escalation counts would need threads or a function in the delegate''s frozen read closure (HOSTED D4)', null),
|
|
166
|
+
('v_open_escalations', 'EXCLUDE', 'cannot be resolved until un-archived', $$j->>'thread_id' in ('{AR.T}', '{AR.ET}') or j->>'task_id' in ('{AR.TASK}', '{AR.TASK2}')$$),
|
|
167
|
+
('v_ops_open_escalations', 'EXCLUDE', 'cannot be resolved until un-archived', $$j->>'thread_id' in ('{AR.T}', '{AR.ET}') or j->>'task_id' in ('{AR.TASK}', '{AR.TASK2}')$$),
|
|
168
|
+
('v_ops_stale_claims', 'EXCLUDE', 'nobody is working on that board', $$j->>'thread_id' in ('{AR.T}', '{AR.ET}') or j->>'task_id' = '{AR.TASK}' or j->>'resource_key' = '{AR.TASK2}'$$),
|
|
169
|
+
('v_board_directory', 'ALREADY', 'its WHERE names status active', $$j->>'board_id' = '{AR.B}'$$),
|
|
170
|
+
('v_thread_feed', 'UNCHANGED', 'history stays readable (owner decision 1)', null),
|
|
171
|
+
('v_board_feed', 'UNCHANGED', 'history stays readable (owner decision 1)', null),
|
|
172
|
+
('v_task_tree', 'UNCHANGED', 'a task''s own history, read by task or root; not a work list', null),
|
|
173
|
+
('v_active_claims', 'UNCHANGED', 'a claim on an archived board still holds its resource against everyone and can still be released', null),
|
|
174
|
+
('v_ownerless_claim_quota', 'UNCHANGED', 'must equal the count acquire_claim enforces', null),
|
|
175
|
+
('v_callback_subscriptions', 'UNCHANGED', 'a subscription can still be disabled while archived, so it must be findable', null),
|
|
176
|
+
('v_callback_delivery_health', 'UNCHANGED', 'deliveries queued before the archive still finish', null),
|
|
177
|
+
('v_callback_endpoint_status', 'UNCHANGED', 'endpoint health, not board work', null),
|
|
178
|
+
('v_callback_health', 'UNCHANGED', 'endpoint health, not board work', null),
|
|
179
|
+
('v_ops_callback_retry_pressure', 'UNCHANGED', 'deliveries queued before the archive still finish', null),
|
|
180
|
+
('v_fleet_delivery_posture', 'UNCHANGED', 'signature and policy evidence of a past delivery', null),
|
|
181
|
+
('v_fleet_receive_policy_status', 'UNCHANGED', 'security configuration', null),
|
|
182
|
+
('v_signature_evidence_status', 'UNCHANGED', 'audit', null),
|
|
183
|
+
('v_agent_liveness', 'UNCHANGED', 'presence, keyed by agent', null),
|
|
184
|
+
('v_ops_presence_heartbeats', 'UNCHANGED', 'presence, keyed by agent', null),
|
|
185
|
+
('v_fleet_machine_status', 'UNCHANGED', 'reads no board-scoped relation', null),
|
|
186
|
+
('v_ops_endpoint_disablement', 'UNCHANGED', 'reads no board-scoped relation', null),
|
|
187
|
+
('v_ops_table_growth', 'UNCHANGED', 'reads no board-scoped relation (sizes by name)', null);
|
|
188
|
+
-- AR-A1
|
|
189
|
+
select is(
|
|
190
|
+
(select coalesce(nullif(concat_ws(' ',
|
|
191
|
+
(select 'unclassified: ' || string_agg(name || coalesce('[' || nullif(array_to_string(rels, '+'), '') || ']', ''), ', ' order by name) from _v where name not in (select name from _vdecl)),
|
|
192
|
+
(select 'stale: ' || string_agg(name, ', ' order by name) from _vdecl where name not in (select name from _v))), ''), '(none)')),
|
|
193
|
+
'(none)', 'AR-A1: every view of agent_board, and of any other schema that depends on it, is classified, by name');
|
|
194
|
+
select is(
|
|
195
|
+
(select count(*) || '/' || count(*) filter (where rels <> '{}') || '/' || string_agg(name, '+' order by name) filter (where rels = '{}') from _v),
|
|
196
|
+
'28/25/v_fleet_machine_status+v_ops_endpoint_disablement+v_ops_table_growth',
|
|
197
|
+
'AR-A1 control: 28 views, all in agent_board, 25 of them reading a board-scoped relation');
|
|
198
|
+
select is(
|
|
199
|
+
(select string_agg(cls || '=' || n, ' ' order by cls) from (select cls, count(*) n from _vdecl group by cls) x),
|
|
200
|
+
'ALREADY=1 COUNT=3 EXCLUDE=4 EXPOSE=1 INHERIT=1 UNCHANGED=18', 'AR-A1: the dispositions');
|
|
201
|
+
-- AR-A2: no view became a definer view, and the replaced ones kept their grants and columns.
|
|
202
|
+
select is(
|
|
203
|
+
(select coalesce(string_agg(c.relname, ', ' order by c.relname), '(none)')
|
|
204
|
+
from pg_class c where c.oid in (select oid from _v)
|
|
205
|
+
and not coalesce('security_invoker=true' = any(c.reloptions), false)),
|
|
206
|
+
'(none)', 'AR-A2: every classified view is security_invoker');
|
|
207
|
+
select is(
|
|
208
|
+
(select string_agg(c.relname || ':' || c.relacl::text, ' ' order by c.relname)
|
|
209
|
+
from pg_class c where c.oid in (select 'agent_board.v_agent_inbox'::regclass union select 'agent_board.v_agent_inbox_counts'::regclass
|
|
210
|
+
union select 'agent_board.v_agent_dashboard'::regclass union select 'agent_board.v_open_escalations'::regclass union select 'agent_board.v_ops_open_escalations'::regclass
|
|
211
|
+
union select 'agent_board.v_ops_stale_claims'::regclass union select 'agent_board.v_board_memberships'::regclass)),
|
|
212
|
+
replace('v_agent_dashboard:{OWN=arwdDxtm/OWN,service_role=r/OWN,authenticated=r/OWN,agent_board_delegate=r/OWN} v_agent_inbox:{OWN=arwdDxtm/OWN,authenticated=r/OWN,service_role=r/OWN,agent_board_delegate=r/OWN} v_agent_inbox_counts:{OWN=arwdDxtm/OWN,authenticated=r/OWN,service_role=r/OWN} v_board_memberships:{OWN=arwdDxtm/OWN,authenticated=r/OWN,service_role=r/OWN,agent_board_delegate=r/OWN} v_open_escalations:{OWN=arwdDxtm/OWN,service_role=r/OWN,authenticated=r/OWN} v_ops_open_escalations:{OWN=arwdDxtm/OWN,authenticated=r/OWN,service_role=r/OWN} v_ops_stale_claims:{OWN=arwdDxtm/OWN,authenticated=r/OWN,service_role=r/OWN}', 'OWN',
|
|
213
|
+
(select pg_get_userbyid(relowner) from pg_class where oid = 'agent_board.v_agent_inbox'::regclass)),
|
|
214
|
+
'AR-A2: the seven replaced views carry exactly the grants they had');
|
|
215
|
+
select is(
|
|
216
|
+
(select string_agg(c.relname || ':' || md5((select string_agg(a.attname || ' ' || format_type(a.atttypid, a.atttypmod), ',' order by a.attnum) from pg_attribute a where a.attrelid = c.oid and a.attnum > 0)), ' ' order by c.relname)
|
|
217
|
+
from pg_class c where c.relnamespace = 'agent_board'::regnamespace and c.relname in ('v_agent_inbox', 'v_agent_inbox_counts', 'v_agent_dashboard', 'v_open_escalations', 'v_ops_open_escalations', 'v_ops_stale_claims')),
|
|
218
|
+
'v_agent_dashboard:a192e21d425a2da0442507cdd65cc5f3 v_agent_inbox:7de4cfa2c0e9f416bb641122aa954bee v_agent_inbox_counts:0a9636aa7f519e655aea389199580603 v_open_escalations:0e92976772ef42f8a6db0eca385ec013 v_ops_open_escalations:6d7495cf2a12ec91735c1dfbc7a13849 v_ops_stale_claims:87e5f32fd464d18817d4afc0706ec60e', 'AR-A2: the six filtered views have exactly the columns they had, in order');
|
|
219
|
+
select is(
|
|
220
|
+
(select string_agg(a.attname || ' ' || format_type(a.atttypid, a.atttypmod), ',' order by a.attnum) from pg_attribute a where a.attrelid = 'agent_board.v_board_memberships'::regclass and a.attnum >= 13),
|
|
221
|
+
'updated_at timestamp with time zone,role text,board_status text', 'AR-A2: v_board_memberships appends board_status last');
|
|
222
|
+
|
|
223
|
+
-- AR-A3: THE READ ROUTINES.
|
|
224
|
+
create temp table _names as select name from _rel union select name from _v where position('.' in name) = 0;
|
|
225
|
+
create temp table _b as
|
|
226
|
+
select p.oid, agent_board_test.stripped_body(p.oid) as b
|
|
227
|
+
from pg_proc p
|
|
228
|
+
where p.pronamespace = 'agent_board'::regnamespace and p.prokind in ('f', 'p')
|
|
229
|
+
and p.prolang <> (select oid from pg_language where lanname = 'internal');
|
|
230
|
+
create temp table _wr as
|
|
231
|
+
select _b.oid,
|
|
232
|
+
b ~ agent_board_test.writes_any_re((select array_agg(name) from _rel)) as writes,
|
|
233
|
+
b ~ agent_board_test.re('\m' || agent_board_test.ci_re('execute') || '\M') as dyn
|
|
234
|
+
from _b;
|
|
235
|
+
-- THE CALL GRAPH. fn_call_closure matches a call by NAME, so a call to an overloaded name reaches every
|
|
236
|
+
-- overload. Its pairs are used as candidates only; the edges are rebuilt here:
|
|
237
|
+
-- * a SQL-standard body (prosqlbody) has its callees RESOLVED in pg_depend: those edges, and no others;
|
|
238
|
+
-- * a text body keeps the by-name edges.
|
|
239
|
+
-- A root is PR-1's (dropped) only when it reaches a writer for CERTAIN: it writes, or it calls a name ALL of whose
|
|
240
|
+
-- overloads certainly write. A root that may reach a writer only through an overloaded name is AMBIGUOUS:
|
|
241
|
+
-- it stays, and must be classified explicitly.
|
|
242
|
+
create temp table _cand as select distinct c.root_oid, c.reached_oid from agent_board_test.fn_call_closure() c where c.root_oid <> c.reached_oid;
|
|
243
|
+
create temp table _edge as
|
|
244
|
+
select distinct x.root_oid as caller, x.reached_oid as callee
|
|
245
|
+
from _cand x
|
|
246
|
+
join pg_proc cr on cr.oid = x.root_oid and cr.prosqlbody is null
|
|
247
|
+
join pg_proc ce on ce.oid = x.reached_oid
|
|
248
|
+
join _b on _b.oid = x.root_oid
|
|
249
|
+
where b ~ agent_board_test.call_re(ce.proname, agent_board_test.bare_ok(cr.oid))
|
|
250
|
+
union
|
|
251
|
+
select d.objid, d.refobjid
|
|
252
|
+
from pg_depend d
|
|
253
|
+
join pg_proc cr on cr.oid = d.objid and cr.prosqlbody is not null and cr.pronamespace = 'agent_board'::regnamespace
|
|
254
|
+
join pg_proc ce on ce.oid = d.refobjid and ce.pronamespace = 'agent_board'::regnamespace
|
|
255
|
+
where d.classid = 'pg_proc'::regclass and d.refclassid = 'pg_proc'::regclass and d.objid <> d.refobjid;
|
|
256
|
+
create temp table _certain as select oid from _wr where writes;
|
|
257
|
+
do $$
|
|
258
|
+
declare n int;
|
|
259
|
+
begin
|
|
260
|
+
loop
|
|
261
|
+
insert into _certain
|
|
262
|
+
select distinct e.caller
|
|
263
|
+
from _edge e
|
|
264
|
+
join pg_proc cr on cr.oid = e.caller
|
|
265
|
+
join pg_proc ce on ce.oid = e.callee
|
|
266
|
+
where e.caller not in (select oid from _certain)
|
|
267
|
+
and case when cr.prosqlbody is not null then e.callee in (select oid from _certain)
|
|
268
|
+
else not exists (select 1 from pg_proc o where o.pronamespace = ce.pronamespace and o.proname = ce.proname and o.prokind in ('f', 'p')
|
|
269
|
+
and o.oid <> e.caller and o.oid not in (select oid from _certain)) end;
|
|
270
|
+
get diagnostics n = row_count;
|
|
271
|
+
exit when n = 0;
|
|
272
|
+
end loop;
|
|
273
|
+
end $$;
|
|
274
|
+
create temp table _cl as
|
|
275
|
+
with recursive t(root_oid, reached_oid) as (
|
|
276
|
+
select oid, oid from _b where oid not in (select oid from _certain)
|
|
277
|
+
union
|
|
278
|
+
select t.root_oid, e.callee from t join _edge e on e.caller = t.reached_oid)
|
|
279
|
+
select root_oid, reached_oid from t;
|
|
280
|
+
create temp table _rw as
|
|
281
|
+
select _b.oid, exists (select 1 from _names n where b ~ agent_board_test.qname_re(n.name)) as reads
|
|
282
|
+
from _b where _b.oid in (select reached_oid from _cl) and _b.oid not in (select oid from _certain)
|
|
283
|
+
union all
|
|
284
|
+
select oid, true from _certain where oid in (select reached_oid from _cl);
|
|
285
|
+
-- A SCALAR is one value of a base or enum type that is not an array, json or xml. A set, a composite, record,
|
|
286
|
+
-- an array, a polymorphic type, and a domain over any of them can carry rows.
|
|
287
|
+
create function pg_temp.scalar(p_type oid) returns boolean language sql as $$
|
|
288
|
+
with recursive t(oid) as (select p_type union all select x.typbasetype from t join pg_type x on x.oid = t.oid where x.typtype = 'd')
|
|
289
|
+
select bool_and(x.typtype in ('d', 'b', 'e') and (x.typtype = 'd' or (x.typcategory <> 'A' and x.typname not in ('json', 'jsonb', 'xml'))))
|
|
290
|
+
from t join pg_type x on x.oid = t.oid $$;
|
|
291
|
+
create temp table _r as
|
|
292
|
+
select p.oid, p.proname::text as name, p.proname || '(' || oidvectortypes(p.proargtypes) || ')' as sig, cl.dyn, cl.ambiguous,
|
|
293
|
+
p.proretset or not pg_temp.scalar(p.prorettype) as returns_rows
|
|
294
|
+
from pg_proc p
|
|
295
|
+
join (select c.root_oid, bool_or(rw.reads) as reads, bool_or(wr.dyn) as dyn, bool_or(rw.oid in (select oid from _certain)) as ambiguous
|
|
296
|
+
from _cl c join _rw rw on rw.oid = c.reached_oid join _wr wr on wr.oid = c.reached_oid group by 1) cl on cl.root_oid = p.oid
|
|
297
|
+
where (cl.reads or cl.dyn) and p.prorettype <> 'trigger'::regtype
|
|
298
|
+
and exists (select 1 from unnest(array['anon', 'authenticated', 'service_role', 'agent_board_delegate']) r where has_function_privilege(r, p.oid, 'EXECUTE'));
|
|
299
|
+
create temp table _rdecl(sig text primary key, cls text not null, why text not null);
|
|
300
|
+
insert into _rdecl values
|
|
301
|
+
('list_my_board_requests()', 'EXCLUDE', 'a list of boards; section E'),
|
|
302
|
+
('invite_board_for_display(text)', 'ALREADY', 'resolves active boards only since 1.82.0 (agent_board_archive_board AB-E3)'),
|
|
303
|
+
('list_board_members(uuid)', 'UNCHANGED', 'the roster of a named board stays readable'),
|
|
304
|
+
('list_board_pending_memberships(uuid)', 'UNCHANGED', 'the roster of a named board stays readable; a pending row is still withdrawable by its subject'),
|
|
305
|
+
('get_send_outcome(uuid, text, text)', 'UNCHANGED', 'the caller''s own receipt'),
|
|
306
|
+
('expand_recipients_dry_run(uuid, jsonb, text)', 'UNCHANGED', 'resolves agents, not boards; its one board-scoped read is a thread key'),
|
|
307
|
+
('evaluate_fleet_sender_policy(uuid, uuid, uuid, uuid, text, uuid, boolean)', 'UNCHANGED', 'a security decision'),
|
|
308
|
+
('agent_liveness_summary()', 'UNCHANGED', 'presence, keyed by agent'),
|
|
309
|
+
('ownerless_quota_census()', 'UNCHANGED', 'must equal the count acquire_claim enforces'),
|
|
310
|
+
('_claimgate_promotion_blockers(text)', 'UNCHANGED', 'a live claim on an archived board still blocks a promotion'),
|
|
311
|
+
('revoke_human_subtree(uuid, uuid, text)', 'UNCHANGED', 'identity revocation; its one board-scoped read is the enrollment audit'),
|
|
312
|
+
('record_portal_step_up(uuid, bigint)', 'PREDICATE', 'returns a boolean'),
|
|
313
|
+
('_delegate_visible_board_ids()', 'UNCHANGED', 'RLS: what a delegate may read, archived or not (an array of ids)'),
|
|
314
|
+
('_delegate_visible_agent(uuid)', 'PREDICATE', 'RLS'),
|
|
315
|
+
('_thread_on_board(uuid[], uuid, uuid)', 'PREDICATE', 'this migration''s own'),
|
|
316
|
+
('_active_board_membership_id(uuid, uuid)', 'PREDICATE', 'membership lookup'),
|
|
317
|
+
('_agent_can_access_thread(uuid, uuid)', 'PREDICATE', 'access'),
|
|
318
|
+
('_agent_has_board_access(uuid, uuid)', 'PREDICATE', 'access'),
|
|
319
|
+
('_current_agent_can_access_task(uuid)', 'PREDICATE', 'access'),
|
|
320
|
+
('_current_agent_can_access_thread(uuid)', 'PREDICATE', 'access'),
|
|
321
|
+
('_current_agent_can_send_to_board(uuid)', 'PREDICATE', 'access'),
|
|
322
|
+
('_current_agent_has_board_access(uuid)', 'PREDICATE', 'access'),
|
|
323
|
+
('_grant_within_ceiling(uuid, uuid, text)', 'PREDICATE', 'authority'),
|
|
324
|
+
('_membership_consent_present(uuid, uuid)', 'PREDICATE', 'consent'),
|
|
325
|
+
('_claimgate_active_ownerless_claims(uuid)', 'PREDICATE', 'quota'),
|
|
326
|
+
('_claimgate_board_claim_policy(uuid)', 'PREDICATE', 'policy'),
|
|
327
|
+
('_claimgate_board_claim_policy_of(uuid)', 'PREDICATE', 'policy'),
|
|
328
|
+
('_claimgate_board_of_thread(uuid)', 'PREDICATE', 'lookup'),
|
|
329
|
+
('_next_callback_attempt_number(uuid)', 'PREDICATE', 'a number'),
|
|
330
|
+
('_next_task_event_sequence(uuid)', 'PREDICATE', 'a number'),
|
|
331
|
+
('mark_retention_metadata(text, timestamp with time zone, uuid, text)', 'DYNAMIC', 'service_role''s retention sweep: EXECUTE over a fixed table list, returns a count');
|
|
332
|
+
select is(
|
|
333
|
+
(select coalesce(nullif(concat_ws(' ',
|
|
334
|
+
(select 'unclassified: ' || string_agg(sig, ', ' order by sig) from _r where sig not in (select sig from _rdecl)),
|
|
335
|
+
(select 'stale: ' || string_agg(sig, ', ' order by sig) from _rdecl where sig not in (select sig from _r))), ''), '(none)')),
|
|
336
|
+
'(none)', 'AR-A3: every read-only routine an API role can execute that reads board-scoped rows, or runs EXECUTE, is classified, by full signature (an overload names itself)');
|
|
337
|
+
select is((select count(*) || '/' || count(distinct name) from _r) || '/' || (select count(*) from _rdecl), '31/31/31',
|
|
338
|
+
'AR-A3 control: 31 routines, one overload each');
|
|
339
|
+
select is(
|
|
340
|
+
(select coalesce(string_agg(r.sig, ', ' order by r.sig), '(none)') from _r r join _rdecl d using (sig) where d.cls <> 'DYNAMIC' and (d.cls = 'PREDICATE') = r.returns_rows),
|
|
341
|
+
'(none)', 'AR-A3: a PREDICATE returns one scalar (no set, composite, array, json, polymorphic or domain over them); every other disposition can return rows');
|
|
342
|
+
select is(
|
|
343
|
+
(select coalesce(string_agg(sig, ', ' order by sig), '(none)') from _r where dyn), (select string_agg(sig, ', ' order by sig) from _rdecl where cls = 'DYNAMIC'),
|
|
344
|
+
'AR-A3: the routines whose closure runs EXECUTE are listed apart, exactly these');
|
|
345
|
+
select is(
|
|
346
|
+
(select coalesce(string_agg(sig, ', ' order by sig), '(none)') from _r where ambiguous), '(none)',
|
|
347
|
+
'AR-A3: no classified routine may reach a writer through an overloaded name (a new one names itself here too)');
|
|
348
|
+
select is(
|
|
349
|
+
(select string_agg(x.f || '=' || x.n, ' ' order by x.f collate "C") from (
|
|
350
|
+
select 'overloaded names whose overloads do not all write' as f, count(*)::text as n from (
|
|
351
|
+
select p.proname from pg_proc p where p.pronamespace = 'agent_board'::regnamespace and p.prokind in ('f', 'p') group by 1
|
|
352
|
+
having count(*) > 1 and bool_or(p.oid not in (select oid from _certain))) y
|
|
353
|
+
union all
|
|
354
|
+
select 'SQL-standard bodies', count(*)::text from pg_proc p where p.pronamespace = 'agent_board'::regnamespace and p.prosqlbody is not null) x),
|
|
355
|
+
'SQL-standard bodies=0 overloaded names whose overloads do not all write=0',
|
|
356
|
+
'AR-A3 control: today every overloaded name is all writers, and no body is SQL-standard, so no call is ambiguous');
|
|
357
|
+
select is(
|
|
358
|
+
(select string_agg(t || '=' || pg_temp.scalar(t::regtype)::text, ' ' order by o)
|
|
359
|
+
from unnest(array['uuid', 'boolean', 'text', 'agent_board.agent_kind', 'uuid[]', 'agent_board.boards', 'agent_board.boards[]', 'record', 'anyelement', 'anyarray', 'jsonb', 'json']) with ordinality x(t, o)),
|
|
360
|
+
'uuid=true boolean=true text=true agent_board.agent_kind=true uuid[]=false agent_board.boards=false agent_board.boards[]=false record=false anyelement=false anyarray=false jsonb=false json=false',
|
|
361
|
+
'AR-A3 control: what counts as a scalar');
|
|
362
|
+
select is(
|
|
363
|
+
(select string_agg(x.proname || '=' || x.reads, ' ' order by x.proname)
|
|
364
|
+
from (select p.proname, string_agg(n.name, '+' order by n.name) as reads from _b join pg_proc p on p.oid = _b.oid join _names n on b ~ agent_board_test.qname_re(n.name)
|
|
365
|
+
where p.proname in ('revoke_human_subtree', 'expand_recipients_dry_run', 'evaluate_fleet_sender_policy', '_claimgate_promotion_blockers') group by 1) x),
|
|
366
|
+
'_claimgate_promotion_blockers=claims evaluate_fleet_sender_policy=fleet_receive_policies+fleet_trust_sets+signature_evidence expand_recipients_dry_run=threads revoke_human_subtree=enrollment_federation_audit',
|
|
367
|
+
'AR-A3: the four row-returning UNCHANGED routines with no behaviour row read exactly what their reason says');
|
|
368
|
+
-- AR-A4: the helper, and the two replaced functions.
|
|
369
|
+
select is(
|
|
370
|
+
(select string_agg(p.proname || ':' || p.prosecdef::text || ':' || p.provolatile::text || ':' || coalesce(array_to_string(p.proconfig, ','), '-') || ':'
|
|
371
|
+
|| coalesce((select string_agg(r, ',' order by r) from unnest(array['anon', 'authenticated', 'service_role', 'agent_board_delegate', 'public']) r
|
|
372
|
+
where has_function_privilege(r, p.oid, 'EXECUTE')), 'none') || ':' || (obj_description(p.oid, 'pg_proc') like '%#918%')::text, ' ' order by p.proname)
|
|
373
|
+
from pg_proc p where p.pronamespace = 'agent_board'::regnamespace and p.proname in ('_thread_on_board', 'list_my_board_requests')),
|
|
374
|
+
'_thread_on_board:false:s:search_path="",plan_cache_mode=force_generic_plan:authenticated,service_role:true list_my_board_requests:true:s:search_path=agent_board, public:authenticated:true',
|
|
375
|
+
'AR-A4: definer flags, volatility, settings, EXECUTE and #918 COMMENT; the one new routine is SECURITY INVOKER, and keeps plan_cache_mode (without it the tasks and threads policies are re-planned per row; plans/evidence/918/pr2-explain)');
|
|
376
|
+
-- AR-A6 (R1): the archive record is not on boards: one FK to agents (a second makes PostgREST's agents(...) embeds
|
|
377
|
+
-- ambiguous), no archive column (boards is readable and published).
|
|
378
|
+
select is(
|
|
379
|
+
(select string_agg(c.conname, ' ' order by c.conname) from pg_constraint c where c.conrelid = 'agent_board.boards'::regclass and c.contype = 'f' and c.confrelid = 'agent_board.agents'::regclass)
|
|
380
|
+
|| '/' || (select count(*) from pg_attribute a where a.attrelid = 'agent_board.boards'::regclass and a.attnum > 0 and not a.attisdropped and a.attname ~* 'archiv'),
|
|
381
|
+
'boards_created_by_agent_id_fkey/0', 'AR-A6 (R1): boards has exactly one FK to agents and no archive column');
|
|
382
|
+
-- AR-A5: which views call it (each passes the archived boards the caller can read).
|
|
383
|
+
select is(
|
|
384
|
+
(select string_agg(v.name, ' ' order by v.name) from _v v where agent_board_test.stripped_viewdef(v.oid, true) ~ agent_board_test.call_re('_thread_on_board')),
|
|
385
|
+
'v_open_escalations v_ops_open_escalations v_ops_stale_claims', 'AR-A5: the views that resolve a thread''s board are exactly these, none of them in the delegate''s read closure');
|
|
386
|
+
|
|
387
|
+
-- ===========================================================================
|
|
388
|
+
-- Fixtures
|
|
389
|
+
-- O and N owners, M member (recipient, assignee, claimant), X left both boards, Z outsider, PR a
|
|
390
|
+
-- pending requester, IV an invitee, OB an observer, PD a declined requester, CR the boards' creator (not a member; declined
|
|
391
|
+
-- requester), D a hosted delegate of the human DO. AC stays active; AR is archived and un-archived.
|
|
392
|
+
-- Each board: one post, one direct message and one multicast in the board's thread, a tasking delivery
|
|
393
|
+
-- to the board and one to M by name, two tasks, an escalation, four claims.
|
|
394
|
+
-- ===========================================================================
|
|
395
|
+
do $$
|
|
396
|
+
declare
|
|
397
|
+
v_who text[] := array['O','M','N','X','Z','PR','PD','CR','IV','OB'];
|
|
398
|
+
i int; b text; v_board uuid; v_t uuid; v_x uuid; r record;
|
|
399
|
+
begin
|
|
400
|
+
for i in 1 .. array_length(v_who, 1) loop
|
|
401
|
+
perform pg_temp.put(v_who[i], '93310000-0000-4000-8000-' || lpad(i::text, 12, '0'));
|
|
402
|
+
perform pg_temp.put(v_who[i] || '.auth', '93310001-0000-4000-8000-' || lpad(i::text, 12, '0'));
|
|
403
|
+
insert into auth.users(id, instance_id, aud, role, email)
|
|
404
|
+
values (pg_temp.u(v_who[i] || '.auth'), '00000000-0000-0000-0000-000000000000', 'authenticated', 'authenticated', 'ab918r-' || lower(v_who[i]) || '@t.test');
|
|
405
|
+
insert into agent_board.agents(id, agent_type, kind, handle, display_name, status, auth_user_id, owner_user_id)
|
|
406
|
+
values (pg_temp.u(v_who[i]), 'agent', 'agent', 'ab918r-' || lower(v_who[i]), 'AB918R ' || v_who[i], 'active', pg_temp.u(v_who[i] || '.auth'), pg_temp.u(v_who[i] || '.auth'));
|
|
407
|
+
end loop;
|
|
408
|
+
insert into agent_board.identity_providers(issuer, audience, supported, policy_approved, allowed_scopes, allowed_board_scopes, allowed_agent_kinds)
|
|
409
|
+
values ('https://ab918r.test', 'message-board', true, true, '{}', '{}', '{agent,human}');
|
|
410
|
+
select * into r from agent_board.enroll_federated_human('https://ab918r.test', 'message-board', 'ab918r-do', '{"sub":"ab918r-do","email":"ab918r-do@t.test","email_verified":true}'::jsonb, 'AB918R DO');
|
|
411
|
+
perform pg_temp.put('DO', r.agent_id::text); perform pg_temp.put('DO.auth', r.auth_user_id::text);
|
|
412
|
+
select * into r from agent_board.exchange_delegate_credential(jsonb_build_object('iss', 'https://ab918r.test', 'sub', 'ab918r-do', 'slot_resource', null,
|
|
413
|
+
'agent_board_delegate_instance', jsonb_build_object('instance_id', 'ab918r_instance_0001', 'grant_id', 'ab918r_grant_000001', 'generation', 1,
|
|
414
|
+
'owner_sub', 'ab918r-do', 'client_id', 'ab918r-client', 'status', 'active', 'client_change', null)));
|
|
415
|
+
perform pg_temp.put('D', r.agent_id::text); perform pg_temp.put('D.auth', r.auth_user_id::text); perform pg_temp.put('D.cred', r.credential_id::text);
|
|
416
|
+
set constraints all immediate;
|
|
417
|
+
set constraints all deferred;
|
|
418
|
+
insert into agent_board.agent_signing_keys(id, agent_id, key_id, key_version, public_key, public_key_format, fingerprint, status, proof_state)
|
|
419
|
+
values ('93310002-0000-4000-8000-000000000001', pg_temp.u('O'), 'primary', 1, public.test_pubkey('ab918r-o'), 'raw_ed25519_base64url', public.test_key_fingerprint('ab918r-o'), 'active', 'proven');
|
|
420
|
+
foreach b in array array['AC', 'AR'] loop
|
|
421
|
+
v_board := case b when 'AC' then '93311000-0000-4000-8000-000000000001' else '93311000-0000-4000-8000-000000000002' end;
|
|
422
|
+
perform pg_temp.put(b || '.B', v_board::text);
|
|
423
|
+
insert into agent_board.boards(id, slug, name, created_by_agent_id, visibility)
|
|
424
|
+
values (v_board, 'ab918r-' || lower(b), 'AB918R ' || b, pg_temp.u('CR'), 'invite_only');
|
|
425
|
+
insert into agent_board.board_memberships(board_id, agent_id, role, membership_state, created_by_agent_id)
|
|
426
|
+
select v_board, pg_temp.u(w), ro, 'joined', pg_temp.u('O')
|
|
427
|
+
from (values ('O','owner'), ('M','member'), ('N','owner'), ('X','member'), ('DO','member'), ('OB','observer')) m(w, ro);
|
|
428
|
+
perform pg_temp.claims(pg_temp.c('DO'));
|
|
429
|
+
perform agent_board.add_delegate_to_board(pg_temp.u('D'), v_board);
|
|
430
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, board_id) values ('coordination', pg_temp.u('O'), 'ab918r ' || b, v_board) returning id into v_t;
|
|
431
|
+
perform pg_temp.put(b || '.T', v_t::text);
|
|
432
|
+
perform pg_temp.claims(pg_temp.c('O'));
|
|
433
|
+
perform * from agent_board.post_board_message(v_t, pg_temp.u('O'), v_board, 's', 'post', p_idempotency_key => 'ab918r-post');
|
|
434
|
+
perform * from agent_board.send_direct_message(v_t, pg_temp.u('O'), pg_temp.u('M'), 'direct', 'direct');
|
|
435
|
+
perform * from agent_board.send_multicast_message(v_t, pg_temp.u('O'), array[pg_temp.u('M'), pg_temp.u('N')], 'multi', 'multi');
|
|
436
|
+
select task_id into v_x from agent_board.create_tasking_delivery(v_t, pg_temp.u('O'), 'board task', p_board_id => v_board);
|
|
437
|
+
perform pg_temp.put(b || '.BTASK', v_x::text);
|
|
438
|
+
perform * from agent_board.create_tasking_delivery(v_t, pg_temp.u('O'), 'named task', p_recipient_agent_ids => array[pg_temp.u('M')]);
|
|
439
|
+
select task_id into v_x from agent_board.create_task(v_t, pg_temp.u('O'), 'task', p_assigned_agent_id => pg_temp.u('M'));
|
|
440
|
+
perform pg_temp.put(b || '.TASK', v_x::text);
|
|
441
|
+
select task_id into v_x from agent_board.create_task(v_t, pg_temp.u('O'), 'task 2', p_assigned_agent_id => pg_temp.u('M'));
|
|
442
|
+
perform pg_temp.put(b || '.TASK2', v_x::text);
|
|
443
|
+
perform * from agent_board.raise_escalation(v_t, pg_temp.u('O'), 'e', p_assigned_agent_id => pg_temp.u('M'));
|
|
444
|
+
perform * from agent_board.set_fleet_receive_policy(pg_temp.u('O'), 'ab918r', 'allow', p_board_id => v_board);
|
|
445
|
+
perform pg_temp.claims(null);
|
|
446
|
+
-- stale claims naming the board by thread, by task only, and by resource_key only; and a live one
|
|
447
|
+
insert into agent_board.claims(thread_id, task_id, agent_id, resource_kind, resource_key, lease_expires_at) values
|
|
448
|
+
(v_t, null, pg_temp.u('M'), 'custom', 'ab918r-thread-' || b, now() - interval '1 hour'),
|
|
449
|
+
(null, pg_temp.u(b || '.TASK'), pg_temp.u('M'), 'custom', 'ab918r-task-' || b, now() - interval '1 hour'),
|
|
450
|
+
(null, null, pg_temp.u('M'), 'task', pg_temp.v(b || '.TASK2'), now() - interval '1 hour'),
|
|
451
|
+
(v_t, null, pg_temp.u('M'), 'custom', 'ab918r-live-' || b, now() + interval '1 hour');
|
|
452
|
+
insert into agent_board.callback_endpoints(owner_agent_id, endpoint_url, endpoint_status, verified_at) values (pg_temp.u('M'), 'https://ab918r.test/' || b, 'verified', now()) returning id into v_x;
|
|
453
|
+
perform pg_temp.put(b || '.EP', v_x::text);
|
|
454
|
+
insert into agent_board.callback_subscriptions(endpoint_id, agent_id, subscription_scope, event_category, board_id, board_membership_id)
|
|
455
|
+
select v_x, pg_temp.u('M'), 'board', 'board.post.delivered', v_board, bm.id from agent_board.board_memberships bm where bm.board_id = v_board and bm.agent_id = pg_temp.u('M');
|
|
456
|
+
perform pg_temp.claims(pg_temp.c('O'));
|
|
457
|
+
perform * from agent_board.post_board_message(v_t, pg_temp.u('O'), v_board, 's', 'post 2'); -- queues the callback
|
|
458
|
+
perform pg_temp.claims(pg_temp.c('SVC'));
|
|
459
|
+
perform * from agent_board.record_callback_delivery_retry((select id from agent_board.callback_outbox_events where endpoint_id = v_x), v_x, 503, 'x', now() + interval '1 hour');
|
|
460
|
+
insert into agent_board.signature_evidence(agent_id, source_kind, source_message_id, operation, payload_digest, verification_status)
|
|
461
|
+
select pg_temp.u('O'), 'message', m.id, 'post_board_message', 'sha256:ab918r', 'unsigned' from agent_board.messages m where m.thread_id = v_t and m.message_sequence = 1 returning source_message_id into v_x;
|
|
462
|
+
perform pg_temp.put(b || '.MSG', v_x::text);
|
|
463
|
+
perform pg_temp.claims(pg_temp.c('M'));
|
|
464
|
+
perform * from agent_board.heartbeat(pg_temp.u('M'), v_t, 'ready');
|
|
465
|
+
perform * from agent_board.mark_read(v_t, pg_temp.u('M'), 1);
|
|
466
|
+
perform pg_temp.claims(pg_temp.c('O')); perform * from agent_board.invite_board_member(v_board, pg_temp.u('IV'), pg_temp.u('O'));
|
|
467
|
+
-- X leaves; PR requests; PD and CR request and are declined
|
|
468
|
+
perform pg_temp.claims(pg_temp.c('X')); perform * from agent_board.leave_board(v_board, pg_temp.u('X'));
|
|
469
|
+
perform pg_temp.claims(pg_temp.c('PR')); perform * from agent_board.request_join_board(v_board, pg_temp.u('PR'));
|
|
470
|
+
perform pg_temp.claims(pg_temp.c('PD')); perform * from agent_board.request_join_board(v_board, pg_temp.u('PD'));
|
|
471
|
+
perform pg_temp.claims(pg_temp.c('CR')); perform * from agent_board.request_join_board(v_board, pg_temp.u('CR'));
|
|
472
|
+
perform pg_temp.claims(pg_temp.c('O'));
|
|
473
|
+
perform * from agent_board.resolve_board_request(v_board, pg_temp.u('PD'), 'removed', pg_temp.u('O'), '{}'::jsonb);
|
|
474
|
+
perform * from agent_board.resolve_board_request(v_board, pg_temp.u('CR'), 'removed', pg_temp.u('O'), '{}'::jsonb);
|
|
475
|
+
perform pg_temp.claims(null);
|
|
476
|
+
-- ET: a thread on the board nobody has posted in, so nobody but its creator N reads it under RLS; an
|
|
477
|
+
-- escalation on it for each of M, OB, IV, PR and CR, and a stale claim of M's
|
|
478
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject, board_id) values ('coordination', pg_temp.u('N'), 'ab918r empty ' || b, v_board) returning id into v_x;
|
|
479
|
+
perform pg_temp.put(b || '.ET', v_x::text);
|
|
480
|
+
insert into agent_board.escalations(thread_id, raised_by_agent_id, assigned_agent_id, summary) select v_x, pg_temp.u('N'), pg_temp.u(w), 'empty thread' from unnest(array['M', 'OB', 'IV', 'PR', 'CR']) w;
|
|
481
|
+
insert into agent_board.claims(thread_id, agent_id, resource_kind, resource_key, lease_expires_at) values (v_x, pg_temp.u('M'), 'custom', 'ab918r-empty-' || b, now() - interval '1 hour');
|
|
482
|
+
end loop;
|
|
483
|
+
-- R2, two boards: an escalation whose thread is on one board (or on none) and whose task is on the other
|
|
484
|
+
insert into agent_board.escalations(thread_id, task_id, raised_by_agent_id, assigned_agent_id, summary) values
|
|
485
|
+
(pg_temp.u('AC.T'), pg_temp.u('AR.TASK'), pg_temp.u('O'), pg_temp.u('M'), 'thread AC, task AR'),
|
|
486
|
+
(pg_temp.u('AR.T'), pg_temp.u('AC.TASK'), pg_temp.u('O'), pg_temp.u('M'), 'thread AR, task AC'),
|
|
487
|
+
(pg_temp.u('AC.T'), pg_temp.u('AC.TASK'), pg_temp.u('O'), pg_temp.u('M'), 'thread AC, task AC'),
|
|
488
|
+
-- OB reads both boards' status and both threads, but not AR.TASK
|
|
489
|
+
(pg_temp.u('AC.T'), pg_temp.u('AR.TASK'), pg_temp.u('O'), pg_temp.u('OB'), 'observer: thread AC, task AR');
|
|
490
|
+
-- board-less: a thread, an escalation on it, a stale claim with no thread, a 'task' claim whose key is not an id
|
|
491
|
+
insert into agent_board.threads(thread_type, created_by_agent_id, subject) values ('coordination', pg_temp.u('O'), 'ab918r boardless') returning id into v_t;
|
|
492
|
+
perform pg_temp.put('FREE.T', v_t::text);
|
|
493
|
+
insert into agent_board.escalations(thread_id, raised_by_agent_id, assigned_agent_id, summary) values (v_t, pg_temp.u('O'), pg_temp.u('M'), 'boardless');
|
|
494
|
+
insert into agent_board.escalations(thread_id, task_id, raised_by_agent_id, assigned_agent_id, summary) values (v_t, pg_temp.u('AR.TASK2'), pg_temp.u('O'), pg_temp.u('M'), 'no board, task AR');
|
|
495
|
+
insert into agent_board.claims(agent_id, resource_kind, resource_key, lease_expires_at) values
|
|
496
|
+
(pg_temp.u('M'), 'custom', 'ab918r-free', now() - interval '1 hour'), (pg_temp.u('M'), 'task', 'ab918r-not-an-id', now() - interval '1 hour');
|
|
497
|
+
update agent_board.presence set expires_at = now() - interval '1 hour' where agent_id = pg_temp.u('M');
|
|
498
|
+
-- These tasks share now() and each starts with event sequence 1. Give them distinct
|
|
499
|
+
-- timestamps so a plan change cannot switch the dashboard's latest task between snapshots.
|
|
500
|
+
update agent_board.tasks t
|
|
501
|
+
set updated_at = now() + fixture.ordinal * interval '1 microsecond'
|
|
502
|
+
from (
|
|
503
|
+
select id, row_number() over (order by (thread_id = pg_temp.u('AR.T')), title, id) as ordinal
|
|
504
|
+
from agent_board.tasks
|
|
505
|
+
where thread_id in (pg_temp.u('AC.T'), pg_temp.u('AR.T'))
|
|
506
|
+
) fixture
|
|
507
|
+
where t.id = fixture.id;
|
|
508
|
+
set constraints all immediate;
|
|
509
|
+
set constraints all deferred;
|
|
510
|
+
end $$;
|
|
511
|
+
select is(
|
|
512
|
+
(select string_agg(w || ':' || si.identity_kind, ' ' order by w collate "C") from unnest(array['M','N','O','X']) w join agent_board.session_identities si on si.agent_id = pg_temp.u(w)),
|
|
513
|
+
'M:legacy N:legacy O:legacy X:legacy', 'AR fixture control: ordinary agents hold a legacy session_identities row, as every committed agent does');
|
|
514
|
+
select is(
|
|
515
|
+
(select string_agg(delivery_mode || '/' || case when board_id is null then 'none' when board_id = pg_temp.u('AC.B') then 'AC' when board_id = pg_temp.u('AR.B') then 'AR' else 'other' end || '=' || n, ' ' order by 1)
|
|
516
|
+
from (select delivery_mode, board_id, count(*) n from agent_board.inbox_items where recipient_agent_id = pg_temp.u('M') and visible_in_inbox group by 1, 2) x),
|
|
517
|
+
'board/AC=2 board/AR=2 direct/none=2 multicast/none=2 tasking/AC=1 tasking/AR=1 tasking/none=2',
|
|
518
|
+
'AR fixture control: M''s visible inbox rows, per mode and board (a direct, a multicast and a named tasking posted in a board''s thread name no board)');
|
|
519
|
+
|
|
520
|
+
-- ===========================================================================
|
|
521
|
+
-- B. The snapshots: every view and every base relation, per caller, at three moments
|
|
522
|
+
-- ===========================================================================
|
|
523
|
+
create temp table _who(who text primary key);
|
|
524
|
+
insert into _who values ('O'), ('M'), ('OB'), ('IV'), ('PR'), ('X'), ('Z'), ('D'), ('SVC');
|
|
525
|
+
create temp table _snap(moment text, who text, rel text, j jsonb, n int);
|
|
526
|
+
-- v_ops_table_growth is not snapshotted: it is seven relation SIZES, which autovacuum may change between two
|
|
527
|
+
-- reads; AR-A1 pins that it reads no board-scoped relation.
|
|
528
|
+
create function pg_temp.grab(p_moment text) returns void language plpgsql as $$
|
|
529
|
+
declare w record; r record; j jsonb;
|
|
530
|
+
begin
|
|
531
|
+
for w in select who from _who loop
|
|
532
|
+
for r in select c.oid, c.relname::text as name from pg_class c
|
|
533
|
+
where c.relnamespace = 'agent_board'::regnamespace and c.relkind in ('r', 'p', 'v', 'm')
|
|
534
|
+
and has_table_privilege(pg_temp.role_of(w.who), c.oid, 'SELECT') and c.relname <> 'v_ops_table_growth' loop
|
|
535
|
+
-- A read that raises is recorded as one ERR row: it must fail an assertion, not abort the file.
|
|
536
|
+
begin
|
|
537
|
+
perform pg_temp.claims(pg_temp.c(w.who));
|
|
538
|
+
execute format('set local role %I', pg_temp.role_of(w.who));
|
|
539
|
+
execute format('select coalesce(jsonb_agg(to_jsonb(t)), ''[]'') from agent_board.%I t', r.name) into j;
|
|
540
|
+
reset role;
|
|
541
|
+
exception when others then
|
|
542
|
+
j := jsonb_build_array(jsonb_build_object('ERR', sqlstate || ' ' || sqlerrm));
|
|
543
|
+
end;
|
|
544
|
+
insert into _snap select p_moment, w.who, r.name, x, count(*) from jsonb_array_elements(j) x group by x;
|
|
545
|
+
insert into _snap values (p_moment, w.who, r.name, null, jsonb_array_length(j));
|
|
546
|
+
end loop;
|
|
547
|
+
end loop;
|
|
548
|
+
perform pg_temp.claims(null);
|
|
549
|
+
end $$;
|
|
550
|
+
-- Rows of p_a that p_b does not hold (by value and multiplicity), as counts.
|
|
551
|
+
create function pg_temp.gone(p_a text, p_b text) returns table(who text, rel text, j jsonb) language sql as $$
|
|
552
|
+
select a.who, a.rel, a.j from _snap a
|
|
553
|
+
where a.moment = p_a and a.j is not null
|
|
554
|
+
and not exists (select 1 from _snap b where b.moment = p_b and b.who = a.who and b.rel = a.rel and b.j = a.j and b.n >= a.n) $$;
|
|
555
|
+
-- What ties a row of an UNCHANGED view to AR; two views are keyed by agent alone and count M's work there.
|
|
556
|
+
create function pg_temp.ar_ids(p_rel text) returns text language sql as $$
|
|
557
|
+
select string_agg(v, '|') from _h
|
|
558
|
+
where k in ('AR.B', 'AR.T', 'AR.TASK', 'AR.TASK2', 'AR.BTASK', 'AR.EP', 'AR.MSG', 'AR.ET')
|
|
559
|
+
or (k = 'M' and p_rel in ('v_ownerless_claim_quota', 'v_ops_presence_heartbeats')) $$;
|
|
560
|
+
|
|
561
|
+
select pg_temp.grab('before');
|
|
562
|
+
select is(pg_temp.run('O', $$select * from agent_board.set_board_archived('{AR.B}', true)$$), 'OK', 'AR-B: O archives AR through the RPC'); -- pgtap-allow-raise-checked: the call runs inside pg_temp.run, which catches every error
|
|
563
|
+
select pg_temp.grab('archived');
|
|
564
|
+
|
|
565
|
+
select is(
|
|
566
|
+
(select coalesce(string_agg(distinct who || ':' || rel || ' ' || (j ->> 'ERR'), '; '), '(none)') from _snap where j ? 'ERR'),
|
|
567
|
+
'(none)', 'AR-B: no read of a relation its caller holds SELECT on raises, before or while archived');
|
|
568
|
+
|
|
569
|
+
-- ===========================================================================
|
|
570
|
+
-- C. Who can read what does not change (base relations)
|
|
571
|
+
-- ===========================================================================
|
|
572
|
+
select is(
|
|
573
|
+
(select string_agg(who || '=' || n, ' ' order by who) from (select s.who, count(distinct s.rel) n from _snap s join _rel r on r.name = s.rel where s.moment = 'before' group by s.who) x),
|
|
574
|
+
'D=11 IV=30 M=30 O=30 OB=30 PR=30 SVC=32 X=30 Z=30', 'AR-C control: the board-scoped base relations each caller reads');
|
|
575
|
+
select is(
|
|
576
|
+
(select coalesce(string_agg(distinct g.who || ':' || g.rel, ' ' order by g.who || ':' || g.rel), '(none)')
|
|
577
|
+
from (select * from pg_temp.gone('before', 'archived') union all select * from pg_temp.gone('archived', 'before')) g where g.rel not like 'v\_%' and g.rel <> 'boards'),
|
|
578
|
+
'(none)', 'AR-C: archiving a board changes no row any caller reads from any base relation but boards (unchanged RLS, no row written)');
|
|
579
|
+
select is(
|
|
580
|
+
(select string_agg(x.who || ':' || x.d, ' ' order by x.who)
|
|
581
|
+
from (select a.who, (select string_agg(k, '+' order by k) from jsonb_object_keys(a.j) k where a.j -> k is distinct from b.j -> k) as d
|
|
582
|
+
from pg_temp.gone('before', 'archived') a
|
|
583
|
+
left join pg_temp.gone('archived', 'before') b on b.who = a.who and b.rel = a.rel and b.j ->> 'id' = a.j ->> 'id'
|
|
584
|
+
where a.rel = 'boards') x),
|
|
585
|
+
'D:status IV:status M:status O:status OB:status PR:status SVC:status',
|
|
586
|
+
'AR-C: the boards row keeps its readers (members, an observer, an invitee, a requester, the delegate, the service) and changes only its status; X and Z still read no row');
|
|
587
|
+
|
|
588
|
+
-- ===========================================================================
|
|
589
|
+
-- D. The views
|
|
590
|
+
-- ===========================================================================
|
|
591
|
+
-- D1: UNCHANGED and EXPOSE.
|
|
592
|
+
select is(
|
|
593
|
+
(select coalesce(string_agg(distinct g.who || ':' || g.rel, ' ' order by g.who || ':' || g.rel), '(none)')
|
|
594
|
+
from (select * from pg_temp.gone('before', 'archived') union all select * from pg_temp.gone('archived', 'before')) g
|
|
595
|
+
join _vdecl d on d.name = g.rel where d.cls = 'UNCHANGED'),
|
|
596
|
+
'(none)', 'AR-D1: no UNCHANGED view gains, loses or changes a row, for any caller');
|
|
597
|
+
select is(
|
|
598
|
+
(select string_agg(d.name || '=' || coalesce((select string_agg(x.who, '+' order by x.who) from (select distinct s.who from _snap s where s.moment = 'archived' and s.rel = d.name and s.j::text ~ pg_temp.ar_ids(d.name)) x), '-'), ' ' order by d.name)
|
|
599
|
+
from _vdecl d where d.cls = 'UNCHANGED'),
|
|
600
|
+
'v_active_claims=M+SVC v_agent_liveness=SVC v_board_feed=D+M+O+OB+SVC v_callback_delivery_health=M+SVC v_callback_endpoint_status=M+SVC v_callback_health=M+SVC v_callback_subscriptions=M+SVC v_fleet_delivery_posture=M+O+OB+SVC+X v_fleet_machine_status=- v_fleet_receive_policy_status=O+SVC v_ops_callback_retry_pressure=M+SVC v_ops_endpoint_disablement=- v_ops_presence_heartbeats=M+SVC v_ops_table_growth=- v_ownerless_claim_quota=SVC v_signature_evidence_status=M+O+OB+SVC+X v_task_tree=M+O+OB+SVC+X v_thread_feed=D+M+O+OB+SVC+X', 'AR-D1 non-vacuity: who still reads AR''s rows through each UNCHANGED view while it is archived ("-": the view names nothing of a board)');
|
|
601
|
+
select is(
|
|
602
|
+
(select coalesce(string_agg(distinct a.who || ':' || (select coalesce(string_agg(k, '+' order by k), 'GONE') from jsonb_object_keys(a.j) k where a.j -> k is distinct from b.j -> k), ' '), '(none)')
|
|
603
|
+
from pg_temp.gone('before', 'archived') a
|
|
604
|
+
left join pg_temp.gone('archived', 'before') b on b.who = a.who and b.rel = a.rel and b.j ->> 'board_membership_id' = a.j ->> 'board_membership_id'
|
|
605
|
+
where a.rel = 'v_board_memberships'),
|
|
606
|
+
'D:board_status IV:board_status M:board_status O:board_status OB:board_status PR:board_status SVC:board_status', 'AR-D1 EXPOSE: v_board_memberships keeps every row and changes board_status only');
|
|
607
|
+
select is(
|
|
608
|
+
pg_temp.val(w, $$select string_agg(board_slug || '=' || board_status, ' ' order by board_slug) from agent_board.v_board_memberships where agent_id = '{$$ || w || $$}' and board_id in ('{AC.B}', '{AR.B}')$$),
|
|
609
|
+
'ab918r-ac=active ab918r-ar=archived', 'AR-D1 EXPOSE: ' || w || ' reads each board''s status in v_board_memberships')
|
|
610
|
+
from unnest(array['M', 'OB', 'IV', 'PR', 'D']) w;
|
|
611
|
+
-- D2: EXCLUDE, INHERIT, ALREADY: exactly AR's rows are missing, nothing is added.
|
|
612
|
+
create temp table _tie as
|
|
613
|
+
select s.moment, s.who, s.rel, s.j, s.n from _snap s join _vdecl d on d.name = s.rel where d.tie is not null and s.j is not null and false;
|
|
614
|
+
do $$
|
|
615
|
+
declare d record;
|
|
616
|
+
begin
|
|
617
|
+
for d in select name, pg_temp.fmt(tie) as tie from _vdecl where tie is not null loop
|
|
618
|
+
execute format('insert into _tie select moment, who, rel, j, n from _snap where rel = %L and j is not null and (%s)', d.name, d.tie);
|
|
619
|
+
end loop;
|
|
620
|
+
end $$;
|
|
621
|
+
select is(
|
|
622
|
+
(select string_agg(d.name || ': ' || (
|
|
623
|
+
select string_agg(w.who || coalesce('-' || (select sum(t.n) from _tie t where t.moment = 'before' and t.rel = d.name and t.who = w.who)
|
|
624
|
+
|| '/' || (select count(*) from pg_temp.gone('before', 'archived') g where g.rel = d.name and g.who = w.who), '0')
|
|
625
|
+
|| '+' || (select count(*) from pg_temp.gone('archived', 'before') g where g.rel = d.name and g.who = w.who)
|
|
626
|
+
|| '=' || (select coalesce(sum(t.n), 0) from _tie t where t.moment = 'archived' and t.rel = d.name and t.who = w.who), ' ' order by w.who)
|
|
627
|
+
from _who w where exists (select 1 from _snap s where s.moment = 'before' and s.rel = d.name and s.who = w.who)), E'\n' order by d.name)
|
|
628
|
+
from _vdecl d where d.tie is not null),
|
|
629
|
+
$want$v_agent_inbox: D-15/15+0=0 IV0+0=0 M-18/18+0=0 O-18/18+0=0 OB-18/18+0=0 PR0+0=0 SVC-18/18+0=0 X-3/0+0=3 Z0+0=0
|
|
630
|
+
v_agent_wake_recovery: IV0+0=0 M-3/3+0=0 O0+0=0 OB-3/3+0=0 PR0+0=0 X-3/0+0=3 Z0+0=0
|
|
631
|
+
v_board_directory: IV-1/1+0=0 M-1/1+0=0 O-1/1+0=0 OB-1/1+0=0 PR-1/1+0=0 SVC0+0=0 X0+0=0 Z0+0=0
|
|
632
|
+
v_open_escalations: IV-1/0+0=1 M-6/5+0=1 O-5/5+0=0 OB-2/0+0=2 PR-1/0+0=1 SVC-10/10+0=0 X0+0=0 Z0+0=0
|
|
633
|
+
v_ops_open_escalations: IV-1/0+0=1 M-6/5+0=1 O-5/5+0=0 OB-2/0+0=2 PR-1/0+0=1 SVC-10/10+0=0 X0+0=0 Z0+0=0
|
|
634
|
+
v_ops_stale_claims: IV0+0=0 M-4/3+0=1 O-1/1+0=0 OB0+0=0 PR0+0=0 SVC-4/4+0=0 X0+0=0 Z0+0=0$want$,
|
|
635
|
+
'AR-D2: per view and caller: -rows of AR before / rows gone + rows added = rows of AR while archived. The inbox loses exactly AR''s rows for every caller who reads its status; the escalation and claim lists keep only what the caller cannot follow, an unread thread or task (AR-D7)');
|
|
636
|
+
-- D3: COUNT. M's numbers, before / archived, each the number of visible rows v_agent_inbox returns.
|
|
637
|
+
create function pg_temp.cnt(p_moment text, p_who text, p_rel text, p_key text, p_agent text, p_cols text) returns text language sql as $$
|
|
638
|
+
select string_agg(coalesce(s.j ->> c, '-'), ',' order by o)
|
|
639
|
+
from _snap s, unnest(string_to_array(p_cols, ',')) with ordinality x(c, o)
|
|
640
|
+
where s.moment = p_moment and s.who = p_who and s.rel = p_rel and s.j ->> p_key = pg_temp.v(p_agent) $$;
|
|
641
|
+
select is(
|
|
642
|
+
(select string_agg(w || ' ' || r.rel || ' ' || coalesce(pg_temp.cnt('before', w, r.rel, r.k, 'M', r.cols), 'none') || ' -> ' || coalesce(pg_temp.cnt('archived', w, r.rel, r.k, 'M', r.cols), 'none'), E'\n' order by r.rel, w)
|
|
643
|
+
from unnest(array['M', 'SVC']) w,
|
|
644
|
+
(values ('v_agent_inbox_counts', 'recipient_agent_id', 'unread_count,pending_action_count,total_visible_count'),
|
|
645
|
+
('v_ops_inbox_backlog', 'agent_id', 'unread_count,pending_action_count,total_visible_count'),
|
|
646
|
+
('v_agent_dashboard', 'agent_id', 'inbox_unread_count,inbox_pending_action_count,open_task_count,active_claim_count,assigned_open_escalation_count')) r(rel, k, cols)),
|
|
647
|
+
$want$M v_agent_dashboard 12,12,4,2,9 -> 9,9,4,2,9
|
|
648
|
+
SVC v_agent_dashboard 12,12,4,2,9 -> 9,9,4,2,9
|
|
649
|
+
M v_agent_inbox_counts 12,12,12 -> 9,9,9
|
|
650
|
+
SVC v_agent_inbox_counts 12,12,12 -> 9,9,9
|
|
651
|
+
M v_ops_inbox_backlog 12,12,12 -> 9,9,9
|
|
652
|
+
SVC v_ops_inbox_backlog 12,12,12 -> 9,9,9$want$, 'AR-D3: M''s counts lose AR''s three rows (two posts, one board task) and nothing else');
|
|
653
|
+
select is(
|
|
654
|
+
(select string_agg(m || ':' || w || '=' || (select coalesce(sum(n), 0) from _snap s where s.moment = m and s.who = w and s.rel = 'v_agent_inbox' and s.j ->> 'recipient_agent_id' = pg_temp.v('M'))
|
|
655
|
+
|| '/' || pg_temp.cnt(m, w, 'v_agent_inbox_counts', 'recipient_agent_id', 'M', 'total_visible_count'), ' ' order by m, w)
|
|
656
|
+
from unnest(array['before', 'archived']) m, unnest(array['M', 'SVC']) w),
|
|
657
|
+
'archived:M=9/9 archived:SVC=9/9 before:M=12/12 before:SVC=12/12', 'AR-D3: v_agent_inbox_counts.total_visible_count equals the rows v_agent_inbox returns, for the recipient and the service');
|
|
658
|
+
select is(
|
|
659
|
+
pg_temp.val('D', $$select string_agg(inbox_unread_count::text, ',') from agent_board.v_agent_dashboard where agent_id = '{D}'$$) || '/' ||
|
|
660
|
+
pg_temp.val('D', $$select count(*)::text from agent_board.v_agent_inbox where recipient_agent_id = '{D}' and action_state = 'new'$$),
|
|
661
|
+
'3/3', 'AR-D3: the delegate''s dashboard count equals its own inbox rows, AR''s excluded');
|
|
662
|
+
-- D4: rows that name no board.
|
|
663
|
+
select is(
|
|
664
|
+
pg_temp.val('M', $$select string_agg(delivery_mode || '/' || coalesce(board_slug, 'none') || '=' || n, ' ' order by 1) from (select delivery_mode, board_slug, count(*) n from agent_board.v_agent_inbox where recipient_agent_id = '{M}' group by 1, 2) x$$),
|
|
665
|
+
'board/ab918r-ac=2 direct/none=2 multicast/none=2 tasking/ab918r-ac=1 tasking/none=2',
|
|
666
|
+
'AR-D4: a direct message, a multicast and a named tasking stay, though posted in the archived board''s thread; the other board''s rows stay');
|
|
667
|
+
select is(
|
|
668
|
+
pg_temp.val('SVC', $$select string_agg(resource_key, ' ' order by resource_key) from agent_board.v_ops_stale_claims where agent_id = '{M}' and resource_key <> '{AC.TASK2}'$$),
|
|
669
|
+
'ab918r-empty-AC ab918r-free ab918r-not-an-id ab918r-task-AC ab918r-thread-AC', 'AR-D4: stale claims with no board, and the active board''s, stay (a non-id task key is not an error)');
|
|
670
|
+
select is(
|
|
671
|
+
(select string_agg(v || ': ' || pg_temp.val('M', format($q$select string_agg(summary || case when thread_id = '{FREE.T}' then ' (no board)' else '' end, ', ' order by summary) from agent_board.%I where thread_id in ('{FREE.T}', '{AR.T}', '{AC.T}')$q$, v)), E'\n' order by v)
|
|
672
|
+
from unnest(array['v_open_escalations', 'v_ops_open_escalations']) v),
|
|
673
|
+
$want$v_open_escalations: boardless (no board), e, thread AC, task AC
|
|
674
|
+
v_ops_open_escalations: boardless (no board), e, thread AC, task AC$want$,
|
|
675
|
+
'AR-D4 (R2): an escalation whose thread OR task is on the archived board leaves both lists, whichever board (or none) the other names; AC''s own and the board-less one stay');
|
|
676
|
+
-- D5: a still-held claim stays visible where it matters.
|
|
677
|
+
select is(
|
|
678
|
+
pg_temp.val('M', $$select count(*)::text from agent_board.v_active_claims where resource_key = 'ab918r-live-AR'$$), '1', 'AR-D5: a live claim on the archived board is still shown as held');
|
|
679
|
+
-- D7 (R3): who reads the status but not the thread keeps the row. Not a defect to widen reads for.
|
|
680
|
+
select is(
|
|
681
|
+
(select string_agg(w || '=' || pg_temp.val(w, $q$select (select string_agg(status, '') from agent_board.boards where id = '{AR.B}') || '/' || (select count(*) from agent_board.threads where id = '{AR.ET}') || '/'
|
|
682
|
+
|| (select count(*) from agent_board.v_open_escalations where thread_id = '{AR.ET}') || '/' || (select count(*) from agent_board.v_ops_open_escalations where thread_id = '{AR.ET}') || '/'
|
|
683
|
+
|| (select count(*) from agent_board.v_ops_stale_claims where thread_id = '{AR.ET}')$q$), ' ' order by o)
|
|
684
|
+
from unnest(array['M', 'OB', 'IV', 'PR', 'CR', 'N', 'SVC']) with ordinality x(w, o)),
|
|
685
|
+
'M=archived/0/1/1/1 OB=archived/0/1/1/0 IV=archived/0/1/1/0 PR=archived/0/1/1/0 CR=archived/0/1/1/0 N=archived/1/0/0/0 SVC=archived/1/0/0/0',
|
|
686
|
+
'AR-D7 (R3): status / thread readable / listed in v_open_escalations, v_ops_open_escalations, v_ops_stale_claims: a thread the caller cannot read keeps its rows listed (member, observer, invitee, requester, creator); its reader and the service lose them');
|
|
687
|
+
select is(
|
|
688
|
+
(select string_agg(w || '=' || pg_temp.val(w, $q$select (select string_agg(status, '') from agent_board.boards where id = '{AR.B}') || '/' || (select count(*) from agent_board.threads where id = '{AR.T}') || '/'
|
|
689
|
+
|| (select count(*) from agent_board.tasks where id = '{AR.TASK}') || '/' || (select count(*) from agent_board.v_open_escalations where summary = 'observer: thread AC, task AR') || '/'
|
|
690
|
+
|| (select count(*) from agent_board.v_ops_open_escalations where summary = 'observer: thread AC, task AR')$q$), ' ' order by o)
|
|
691
|
+
from unnest(array['OB', 'M', 'O', 'SVC']) with ordinality x(w, o)),
|
|
692
|
+
'OB=archived/1/0/1/1 M=archived/1/1/0/0 O=archived/1/1/0/0 SVC=archived/1/1/0/0',
|
|
693
|
+
'AR-D7 (R3): status / task''s thread readable / task readable / listed in v_open_escalations, v_ops_open_escalations: following a task needs the task row AND its thread; an observer who reads the status and the thread but not the task still sees the escalation');
|
|
694
|
+
-- D6: what the rule costs. A member who leaves while the board is archived can no longer read its
|
|
695
|
+
-- status, so the rows they keep are listed again, as an active board's are.
|
|
696
|
+
select is(
|
|
697
|
+
pg_temp.val('M', $$select count(*)::text from agent_board.v_agent_inbox where board_id = '{AR.B}'$$) || '/' ||
|
|
698
|
+
pg_temp.probe('M', $$select * from agent_board.leave_board('{AR.B}', '{M}')$$, 'M', $$select count(*)::text from agent_board.v_agent_inbox where board_id = '{AR.B}'$$) || '/' || -- pgtap-allow-raise-checked: the call runs inside pg_temp.probe, which catches every error
|
|
699
|
+
pg_temp.probe('M', $$select * from agent_board.leave_board('{AC.B}', '{M}')$$, 'M', $$select count(*)::text from agent_board.v_agent_inbox where board_id = '{AC.B}'$$), -- pgtap-allow-raise-checked: the call runs inside pg_temp.probe, which catches every error
|
|
700
|
+
'0/3/3', 'AR-D6 (the cost of the rule): a member sees none of AR''s rows; after leaving it sees its 3 again, exactly as it keeps 3 after leaving the active board');
|
|
701
|
+
|
|
702
|
+
-- ===========================================================================
|
|
703
|
+
-- E. The routines
|
|
704
|
+
-- ===========================================================================
|
|
705
|
+
create temp table _req(moment text, who text, slug text, state text);
|
|
706
|
+
create function pg_temp.reqs(p_who text) returns text language sql as $$
|
|
707
|
+
select coalesce(pg_temp.val(p_who, $q$select string_agg(board_slug || ':' || membership_state, ' ' order by board_slug) from agent_board.list_my_board_requests() where board_id in ('{AC.B}', '{AR.B}')$q$), 'none') $$;
|
|
708
|
+
select is(pg_temp.reqs('PR'), 'ab918r-ac:requested', 'AR-E1 list_my_board_requests: a pending requester, who can read the board''s status, is not shown AR');
|
|
709
|
+
select is(pg_temp.reqs('CR'), 'ab918r-ac:left', 'AR-E1: nor is the board''s creator, declined');
|
|
710
|
+
select is(pg_temp.reqs('PD'), 'ab918r-ac:left ab918r-ar:left', 'AR-E1: a declined requester, who cannot read the status, is shown what it was shown before (the list is not a status oracle)');
|
|
711
|
+
select is(pg_temp.val('PD', $$select count(*)::text from agent_board.boards where id = '{AR.B}'$$) || pg_temp.val('CR', $$select count(*)::text from agent_board.boards where id = '{AR.B}'$$), '01',
|
|
712
|
+
'AR-E1 control: PD reads no boards row for AR, CR reads it');
|
|
713
|
+
select is(
|
|
714
|
+
pg_temp.val('O', format($$select count(*)::text from agent_board.%s('{%s.B}')$$, f, b)), n, 'AR-E2 UNCHANGED: ' || f || ' of ' || b)
|
|
715
|
+
from (values ('list_board_members', 'AC', '6'), ('list_board_members', 'AR', '6'), ('list_board_pending_memberships', 'AC', '2'), ('list_board_pending_memberships', 'AR', '2')) x(f, b, n);
|
|
716
|
+
select is(
|
|
717
|
+
pg_temp.val('O', format($$select status || ':' || (board_id = '{%s.B}')::text from agent_board.get_send_outcome('{%s.T}', 'ab918r-post')$$, b, b)), 'confirmed:true', 'AR-E2 UNCHANGED: get_send_outcome on ' || b)
|
|
718
|
+
from unnest(array['AC', 'AR']) b;
|
|
719
|
+
select is(
|
|
720
|
+
pg_temp.val('SVC', $$select count(*)::text from agent_board.ownerless_quota_census()$$) || '/' || pg_temp.val('SVC', $$select active_ownerless_claims::text from agent_board.v_ownerless_claim_quota where agent_id = '{M}'$$)
|
|
721
|
+
|| '/' || pg_temp.val('SVC', $$select agent_board._claimgate_active_ownerless_claims('{M}')::text$$),
|
|
722
|
+
'1/2/2', 'AR-E2 UNCHANGED: the ownerless quota still counts the live claim on AR, as the enforcement does');
|
|
723
|
+
select is(
|
|
724
|
+
pg_temp.val('SVC', $$select (reporting_agents > 0)::text from agent_board.agent_liveness_summary()$$), 'true', 'AR-E2 UNCHANGED: agent_liveness_summary still answers');
|
|
725
|
+
select is(
|
|
726
|
+
pg_temp.val('D', $$select ('{AR.B}' = any(agent_board._delegate_visible_board_ids()))::text$$) || pg_temp.val('M', $$select agent_board._current_agent_has_board_access('{AR.B}')::text || agent_board._current_agent_can_access_thread('{AR.T}')::text$$),
|
|
727
|
+
'truetruetrue', 'AR-E2: the read predicates still admit AR''s members and delegate');
|
|
728
|
+
-- E3: the helper answers nothing its caller could not select.
|
|
729
|
+
select is(
|
|
730
|
+
(select string_agg(w || '=' || pg_temp.val(w, $$select agent_board._thread_on_board(array['{AR.B}'::uuid], '{AR.T}')::text || agent_board._thread_on_board(array['{AR.B}'::uuid], null, '{AR.TASK}')::text$$)
|
|
731
|
+
|| '/' || pg_temp.val(w, $$select (count(*) > 0)::text from agent_board.threads where id = '{AR.T}'$$) || pg_temp.val(w, $$select (count(*) > 0)::text from agent_board.tasks where id = '{AR.TASK}'$$), ' ' order by o)
|
|
732
|
+
from unnest(array['O', 'M', 'Z', 'SVC']) with ordinality x(w, o)),
|
|
733
|
+
'O=truetrue/truetrue M=truetrue/truetrue Z=falsefalse/falsefalse SVC=truetrue/truetrue', 'AR-E3: _thread_on_board is true exactly for callers who can select the thread and the task themselves');
|
|
734
|
+
select is(
|
|
735
|
+
pg_temp.val('M', $$select concat_ws(',', agent_board._thread_on_board(null, '{AR.T}'), agent_board._thread_on_board('{}', '{AR.T}'), agent_board._thread_on_board(array['{AR.B}'::uuid], null),
|
|
736
|
+
agent_board._thread_on_board(array['{AC.B}'::uuid], '{AR.T}'), agent_board._thread_on_board(array['{AR.B}'::uuid], '{FREE.T}'), agent_board._thread_on_board(array['{AC.B}'::uuid], null, '{AR.TASK}'))$$),
|
|
737
|
+
'f,f,f,f,f,f', 'AR-E3: false, never NULL, for an empty or NULL list, no thread, another board and a board-less thread');
|
|
738
|
+
-- The helper's plan is generic and cached, and carries the threads and tasks policies: one backend, callers and
|
|
739
|
+
-- roles interleaved, each gets its own answer (agent_board_archive_board_reads_plan.test.sql does the same
|
|
740
|
+
-- across transactions).
|
|
741
|
+
create function pg_temp.interleaved() returns text language plpgsql as $$
|
|
742
|
+
declare w text; a text := '';
|
|
743
|
+
begin
|
|
744
|
+
foreach w in array array['O', 'Z', 'M', 'SVC', 'Z', 'O', 'OB', 'PR', 'SVC', 'Z', 'M', 'X', 'SVC', 'PR', 'O'] loop
|
|
745
|
+
a := a || w || '=' || pg_temp.val(w, $q$select bool_and(agent_board._thread_on_board(array['{AR.B}'::uuid], '{AR.T}'))::text || '/' || bool_and(agent_board._thread_on_board(array['{AR.B}'::uuid], null, '{AR.TASK}'))::text || '/'
|
|
746
|
+
|| bool_or(agent_board._thread_on_board(array['{AR.B}'::uuid], '{AR.ET}'))::text from generate_series(1, 7)$q$) || ' ';
|
|
747
|
+
end loop;
|
|
748
|
+
return rtrim(a);
|
|
749
|
+
end $$;
|
|
750
|
+
select is(pg_temp.interleaved(),
|
|
751
|
+
'O=true/true/false Z=false/false/false M=true/true/false SVC=true/true/true Z=false/false/false O=true/true/false OB=true/false/false PR=false/false/false SVC=true/true/true Z=false/false/false M=true/true/false X=true/false/false SVC=true/true/true PR=false/false/false O=true/true/false',
|
|
752
|
+
'AR-E3: thread / task / unread thread, seven calls each, callers and roles interleaved in one backend: each caller its own answer, service_role after authenticated and back');
|
|
753
|
+
select alike(pg_temp.val('D', $$select agent_board._thread_on_board(array['{AR.B}'::uuid], '{AR.T}')::text$$), 'ERR 42501 permission denied for function%', 'AR-E3: agent_board_delegate cannot execute it');
|
|
754
|
+
|
|
755
|
+
-- ===========================================================================
|
|
756
|
+
-- F. Un-archive: every row comes back
|
|
757
|
+
-- ===========================================================================
|
|
758
|
+
select is(pg_temp.run('O', $$select * from agent_board.set_board_archived('{AR.B}', false)$$), 'OK', 'AR-F: O un-archives AR'); -- pgtap-allow-raise-checked: the call runs inside pg_temp.run, which catches every error
|
|
759
|
+
select pg_temp.grab('back');
|
|
760
|
+
select is(
|
|
761
|
+
(select coalesce(string_agg(distinct g.who || ':' || g.rel, ' ' order by g.who || ':' || g.rel), '(none)')
|
|
762
|
+
from (select * from pg_temp.gone('before', 'back') union all select * from pg_temp.gone('back', 'before')) g),
|
|
763
|
+
'(none)', 'AR-F: for every caller, every view and base relation holds exactly the rows it held before the archive');
|
|
764
|
+
select is(
|
|
765
|
+
(select concat_ws('/', count(*) > 400, count(*) = count(*) filter (where b.n = a.n), sum(a.n) > 1000) from _snap a join _snap b using (who, rel) where a.moment = 'before' and b.moment = 'back' and a.j is null and b.j is null),
|
|
766
|
+
't/t/t', 'AR-F control: over 400 relation reads compared, every row count equal, over 1000 rows');
|
|
767
|
+
select is(pg_temp.reqs('PR') || ' | ' || pg_temp.reqs('CR'), 'ab918r-ac:requested ab918r-ar:requested | ab918r-ac:left ab918r-ar:left', 'AR-F: list_my_board_requests shows the request again');
|
|
768
|
+
|
|
769
|
+
-- ===========================================================================
|
|
770
|
+
-- G. The upgrade path (tests/seeds/20261203000100_….pre.sql): a board 1.82.0 archived
|
|
771
|
+
-- ===========================================================================
|
|
772
|
+
select pg_temp.put('SO', '91891000-0000-4000-8000-0000000000a1'), pg_temp.put('SO.auth', '91891000-0000-4000-8000-0000000000b1'),
|
|
773
|
+
pg_temp.put('SM', '91891000-0000-4000-8000-0000000000a2'), pg_temp.put('SM.auth', '91891000-0000-4000-8000-0000000000b2');
|
|
774
|
+
create function pg_temp.seed_lists() returns text language sql as $$
|
|
775
|
+
select pg_temp.val('SM', $q$select concat_ws('/', (select count(*) from agent_board.v_agent_inbox), (select count(*) from agent_board.v_agent_wake_recovery), (select coalesce(sum(total_visible_count), 0) from agent_board.v_agent_inbox_counts where recipient_agent_id = '{SM}'),
|
|
776
|
+
(select inbox_unread_count from agent_board.v_agent_dashboard where agent_id = '{SM}'), (select count(*) from agent_board.v_open_escalations), (select count(*) from agent_board.v_ops_open_escalations), (select count(*) from agent_board.v_ops_stale_claims))$q$) $$;
|
|
777
|
+
select is(pg_temp.seed_lists(), '0/0/0/0/0/0/0', 'AR-G: its rows are out of the lists from this migration on');
|
|
778
|
+
select is(pg_temp.val('SM', $$select board_status || '/' || (select count(*) from agent_board.v_thread_feed) || '/' || (select count(*) from agent_board.v_board_feed) from agent_board.v_board_memberships$$), 'archived/1/1', 'AR-G: its member reads the status and the history');
|
|
779
|
+
select is(pg_temp.run('SO', $$select * from agent_board.set_board_archived('91891000-0000-4000-8000-000000000001', false)$$), 'OK', 'AR-G: its owner un-archives it'); -- pgtap-allow-raise-checked: the call runs inside pg_temp.run, which catches every error
|
|
780
|
+
select is(pg_temp.seed_lists(), '1/1/1/1/1/1/1', 'AR-G: every row comes back');
|
|
781
|
+
select is(pg_temp.run('SO', $$select * from agent_board.set_board_archived('91891000-0000-4000-8000-000000000001', true)$$), 'OK', 'AR-G: and archives it again'); -- pgtap-allow-raise-checked: the call runs inside pg_temp.run, which catches every error
|
|
782
|
+
select is(pg_temp.seed_lists(), '0/0/0/0/0/0/0', 'AR-G: out of the lists again');
|
|
783
|
+
|
|
784
|
+
select * from finish();
|
|
785
|
+
rollback;
|