@consiliency/agent-board-schema 1.76.0 → 1.80.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 (123) hide show
  1. package/fixtures/already-applied.json +16 -0
  2. package/migrations/20261120000200_agent_board_thread_key_serialize.sql +118 -0
  3. package/migrations/20261121000100_agent_board_thread_key_guards.sql +196 -0
  4. package/migrations/20261124000100_agent_board_step_up_enforcement.sql +1213 -0
  5. package/migrations/20261126000100_agent_board_membership_removal_requires_active.sql +243 -0
  6. package/package.json +1 -1
  7. package/schema-manifest.json +39 -3
  8. package/src/deployment-drift-main.test.mjs +89 -6
  9. package/src/deployment-drift.test.mjs +20 -1
  10. package/src/tooling.test.mjs +11 -2
  11. package/tests/agent_board_717_message_replay_scope.test.sql +39 -2
  12. package/tests/agent_board_717_tasking_replay_scope.test.sql +21 -2
  13. package/tests/agent_board_membership_removal_state.test.sql +228 -0
  14. package/tests/agent_board_role_guard.test.sql +3 -1
  15. package/tests/agent_board_send_outcome.test.sql +39 -1
  16. package/tests/agent_board_session_identity_approval_revocation.test.sql +5 -0
  17. package/tests/agent_board_step_up.test.sql +991 -0
  18. package/tests/agent_board_step_up_helper.test.sql +132 -28
  19. package/tests/agent_board_thread_key_guards.test.sql +186 -0
  20. package/tests/agent_board_thread_key_serialize.test.sql +182 -0
  21. package/tests/agent_board_write_enforcement_class.test.sql +3 -2
  22. package/tests/catalog/step_up_gated_closure.json +24 -0
  23. package/tests/fixtures/pgtap-unexposed.sql +9 -4
  24. package/tests/isolation/expected/create_or_find_thread_same_key_race.out +641 -0
  25. package/tests/isolation/expected/session_identity_links.out +4 -0
  26. package/tests/isolation/specs/create_or_find_thread_same_key_race.spec +252 -0
  27. package/tests/isolation/specs/session_identity_links.spec +5 -0
  28. package/tests/mutations/669/run.sh +12 -4
  29. package/tests/mutations/669b/M11_ceil_first.sql +10 -0
  30. package/tests/mutations/669b/M11_gate_first.sql +10 -0
  31. package/tests/mutations/669b/M11_grant_first.sql +10 -0
  32. package/tests/mutations/669b/M11_inv_first.sql +10 -0
  33. package/tests/mutations/669b/M11_mem_first.sql +10 -0
  34. package/tests/mutations/669b/M11_readmit_first.sql +10 -0
  35. package/tests/mutations/669b/M13a_gate_member_preset.sql +12 -0
  36. package/tests/mutations/669b/M13b_ungate_admin_preset.sql +12 -0
  37. package/tests/mutations/669b/M16_actor_is_target.sql +14 -0
  38. package/tests/mutations/669b/M19_drop_readmit.sql +9 -0
  39. package/tests/mutations/669b/M1_drop_grant.sql +9 -0
  40. package/tests/mutations/669b/M20_drop_d5_refusal.sql +9 -0
  41. package/tests/mutations/669b/M21_drop_removal.sql +9 -0
  42. package/tests/mutations/669b/M21b_only_removed.sql +9 -0
  43. package/tests/mutations/669b/M21c_gate_members.sql +12 -0
  44. package/tests/mutations/669b/M21d_requested_state.sql +9 -0
  45. package/tests/mutations/669b/M23b_api_wrapper_over_audit_writer.sql +6 -0
  46. package/tests/mutations/669b/M23c_drop_register_type_refusal.sql +9 -0
  47. package/tests/mutations/669b/M24_drop_kind_refusal.sql +9 -0
  48. package/tests/mutations/669b/M24b_drop_auth_bind_refusal.sql +9 -0
  49. package/tests/mutations/669b/M2a_drop_gate.sql +9 -0
  50. package/tests/mutations/669b/M2b_drop_ceiling.sql +9 -0
  51. package/tests/mutations/669b/M2c_drop_invite.sql +9 -0
  52. package/tests/mutations/669b/MAL2_drop_otp.sql +9 -0
  53. package/tests/mutations/669b/MAL3_admit_passkey.sql +9 -0
  54. package/tests/mutations/669b/MD7a_no_revocation.sql +2 -0
  55. package/tests/mutations/669b/MD7b_overwrite_revoked.sql +2 -0
  56. package/tests/mutations/669b/MD7c_revoke_members.sql +2 -0
  57. package/tests/mutations/669b/MD7d_revoke_expired.sql +2 -0
  58. package/tests/mutations/669b/ME1_wrapper_over_gate.sql +7 -0
  59. package/tests/mutations/669b/MEC2_write_after_helper.sql +10 -0
  60. package/tests/mutations/669b/MQX_query_to_xml_definer.sql +6 -0
  61. package/tests/mutations/669b/MQXb_ts_stat_other_schema.sql +4 -0
  62. package/tests/mutations/669b/MTR_trigger_route.sql +9 -0
  63. package/tests/mutations/669b/MTRb_new_assign.sql +11 -0
  64. package/tests/mutations/669b/MTRc_sync_assigns_value.sql +47 -0
  65. package/tests/mutations/669b/MTX1_tombstone_second_gated_call.sql +15 -0
  66. package/tests/mutations/669b/MTX2_tombstone_non_delegate_target.sql +10 -0
  67. package/tests/mutations/669b/MXS_other_schema_names_gated.sql +5 -0
  68. package/tests/mutations/669b/run.sh +83 -0
  69. package/tests/mutations/670/M18a_read_operation_default_dropped.sql +11 -0
  70. package/tests/mutations/670/M18b_read_operation_default_changed.sql +7 -0
  71. package/tests/mutations/670/M3-DM_helper_on_replay_branch.sql +10 -0
  72. package/tests/mutations/670/M3-MC_helper_on_replay_branch.sql +10 -0
  73. package/tests/mutations/s12/M0_identity.sql +56 -0
  74. package/tests/mutations/s12/M1_serialise_actor_key.sql +57 -0
  75. package/tests/mutations/s12/M1b_serialise_key_only.sql +57 -0
  76. package/tests/mutations/s12/M2_unique_index_no_handler.sql +9 -0
  77. package/tests/mutations/s12/M3_no_trim.sql +56 -0
  78. package/tests/mutations/s12/M4_casefold.sql +56 -0
  79. package/tests/mutations/s12/M5_lookup_ignores_actor.sql +56 -0
  80. package/tests/mutations/s12/M6_reuse_reports_false.sql +56 -0
  81. package/tests/mutations/s12/run.sh +112 -0
  82. package/tests/mutations/s12-fix/M0_identity.sql +83 -0
  83. package/tests/mutations/s12-fix/M10_security_invoker.sql +9 -0
  84. package/tests/mutations/s12-fix/M11_search_path_changed.sql +9 -0
  85. package/tests/mutations/s12-fix/M12_reuse_writes_task.sql +84 -0
  86. package/tests/mutations/s12-fix/M13_dead_inbox_write.sql +84 -0
  87. package/tests/mutations/s12-fix/M14_metadata_unchecked.sql +78 -0
  88. package/tests/mutations/s12-fix/M15_metadata_check_after_lock.sql +84 -0
  89. package/tests/mutations/s12-fix/M16_sql_null_metadata_refused.sql +83 -0
  90. package/tests/mutations/s12-fix/M17_isolation_unchecked.sql +70 -0
  91. package/tests/mutations/s12-fix/M18_isolation_check_after_lock.sql +82 -0
  92. package/tests/mutations/s12-fix/M19_isolation_rr_only.sql +83 -0
  93. package/tests/mutations/s12-fix/M1_lock_dropped.sql +78 -0
  94. package/tests/mutations/s12-fix/M1b_lock_key_only.sql +83 -0
  95. package/tests/mutations/s12-fix/M20_isolation_unsafe_levels_only.sql +83 -0
  96. package/tests/mutations/s12-fix/M21_isolation_check_miss_path_only.sql +83 -0
  97. package/tests/mutations/s12-fix/M22_isolation_inverted.sql +83 -0
  98. package/tests/mutations/s12-fix/M23_isolation_wrong_sqlstate.sql +83 -0
  99. package/tests/mutations/s12-fix/M24_create_thread_key_unchecked.sql +37 -0
  100. package/tests/mutations/s12-fix/M25_create_thread_key_nonnull_only.sql +45 -0
  101. package/tests/mutations/s12-fix/M26_create_thread_key_casefold.sql +45 -0
  102. package/tests/mutations/s12-fix/M27_create_thread_no_object_guard.sql +45 -0
  103. package/tests/mutations/s12-fix/M28_create_thread_key_stripped.sql +40 -0
  104. package/tests/mutations/s12-fix/M29_create_thread_key_nested_refused.sql +45 -0
  105. package/tests/mutations/s12-fix/M2_lock_actor_only.sql +83 -0
  106. package/tests/mutations/s12-fix/M30_create_thread_security_invoker.sql +9 -0
  107. package/tests/mutations/s12-fix/M31_create_thread_search_path_changed.sql +9 -0
  108. package/tests/mutations/s12-fix/M32_class_new_thread_inserter.sql +14 -0
  109. package/tests/mutations/s12-fix/M33_class_metadata_update.sql +12 -0
  110. package/tests/mutations/s12-fix/M33b_class_metadata_update_upper.sql +17 -0
  111. package/tests/mutations/s12-fix/M33c_class_metadata_update_row_form.sql +16 -0
  112. package/tests/mutations/s12-fix/M33d_class_metadata_merge_update.sql +16 -0
  113. package/tests/mutations/s12-fix/M33e_class_metadata_upsert.sql +16 -0
  114. package/tests/mutations/s12-fix/M34_class_api_role_insert.sql +9 -0
  115. package/tests/mutations/s12-fix/M3_lock_after_lookup.sql +83 -0
  116. package/tests/mutations/s12-fix/M4_lock_raw_key.sql +83 -0
  117. package/tests/mutations/s12-fix/M5_no_trim.sql +83 -0
  118. package/tests/mutations/s12-fix/M6_casefold.sql +83 -0
  119. package/tests/mutations/s12-fix/M7_lookup_ignores_actor.sql +82 -0
  120. package/tests/mutations/s12-fix/M8_reuse_reports_false.sql +83 -0
  121. package/tests/mutations/s12-fix/M9_blank_key_admitted.sql +83 -0
  122. package/tests/mutations/s12-fix/run.sh +288 -0
  123. package/tests/seeds/20261124000100_agent_board_step_up_enforcement.sql.pre.sql +47 -0
@@ -0,0 +1,2 @@
1
+ -- D7 also rewrote an already-revoked token (revoker NULL, revoked now); must fail D7-2
2
+ update agent_board.board_invite_tokens set revoked_at = now(), revoked_by_agent_id = null where code = 'su669b-seed-admin-gone';
@@ -0,0 +1,2 @@
1
+ -- D7 also revoked member-preset tokens; must fail D7-3 and the R-18 control
2
+ update agent_board.board_invite_tokens set revoked_at = now(), revoked_by_agent_id = null where code = 'su669b-seed-member-live';
@@ -0,0 +1,2 @@
1
+ -- D7 also revoked expired admin tokens; must fail D7-4
2
+ update agent_board.board_invite_tokens set revoked_at = now(), revoked_by_agent_id = null where code = 'su669b-seed-admin-old';
@@ -0,0 +1,7 @@
1
+ -- a service-only SECURITY DEFINER wrapper over set_board_role_gate; must fail E-1 (reverse closure) and EC-SU-1, naming the wrapper
2
+ create function agent_board.su669b_wrap_gate(p_board uuid) returns void language plpgsql security definer set search_path = '' as $f$
3
+ begin
4
+ perform agent_board.set_board_role_gate(p_board, true);
5
+ end $f$;
6
+ revoke all on function agent_board.su669b_wrap_gate(uuid) from public, anon, authenticated;
7
+ grant execute on function agent_board.su669b_wrap_gate(uuid) to service_role;
@@ -0,0 +1,10 @@
1
+ -- add a write AFTER the helper call in set_board_role_gate; must fail EC-SU-2 (D1 tail)
2
+ do $m$
3
+ declare d text := pg_get_functiondef('agent_board.set_board_role_gate(uuid,boolean)'::regprocedure); n text;
4
+ begin
5
+ n := d;
6
+ if strpos(n, $q$perform agent_board._require_human_step_up('set_board_role_gate');$q$) = 0 then raise exception 'MUTATION ANCHOR MISS (1)'; end if;
7
+ n := replace(n, $q$perform agent_board._require_human_step_up('set_board_role_gate');$q$, $q$perform agent_board._require_human_step_up('set_board_role_gate');
8
+ update agent_board.boards set updated_at = now() where id = p_board_id;$q$);
9
+ execute n;
10
+ end $m$;
@@ -0,0 +1,6 @@
1
+ -- (#802 P-1; run with STEPUP_TEST=agent_board_step_up_helper.test.sql) an authenticated-executable SECURITY DEFINER sql function reaching record_portal_step_up through query_to_xml; must fail both EC-SU-4q rows, naming it
2
+ create function agent_board.su669b_mqx(p_agent uuid) returns xml language sql security definer set search_path = '' as $f$
3
+ select pg_catalog.query_to_xml(pg_catalog.format('select agent_board.record_portal_step_up(%L::uuid, 0::bigint)', p_agent), true, false, '')
4
+ $f$;
5
+ revoke all on function agent_board.su669b_mqx(uuid) from public;
6
+ grant execute on function agent_board.su669b_mqx(uuid) to authenticated;
@@ -0,0 +1,4 @@
1
+ -- (#802 P-1/P-3; run with STEPUP_TEST=agent_board_step_up_helper.test.sql) a public-schema SECURITY DEFINER sql function running a query string through bare TS_STAT; must fail EC-SU-4q (definer row) and EC-SU-4h (it names record_portal_step_up in a literal)
2
+ create function public.su669b_mqxb() returns bigint language sql security definer set search_path = pg_catalog as $f$
3
+ select count(*) from TS_STAT('select agent_board.record_portal_step_up(null::uuid, 0::bigint)::text::tsvector')
4
+ $f$;
@@ -0,0 +1,9 @@
1
+ -- (#802 P-2) a service-only trigger function on board_memberships that writes agents.kind (inert: WHERE false); must fail EC-SU-4t (trigger edges), naming the API writers of board_memberships
2
+ create function agent_board.su669b_mtr() returns trigger language plpgsql security definer set search_path = '' as $f$
3
+ begin
4
+ update agent_board.agents set kind = 'human' where false;
5
+ return null;
6
+ end $f$;
7
+ revoke all on function agent_board.su669b_mtr() from public, anon, authenticated;
8
+ create trigger su669b_mtr after insert or update on agent_board.board_memberships
9
+ for each row execute function agent_board.su669b_mtr();
@@ -0,0 +1,11 @@
1
+ -- (#802 P-2) a second BEFORE trigger on agents that assigns NEW.kind (inert: IF false); must fail EC-SU-4t (the NEW-assigning trigger class), naming su669b_mtrb
2
+ create function agent_board.su669b_mtrb() returns trigger language plpgsql set search_path = '' as $f$
3
+ begin
4
+ if false then
5
+ new.kind := 'human';
6
+ end if;
7
+ return new;
8
+ end $f$;
9
+ revoke all on function agent_board.su669b_mtrb() from public, anon, authenticated;
10
+ create trigger su669b_mtrb before insert or update on agent_board.agents
11
+ for each row execute function agent_board.su669b_mtrb();
@@ -0,0 +1,47 @@
1
+ -- (#802 P-2) sync_agent_principal_columns also assigns NEW.auth_user_id a VALUE (inert: IF false); must fail EC-SU-4t's positive exemption row only
2
+ create or replace function agent_board.sync_agent_principal_columns()
3
+ returns trigger
4
+ language plpgsql
5
+ set search_path = ''
6
+ as $$
7
+ begin
8
+ if new.kind is null
9
+ or (
10
+ tg_op = 'UPDATE'
11
+ and new.kind is not distinct from old.kind
12
+ and new.agent_type is distinct from old.agent_type
13
+ and new.agent_type is not null
14
+ ) then
15
+ new.kind := new.agent_type::agent_board.agent_kind;
16
+ end if;
17
+
18
+ if new.agent_type is null
19
+ or (
20
+ tg_op = 'UPDATE'
21
+ and new.agent_type is not distinct from old.agent_type
22
+ and new.kind is distinct from old.kind
23
+ and new.kind is not null
24
+ ) then
25
+ new.agent_type := new.kind::text;
26
+ end if;
27
+
28
+ if new.kind is null or new.agent_type is null then
29
+ raise exception 'kind and agent_type are both required' using errcode = '23502';
30
+ end if;
31
+
32
+ if new.kind::text <> new.agent_type then
33
+ raise exception 'kind and agent_type must match' using errcode = '22000';
34
+ end if;
35
+
36
+ if new.kind = 'system'::agent_board.agent_kind then
37
+ new.owner_user_id := null;
38
+ new.auth_user_id := null;
39
+ end if;
40
+
41
+ if false then
42
+ new.auth_user_id := new.owner_user_id;
43
+ end if;
44
+
45
+ return new;
46
+ end;
47
+ $$;
@@ -0,0 +1,15 @@
1
+ -- tombstone_delegate_instance gains a second GATED call (set_board_role_gate); must fail TX-1
2
+ do $m$
3
+ declare d text := pg_get_functiondef('agent_board.tombstone_delegate_instance(text,text,text,text)'::regprocedure); n text;
4
+ begin
5
+ n := d;
6
+ if strpos(n, $q$ perform agent_board.update_board_membership(v_board, v_row.agent_id, 'left');
7
+ end loop;$q$) = 0 then raise exception 'MUTATION ANCHOR MISS (1)'; end if;
8
+ n := replace(n, $q$ perform agent_board.update_board_membership(v_board, v_row.agent_id, 'left');
9
+ end loop;$q$, $q$ perform agent_board.update_board_membership(v_board, v_row.agent_id, 'left');
10
+ end loop;
11
+ if false then
12
+ perform agent_board.set_board_role_gate(v_board, false);
13
+ end if;$q$);
14
+ execute n;
15
+ end $m$;
@@ -0,0 +1,10 @@
1
+ -- tombstone_delegate_instance's update_board_membership call targets v_owner, not v_row.agent_id (a non-delegate_instances target); must fail TX-2
2
+ do $m$
3
+ declare d text := pg_get_functiondef('agent_board.tombstone_delegate_instance(text,text,text,text)'::regprocedure); n text;
4
+ begin
5
+ n := d;
6
+ if strpos(n, $q$perform agent_board.update_board_membership(v_board, v_row.agent_id, 'left');$q$) = 0 then raise exception 'MUTATION ANCHOR MISS (1)'; end if;
7
+ n := replace(n, $q$perform agent_board.update_board_membership(v_board, v_row.agent_id, 'left');$q$,
8
+ $q$perform agent_board.update_board_membership(v_board, v_owner, 'left');$q$);
9
+ execute n;
10
+ end $m$;
@@ -0,0 +1,5 @@
1
+ -- (#802 P-3; run with STEPUP_TEST=agent_board_step_up_helper.test.sql) a public-schema SECURITY DEFINER wrapper over grant_board_role (not record_portal_step_up, which is all the pre-#802 EC-SU-4h scanned for); must fail EC-SU-4h, naming public.su669b_mxs->grant_board_role
2
+ create function public.su669b_mxs(p_board uuid, p_agent uuid) returns void language plpgsql security definer set search_path = '' as $f$
3
+ begin
4
+ perform agent_board.grant_board_role(p_board, p_agent, 'owner', null);
5
+ end $f$;
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env bash
2
+ # #669 STEPUP PR-B mutation receipts (AGENTS.md rule 2; plan §10.6 gate-presence/placement rows, E-1(7),
3
+ # E-2(6) M23b/M23c, E-3 M24, E-5 M21b/M21d, the allowlist freeze and D7). Same method as ../669/run.sh;
4
+ # the fingerprint additionally covers the invite-token revocation state, so the D7 data mutants count
5
+ # as applied. Run against a database whose bundle was applied by run-agent-board-schema-tests.sh (the
6
+ # D7 seed is in tests/seeds/). Connection comes ONLY from the libpq
7
+ # environment (PGHOST/PGPORT/PGUSER/PGPASSWORD/PGDATABASE), never argv. The target must be an
8
+ # OWNED database holding this tree's applied bundle (CI: a throwaway job's Supabase; never a
9
+ # shared cluster).
10
+ #
11
+ # Per mutation:
12
+ # applied the mutation runs ALONE in a rolled-back transaction with ON_ERROR_STOP, and the
13
+ # catalog fingerprint (every non-system routine with its argument names, ACL, body and
14
+ # comment; the ledger's ACL and policies; the ledger's and the fedsession audit's
15
+ # columns) read inside that transaction must differ from the baseline. An anchor miss raises 'MUTATION ANCHOR MISS' and is reported, never
16
+ # counted as a kill.
17
+ # pgTAP the mutation and agent_board_step_up_helper.test.sql run in ONE transaction that is
18
+ # rolled back. The receipt records plan, emitted, ok and not ok counts and every
19
+ # failing line, counted from the TAP, never inferred from an absence. An aborted file
20
+ # (emitted < plan) is reported as ABORTED, never as a kill.
21
+ # The fingerprint must equal the baseline after every leg (nothing leaked past a rollback).
22
+ # Usage: run.sh <receipt-path> [mutation.sql ...] (default: every *.sql beside this script)
23
+ set -uo pipefail
24
+ HERE="$(cd "$(dirname "$0")" && pwd)"
25
+ PKG="$(cd "$HERE/../../.." && pwd)"
26
+ TEST="${STEPUP_TEST:-$PKG/tests/agent_board_step_up.test.sql}"
27
+ OUT="${1:?receipt path}"; shift
28
+ if [ "$#" -gt 0 ]; then
29
+ MUTS=()
30
+ for m in "$@"; do
31
+ # A relative or missing path made psql's \i fail with a lowercase "error:", which the
32
+ # applied() check below did not match, so a mutant that never loaded read as "applied".
33
+ [ -f "$m" ] || { echo "FATAL: mutation file not found: $m" >&2; exit 3; }
34
+ MUTS+=("$(cd "$(dirname "$m")" && pwd)/$(basename "$m")")
35
+ done
36
+ else mapfile -t MUTS < <(find "$HERE" -maxdepth 1 -name '*.sql' | sort -V); fi
37
+ [ -n "${PGDATABASE:-}" ] || { echo "FATAL: PGDATABASE unset (libpq environment required)" >&2; exit 3; }
38
+ P=(psql -X -qtA)
39
+ FPSQL="select md5(coalesce((select string_agg(p.oid::regprocedure::text || coalesce(p.proargnames::text, '') || coalesce(p.proacl::text, '') || p.prosrc || p.provolatile::text || p.prosecdef::text || coalesce(obj_description(p.oid, 'pg_proc'), ''), '|' order by p.oid::regprocedure::text) from pg_proc p where p.pronamespace not in ('pg_catalog'::regnamespace, 'information_schema'::regnamespace)), '') || coalesce((select string_agg(attrelid::regclass::text || '.' || attname || ':' || atttypid::regtype::text, '|' order by attrelid::regclass::text, attnum) from pg_attribute where attrelid in ('agent_board.portal_step_up_proofs'::regclass, 'agent_board.enrollment_federation_audit'::regclass) and attnum > 0 and not attisdropped), '') || coalesce((select relacl::text from pg_class where oid = 'agent_board.portal_step_up_proofs'::regclass), '') || coalesce((select string_agg(code || ':' || coalesce(revoked_at::text, '-') || ':' || coalesce(revoked_by_agent_id::text, '-'), '|' order by code) from agent_board.board_invite_tokens where code like 'su669b-seed-%'), '') || coalesce((select string_agg(polname || pg_get_expr(polqual, polrelid), '|' order by polname) from pg_policy where polrelid = 'agent_board.portal_step_up_proofs'::regclass), ''))"
40
+ fp() { "${P[@]}" -c "$FPSQL"; }
41
+ ORIG="$(fp)"
42
+
43
+ applied() { # $1 mutation file -> prints 'applied' / 'NOT APPLIED: <why>'
44
+ local out
45
+ out="$(printf 'begin;\n\\set ON_ERROR_STOP 1\n\\i %s\n%s;\nrollback;\n' "$1" "$FPSQL" | "${P[@]}" 2>&1)"
46
+ if grep -qiE '(error|fatal):' <<<"$out"; then
47
+ echo "NOT APPLIED: $(grep -iE '(error|fatal):' <<<"$out" | head -1)"
48
+ elif [ "$(tail -1 <<<"$out")" = "$ORIG" ]; then
49
+ echo "NOT APPLIED: catalog fingerprint unchanged (no-op)"
50
+ else
51
+ echo "applied"
52
+ fi
53
+ }
54
+
55
+ tap() { # $1 label, $2 mutation file or empty
56
+ local out plan ok nok
57
+ if [ -n "$2" ]; then out="$(printf 'begin;\n\\i %s\n\\i %s\nrollback;\n' "$2" "$TEST" | "${P[@]}" 2>&1)"
58
+ else out="$(printf 'begin;\n\\i %s\nrollback;\n' "$TEST" | "${P[@]}" 2>&1)"; fi
59
+ plan="$(sed -nE 's/^1\.\.([0-9]+)$/\1/p' <<<"$out" | head -1)"
60
+ ok="$(grep -cE '^ok [0-9]+' <<<"$out")"; nok="$(grep -cE '^not ok [0-9]+' <<<"$out")"
61
+ printf '%s: pgtap plan=%s emitted=%s ok=%s not_ok=%s%s\n' "$1" "${plan:-NONE}" "$((ok + nok))" "$ok" "$nok" \
62
+ "$([ "${plan:-x}" = "$((ok + nok))" ] || echo ' ABORTED')"
63
+ grep -E '^not ok [0-9]+' <<<"$out" | sed 's/^/ /'
64
+ grep -E '^#\s+(have|want|caught|wanted|died):' <<<"$out" | head -8 | sed 's/^/ /'
65
+ grep -iE '(error|fatal):' <<<"$out" | head -3 | sed 's/^/ ERR /'
66
+ }
67
+
68
+ {
69
+ echo "# #669 STEPUP PR-B mutation receipt -- $(date -u +%FT%TZ)"
70
+ echo "# tree $(git -C "$PKG" rev-parse HEAD)$(git -C "$PKG" diff --quiet HEAD -- . || echo ' + uncommitted changes')"
71
+ echo "# catalog fingerprint before: $ORIG"
72
+ tap "BASELINE" ""
73
+ for m in "${MUTS[@]}"; do
74
+ name="$(basename "$m" .sql)"
75
+ echo "-- $name: $(head -1 "$m" | sed 's/^-- //') [sha256 $(sha256sum "$m" | cut -c1-16)]"
76
+ echo " $(applied "$m")"
77
+ [ "$(fp)" = "$ORIG" ] || { echo "FATAL: fingerprint changed after the rolled-back apply leg of $name"; exit 3; }
78
+ tap "$name" "$m"
79
+ [ "$(fp)" = "$ORIG" ] || { echo "FATAL: fingerprint changed after the rolled-back pgTAP leg of $name"; exit 3; }
80
+ done
81
+ tap "AFTER" ""
82
+ echo "# catalog fingerprint after: $(fp)"
83
+ } | tee "$OUT"
@@ -0,0 +1,11 @@
1
+ -- read: p_operation loses its SQL default (drop + recreate; ACL and comment restated)
2
+ do $m$ declare f regprocedure := 'agent_board.get_send_outcome(uuid,text,text)'::regprocedure; d text := pg_get_functiondef(f); c text := obj_description(f, 'pg_proc'); n text; begin
3
+ if position($q$p_operation text DEFAULT 'post_board_message'::text$q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.get_send_outcome(uuid,text,text)'; end if;
4
+ n := replace(d, $q$p_operation text DEFAULT 'post_board_message'::text$q$, $q$p_operation text$q$);
5
+ if n = d then raise exception 'mutation is a no-op'; end if;
6
+ execute 'drop function agent_board.get_send_outcome(uuid,text,text)';
7
+ execute n;
8
+ execute format('comment on function agent_board.get_send_outcome(uuid,text,text) is %L', c);
9
+ end $m$;
10
+ revoke all on function agent_board.get_send_outcome(uuid,text,text) from public, anon, authenticated, service_role, agent_board_delegate;
11
+ grant execute on function agent_board.get_send_outcome(uuid,text,text) to authenticated, agent_board_delegate;
@@ -0,0 +1,7 @@
1
+ -- read: p_operation's SQL default names another operation (send_direct_message)
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.get_send_outcome(uuid,text,text)'::regprocedure); n text; begin
3
+ if position($q$p_operation text DEFAULT 'post_board_message'::text$q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.get_send_outcome(uuid,text,text)'; end if;
4
+ n := replace(d, $q$p_operation text DEFAULT 'post_board_message'::text$q$, $q$p_operation text DEFAULT 'send_direct_message'::text$q$);
5
+ if n = d then raise exception 'mutation is a no-op'; end if;
6
+ execute n;
7
+ end $m$;
@@ -0,0 +1,10 @@
1
+ -- send_direct_message: the helper is called on the replay branch too
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'::regprocedure); n text; begin
3
+ if position($q$ if v_inserted and v_idempotency_key is not null then
4
+ $q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb)'; end if;
5
+ n := replace(d, $q$ if v_inserted and v_idempotency_key is not null then
6
+ $q$, $q$ if v_idempotency_key is not null then
7
+ $q$);
8
+ if n = d then raise exception 'mutation is a no-op'; end if;
9
+ execute n;
10
+ end $m$;
@@ -0,0 +1,10 @@
1
+ -- send_multicast_message: the helper is called on the replay branch too
2
+ do $m$ declare d text := pg_get_functiondef('agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb)'::regprocedure); n text; begin
3
+ if position($q$ if v_inserted and v_idempotency_key is not null then
4
+ $q$ in d) = 0 then raise exception 'mutation anchor not found in agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb)'; end if;
5
+ n := replace(d, $q$ if v_inserted and v_idempotency_key is not null then
6
+ $q$, $q$ if v_idempotency_key is not null then
7
+ $q$);
8
+ if n = d then raise exception 'mutation is a no-op'; end if;
9
+ execute n;
10
+ end $m$;
@@ -0,0 +1,56 @@
1
+ -- CONTROL: re-create the shipped body unchanged. The spec must stay byte-identical to its golden.
2
+ -- SCRATCH ONLY: applied to a throwaway template by run.sh, never to a shipped migration.
3
+ create or replace function agent_board.create_or_find_thread(
4
+ p_created_by_agent_id uuid,
5
+ p_subject text,
6
+ p_idempotency_key text,
7
+ p_thread_type text default 'coordination',
8
+ p_metadata jsonb default '{}'::jsonb
9
+ )
10
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
11
+ language plpgsql
12
+ security definer
13
+ set search_path = agent_board, public
14
+ as $$
15
+ declare
16
+ v_actor_agent_id uuid;
17
+ v_key text := nullif(trim(p_idempotency_key), '');
18
+ v_thread agent_board.threads%rowtype;
19
+ begin
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+ if nullif(trim(p_subject), '') is null or v_key is null then
22
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
23
+ end if;
24
+
25
+ select *
26
+ into v_thread
27
+ from agent_board.threads t
28
+ where t.created_by_agent_id = v_actor_agent_id
29
+ and t.metadata ->> 'addressingThreadKey' = v_key
30
+ order by t.created_at desc
31
+ limit 1
32
+ for update;
33
+
34
+ if v_thread.id is not null then
35
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
36
+ return;
37
+ end if;
38
+
39
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
40
+ values (
41
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
42
+ v_actor_agent_id,
43
+ trim(p_subject),
44
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
45
+ )
46
+ returning * into v_thread;
47
+
48
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
49
+ end;
50
+ $$;
51
+
52
+ do $chk$ begin
53
+ if position($m$v_key text := nullif(trim(p_idempotency_key), '');$m$ in pg_get_functiondef('agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
54
+ raise exception 'S-12 mutation M0_identity NOT applied';
55
+ end if;
56
+ end $chk$;
@@ -0,0 +1,57 @@
1
+ -- Serialise the miss path on an advisory xact lock keyed by (actor, key). Shows what a body that HAS the S-12 property produces.
2
+ -- SCRATCH ONLY: applied to a throwaway template by run.sh, never to a shipped migration.
3
+ create or replace function agent_board.create_or_find_thread(
4
+ p_created_by_agent_id uuid,
5
+ p_subject text,
6
+ p_idempotency_key text,
7
+ p_thread_type text default 'coordination',
8
+ p_metadata jsonb default '{}'::jsonb
9
+ )
10
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
11
+ language plpgsql
12
+ security definer
13
+ set search_path = agent_board, public
14
+ as $$
15
+ declare
16
+ v_actor_agent_id uuid;
17
+ v_key text := nullif(trim(p_idempotency_key), '');
18
+ v_thread agent_board.threads%rowtype;
19
+ begin
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+ if nullif(trim(p_subject), '') is null or v_key is null then
22
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
23
+ end if;
24
+ perform pg_advisory_xact_lock(hashtextextended(v_actor_agent_id::text || ':' || v_key, 0));
25
+
26
+ select *
27
+ into v_thread
28
+ from agent_board.threads t
29
+ where t.created_by_agent_id = v_actor_agent_id
30
+ and t.metadata ->> 'addressingThreadKey' = v_key
31
+ order by t.created_at desc
32
+ limit 1
33
+ for update;
34
+
35
+ if v_thread.id is not null then
36
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
37
+ return;
38
+ end if;
39
+
40
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
41
+ values (
42
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
43
+ v_actor_agent_id,
44
+ trim(p_subject),
45
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
46
+ )
47
+ returning * into v_thread;
48
+
49
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
50
+ end;
51
+ $$;
52
+
53
+ do $chk$ begin
54
+ if position($m$pg_advisory_xact_lock(hashtextextended(v_actor_agent_id::text$m$ in pg_get_functiondef('agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
55
+ raise exception 'S-12 mutation M1_serialise_actor_key NOT applied';
56
+ end if;
57
+ end $chk$;
@@ -0,0 +1,57 @@
1
+ -- Serialise on the key ALONE (over-broad). Different actors must not wait on each other.
2
+ -- SCRATCH ONLY: applied to a throwaway template by run.sh, never to a shipped migration.
3
+ create or replace function agent_board.create_or_find_thread(
4
+ p_created_by_agent_id uuid,
5
+ p_subject text,
6
+ p_idempotency_key text,
7
+ p_thread_type text default 'coordination',
8
+ p_metadata jsonb default '{}'::jsonb
9
+ )
10
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
11
+ language plpgsql
12
+ security definer
13
+ set search_path = agent_board, public
14
+ as $$
15
+ declare
16
+ v_actor_agent_id uuid;
17
+ v_key text := nullif(trim(p_idempotency_key), '');
18
+ v_thread agent_board.threads%rowtype;
19
+ begin
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+ if nullif(trim(p_subject), '') is null or v_key is null then
22
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
23
+ end if;
24
+ perform pg_advisory_xact_lock(hashtextextended(v_key, 0));
25
+
26
+ select *
27
+ into v_thread
28
+ from agent_board.threads t
29
+ where t.created_by_agent_id = v_actor_agent_id
30
+ and t.metadata ->> 'addressingThreadKey' = v_key
31
+ order by t.created_at desc
32
+ limit 1
33
+ for update;
34
+
35
+ if v_thread.id is not null then
36
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
37
+ return;
38
+ end if;
39
+
40
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
41
+ values (
42
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
43
+ v_actor_agent_id,
44
+ trim(p_subject),
45
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
46
+ )
47
+ returning * into v_thread;
48
+
49
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
50
+ end;
51
+ $$;
52
+
53
+ do $chk$ begin
54
+ if position($m$pg_advisory_xact_lock(hashtextextended(v_key, 0))$m$ in pg_get_functiondef('agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
55
+ raise exception 'S-12 mutation M1b_serialise_key_only NOT applied';
56
+ end if;
57
+ end $chk$;
@@ -0,0 +1,9 @@
1
+ -- Add a unique index on (actor, key) WITHOUT handling the conflict: the losing caller surfaces 23505.
2
+ -- SCRATCH ONLY: applied to a throwaway template by run.sh, never to a shipped migration.
3
+ create unique index s12_mut_uniq on agent_board.threads(created_by_agent_id, (metadata ->> 'addressingThreadKey'));
4
+
5
+ do $chk$ begin
6
+ if to_regclass('agent_board.s12_mut_uniq') is null then
7
+ raise exception 'S-12 mutation M2_unique_index_no_handler NOT applied';
8
+ end if;
9
+ end $chk$;
@@ -0,0 +1,56 @@
1
+ -- Drop the trim() normalization of the key.
2
+ -- SCRATCH ONLY: applied to a throwaway template by run.sh, never to a shipped migration.
3
+ create or replace function agent_board.create_or_find_thread(
4
+ p_created_by_agent_id uuid,
5
+ p_subject text,
6
+ p_idempotency_key text,
7
+ p_thread_type text default 'coordination',
8
+ p_metadata jsonb default '{}'::jsonb
9
+ )
10
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
11
+ language plpgsql
12
+ security definer
13
+ set search_path = agent_board, public
14
+ as $$
15
+ declare
16
+ v_actor_agent_id uuid;
17
+ v_key text := nullif(p_idempotency_key, '');
18
+ v_thread agent_board.threads%rowtype;
19
+ begin
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+ if nullif(trim(p_subject), '') is null or v_key is null then
22
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
23
+ end if;
24
+
25
+ select *
26
+ into v_thread
27
+ from agent_board.threads t
28
+ where t.created_by_agent_id = v_actor_agent_id
29
+ and t.metadata ->> 'addressingThreadKey' = v_key
30
+ order by t.created_at desc
31
+ limit 1
32
+ for update;
33
+
34
+ if v_thread.id is not null then
35
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
36
+ return;
37
+ end if;
38
+
39
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
40
+ values (
41
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
42
+ v_actor_agent_id,
43
+ trim(p_subject),
44
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
45
+ )
46
+ returning * into v_thread;
47
+
48
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
49
+ end;
50
+ $$;
51
+
52
+ do $chk$ begin
53
+ if position($m$v_key text := nullif(p_idempotency_key, '')$m$ in pg_get_functiondef('agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
54
+ raise exception 'S-12 mutation M3_no_trim NOT applied';
55
+ end if;
56
+ end $chk$;
@@ -0,0 +1,56 @@
1
+ -- Add case-folding the shipped contract does not have.
2
+ -- SCRATCH ONLY: applied to a throwaway template by run.sh, never to a shipped migration.
3
+ create or replace function agent_board.create_or_find_thread(
4
+ p_created_by_agent_id uuid,
5
+ p_subject text,
6
+ p_idempotency_key text,
7
+ p_thread_type text default 'coordination',
8
+ p_metadata jsonb default '{}'::jsonb
9
+ )
10
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
11
+ language plpgsql
12
+ security definer
13
+ set search_path = agent_board, public
14
+ as $$
15
+ declare
16
+ v_actor_agent_id uuid;
17
+ v_key text := nullif(lower(trim(p_idempotency_key)), '');
18
+ v_thread agent_board.threads%rowtype;
19
+ begin
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+ if nullif(trim(p_subject), '') is null or v_key is null then
22
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
23
+ end if;
24
+
25
+ select *
26
+ into v_thread
27
+ from agent_board.threads t
28
+ where t.created_by_agent_id = v_actor_agent_id
29
+ and t.metadata ->> 'addressingThreadKey' = v_key
30
+ order by t.created_at desc
31
+ limit 1
32
+ for update;
33
+
34
+ if v_thread.id is not null then
35
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
36
+ return;
37
+ end if;
38
+
39
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
40
+ values (
41
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
42
+ v_actor_agent_id,
43
+ trim(p_subject),
44
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
45
+ )
46
+ returning * into v_thread;
47
+
48
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
49
+ end;
50
+ $$;
51
+
52
+ do $chk$ begin
53
+ if position($m$nullif(lower(trim(p_idempotency_key)), '')$m$ in pg_get_functiondef('agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
54
+ raise exception 'S-12 mutation M4_casefold NOT applied';
55
+ end if;
56
+ end $chk$;
@@ -0,0 +1,56 @@
1
+ -- Reuse lookup ignores the actor: any actor's thread under the key is reused.
2
+ -- SCRATCH ONLY: applied to a throwaway template by run.sh, never to a shipped migration.
3
+ create or replace function agent_board.create_or_find_thread(
4
+ p_created_by_agent_id uuid,
5
+ p_subject text,
6
+ p_idempotency_key text,
7
+ p_thread_type text default 'coordination',
8
+ p_metadata jsonb default '{}'::jsonb
9
+ )
10
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
11
+ language plpgsql
12
+ security definer
13
+ set search_path = agent_board, public
14
+ as $$
15
+ declare
16
+ v_actor_agent_id uuid;
17
+ v_key text := nullif(trim(p_idempotency_key), '');
18
+ v_thread agent_board.threads%rowtype;
19
+ begin
20
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
21
+ if nullif(trim(p_subject), '') is null or v_key is null then
22
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
23
+ end if;
24
+
25
+ select *
26
+ into v_thread
27
+ from agent_board.threads t
28
+ where true
29
+ and t.metadata ->> 'addressingThreadKey' = v_key
30
+ order by t.created_at desc
31
+ limit 1
32
+ for update;
33
+
34
+ if v_thread.id is not null then
35
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, true;
36
+ return;
37
+ end if;
38
+
39
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
40
+ values (
41
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
42
+ v_actor_agent_id,
43
+ trim(p_subject),
44
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
45
+ )
46
+ returning * into v_thread;
47
+
48
+ return query select v_thread.id, v_thread.thread_type, v_thread.subject, v_thread.status, v_thread.last_message_sequence, v_thread.created_at, v_thread.updated_at, false;
49
+ end;
50
+ $$;
51
+
52
+ do $chk$ begin
53
+ if position($m$where true$m$ in pg_get_functiondef('agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
54
+ raise exception 'S-12 mutation M5_lookup_ignores_actor NOT applied';
55
+ end if;
56
+ end $chk$;