@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.
Files changed (195) hide show
  1. package/fixtures/already-applied.json +32 -4
  2. package/migrations/20261026000100_agent_board_thread_association.sql +1287 -0
  3. package/migrations/20261101000100_agent_board_session_measured_mint.sql +145 -0
  4. package/migrations/20261104065658_agent_board_reply_delivery.sql +659 -0
  5. package/migrations/20261104170000_agent_board_tasking_replay_scope.sql +320 -0
  6. package/migrations/20261104180000_agent_board_delivery_writer_execute.sql +51 -0
  7. package/migrations/20261104190000_agent_board_message_replay_scope.sql +373 -0
  8. package/migrations/20261108000100_agent_board_send_outcome.sql +708 -0
  9. package/package.json +1 -1
  10. package/schema-manifest.json +66 -3
  11. package/src/deployment-drift-main.test.mjs +20 -3
  12. package/src/deployment-drift-script.test.mjs +77 -2
  13. package/src/deployment-drift.test.mjs +60 -0
  14. package/src/tooling.test.mjs +9 -2
  15. package/tests/agent_board_661_reply_delivery.test.sql +185 -0
  16. package/tests/agent_board_667_boundary.test.sql +162 -0
  17. package/tests/agent_board_667_conversion_branches.test.sql +91 -0
  18. package/tests/agent_board_667_conversion_profile.test.sql +112 -0
  19. package/tests/agent_board_667_history_class.test.sql +787 -0
  20. package/tests/agent_board_667_native_cast_effect.test.sql +88 -0
  21. package/tests/agent_board_667_realtime_apply_behavior.test.sql +117 -0
  22. package/tests/agent_board_667_realtime_apply_controls.test.sql +143 -0
  23. package/tests/agent_board_667_realtime_apply_profile.test.sql +61 -0
  24. package/tests/agent_board_667_retention.test.sql +201 -0
  25. package/tests/agent_board_667_routing.test.sql +294 -0
  26. package/tests/agent_board_667_sender_effects.test.sql +100 -0
  27. package/tests/agent_board_667_sender_profile_controls.test.sql +29 -0
  28. package/tests/agent_board_667_storage_delimiter_behavior.test.sql +84 -0
  29. package/tests/agent_board_667_storage_delimiter_controls.test.sql +23 -0
  30. package/tests/agent_board_667_storage_delimiter_profile.test.sql +61 -0
  31. package/tests/agent_board_667_storage_search_behavior.test.sql +93 -0
  32. package/tests/agent_board_667_storage_search_controls.test.sql +22 -0
  33. package/tests/agent_board_667_storage_search_profile.test.sql +60 -0
  34. package/tests/agent_board_667_storage_timestamp_behavior.test.sql +58 -0
  35. package/tests/agent_board_667_storage_timestamp_controls.test.sql +22 -0
  36. package/tests/agent_board_667_storage_timestamp_profile.test.sql +54 -0
  37. package/tests/agent_board_667_writers.test.sql +281 -0
  38. package/tests/agent_board_717_message_replay_scope.test.sql +828 -0
  39. package/tests/agent_board_717_native_surface.test.sql +223 -0
  40. package/tests/agent_board_717_tasking_replay_scope.test.sql +661 -0
  41. package/tests/agent_board_addressing.test.sql +8 -5
  42. package/tests/agent_board_claim_subject_gate.test.sql +7 -3
  43. package/tests/agent_board_claimpolicy_race.test.sql +39 -11
  44. package/tests/agent_board_delegate_role_class.test.sql +35 -17
  45. package/tests/agent_board_delivery_writer_execute.test.sql +373 -0
  46. package/tests/agent_board_fk_lock_edges.test.sql +3 -0
  47. package/tests/agent_board_hosted_delegates.test.sql +13 -3
  48. package/tests/agent_board_inbox_rpcs.test.sql +5 -3
  49. package/tests/agent_board_obsgate.test.sql +8 -4
  50. package/tests/agent_board_obsgate4.test.sql +2 -2
  51. package/tests/agent_board_outbox_wake_ordering.test.sql +25 -2
  52. package/tests/agent_board_own_post_not_delivered.test.sql +11 -9
  53. package/tests/agent_board_send_outcome.test.sql +805 -0
  54. package/tests/agent_board_session_identity_behavior_classes.test.sql +2 -2
  55. package/tests/agent_board_session_identity_limits.test.sql +1 -1
  56. package/tests/agent_board_session_identity_measured_mint.test.sql +342 -0
  57. package/tests/agent_board_session_identity_predicate_isolation.test.sql +146 -0
  58. package/tests/agent_board_sidserver_authority.test.sql +69 -0
  59. package/tests/agent_board_sidserver_catalog.test.sql +47 -0
  60. package/tests/agent_board_signed_write_gateway.test.sql +4 -2
  61. package/tests/agent_board_task_thread_lock_order.test.sql +67 -42
  62. package/tests/agent_board_v2_invariants.test.sql +5 -2
  63. package/tests/agent_board_write_enforcement_class.test.sql +12 -8
  64. package/tests/catalog/function_execute_privileges.json +65 -5
  65. package/tests/catalog/p_metadata_parameters.json +24 -0
  66. package/tests/fixtures/agent_board_667_apply_rls_guard.sql +195 -0
  67. package/tests/fixtures/agent_board_667_apply_rls_provenance.md +125 -0
  68. package/tests/fixtures/agent_board_667_apply_rls_reference.sql +1903 -0
  69. package/tests/fixtures/agent_board_667_conversion_guard.sql +162 -0
  70. package/tests/fixtures/agent_board_667_conversion_reference.md +23 -0
  71. package/tests/fixtures/agent_board_667_conversion_reference.sql +5325 -0
  72. package/tests/fixtures/agent_board_667_delimiter_guard.sql +92 -0
  73. package/tests/fixtures/agent_board_667_delimiter_provenance.md +116 -0
  74. package/tests/fixtures/agent_board_667_delimiter_reference.sql +10150 -0
  75. package/tests/fixtures/agent_board_667_search_guard.sql +92 -0
  76. package/tests/fixtures/agent_board_667_search_provenance.md +141 -0
  77. package/tests/fixtures/agent_board_667_search_reference.sql +4779 -0
  78. package/tests/fixtures/agent_board_667_sender_guard.sql +102 -0
  79. package/tests/fixtures/agent_board_667_sender_reference.md +20 -0
  80. package/tests/fixtures/agent_board_667_sender_reference.sql +875 -0
  81. package/tests/fixtures/agent_board_667_timestamp_graph.sql +84 -0
  82. package/tests/fixtures/agent_board_667_timestamp_guard.sql +94 -0
  83. package/tests/fixtures/agent_board_667_timestamp_provenance.md +99 -0
  84. package/tests/fixtures/agent_board_667_timestamp_reference.sql +2938 -0
  85. package/tests/fixtures/isolation-substrate.sql +8 -0
  86. package/tests/fixtures/pgtap-unexposed.sql +61 -0
  87. package/tests/isolation/expected/association_artifact_anchor_order.out +108 -0
  88. package/tests/isolation/expected/association_board_vs_board.out +71 -0
  89. package/tests/isolation/expected/association_post_wait_authority.out +77 -0
  90. package/tests/isolation/expected/association_strong_history_writers.out +203 -0
  91. package/tests/isolation/expected/association_strong_isolation.out +185 -0
  92. package/tests/isolation/expected/association_vs_artifact_claim_escalation.out +199 -0
  93. package/tests/isolation/expected/association_vs_heartbeat.out +78 -0
  94. package/tests/isolation/expected/association_vs_multi_anchor_artifact.out +67 -0
  95. package/tests/isolation/expected/association_vs_private_message.out +67 -0
  96. package/tests/isolation/expected/association_vs_task_and_conflict.out +133 -0
  97. package/tests/isolation/expected/association_vs_taskful_cross_thread.out +133 -0
  98. package/tests/isolation/expected/message_replay_scope.out +209 -0
  99. package/tests/isolation/expected/send_outcome_cross_board.out +40 -0
  100. package/tests/isolation/expected/send_outcome_independence.out +295 -0
  101. package/tests/isolation/expected/send_outcome_membership.out +140 -0
  102. package/tests/isolation/expected/send_outcome_overlap.out +89 -0
  103. package/tests/isolation/expected/send_outcome_reader.out +87 -0
  104. package/tests/isolation/expected/send_outcome_recovery.out +107 -0
  105. package/tests/isolation/expected/tasking_replay_scope.out +125 -0
  106. package/tests/isolation/specs/association_artifact_anchor_order.spec +46 -0
  107. package/tests/isolation/specs/association_board_vs_board.spec +37 -0
  108. package/tests/isolation/specs/association_post_wait_authority.spec +48 -0
  109. package/tests/isolation/specs/association_strong_history_writers.spec +61 -0
  110. package/tests/isolation/specs/association_strong_isolation.spec +50 -0
  111. package/tests/isolation/specs/association_vs_artifact_claim_escalation.spec +41 -0
  112. package/tests/isolation/specs/association_vs_heartbeat.spec +47 -0
  113. package/tests/isolation/specs/association_vs_multi_anchor_artifact.spec +38 -0
  114. package/tests/isolation/specs/association_vs_private_message.spec +33 -0
  115. package/tests/isolation/specs/association_vs_task_and_conflict.spec +38 -0
  116. package/tests/isolation/specs/association_vs_taskful_cross_thread.spec +42 -0
  117. package/tests/isolation/specs/message_replay_scope.spec +77 -0
  118. package/tests/isolation/specs/send_outcome_cross_board.spec +66 -0
  119. package/tests/isolation/specs/send_outcome_independence.spec +95 -0
  120. package/tests/isolation/specs/send_outcome_membership.spec +73 -0
  121. package/tests/isolation/specs/send_outcome_overlap.spec +69 -0
  122. package/tests/isolation/specs/send_outcome_reader.spec +61 -0
  123. package/tests/isolation/specs/send_outcome_recovery.spec +68 -0
  124. package/tests/isolation/specs/tasking_replay_scope.spec +72 -0
  125. package/tests/mutations/670/E1_helper_author_from_message.sql +7 -0
  126. package/tests/mutations/670/E2_helper_no_null_check.sql +13 -0
  127. package/tests/mutations/670/M10_lock_removed.sql +10 -0
  128. package/tests/mutations/670/M11_table_granted_to_authenticated.sql +2 -0
  129. package/tests/mutations/670/M12_helper_granted_to_service_role.sql +2 -0
  130. package/tests/mutations/670/M13_message_fk_no_cascade.sql +8 -0
  131. package/tests/mutations/670/M14_post_message_mints_from_metadata.sql +11 -0
  132. package/tests/mutations/670/M15_second_receipt_writer.sql +9 -0
  133. package/tests/mutations/670/M16_read_no_btrim.sql +7 -0
  134. package/tests/mutations/670/M17_read_volatile.sql +2 -0
  135. package/tests/mutations/670/M1_helper_no_service_role_guard.sql +13 -0
  136. package/tests/mutations/670/M3_helper_on_replay_branch.sql +10 -0
  137. package/tests/mutations/670/M4_read_checks_thread_existence.sql +10 -0
  138. package/tests/mutations/670/M5a_read_requires_thread_access.sql +10 -0
  139. package/tests/mutations/670/M5b_read_requires_board_membership.sql +11 -0
  140. package/tests/mutations/670/M6_read_admits_service_role.sql +14 -0
  141. package/tests/mutations/670/M7_confirmed_adds_message_sequence.sql +12 -0
  142. package/tests/mutations/670/M8_delivery_swallows_errors.sql +19 -0
  143. package/tests/mutations/670/M9_lock_key_includes_board.sql +10 -0
  144. package/tests/mutations/670/run.sh +84 -0
  145. package/tests/mutations/717-f1/M0_prefix_main_acl_and_comments.sql +11 -0
  146. package/tests/mutations/717-f1/M10_wrapper_stops_delegating.sql +7 -0
  147. package/tests/mutations/717-f1/M11_transitive_recipient_content.sql +8 -0
  148. package/tests/mutations/717-f1/M12_transitive_recipient_visibility.sql +8 -0
  149. package/tests/mutations/717-f1/M13_transitive_worker_content.sql +15 -0
  150. package/tests/mutations/717-f1/M1a_regrant_upsert_delivery_recipient.sql +1 -0
  151. package/tests/mutations/717-f1/M1b_regrant_upsert_inbox_item.sql +1 -0
  152. package/tests/mutations/717-f1/M1c_regrant_queue_inbox_callbacks.sql +1 -0
  153. package/tests/mutations/717-f1/M1d_regrant_queue_board_post_callbacks.sql +1 -0
  154. package/tests/mutations/717-f1/M1e_regrant_next_thread_message_sequence.sql +1 -0
  155. package/tests/mutations/717-f1/M2_blanket_schema_grant.sql +1 -0
  156. package/tests/mutations/717-f1/M3_new_overload_of_sink.sql +10 -0
  157. package/tests/mutations/717-f1/M4_new_route_calling_sink.sql +7 -0
  158. package/tests/mutations/717-f1/M5_actor_trusts_supplied_id.sql +5 -0
  159. package/tests/mutations/717-f1/M6_signed_route_granted_to_authenticated.sql +1 -0
  160. package/tests/mutations/717-f1/M7_worker_granted_to_authenticated.sql +1 -0
  161. package/tests/mutations/717-f1/M8_recipient_state_rewrites_visibility.sql +5 -0
  162. package/tests/mutations/717-f1/M9_public_route_revoked_from_service_role.sql +1 -0
  163. package/tests/mutations/717-f1/outbox_wake_ordering/W1_regrant_queue_inbox_callbacks.sql +1 -0
  164. package/tests/mutations/717-f1/outbox_wake_ordering/W2_revoke_worker_from_service_role.sql +1 -0
  165. package/tests/mutations/717-f1/outbox_wake_ordering/W3_invoker_caller_of_sink.sql +1 -0
  166. package/tests/mutations/717-f1/outbox_wake_ordering/W4_public_regrant_board_post_sink.sql +1 -0
  167. package/tests/mutations/717-f1/run.sh +17 -0
  168. package/tests/mutations/717-f2b/C1_new_delivery_route.sql +4 -0
  169. package/tests/mutations/717-f2b/C2_new_replay_lookup.sql +3 -0
  170. package/tests/mutations/717-f2b/C3_internal_regranted.sql +2 -0
  171. package/tests/mutations/717-f2b/C4_wrapper_writes_delivery.sql +9 -0
  172. package/tests/mutations/717-f2b/C5_class_member_dynamic_sql.sql +9 -0
  173. package/tests/mutations/717-f2b/C6_drop_board_scope_constraint.sql +2 -0
  174. package/tests/mutations/717-f2b/C7_state_private_helper_content.sql +14 -0
  175. package/tests/mutations/717-f2b/L1_dm_lock_split_by_recipient.sql +8 -0
  176. package/tests/mutations/717-f2b/L2_dm_lock_removed.sql +12 -0
  177. package/tests/mutations/717-f2b/L3_mc_lock_split_by_recipients.sql +8 -0
  178. package/tests/mutations/717-f2b/L4_mc_lock_removed.sql +12 -0
  179. package/tests/mutations/717-f2b/M0_prefix_definitions.sql +17 -0
  180. package/tests/mutations/717-f2b/M10_mc_lookup_any_author.sql +15 -0
  181. package/tests/mutations/717-f2b/M11_dm_swallow_refusal.sql +8 -0
  182. package/tests/mutations/717-f2b/M12_mc_swallow_refusal.sql +8 -0
  183. package/tests/mutations/717-f2b/M13_signed_dm_swallows_refusal.sql +32 -0
  184. package/tests/mutations/717-f2b/M14_signed_mc_swallows_refusal.sql +20 -0
  185. package/tests/mutations/717-f2b/M15_mc_any_source.sql +7 -0
  186. package/tests/mutations/717-f2b/M1_dm_drop_recipient_proof.sql +16 -0
  187. package/tests/mutations/717-f2b/M2_mc_drop_recipient_proof.sql +16 -0
  188. package/tests/mutations/717-f2b/M3_dm_metadata_as_proof.sql +12 -0
  189. package/tests/mutations/717-f2b/M4_mc_metadata_as_proof.sql +16 -0
  190. package/tests/mutations/717-f2b/M5_dm_any_mode.sql +11 -0
  191. package/tests/mutations/717-f2b/M6_dm_board_delivery_counts.sql +11 -0
  192. package/tests/mutations/717-f2b/M7_mc_any_mode.sql +11 -0
  193. package/tests/mutations/717-f2b/M8_dm_thread_stamp_as_proof.sql +12 -0
  194. package/tests/mutations/717-f2b/M9_dm_lookup_any_author.sql +15 -0
  195. package/tests/mutations/717-f2b/run.sh +82 -0
@@ -0,0 +1,787 @@
1
+ -- Catalog-derived history and writer dispositions, coupled to #667 behavior and
2
+ -- mutation witnesses. Unsupported routine and entry paths fail by full identity.
3
+ create extension if not exists pgtap with schema extensions;
4
+ set search_path = extensions, public, agent_board;
5
+ begin;
6
+ -- Like WEC-24, own the parser prerequisite inside this test's transaction. CI
7
+ -- starts with the extension available but not installed; another file's rolled
8
+ -- back installation cannot supply it. Missing availability still fails closed.
9
+ do $$
10
+ begin
11
+ if exists (select 1 from pg_available_extensions where name='plpgsql_check') then
12
+ execute 'create extension if not exists plpgsql_check with schema extensions';
13
+ end if;
14
+ end $$;
15
+ select plan(61);
16
+ -- Procedure bodies and CALL edges are outside the function parsers below.
17
+ -- Fail closed on every non-extension application procedure until its complete
18
+ -- history and caller effects receive an explicit positive disposition.
19
+ select is(
20
+ (select string_agg(format('%I.%I(%s)', n.nspname, p.proname,
21
+ pg_get_function_identity_arguments(p.oid)), ', '
22
+ order by n.nspname, p.proname, p.oid::regprocedure::text)
23
+ from pg_proc p join pg_namespace n on n.oid=p.pronamespace
24
+ where p.prokind='p'
25
+ and n.nspname not in ('pg_catalog','information_schema','agent_board_test')
26
+ and n.nspname not like 'pg_%'
27
+ and not exists (select 1 from pg_depend d
28
+ where d.classid='pg_proc'::regclass and d.objid=p.oid and d.deptype='e')),
29
+ null::text, 'XB application procedures and CALL paths require explicit disposition');
30
+ -- Supabase ships DDL event-trigger handlers outside extension membership. They
31
+ -- are not ordinary RPC entry points: prove the PostgreSQL refusal for every
32
+ -- catalog-derived handler instead of excluding a platform schema or name. The
33
+ -- control keeps this exemption exercised on minimal local substrates too.
34
+ create schema xb_event_trigger_probe;
35
+ create function xb_event_trigger_probe.not_rpc() returns event_trigger language plpgsql as $body$
36
+ begin execute 'select 1'; end $body$;
37
+ create function pg_temp.xb_event_trigger_call_problem(p_fn oid) returns text language plpgsql as $body$
38
+ begin
39
+ execute format('select %s()',p_fn::regproc);
40
+ return 'ordinary invocation succeeded';
41
+ exception when others then
42
+ if sqlstate='0A000' and sqlerrm='trigger functions can only be called as triggers' then
43
+ return null;
44
+ end if;
45
+ return sqlstate || ':' || sqlerrm;
46
+ end $body$;
47
+ select is((select case when count(*)=0 then 'event-trigger control missing'
48
+ else string_agg(format('%s: %s',f.oid::regprocedure,x.problem), ', ' order by f.oid::regprocedure::text)
49
+ filter (where x.problem is not null) end
50
+ from pg_proc f join pg_namespace n on n.oid=f.pronamespace
51
+ cross join lateral pg_temp.xb_event_trigger_call_problem(f.oid) x(problem)
52
+ where f.prokind='f' and f.prorettype='event_trigger'::regtype
53
+ and n.nspname not in ('pg_catalog','information_schema','agent_board_test') and n.nspname not like 'pg_%'
54
+ and not exists (select 1 from pg_depend d where d.classid='pg_proc'::regclass and d.objid=f.oid and d.deptype='e')),
55
+ null::text, 'XB DDL event-trigger handlers cannot be invoked as ordinary RPCs');
56
+ -- Enumerate from constraints, not a hand-written list. The receipt exemption is
57
+ -- permitted only while its complete catalog shape has no caller-content columns.
58
+ -- Each relation must carry the exact closed predicate, not merely its name: a
59
+ -- name scan survived `... where thread_id = p_thread_id and false` (#707 B1).
60
+ -- The exemption is positive: read_receipts must itself be a direct thread FK.
61
+ select is(
62
+ (select string_agg(problem, ', ' order by problem) from (
63
+ select 'agent_board.read_receipts: exemption is not a direct thread FK' as problem
64
+ where not exists (select 1 from pg_constraint where contype='f'
65
+ and conrelid='agent_board.read_receipts'::regclass and confrelid='agent_board.threads'::regclass)
66
+ union all
67
+ select 'direct thread-FK history census is empty'
68
+ where not exists (select 1 from pg_constraint where contype='f' and confrelid='agent_board.threads'::regclass
69
+ and conrelid <> 'agent_board.read_receipts'::regclass)
70
+ union all
71
+ select format('%I.%I', n.nspname, c.relname)
72
+ from (select distinct conrelid from pg_constraint
73
+ where contype='f' and confrelid='agent_board.threads'::regclass
74
+ and conrelid <> 'agent_board.read_receipts'::regclass) fk
75
+ join pg_class c on c.oid=fk.conrelid
76
+ join pg_namespace n on n.oid=c.relnamespace
77
+ where agent_board_test.stripped_body('agent_board._associate_empty_thread(uuid,uuid)'::regprocedure)
78
+ !~ agent_board_test.re(agent_board_test.ci_re('exists') || '\s*\(\s*' || agent_board_test.ci_re('select')
79
+ || '\s+1\s+' || agent_board_test.ci_re('from') || '\s+' || agent_board_test.qname_frag(n.nspname||'.'||c.relname,false)
80
+ || '\s+' || agent_board_test.ci_re('where') || '\s+' || agent_board_test.col_ref_frag('thread_id')
81
+ || '\s*=\s*' || agent_board_test.ident_re('p_thread_id') || '\s*\)')) p),
82
+ null::text, 'XB every direct thread-FK relation has its exact thread-history predicate');
83
+ select is(
84
+ (select array_agg(a.attname::text order by a.attnum) from pg_attribute a
85
+ where a.attrelid='agent_board.read_receipts'::regclass and a.attnum>0 and not a.attisdropped),
86
+ array['thread_id','agent_id','last_read_message_sequence','last_read_at']::text[],
87
+ 'XB receipt exemption has only subject, bounded sequence and timestamp columns');
88
+ -- Every catalog-discovered one-hop edge must be an enforced, validated single-column
89
+ -- FK to its parent. This pins parent existence, not the remaining writer closure.
90
+ select is(
91
+ (with direct as (select distinct conrelid from pg_constraint where contype='f' and confrelid='agent_board.threads'::regclass), edges as (
92
+ select f.oid, f.conrelid, f.conname, f.convalidated, cardinality(f.conkey) as nkeys
93
+ from pg_constraint f join direct d on d.conrelid=f.confrelid where f.contype='f')
94
+ select string_agg(format('%s.%I',conrelid::regclass,conname), ', ' order by conrelid::regclass::text, conname)
95
+ from edges where not convalidated or nkeys<>1),
96
+ null::text, 'XB one-hop parent-existence FKs are validated and single-column');
97
+ -- Reviewed one-hop FK identities, derived from the fresh pg_constraint census.
98
+ -- Each entry is positively checked against a validated FK below. This records
99
+ -- relational parent existence only; parent history and writer behavior still
100
+ -- require the separate witnesses and closure proof.
101
+ create temp table xb_onehop_reviewed(child oid,parent oid, constraint_name text, primary key(child,parent,constraint_name)) on commit drop;
102
+ insert into xb_onehop_reviewed values
103
+ ('agent_board.artifacts'::regclass,'agent_board.messages'::regclass,'artifacts_message_id_fkey'),
104
+ ('agent_board.artifacts'::regclass,'agent_board.task_events'::regclass,'artifacts_task_event_id_fkey'),
105
+ ('agent_board.artifacts'::regclass,'agent_board.tasks'::regclass,'artifacts_task_id_fkey'),
106
+ ('agent_board.callback_outbox_events'::regclass,'agent_board.messages'::regclass,'callback_outbox_events_source_message_id_fkey'),
107
+ ('agent_board.callback_outbox_events'::regclass,'agent_board.tasks'::regclass,'callback_outbox_events_source_task_id_fkey'),
108
+ ('agent_board.claims'::regclass,'agent_board.tasks'::regclass,'claims_task_id_fkey'),
109
+ ('agent_board.conflicts'::regclass,'agent_board.tasks'::regclass,'conflicts_task_id_fkey'),
110
+ ('agent_board.delegations'::regclass,'agent_board.tasks'::regclass,'delegations_child_task_id_fkey'),
111
+ ('agent_board.delegations'::regclass,'agent_board.tasks'::regclass,'delegations_parent_task_id_fkey'),
112
+ ('agent_board.delivery_recipients'::regclass,'agent_board.messages'::regclass,'delivery_recipients_source_message_id_fkey'),
113
+ ('agent_board.delivery_recipients'::regclass,'agent_board.tasks'::regclass,'delivery_recipients_source_task_id_fkey'),
114
+ ('agent_board.escalations'::regclass,'agent_board.tasks'::regclass,'escalations_task_id_fkey'),
115
+ ('agent_board.inbox_items'::regclass,'agent_board.messages'::regclass,'inbox_items_source_message_id_fkey'),
116
+ ('agent_board.inbox_items'::regclass,'agent_board.tasks'::regclass,'inbox_items_source_task_id_fkey'),
117
+ ('agent_board.messages'::regclass,'agent_board.tasks'::regclass,'messages_task_id_fkey'),
118
+ ('agent_board.send_outcomes'::regclass,'agent_board.messages'::regclass,'send_outcomes_message_id_fkey'),
119
+ ('agent_board.signature_evidence'::regclass,'agent_board.artifacts'::regclass,'signature_evidence_source_artifact_id_fkey'),
120
+ ('agent_board.signature_evidence'::regclass,'agent_board.messages'::regclass,'signature_evidence_source_message_id_fkey'),
121
+ ('agent_board.signature_evidence'::regclass,'agent_board.task_events'::regclass,'signature_evidence_source_task_event_id_fkey'),
122
+ ('agent_board.task_events'::regclass,'agent_board.tasks'::regclass,'task_events_task_id_fkey'),
123
+ ('agent_board.tasks'::regclass,'agent_board.tasks'::regclass,'tasks_parent_task_id_fkey'),
124
+ ('agent_board.tasks'::regclass,'agent_board.tasks'::regclass,'tasks_root_task_id_fkey');
125
+ create temp table xb_onehop_actual on commit drop as
126
+ with direct as (select distinct conrelid from pg_constraint where contype='f' and confrelid='agent_board.threads'::regclass)
127
+ select f.conrelid as child,f.confrelid as parent,f.conname::text as constraint_name,f.convalidated
128
+ from pg_constraint f join direct d on d.conrelid=f.confrelid where f.contype='f';
129
+ select is((select string_agg(format('%s -> %s (%s)',a.child::regclass,a.parent::regclass,a.constraint_name), ', '
130
+ order by a.child::regclass::text,a.parent::regclass::text,a.constraint_name)
131
+ from xb_onehop_actual a where not exists (select 1 from xb_onehop_reviewed r
132
+ where r.child=a.child and r.parent=a.parent and r.constraint_name=a.constraint_name)),
133
+ null::text, 'XB unreviewed one-hop FK names itself');
134
+ select is((select string_agg(format('%s -> %s (%s)',r.child::regclass,r.parent::regclass,r.constraint_name), ', '
135
+ order by r.child::regclass::text,r.parent::regclass::text,r.constraint_name)
136
+ from xb_onehop_reviewed r where not exists (select 1 from xb_onehop_actual a
137
+ where a.child=r.child and a.parent=r.parent and a.constraint_name=r.constraint_name and a.convalidated)),
138
+ null::text, 'XB each reviewed one-hop entry is a real validated FK to its claimed parent');
139
+
140
+ -- Every actual one-hop edge points to a direct thread-FK parent. Bind each
141
+ -- discovered parent to the actual association predicate; the boundary and
142
+ -- writer behavior tests separately prove task, message, event and artifact
143
+ -- predicates with isolated history and omission mutations. This does not claim
144
+ -- that parent existence alone proves each descendant's anchor semantics.
145
+ select is((select string_agg(parent::regclass::text, ', ' order by parent::regclass::text)
146
+ from (select distinct parent from xb_onehop_actual) parents
147
+ join pg_class c on c.oid=parents.parent join pg_namespace n on n.oid=c.relnamespace
148
+ where agent_board_test.stripped_body('agent_board._associate_empty_thread(uuid,uuid)'::regprocedure)
149
+ !~ agent_board_test.re(agent_board_test.ci_re('exists') || '\s*\(\s*' || agent_board_test.ci_re('select')
150
+ || '\s+1\s+' || agent_board_test.ci_re('from') || '\s+' || agent_board_test.qname_frag(n.nspname||'.'||c.relname,false)
151
+ || '\s+' || agent_board_test.ci_re('where') || '\s+' || agent_board_test.col_ref_frag('thread_id')
152
+ || '\s*=\s*' || agent_board_test.ident_re('p_thread_id') || '\s*\)')),
153
+ null::text, 'XB every discovered one-hop parent has its own thread-history predicate');
154
+
155
+ -- A bounded production PL/pgSQL call graph, built from catalog identities and
156
+ -- parser-resolved OIDs. SQL-language and trigger edges are checked separately
157
+ -- below; application procedures fail closed above. Parser failures and dynamic
158
+ -- SQL remain visible by full identity instead of being treated as safe.
159
+ create function pg_temp.xb_dependencies(p_fn oid) returns table(dep_oid oid, problem text) language plpgsql as $body$
160
+ begin
161
+ return query select d.oid::oid, null::text
162
+ from extensions.plpgsql_show_dependency_tb(p_fn::regprocedure,0::oid) d where d.type='FUNCTION';
163
+ exception when others then
164
+ return query select null::oid, sqlstate || ':' || sqlerrm;
165
+ end $body$;
166
+ create temp table xb_plpgsql on commit drop as
167
+ select p.oid, format('%I.%I(%s)',n.nspname,p.proname,oidvectortypes(p.proargtypes)) as identity,
168
+ (has_function_privilege('authenticated',p.oid,'EXECUTE') or has_function_privilege('service_role',p.oid,'EXECUTE')
169
+ or has_function_privilege('anon',p.oid,'EXECUTE') or has_function_privilege('agent_board_delegate',p.oid,'EXECUTE')) as root,
170
+ agent_board_test.stripped_body(p.oid) ~ agent_board_test.re('\m' || agent_board_test.ci_re('execute') || '\M') as opaque
171
+ from pg_proc p join pg_namespace n on n.oid=p.pronamespace join pg_language l on l.oid=p.prolang
172
+ where p.prokind='f' and l.lanname='plpgsql' and p.prorettype not in ('trigger'::regtype,'event_trigger'::regtype)
173
+ and n.nspname not in ('pg_catalog','information_schema','agent_board_test') and n.nspname not like 'pg_%'
174
+ and not exists (select 1 from pg_depend d where d.classid='pg_proc'::regclass and d.objid=p.oid and d.deptype='e');
175
+ create temp table xb_edges on commit drop as
176
+ select f.oid as source,d.dep_oid,d.problem from xb_plpgsql f cross join lateral pg_temp.xb_dependencies(f.oid) d;
177
+ create temp table xb_reachable on commit drop as
178
+ with recursive reach(root,oid,path) as (
179
+ select oid,oid,array[oid] from xb_plpgsql where root
180
+ union all select r.root,e.dep_oid,r.path||e.dep_oid from reach r join xb_edges e on e.source=r.oid
181
+ join xb_plpgsql f on f.oid=e.dep_oid where not e.dep_oid=any(r.path)
182
+ ) select distinct oid from reach;
183
+ select ok((select count(*)>0 from xb_plpgsql where root) and (select count(*)>0 from xb_edges where dep_oid is not null),
184
+ 'XB PL/pgSQL callable roots and parser edges are nonempty');
185
+ select is((select string_agg(distinct f.identity || ':' || e.problem, ', ' order by f.identity || ':' || e.problem)
186
+ from xb_plpgsql f join xb_edges e on e.source=f.oid where e.problem is not null),
187
+ null::text, 'XB PL/pgSQL parser failures name themselves even on internal functions');
188
+ -- The exact reviewed body is bounded to four UPDATE templates over eight
189
+ -- literal validator targets. Its real effects on all eight targets, including
190
+ -- every row-level side write and anchor, are exercised in the companion
191
+ -- agent_board_667_retention test. The body binding forces a fresh disposition
192
+ -- for any new template, target, call or expression; it is not behavioral proof.
193
+ select is(md5(pg_get_functiondef('agent_board.mark_retention_metadata(text,timestamptz,uuid,text)'::regprocedure)),
194
+ 'ce1b6fb7c62f4e4b4110fcf09f18211e', 'XB retention reviewed validator and UPDATE templates remain exact');
195
+ -- Conversion templates select typed scalar literals, not relations. Their type,
196
+ -- operator, range and text-search dispatch must stay inside the finite primitive
197
+ -- reference; custom or drifted callbacks close admission. Other platform dynamic
198
+ -- SQL (including apply_rls and Storage) remains unresolved by this admission.
199
+ \ir fixtures/agent_board_667_conversion_guard.sql
200
+ select is((select string_agg(identity||': '||problem, ', ' order by identity,problem) from (
201
+ select * from pg_temp.xb_cf_problems() union all select * from pg_temp.xb_cf_wrapper_problems()) p),
202
+ null::text,'XB native conversion dispatch and metadata require exact disposition')
203
+ where exists(select 1 from pg_temp.xb_cf_wrappers where to_regprocedure(identity) is not null);
204
+ select * from skip('native conversion wrappers absent; no platform disposition credit',1)
205
+ where not exists(select 1 from pg_temp.xb_cf_wrappers where to_regprocedure(identity) is not null);
206
+ create temp table xb_native_conversion_admitted on commit drop as select fn from pg_temp.xb_cf_admitted();
207
+ -- The two native senders have a separate, positive destination/effect class.
208
+ -- Storage readers and apply_rls require their own positive proofs.
209
+ \ir fixtures/agent_board_667_sender_guard.sql
210
+ select is((select string_agg(identity||': '||problem, ', ' order by identity,problem) from pg_temp.xb_sf_problems()),
211
+ null::text,'XB native sender destination and effects require exact disposition')
212
+ where exists(select 1 from pg_temp.xb_sf_reference where kind='wrapper' and to_regprocedure(identity) is not null);
213
+ select * from skip('native senders absent; no platform sender disposition credit',1)
214
+ where not exists(select 1 from pg_temp.xb_sf_reference where kind='wrapper' and to_regprocedure(identity) is not null);
215
+ insert into xb_native_conversion_admitted select fn from pg_temp.xb_sf_admitted();
216
+ -- One Storage SELECT template has its own target, resolution, aggregate and
217
+ -- callback proof. Each other reader requires a separate disposition.
218
+ \ir fixtures/agent_board_667_timestamp_guard.sql
219
+ select is((select string_agg(identity||': '||problem, ', ' order by identity,problem) from pg_temp.xb_st_problems()),
220
+ null::text,'XB native timestamp reader and effects require exact disposition')
221
+ where to_regprocedure('storage.search_by_timestamp(text,text,integer,integer,text,text,text,text,text,text,text)') is not null;
222
+ select * from skip('native timestamp reader absent; no native Storage disposition credit',1)
223
+ where to_regprocedure('storage.search_by_timestamp(text,text,integer,integer,text,text,text,text,text,text,text)') is null;
224
+ insert into xb_native_conversion_admitted select fn from pg_temp.xb_st_admitted();
225
+ -- Search has separate source-derived constructor, expression and callback proof.
226
+ -- The delimiter listing and apply_rls have separate proofs below.
227
+ \ir fixtures/agent_board_667_search_guard.sql
228
+ select is((select string_agg(identity||': '||problem, ', ' order by identity,problem) from pg_temp.xb_ss_problems()),
229
+ null::text,'XB native search constructor and effect class require exact disposition')
230
+ where to_regprocedure('storage.search(text,text,integer,integer,integer,text,text,text,text,text)') is not null;
231
+ select * from skip('native search absent; no native search disposition credit',1)
232
+ where to_regprocedure('storage.search(text,text,integer,integer,integer,text,text,text,text,text)') is null;
233
+ insert into xb_native_conversion_admitted select fn from pg_temp.xb_ss_admitted();
234
+ -- Delimiter listing is a distinct source/record/dispatch effect class.
235
+ \ir fixtures/agent_board_667_delimiter_guard.sql
236
+ select is((select string_agg(identity||': '||problem, ', ' order by identity,problem) from pg_temp.xb_sd_problems()),
237
+ null::text,'XB native delimiter constructor and effect class require exact disposition')
238
+ where to_regprocedure('storage.list_objects_with_delimiter(text,text,text,integer,text,text,text,text,text,timestamp with time zone,text)') is not null;
239
+ select * from skip('native delimiter absent; no native delimiter disposition credit',1)
240
+ where to_regprocedure('storage.list_objects_with_delimiter(text,text,text,integer,text,text,text,text,text,timestamp with time zone,text)') is null;
241
+ insert into xb_native_conversion_admitted select fn from pg_temp.xb_sd_admitted();
242
+ -- apply_rls prepares a SELECT over a caller-selected row-security relation and
243
+ -- executes it as each subscriber role. Its target, policy, helper-body and
244
+ -- built-in closure is separate; same-named relations close search-path redirection.
245
+ \ir fixtures/agent_board_667_apply_rls_guard.sql
246
+ select is((select string_agg(identity||': '||problem, ', ' order by identity,problem) from pg_temp.xb_ar_problems()),
247
+ null::text,'XB native apply_rls target, policy and helper closure require exact disposition')
248
+ where to_regprocedure('realtime.apply_rls(jsonb,integer)') is not null;
249
+ select * from skip('native apply_rls absent; no native apply_rls disposition credit',1)
250
+ where to_regprocedure('realtime.apply_rls(jsonb,integer)') is null;
251
+ insert into xb_native_conversion_admitted select fn from pg_temp.xb_ar_admitted();
252
+
253
+ select is((select string_agg(f.identity, ', ' order by f.identity) from xb_plpgsql f
254
+ where f.opaque and f.oid <> 'agent_board.mark_retention_metadata(text,timestamptz,uuid,text)'::regprocedure
255
+ and not exists(select 1 from xb_native_conversion_admitted a where a.fn=f.oid)),
256
+ null::text, 'XB other opaque PL/pgSQL identities require disposition');
257
+ -- Failure evidence only. Keep each unresolved definition on one JSON-escaped
258
+ -- diagnostic line so CI records its exact body and metadata without accepting it.
259
+ select diag(' have: OPAQUE_DEFINITION ' || jsonb_build_object(
260
+ 'identity',f.identity,'definition',pg_get_functiondef(f.oid),
261
+ 'definition_md5',md5(pg_get_functiondef(f.oid)),
262
+ 'owner',pg_get_userbyid(p.proowner),'acl',p.proacl,
263
+ 'volatility',p.provolatile,'security_definer',p.prosecdef)::text)
264
+ from xb_plpgsql f join pg_proc p on p.oid=f.oid
265
+ where f.opaque and f.oid <> 'agent_board.mark_retention_metadata(text,timestamptz,uuid,text)'::regprocedure
266
+ and not exists(select 1 from xb_native_conversion_admitted a where a.fn=f.oid)
267
+ order by f.identity;
268
+ -- source-scan: raw-subject the literal validator values are data; the exact definition binding above prevents comments or an alternate body from satisfying this extraction
269
+ select is((select array_agg(m[1] order by m[1]) from regexp_matches(
270
+ substring(pg_get_functiondef('agent_board.mark_retention_metadata(text,timestamptz,uuid,text)'::regprocedure)
271
+ from 'if p_table_name not in \((.*?)\) then'), '''([a-z_]+)''', 'g') m),
272
+ array['board_memberships','callback_delivery_attempts','callback_outbox_events','delivery_recipients',
273
+ 'inbox_items','messages','presence','task_events']::text[], 'XB retention validator admits exactly eight named targets');
274
+ select is((with direct as (select distinct conrelid from pg_constraint where contype='f' and confrelid='agent_board.threads'::regclass),
275
+ onehop as (select f.conrelid from pg_constraint f join direct d on d.conrelid=f.confrelid where f.contype='f'),
276
+ targets as (select c.oid,c.relname from pg_class c join pg_namespace n on n.oid=c.relnamespace
277
+ where n.nspname='agent_board' and c.relname=any(array['messages','task_events','presence','callback_outbox_events',
278
+ 'callback_delivery_attempts','inbox_items','delivery_recipients','board_memberships']))
279
+ select string_agg(relname || ':' || case when oid in (select conrelid from direct) then 'direct'
280
+ when oid in (select conrelid from onehop) then 'onehop' else 'outside' end, ', ' order by relname) from targets),
281
+ 'board_memberships:outside, callback_delivery_attempts:outside, callback_outbox_events:onehop, delivery_recipients:onehop, inbox_items:onehop, messages:direct, presence:direct, task_events:direct',
282
+ 'XB retention targets are related to the actual history FK graph');
283
+
284
+ -- Additional fail-closed entry-point checks. The source matcher below recognises
285
+ -- agent_board-qualified or bare relation writes, so a discovered history relation
286
+ -- in any other namespace is itself an unresolved named finding. SQL bodies are
287
+ -- scanned conservatively against every catalog-derived writer name; a matching
288
+ -- route is reported for behavioral disposition, never assumed safe. This does
289
+ -- not yet resolve arbitrary SQL calls or prove every writer's lock behavior.
290
+ create temp table xb_history_relations on commit drop as
291
+ with direct as (select distinct conrelid from pg_constraint where contype='f' and confrelid='agent_board.threads'::regclass)
292
+ select conrelid as relid from direct
293
+ union select f.conrelid from pg_constraint f join direct d on d.conrelid=f.confrelid where f.contype='f';
294
+ select is((select string_agg(r.relid::regclass::text, ', ' order by r.relid::regclass::text)
295
+ from xb_history_relations r join pg_class c on c.oid=r.relid join pg_namespace n on n.oid=c.relnamespace
296
+ where n.nspname<>'agent_board'), null::text, 'XB history relations outside the supported matcher schema name themselves');
297
+ select is((select string_agg(format('%s.%I -> %s',t.tgrelid::regclass,t.tgname,t.tgfoid::regprocedure), ', '
298
+ order by t.tgrelid::regclass::text,t.tgname)
299
+ from pg_trigger t join xb_history_relations r on r.relid=t.tgrelid where not t.tgisinternal),
300
+ null::text, 'XB DML trigger entry points on history relations name themselves');
301
+ select is((select string_agg(format('%s.%I',rw.ev_class::regclass,rw.rulename), ', ' order by rw.ev_class::regclass::text,rw.rulename)
302
+ from pg_rewrite rw join xb_history_relations r on r.relid=rw.ev_class where rw.ev_type<>'1'),
303
+ null::text, 'XB DML rule entry points on history relations name themselves');
304
+ -- No non-extension application function in an unsupported language is silently
305
+ -- omitted from the SQL/PL/pgSQL graph. It must be named and disposed first.
306
+ select is((select string_agg(format('%s [%s]',p.oid::regprocedure,l.lanname), ', '
307
+ order by p.oid::regprocedure::text)
308
+ from pg_proc p join pg_namespace n on n.oid=p.pronamespace join pg_language l on l.oid=p.prolang
309
+ where p.prokind='f' and p.prorettype<>'trigger'::regtype and l.lanname not in ('sql','plpgsql')
310
+ and n.nspname not in ('pg_catalog','information_schema','agent_board_test') and n.nspname not like 'pg_%'
311
+ and not exists (select 1 from pg_depend d where d.classid='pg_proc'::regclass and d.objid=p.oid and d.deptype='e')),
312
+ null::text, 'XB unsupported application function languages name themselves');
313
+ create temp table xb_routines on commit drop as
314
+ select p.oid,n.nspname,p.proname,l.lanname,agent_board_test.stripped_body(p.oid) as body,
315
+ (has_function_privilege('authenticated',p.oid,'EXECUTE') or has_function_privilege('service_role',p.oid,'EXECUTE')
316
+ or has_function_privilege('anon',p.oid,'EXECUTE') or has_function_privilege('agent_board_delegate',p.oid,'EXECUTE')) as callable
317
+ from pg_proc p join pg_namespace n on n.oid=p.pronamespace join pg_language l on l.oid=p.prolang
318
+ where p.prokind='f' and p.prorettype<>'trigger'::regtype and l.lanname in ('sql','plpgsql')
319
+ and n.nspname not in ('pg_catalog','information_schema','agent_board_test') and n.nspname not like 'pg_%'
320
+ and not exists (select 1 from pg_depend d where d.classid='pg_proc'::regclass and d.objid=p.oid and d.deptype='e');
321
+ -- Enumerate direct thread-FK INSERT sites from the live catalog and source,
322
+ -- then compare them to independently reviewed exact signatures and relations.
323
+ -- The mechanism checks below are structural prerequisites, coupled to the
324
+ -- focused behavior/mutation tests and the call-edge assertions below.
325
+ create temp table xb_direct_insert_actual on commit drop as
326
+ select distinct f.oid, d.relid from xb_routines f cross join lateral (
327
+ select distinct fk.conrelid as relid,c.relname from pg_constraint fk join pg_class c on c.oid=fk.conrelid
328
+ where fk.contype='f' and fk.confrelid='agent_board.threads'::regclass) d
329
+ where f.body ~ agent_board_test.stmt_re('insert into',d.relname);
330
+ create temp table xb_direct_insert_reviewed(fn oid,relid oid,mechanism text,primary key(fn,relid)) on commit drop;
331
+ insert into xb_direct_insert_reviewed values
332
+ ('agent_board.heartbeat(uuid,uuid,text,text,interval,jsonb)'::regprocedure,'agent_board.presence'::regclass,'locked_post'),
333
+ ('agent_board._insert_thread_message(uuid,uuid,text,text,uuid,jsonb)'::regprocedure,'agent_board.messages'::regclass,'message_sequence'),
334
+ ('agent_board.create_task(uuid,uuid,text,text,uuid,uuid,text,text,jsonb)'::regprocedure,'agent_board.tasks'::regclass,'locked_post'),
335
+ ('agent_board.create_task(uuid,uuid,text,text,uuid,uuid,text,text,jsonb)'::regprocedure,'agent_board.task_events'::regclass,'locked_post'),
336
+ ('agent_board.acquire_claim(uuid,text,text,timestamptz,uuid,uuid,jsonb)'::regprocedure,'agent_board.claims'::regclass,'locked_post'),
337
+ ('agent_board.mark_read(uuid,uuid,bigint)'::regprocedure,'agent_board.read_receipts'::regclass,'receipt'),
338
+ ('agent_board.add_artifact(uuid,text,text,text,uuid,uuid,uuid,uuid,jsonb)'::regprocedure,'agent_board.artifacts'::regclass,'locked_post'),
339
+ ('agent_board.raise_conflict(uuid,uuid,text,text,text,uuid,jsonb)'::regprocedure,'agent_board.conflicts'::regclass,'locked_post'),
340
+ ('agent_board.raise_escalation(uuid,uuid,text,text,text,uuid,uuid,jsonb)'::regprocedure,'agent_board.escalations'::regclass,'locked_post'),
341
+ ('agent_board._append_task_event(text,uuid,uuid,text,jsonb,text,uuid,timestamptz)'::regprocedure,'agent_board.task_events'::regclass,'task_parent');
342
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
343
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
344
+ from xb_direct_insert_actual a join pg_proc p on p.oid=a.oid join pg_namespace pn on pn.oid=p.pronamespace
345
+ join pg_class c on c.oid=a.relid join pg_namespace rn on rn.oid=c.relnamespace
346
+ where not exists (select 1 from xb_direct_insert_reviewed r where r.fn=a.oid and r.relid=a.relid)),
347
+ null::text, 'XB unreviewed direct thread-history inserters name their full identities');
348
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
349
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
350
+ from xb_direct_insert_reviewed r join pg_proc p on p.oid=r.fn join pg_namespace pn on pn.oid=p.pronamespace
351
+ join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
352
+ where not exists (select 1 from xb_direct_insert_actual a where a.oid=r.fn and a.relid=r.relid)),
353
+ null::text, 'XB each reviewed direct inserter actually writes its stated relation');
354
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I [%s]',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname,r.mechanism), ', '
355
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
356
+ from xb_direct_insert_reviewed r join xb_routines f on f.oid=r.fn
357
+ join pg_proc p on p.oid=r.fn join pg_namespace pn on pn.oid=p.pronamespace
358
+ join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
359
+ where not case r.mechanism
360
+ when 'locked_post' then f.body ~ agent_board_test.row_lock_re('threads')
361
+ and f.body ~ agent_board_test.call_re('_current_agent_can_post_to_thread',true)
362
+ when 'message_sequence' then f.body ~ agent_board_test.call_re('_next_thread_message_sequence',true)
363
+ and f.body ~ agent_board_test.call_re('_current_agent_can_post_to_thread',true)
364
+ when 'receipt' then f.body ~ agent_board_test.call_re('_agent_can_access_thread',true)
365
+ when 'task_parent' then f.body ~ agent_board_test.row_lock_re('threads')
366
+ and f.body ~ agent_board_test.call_re('_current_agent_can_mutate_task',true)
367
+ and exists(select 1 from xb_onehop_actual a where a.child='agent_board.task_events'::regclass
368
+ and a.parent='agent_board.tasks'::regclass and a.constraint_name='task_events_task_id_fkey' and a.convalidated)
369
+ else false end), null::text, 'XB each direct inserter has its positively checked mechanism prerequisites');
370
+ -- For each conditional first-history writer, bind the thread row being
371
+ -- locked to the same argument rechecked for posting before that relation's
372
+ -- INSERT. Runtime race witnesses separately prove lock/wait behavior.
373
+ create temp table xb_locked_writer_arguments(fn oid primary key,thread_arg text not null) on commit drop;
374
+ insert into xb_locked_writer_arguments values
375
+ ('agent_board.heartbeat(uuid,uuid,text,text,interval,jsonb)'::regprocedure,'v_effective_thread'),
376
+ ('agent_board.create_task(uuid,uuid,text,text,uuid,uuid,text,text,jsonb)'::regprocedure,'p_thread_id'),
377
+ ('agent_board.acquire_claim(uuid,text,text,timestamptz,uuid,uuid,jsonb)'::regprocedure,'v_thread_id'),
378
+ ('agent_board.add_artifact(uuid,text,text,text,uuid,uuid,uuid,uuid,jsonb)'::regprocedure,'v_anchor_thread'),
379
+ ('agent_board.raise_conflict(uuid,uuid,text,text,text,uuid,jsonb)'::regprocedure,'p_thread_id'),
380
+ ('agent_board.raise_escalation(uuid,uuid,text,text,text,uuid,uuid,jsonb)'::regprocedure,'p_thread_id');
381
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
382
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
383
+ from xb_direct_insert_reviewed r join xb_locked_writer_arguments a on a.fn=r.fn join xb_routines f on f.oid=r.fn
384
+ join pg_proc p on p.oid=r.fn join pg_namespace pn on pn.oid=p.pronamespace
385
+ join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
386
+ where r.mechanism='locked_post' and f.body !~ agent_board_test.re(agent_board_test.qname_frag('threads',true)
387
+ || agent_board_test.same_stmt() || agent_board_test.col_ref_frag('id') || '\s*=\s*' || agent_board_test.ident_re(a.thread_arg)
388
+ || agent_board_test.same_stmt() || '\m' || agent_board_test.ci_re('for no key update') || '[^;]*;[\s\S]*?'
389
+ || agent_board_test.call_frag('_current_agent_can_post_to_thread',true) || '\s*' || agent_board_test.ident_re(a.thread_arg)
390
+ || '\s*\)[\s\S]*?' || agent_board_test.stmt_frag('insert into',c.relname,true))),null::text,
391
+ 'XB locked direct writers recheck the locked thread posting gate before insertion');
392
+ -- Materialize source-derived writer pairs once before classifying their
393
+ -- operations. The same catalog population drives both UPDATE classification
394
+ -- and the recursive caller graph below.
395
+ create temp table xb_writer_patterns on commit drop as
396
+ select h.relid, agent_board_test.write_re(c.relname) as pattern
397
+ from xb_history_relations h join pg_class c on c.oid=h.relid;
398
+ create temp table xb_history_writer_pairs on commit drop as
399
+ select distinct f.oid,r.relid from xb_routines f cross join xb_writer_patterns r where f.body ~ r.pattern;
400
+ -- An UPDATE-only path can operate on a pre-existing history row without a
401
+ -- first-association lock only while its catalog-derived parent/thread FK
402
+ -- columns cannot be reassigned and the routine cannot insert or delete that
403
+ -- relation. Enumerate the population independently from the dispositions.
404
+ create temp table xb_update_only_actual on commit drop as
405
+ select distinct f.oid,h.relid from xb_history_writer_pairs h join xb_routines f on f.oid=h.oid
406
+ join pg_class c on c.oid=h.relid
407
+ where f.body ~ agent_board_test.stmt_re('update',c.relname)
408
+ and f.body !~ agent_board_test.stmt_re('insert into',c.relname);
409
+ create temp table xb_update_only_reviewed(fn oid,relid oid,allowed_columns text[] not null,primary key(fn,relid)) on commit drop;
410
+ insert into xb_update_only_reviewed values
411
+ ('agent_board._append_task_event(text,uuid,uuid,text,jsonb,text,uuid,timestamptz)'::regprocedure,'agent_board.tasks'::regclass,array['assigned_agent_id','completed_at','status','updated_at']::text[]),
412
+ ('agent_board.claim_callback_outbox_events(integer)'::regprocedure,'agent_board.callback_outbox_events'::regclass,array['locked_at','outbox_status','updated_at']::text[]),
413
+ ('agent_board.disable_callback_delivery_endpoint(uuid,uuid,text,text,jsonb)'::regprocedure,'agent_board.callback_outbox_events'::regclass,array['last_error_at','locked_at','metadata','outbox_status','updated_at']::text[]),
414
+ ('agent_board.record_callback_delivery_retry(uuid,uuid,integer,text,timestamptz,jsonb)'::regprocedure,'agent_board.callback_outbox_events'::regclass,array['available_at','last_error_at','locked_at','metadata','outbox_status','updated_at']::text[]),
415
+ ('agent_board.record_callback_delivery_success(uuid,uuid,integer,jsonb)'::regprocedure,'agent_board.callback_outbox_events'::regclass,array['delivered_at','last_error_at','locked_at','metadata','outbox_status','updated_at']::text[]),
416
+ ('agent_board.record_callback_delivery_terminal_failure(uuid,uuid,integer,text,jsonb)'::regprocedure,'agent_board.callback_outbox_events'::regclass,array['last_error_at','locked_at','metadata','outbox_status','updated_at']::text[]),
417
+ ('agent_board.update_inbox_item_state(uuid,uuid,text,jsonb)'::regprocedure,'agent_board.inbox_items'::regclass,array['action_state','metadata','updated_at']::text[]),
418
+ ('agent_board.reclaim_fleet_machine(uuid,uuid,uuid,text,uuid)'::regprocedure,'agent_board.claims'::regclass,array['metadata','released_at','status','updated_at']::text[]),
419
+ ('agent_board.release_claim(uuid)'::regprocedure,'agent_board.claims'::regclass,array['released_at','resource_key','status','updated_at']::text[]),
420
+ ('agent_board.renew_claim(uuid,timestamptz)'::regprocedure,'agent_board.claims'::regclass,array['lease_expires_at','resource_key','updated_at']::text[]),
421
+ ('agent_board.resolve_conflict(uuid,uuid,jsonb)'::regprocedure,'agent_board.conflicts'::regclass,array['details','resolved_at','resolved_by_agent_id','status','updated_at']::text[]),
422
+ ('agent_board.resolve_escalation(uuid,uuid,jsonb)'::regprocedure,'agent_board.escalations'::regclass,array['details','resolved_at','resolved_by_agent_id','status','updated_at']::text[]),
423
+ ('agent_board.set_board_claim_policy(uuid,text)'::regprocedure,'agent_board.claims'::regclass,array['lease_expires_at','updated_at']::text[]);
424
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
425
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
426
+ from xb_update_only_actual a join pg_proc p on p.oid=a.oid join pg_namespace pn on pn.oid=p.pronamespace
427
+ join pg_class c on c.oid=a.relid join pg_namespace rn on rn.oid=c.relnamespace
428
+ where not exists(select 1 from xb_update_only_reviewed r where r.fn=a.oid and r.relid=a.relid)), null::text,
429
+ 'XB unreviewed existing-row history writers name their full identities');
430
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
431
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
432
+ from xb_update_only_reviewed r join pg_proc p on p.oid=r.fn join pg_namespace pn on pn.oid=p.pronamespace
433
+ join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
434
+ where not exists(select 1 from xb_update_only_actual a where a.oid=r.fn and a.relid=r.relid)), null::text,
435
+ 'XB every reviewed existing-row path actually updates its claimed relation');
436
+ select is((select string_agg(distinct format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
437
+ order by format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname))
438
+ from xb_update_only_reviewed r join xb_routines f on f.oid=r.fn join pg_proc p on p.oid=r.fn
439
+ join pg_namespace pn on pn.oid=p.pronamespace join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
440
+ where f.body ~ agent_board_test.stmt_re('delete from',c.relname)
441
+ or exists(select 1 from pg_constraint fk join lateral unnest(fk.conkey) k(attnum) on true
442
+ join pg_attribute a on a.attrelid=fk.conrelid and a.attnum=k.attnum
443
+ where fk.contype='f' and fk.conrelid=r.relid and (fk.confrelid='agent_board.threads'::regclass
444
+ or fk.confrelid in (select relid from xb_history_relations))
445
+ and f.body ~ agent_board_test.col_write_re(c.relname,a.attname))), null::text,
446
+ 'XB existing-row paths neither delete history nor reassign a catalog-derived thread or parent anchor');
447
+ -- Pin the actual assigned columns for each independently discovered update
448
+ -- path, not just the absence of parent-column assignments. A new state effect
449
+ -- therefore requires its own disposition.
450
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
451
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
452
+ from xb_update_only_reviewed r join xb_routines f on f.oid=r.fn join pg_proc p on p.oid=r.fn
453
+ join pg_namespace pn on pn.oid=p.pronamespace join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
454
+ where r.allowed_columns is distinct from coalesce((select array_agg(a.attname::text order by a.attname::text)
455
+ from pg_attribute a where a.attrelid=r.relid and a.attnum>0 and not a.attisdropped
456
+ and f.body ~ agent_board_test.col_write_re(c.relname,a.attname)),array[]::text[])),null::text,
457
+ 'XB each existing-row writer assigns exactly its reviewed catalog columns');
458
+ -- ON CONFLICT DO UPDATE can mutate an existing row even though the routine
459
+ -- is also an inserter. Enumerate those branches separately and pin the SET
460
+ -- columns; behavioral retry controls prove the two delivery upserts preserve
461
+ -- their original source anchors, while heartbeat remains a locked writer.
462
+ create temp table xb_upsert_actual on commit drop as
463
+ select w.oid,w.relid,m[1] as update_fragment from xb_history_writer_pairs w
464
+ join xb_routines f on f.oid=w.oid join pg_class c on c.oid=w.relid
465
+ cross join lateral regexp_matches(f.body,agent_board_test.re(agent_board_test.ci_re('on conflict')
466
+ || agent_board_test.same_stmt() || agent_board_test.ci_re('do update') || '\s+'
467
+ || agent_board_test.ci_re('set') || '([^;]*)'),'g') m
468
+ where f.body ~ agent_board_test.stmt_re('insert into',c.relname);
469
+ create temp table xb_upsert_reviewed(fn oid,relid oid,allowed_columns text[] not null,primary key(fn,relid)) on commit drop;
470
+ insert into xb_upsert_reviewed values
471
+ ('agent_board.heartbeat(uuid,uuid,text,text,interval,jsonb)'::regprocedure,'agent_board.presence'::regclass,
472
+ array['activity_summary','availability','expires_at','last_heartbeat_at','metadata','thread_id','updated_at']),
473
+ ('agent_board.mark_read(uuid,uuid,bigint)'::regprocedure,'agent_board.read_receipts'::regclass,
474
+ array['last_read_at','last_read_message_sequence']),
475
+ ('agent_board._upsert_delivery_recipient(text,uuid,text,uuid,uuid,uuid,uuid,uuid,jsonb)'::regprocedure,'agent_board.delivery_recipients'::regclass,
476
+ array['metadata']),
477
+ ('agent_board._upsert_inbox_item(uuid,uuid,uuid,text,text,uuid,uuid,uuid,text,text,text,boolean,boolean,jsonb,jsonb)'::regprocedure,'agent_board.inbox_items'::regclass,
478
+ array['board_id','fetch_descriptor','metadata','preview','priority','subject','updated_at','visible_in_board_feed','visible_in_inbox']);
479
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
480
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
481
+ from xb_upsert_actual a join pg_proc p on p.oid=a.oid join pg_namespace pn on pn.oid=p.pronamespace
482
+ join pg_class c on c.oid=a.relid join pg_namespace rn on rn.oid=c.relnamespace
483
+ where not exists(select 1 from xb_upsert_reviewed r where r.fn=a.oid and r.relid=a.relid)),null::text,
484
+ 'XB unreviewed history upsert branches name their full identities');
485
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
486
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
487
+ from xb_upsert_reviewed r join pg_proc p on p.oid=r.fn join pg_namespace pn on pn.oid=p.pronamespace
488
+ join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
489
+ where (select count(*) from xb_upsert_actual a where a.oid=r.fn and a.relid=r.relid)<>1),null::text,
490
+ 'XB each reviewed history upsert has exactly one parsed update branch');
491
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
492
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
493
+ from xb_upsert_reviewed r join xb_upsert_actual u on u.oid=r.fn and u.relid=r.relid
494
+ join pg_proc p on p.oid=r.fn join pg_namespace pn on pn.oid=p.pronamespace
495
+ join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
496
+ where r.allowed_columns is distinct from coalesce((select array_agg(a.attname::text order by a.attname::text)
497
+ from pg_attribute a where a.attrelid=r.relid and a.attnum>0 and not a.attisdropped
498
+ and (regexp_match(agent_board_test.stripped_body(r.fn),agent_board_test.re(agent_board_test.ci_re('on conflict')
499
+ || agent_board_test.same_stmt() || agent_board_test.ci_re('do update') || '\s+'
500
+ || agent_board_test.ci_re('set') || '([^;]*)')))[1]
501
+ ~ agent_board_test.re(agent_board_test.col_frag(a.attname) || '\s*=')),array[]::text[])),null::text,
502
+ 'XB each history upsert assigns exactly its reviewed catalog columns');
503
+ -- A routine can contain a separate UPDATE as well as an INSERT into the
504
+ -- same history relation. Such a mixed writer is neither an update-only
505
+ -- exemption nor necessarily an ON CONFLICT branch, so enumerate it explicitly.
506
+ create temp table xb_mixed_writers on commit drop as
507
+ select w.oid,w.relid from xb_history_writer_pairs w join xb_routines f on f.oid=w.oid join pg_class c on c.oid=w.relid
508
+ where f.body ~ agent_board_test.stmt_re('insert into',c.relname) and f.body ~ agent_board_test.stmt_re('update',c.relname);
509
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
510
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
511
+ from xb_mixed_writers w join pg_proc p on p.oid=w.oid join pg_namespace pn on pn.oid=p.pronamespace
512
+ join pg_class c on c.oid=w.relid join pg_namespace rn on rn.oid=c.relnamespace
513
+ where not (w.oid='agent_board.acquire_claim(uuid,text,text,timestamptz,uuid,uuid,jsonb)'::regprocedure
514
+ and w.relid='agent_board.claims'::regclass)),null::text,
515
+ 'XB unreviewed mixed insert and update writers name their full identities');
516
+ select ok(exists(select 1 from xb_mixed_writers where oid='agent_board.acquire_claim(uuid,text,text,timestamptz,uuid,uuid,jsonb)'::regprocedure
517
+ and relid='agent_board.claims'::regclass), 'XB reviewed acquire-claim mixed writer actually exists');
518
+ select is((select array_agg(a.attname::text order by a.attname::text) from pg_attribute a
519
+ where a.attrelid='agent_board.claims'::regclass and a.attnum>0 and not a.attisdropped
520
+ and agent_board_test.stripped_body('agent_board.acquire_claim(uuid,text,text,timestamptz,uuid,uuid,jsonb)'::regprocedure)
521
+ ~ agent_board_test.re(agent_board_test.col_assign_frag('claims',a.attname,true))),
522
+ array['status','updated_at']::text[], 'XB acquire-claim existing-row update assigns only status and timestamp');
523
+ -- Classify INSERTs into relations one enforced edge beyond a direct
524
+ -- thread-FK parent. The anchor-column assertion is a structural premise;
525
+ -- behavior of the common parent predicates is witnessed separately.
526
+ create temp table xb_onehop_insert_actual on commit drop as
527
+ select distinct w.oid,w.relid from xb_history_writer_pairs w join xb_routines f on f.oid=w.oid
528
+ join pg_class c on c.oid=w.relid
529
+ where w.relid in (select child from xb_onehop_actual)
530
+ and w.relid not in (select conrelid from pg_constraint where contype='f' and confrelid='agent_board.threads'::regclass)
531
+ and f.body ~ agent_board_test.stmt_re('insert into',c.relname);
532
+ create temp table xb_onehop_insert_reviewed(fn oid,relid oid,anchors text[],primary key(fn,relid)) on commit drop;
533
+ insert into xb_onehop_insert_reviewed values
534
+ ('agent_board._queue_board_post_callbacks(uuid,uuid,text,text,text,jsonb)'::regprocedure,'agent_board.callback_outbox_events'::regclass,array['source_message_id']),
535
+ ('agent_board._queue_inbox_callbacks(uuid,text)'::regprocedure,'agent_board.callback_outbox_events'::regclass,array['source_message_id','source_task_id']),
536
+ ('agent_board._record_send_outcome(text,uuid,text,uuid)'::regprocedure,'agent_board.send_outcomes'::regclass,array['message_id']),
537
+ ('agent_board._record_signed_write_evidence(text,uuid,text,jsonb,jsonb)'::regprocedure,'agent_board.signature_evidence'::regclass,array['source_message_id','source_task_event_id']),
538
+ ('agent_board._upsert_delivery_recipient(text,uuid,text,uuid,uuid,uuid,uuid,uuid,jsonb)'::regprocedure,'agent_board.delivery_recipients'::regclass,array['source_message_id','source_task_id']),
539
+ ('agent_board._upsert_inbox_item(uuid,uuid,uuid,text,text,uuid,uuid,uuid,text,text,text,boolean,boolean,jsonb,jsonb)'::regprocedure,'agent_board.inbox_items'::regclass,array['source_message_id','source_task_id']),
540
+ ('agent_board.delegate_task(uuid,uuid,text,text,uuid,text,text,jsonb)'::regprocedure,'agent_board.delegations'::regclass,array['child_task_id','parent_task_id']),
541
+ ('agent_board.evaluate_signed_write_fleet_policy(text,jsonb,jsonb)'::regprocedure,'agent_board.signature_evidence'::regclass,array[]::text[]);
542
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
543
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
544
+ from xb_onehop_insert_actual a join pg_proc p on p.oid=a.oid join pg_namespace pn on pn.oid=p.pronamespace
545
+ join pg_class c on c.oid=a.relid join pg_namespace rn on rn.oid=c.relnamespace
546
+ where not exists(select 1 from xb_onehop_insert_reviewed r where r.fn=a.oid and r.relid=a.relid)),null::text,
547
+ 'XB unreviewed one-hop inserters name their full identities');
548
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
549
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
550
+ from xb_onehop_insert_reviewed r join pg_proc p on p.oid=r.fn join pg_namespace pn on pn.oid=p.pronamespace
551
+ join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
552
+ where not exists(select 1 from xb_onehop_insert_actual a where a.oid=r.fn and a.relid=r.relid)),null::text,
553
+ 'XB each reviewed one-hop inserter actually writes its claimed relation');
554
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
555
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
556
+ from xb_onehop_insert_reviewed r join xb_routines f on f.oid=r.fn join pg_proc p on p.oid=r.fn
557
+ join pg_namespace pn on pn.oid=p.pronamespace join pg_class c on c.oid=r.relid join pg_namespace rn on rn.oid=c.relnamespace
558
+ where r.anchors is distinct from coalesce((select array_agg(distinct a.attname::text order by a.attname::text)
559
+ from pg_constraint fk join lateral unnest(fk.conkey) k(attnum) on true
560
+ join pg_attribute a on a.attrelid=fk.conrelid and a.attnum=k.attnum
561
+ where fk.contype='f' and fk.convalidated and fk.conrelid=r.relid
562
+ and fk.confrelid in (select distinct conrelid from pg_constraint where contype='f' and confrelid='agent_board.threads'::regclass)
563
+ and f.body ~ agent_board_test.col_write_re(c.relname,a.attname)),array[]::text[])),null::text,
564
+ 'XB each one-hop inserter writes exactly its reviewed parent-anchor columns');
565
+ -- A writer detected by the shared catalog matcher but not classified as an
566
+ -- INSERT or UPDATE is not silently omitted (for example DELETE, MERGE, COPY,
567
+ -- TRUNCATE or a future syntax matched by write_re).
568
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
569
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
570
+ from xb_history_writer_pairs w join xb_routines f on f.oid=w.oid join pg_proc p on p.oid=w.oid
571
+ join pg_namespace pn on pn.oid=p.pronamespace join pg_class c on c.oid=w.relid join pg_namespace rn on rn.oid=c.relnamespace
572
+ where f.body !~ agent_board_test.stmt_re('insert into',c.relname)
573
+ and f.body !~ agent_board_test.stmt_re('update',c.relname)),null::text,
574
+ 'XB unsupported history write forms name their full identities');
575
+ -- A routine may combine a reviewed INSERT or UPDATE with a separate DELETE;
576
+ -- check deletion independently so the supported statement cannot mask it.
577
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
578
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
579
+ from xb_history_writer_pairs w join xb_routines f on f.oid=w.oid join pg_proc p on p.oid=w.oid
580
+ join pg_namespace pn on pn.oid=p.pronamespace join pg_class c on c.oid=w.relid join pg_namespace rn on rn.oid=c.relnamespace
581
+ where f.body ~ agent_board_test.stmt_re('delete from',c.relname)),null::text,
582
+ 'XB history writers with a deletion statement name their full identities');
583
+ -- Supported INSERT/UPDATE text cannot hide a second unsupported operation on
584
+ -- the same relation. Match the other write forms independently by target.
585
+ create temp table xb_other_write_patterns on commit drop as
586
+ select h.relid,agent_board_test.re('(?:' || agent_board_test.stmt_frag('merge into',c.relname,true)
587
+ || '|' || agent_board_test.lead_guard() || agent_board_test.ci_re('truncate')
588
+ || '(?:\s+' || agent_board_test.ci_re('table') || ')?\s+(?:' || agent_board_test.ci_re('only') || '\s+)?'
589
+ || '(?:[A-Za-z_"][A-Za-z0-9_."]*\s*\*?\s*,\s*(?:' || agent_board_test.ci_re('only') || '\s+)?)*'
590
+ || agent_board_test.qname_frag(c.relname,true)
591
+ || '|' || agent_board_test.stmt_frag('copy',c.relname,true)
592
+ || '\s*(?:\([^)]*\))?\s*' || agent_board_test.ci_re('from') || agent_board_test.trail_guard()
593
+ || '|\m' || agent_board_test.ci_re('select') || '\M' || agent_board_test.same_stmt()
594
+ || '\m' || agent_board_test.ci_re('into') || '\s+(?:(?:' || agent_board_test.ci_re('temporary')
595
+ || '|' || agent_board_test.ci_re('temp') || '|' || agent_board_test.ci_re('unlogged') || ')\s+)?'
596
+ || '(?:' || agent_board_test.ci_re('table') || '\s+)?' || agent_board_test.qname_frag(c.relname,true) || ')') as pattern
597
+ from xb_history_relations h join pg_class c on c.oid=h.relid;
598
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I',pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname), ', '
599
+ order by pn.nspname,p.proname,oidvectortypes(p.proargtypes),rn.nspname,c.relname)
600
+ from xb_history_writer_pairs w join xb_routines f on f.oid=w.oid join pg_proc p on p.oid=w.oid
601
+ join pg_namespace pn on pn.oid=p.pronamespace join pg_class c on c.oid=w.relid join pg_namespace rn on rn.oid=c.relnamespace
602
+ join xb_other_write_patterns pat on pat.relid=w.relid where f.body ~ pat.pattern),null::text,
603
+ 'XB unsupported additional history write forms name their full identities');
604
+ create temp table xb_source_writers on commit drop as
605
+ select distinct f.oid,f.nspname,f.proname from xb_routines f join xb_history_writer_pairs w on w.oid=f.oid;
606
+ -- SQL string bodies have no reliable pg_depend call edges. Conservatively
607
+ -- match each routine name and include every overload when arguments are opaque.
608
+ -- This catches transitive SQL-to-SQL paths; it does not claim semantic resolution.
609
+ create temp table xb_sql_call_patterns on commit drop as
610
+ select oid, agent_board_test.call_re(nspname||'.'||proname,true) as pattern from xb_routines;
611
+ create temp table xb_sql_call_edges on commit drop as
612
+ select s.oid as source,t.oid as target from xb_routines s cross join xb_sql_call_patterns t
613
+ where s.lanname='sql' and s.oid<>t.oid and s.body ~ t.pattern;
614
+ create temp table xb_all_call_edges on commit drop as
615
+ select source,target from xb_sql_call_edges
616
+ union select source,dep_oid as target from xb_edges where dep_oid is not null;
617
+ create temp table xb_reaches_writer on commit drop as
618
+ with recursive reaches_writer(oid) as (
619
+ select oid from xb_source_writers
620
+ union select e.source from xb_all_call_edges e join reaches_writer r on r.oid=e.target
621
+ ) select oid from reaches_writer;
622
+ select is((select string_agg(s.oid::regprocedure::text, ', ' order by s.oid::regprocedure::text)
623
+ from xb_routines s join xb_reaches_writer r on r.oid=s.oid where s.lanname='sql'),
624
+ null::text, 'XB SQL routes through SQL or PL/pgSQL to history writers name themselves');
625
+ -- Derive every direct sink caller independently of the reviewed disposition.
626
+ -- The stripped call has simple positional arguments through position eight;
627
+ -- if a new spelling cannot be parsed it is a named failure rather than a
628
+ -- guessed NULL-only route. String literals are intentionally stripped.
629
+ create temp table xb_sink_calls on commit drop as
630
+ select f.oid, regexp_split_to_array(m[1],',') as arguments,
631
+ regexp_replace((regexp_split_to_array(m[1],','))[7], '\s+', '', 'g') as board_arg
632
+ from xb_routines f cross join lateral regexp_matches(f.body,
633
+ agent_board_test.call_re('_upsert_delivery_recipient',true) || '((?:[^,;]*,){7}[^,;]*)','g') m;
634
+ select is((select string_agg(f.oid::regprocedure::text, ', ' order by f.oid::regprocedure::text)
635
+ from xb_routines f where f.body ~ agent_board_test.call_re('_upsert_delivery_recipient',true)
636
+ and (select count(*) from regexp_matches(f.body, agent_board_test.call_re('_upsert_delivery_recipient',true),'g'))
637
+ <> (select count(*) from xb_sink_calls c where c.oid=f.oid)), null::text,
638
+ 'XB unparseable delivery-sink calls name their full routine identity');
639
+ -- Any complex positional expression can conceal nested commas and change
640
+ -- argument numbering; refuse that call by identity until reviewed.
641
+ select is((select string_agg(distinct c.oid::regprocedure::text, ', ' order by c.oid::regprocedure::text)
642
+ from xb_sink_calls c cross join lateral unnest(c.arguments) a(arg)
643
+ where translate(a.arg,E' \t\r\n','') !~ '^([a-z_][a-z0-9_.]*)?$'), null::text,
644
+ 'XB complex delivery-sink arguments require disposition by caller identity');
645
+ select is((select string_agg(format('%I.%I(%s)[%s]',n.nspname,p.proname,replace(oidvectortypes(p.proargtypes), ', ', ','),c.board_arg), ', '
646
+ order by n.nspname::text collate "C",p.proname::text collate "C",oidvectortypes(p.proargtypes),c.board_arg)
647
+ from xb_sink_calls c join pg_proc p on p.oid=c.oid join pg_namespace n on n.oid=p.pronamespace),
648
+ 'agent_board._deliver_board_message(uuid,text,text,text)[p_board_id], agent_board.create_tasking_delivery(uuid,uuid,text,text,uuid[],uuid,text,text,text,jsonb)[null], agent_board.create_tasking_delivery(uuid,uuid,text,text,uuid[],uuid,text,text,text,jsonb)[p_board_id], agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)[null], agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb)[null]',
649
+ 'XB every delivery-sink call has an explicitly classified board argument');
650
+ select is((select string_agg(oid::regprocedure::text, ', ' order by oid::regprocedure::text)
651
+ from (select distinct c.oid from xb_sink_calls c where c.board_arg<>'null') c
652
+ where not exists (select 1 from xb_routines f where f.oid=c.oid
653
+ and f.body ~ agent_board_test.call_re('_associate_empty_thread',true))), null::text,
654
+ 'XB each non-NULL board sink caller invokes the early association boundary');
655
+ select ok((select count(*)=2 and bool_and(board_arg='null') from xb_sink_calls c
656
+ join pg_proc p on p.oid=c.oid where p.proname in ('send_direct_message','send_multicast_message')),
657
+ 'XB direct and multicast sink paths positively bind a NULL board');
658
+ -- Catalog/OID call closure includes descriptor and signed wrappers, not only
659
+ -- the routines that directly name the sink. Unknown intermediaries are named.
660
+ create temp table xb_sink_caller_closure on commit drop as
661
+ with recursive t(oid) as (select distinct oid from xb_sink_calls
662
+ union select e.source from xb_all_call_edges e join t on t.oid=e.target)
663
+ select oid from t;
664
+ create temp table xb_sink_wrapper_reviewed(fn oid not null,target oid not null,primary key(fn,target)) on commit drop;
665
+ insert into xb_sink_wrapper_reviewed values
666
+ ('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure,'agent_board._deliver_board_message(uuid,text,text,text)'::regprocedure),
667
+ ('agent_board.post_message(uuid,uuid,text,text,uuid,jsonb)'::regprocedure,'agent_board._deliver_board_message(uuid,text,text,text)'::regprocedure),
668
+ ('agent_board.post_board_message(uuid,uuid,text,text,text,text,text,text,text,jsonb)'::regprocedure,'agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure),
669
+ ('agent_board.create_tasking_delivery(uuid,uuid,text,text,jsonb,text,text,text,text,text,jsonb)'::regprocedure,'agent_board.create_tasking_delivery(uuid,uuid,text,text,uuid[],uuid,text,text,text,jsonb)'::regprocedure),
670
+ ('agent_board.send_direct_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb)'::regprocedure,'agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure),
671
+ ('agent_board.send_multicast_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb)'::regprocedure,'agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb)'::regprocedure),
672
+ ('agent_board.signed_post_board_message(jsonb,jsonb)'::regprocedure,'agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure),
673
+ ('agent_board.signed_create_tasking_delivery(jsonb,jsonb)'::regprocedure,'agent_board.create_tasking_delivery(uuid,uuid,text,text,uuid[],uuid,text,text,text,jsonb)'::regprocedure),
674
+ ('agent_board.signed_send_direct_message(jsonb,jsonb)'::regprocedure,'agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure),
675
+ ('agent_board.signed_send_multicast_message(jsonb,jsonb)'::regprocedure,'agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb)'::regprocedure),
676
+ ('agent_board.signed_create_tasking_delivery(jsonb,jsonb)'::regprocedure,'agent_board.create_tasking_delivery(uuid,uuid,text,text,jsonb,text,text,text,text,text,jsonb)'::regprocedure),
677
+ ('agent_board.signed_send_multicast_message(jsonb,jsonb)'::regprocedure,'agent_board.send_multicast_message(uuid,uuid,jsonb,text,text,text,text,text,text,jsonb)'::regprocedure);
678
+ select is((select string_agg(format('%I.%I(%s)',n.nspname,p.proname,oidvectortypes(p.proargtypes)), ', '
679
+ order by n.nspname,p.proname,oidvectortypes(p.proargtypes))
680
+ from xb_sink_caller_closure t join pg_proc p on p.oid=t.oid join pg_namespace n on n.oid=p.pronamespace
681
+ where not exists(select 1 from xb_sink_calls c where c.oid=t.oid)
682
+ and not exists(select 1 from xb_sink_wrapper_reviewed r where r.fn=t.oid)), null::text,
683
+ 'XB unreviewed transitive delivery callers name their full identities');
684
+ select is((select string_agg(format('%I.%I(%s)',n.nspname,p.proname,oidvectortypes(p.proargtypes)), ', '
685
+ order by n.nspname,p.proname,oidvectortypes(p.proargtypes))
686
+ from xb_sink_wrapper_reviewed r join pg_proc p on p.oid=r.fn join pg_namespace n on n.oid=p.pronamespace
687
+ where not exists(select 1 from xb_all_call_edges e where e.source=r.fn and e.target=r.target)
688
+ or not exists(select 1 from xb_sink_caller_closure t where t.oid=r.fn)), null::text,
689
+ 'XB each reviewed delivery wrapper reaches its exact sink-caller overload');
690
+ select is((select string_agg(format('%I.%I(%s) -> %I.%I(%s)',sn.nspname,sp.proname,oidvectortypes(sp.proargtypes),
691
+ tn.nspname,tp.proname,oidvectortypes(tp.proargtypes)), ', ' order by sn.nspname,sp.proname,oidvectortypes(sp.proargtypes),tn.nspname,tp.proname)
692
+ from xb_all_call_edges e
693
+ join pg_proc sp on sp.oid=e.source join pg_namespace sn on sn.oid=sp.pronamespace
694
+ join pg_proc tp on tp.oid=e.target join pg_namespace tn on tn.oid=tp.pronamespace
695
+ where e.source in (select fn from xb_sink_wrapper_reviewed) and e.target in (select oid from xb_sink_caller_closure)
696
+ and not exists(select 1 from xb_sink_wrapper_reviewed r where r.fn=e.source and r.target=e.target)), null::text,
697
+ 'XB delivery wrappers have no other undispositioned delivery-call edge');
698
+ -- Delegation does not call the sink: it creates its child against the parent
699
+ -- task's stored thread. The writer test proves the resulting parent/child state.
700
+ select ok((select f.body ~ agent_board_test.re(agent_board_test.call_frag('create_task',true)
701
+ || '\s*' || agent_board_test.ident_re('v_parent') || '\s*\.\s*' || agent_board_test.ident_re('thread_id') || '\s*,')
702
+ and exists(select 1 from xb_edges e where e.source=f.oid and e.dep_oid='agent_board.create_task(uuid,uuid,text,text,uuid,uuid,text,text,jsonb)'::regprocedure)
703
+ and not exists(select 1 from xb_sink_caller_closure t where t.oid=f.oid)
704
+ from xb_routines f where f.oid='agent_board.delegate_task(uuid,uuid,text,text,uuid,text,text,jsonb)'::regprocedure),
705
+ 'XB delegation binds create_task to its parent thread and is not a sink caller');
706
+ -- Trigger callers can live on a relation outside the history FK graph. Parse each
707
+ -- non-extension trigger with its actual row type; unresolved or dynamic bodies,
708
+ -- any history relation dependency, and calls reaching a writer are reported.
709
+ create function pg_temp.xb_trigger_dependencies(p_fn oid,p_rel oid)
710
+ returns table(dep_oid oid,dep_type text,problem text) language plpgsql as $body$
711
+ begin
712
+ return query select d.oid::oid,d.type::text,null::text
713
+ from extensions.plpgsql_show_dependency_tb(p_fn::regprocedure,p_rel::regclass) d;
714
+ exception when others then return query select null::oid,null::text,sqlstate || ':' || sqlerrm;
715
+ end $body$;
716
+ create temp table xb_application_triggers on commit drop as
717
+ select t.oid,t.tgrelid,t.tgname,t.tgfoid,
718
+ agent_board_test.stripped_body(t.tgfoid) ~ agent_board_test.re('\m' || agent_board_test.ci_re('execute') || '\M') as opaque
719
+ from pg_trigger t join pg_class c on c.oid=t.tgrelid join pg_namespace n on n.oid=c.relnamespace
720
+ where not t.tgisinternal and n.nspname not in ('pg_catalog','information_schema','agent_board_test')
721
+ and n.nspname not like 'pg_%'
722
+ and not exists (select 1 from pg_depend d where d.classid='pg_trigger'::regclass and d.objid=t.oid and d.deptype='e');
723
+ create temp table xb_trigger_deps on commit drop as
724
+ select t.oid,t.tgrelid,t.tgname,t.tgfoid,t.opaque,d.dep_oid,d.dep_type,d.problem
725
+ from xb_application_triggers t left join lateral pg_temp.xb_trigger_dependencies(t.tgfoid,t.tgrelid) d on true;
726
+ select ok((select count(*)>0 from xb_application_triggers), 'XB application trigger parser population is nonempty');
727
+ select is((select string_agg(distinct format('%s.%I -> %s [%s]',t.tgrelid::regclass,t.tgname,t.tgfoid::regprocedure,
728
+ case when t.problem is not null then 'PARSER-ERROR:'||t.problem when t.opaque then 'DYNAMIC'
729
+ when t.dep_type='RELATION' then 'HISTORY-RELATION:'||t.dep_oid::regclass::text
730
+ else 'WRITER-CALL:'||t.dep_oid::regprocedure::text end), ', '
731
+ order by format('%s.%I -> %s [%s]',t.tgrelid::regclass,t.tgname,t.tgfoid::regprocedure,
732
+ case when t.problem is not null then 'PARSER-ERROR:'||t.problem when t.opaque then 'DYNAMIC'
733
+ when t.dep_type='RELATION' then 'HISTORY-RELATION:'||t.dep_oid::regclass::text
734
+ else 'WRITER-CALL:'||t.dep_oid::regprocedure::text end))
735
+ from xb_trigger_deps t where t.problem is not null or t.opaque
736
+ or (t.dep_type='RELATION' and t.dep_oid in (select relid from xb_history_relations))
737
+ or (t.dep_type='FUNCTION' and t.dep_oid in (select oid from xb_reaches_writer))),
738
+ null::text, 'XB trigger paths to history or writers and unresolved triggers name themselves');
739
+
740
+ -- DML rules can originate on a relation outside the FK class. Their stored
741
+ -- actions may write another relation or call a writer; fail closed on every
742
+ -- non-extension application DML rule until its effect is positively reviewed.
743
+ select is((select string_agg(format('%s.%I',rw.ev_class::regclass,rw.rulename), ', '
744
+ order by rw.ev_class::regclass::text,rw.rulename)
745
+ from pg_rewrite rw join pg_class c on c.oid=rw.ev_class join pg_namespace n on n.oid=c.relnamespace
746
+ where rw.ev_type<>'1' and n.nspname not in ('pg_catalog','information_schema','agent_board_test')
747
+ and n.nspname not like 'pg_%'
748
+ and not exists (select 1 from pg_depend d where d.classid='pg_class'::regclass and d.objid=c.oid and d.deptype='e')),
749
+ null::text, 'XB application DML rules name themselves for disposition');
750
+
751
+ -- Canary relations and overloads are deliberately created in another schema.
752
+ -- These assertions pin discovery primitives, not a proof that every writer has a
753
+ -- safe lock or that arbitrary dynamic SQL can be resolved.
754
+ create schema xb_class_probe;
755
+ create table xb_class_probe.onehop (id uuid primary key, task_id uuid references agent_board.tasks(id));
756
+ create function xb_class_probe.writer(p_id uuid, p_task uuid) returns void language plpgsql as $body$
757
+ begin insert into xb_class_probe.onehop(id,task_id) values (p_id,p_task); end $body$;
758
+ create function xb_class_probe.writer(p_id uuid, p_task uuid, p_extra text) returns void language plpgsql as $body$
759
+ begin insert into xb_class_probe.onehop(id,task_id) values (p_id,p_task); end $body$;
760
+ create function xb_class_probe.relay(p_id uuid, p_task uuid) returns void language plpgsql as $body$
761
+ begin perform xb_class_probe.writer(p_id,p_task); end $body$;
762
+ create function xb_class_probe.opaque(p_id uuid, p_task uuid) returns void language plpgsql as $body$
763
+ begin execute 'select xb_class_probe.writer($1,$2)' using p_id,p_task; end $body$;
764
+ select is(
765
+ (with direct as (select distinct conrelid from pg_constraint where contype='f' and confrelid='agent_board.threads'::regclass)
766
+ select string_agg(format('%s.%I', f.conrelid::regclass, f.conname), ', ' order by f.conrelid::regclass::text, f.conname)
767
+ from pg_constraint f join direct d on d.conrelid=f.confrelid
768
+ where f.contype='f' and f.conrelid='xb_class_probe.onehop'::regclass),
769
+ 'xb_class_probe.onehop.onehop_task_id_fkey', 'XB valid injected one-hop FK is discovered and named');
770
+ select is(
771
+ (select count(*) from pg_proc p join pg_namespace n on n.oid=p.pronamespace
772
+ where n.nspname='xb_class_probe' and p.proname='writer'
773
+ and exists (select 1 from extensions.plpgsql_show_dependency_tb(p.oid::regprocedure,0::oid) d
774
+ where d.type='RELATION' and d.oid='xb_class_probe.onehop'::regclass)),
775
+ 2::bigint, 'XB injected cross-schema writer overloads are independently discovered');
776
+ select ok(
777
+ exists (select 1 from extensions.plpgsql_show_dependency_tb('xb_class_probe.relay(uuid,uuid)'::regprocedure,0::oid) d
778
+ where d.type='FUNCTION' and d.oid='xb_class_probe.writer(uuid,uuid)'::regprocedure),
779
+ 'XB parser resolves the injected cross-schema relay to its exact writer overload');
780
+ select is(
781
+ (select string_agg(p.oid::regprocedure::text, ', ' order by p.oid::regprocedure::text)
782
+ from pg_proc p join pg_namespace n on n.oid=p.pronamespace
783
+ where n.nspname='xb_class_probe'
784
+ and agent_board_test.stripped_body(p.oid) ~ agent_board_test.re('\m' || agent_board_test.ci_re('execute') || '\M')),
785
+ 'xb_class_probe.opaque(uuid,uuid)', 'XB opaque injected SQL path is flagged by full identity');
786
+ select * from finish();
787
+ rollback;