@consiliency/agent-board-schema 1.75.0 → 1.79.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fixtures/already-applied.json +16 -0
- package/migrations/20261109000100_agent_board_step_up_groundwork.sql +343 -0
- package/migrations/20261120000200_agent_board_thread_key_serialize.sql +118 -0
- package/migrations/20261121000100_agent_board_thread_key_guards.sql +196 -0
- package/migrations/20261124000100_agent_board_step_up_enforcement.sql +1213 -0
- package/package.json +1 -1
- package/schema-manifest.json +39 -3
- package/src/deployment-drift-main.test.mjs +89 -6
- package/src/deployment-drift.test.mjs +20 -1
- package/src/tooling.test.mjs +10 -2
- package/tests/agent_board_717_message_replay_scope.test.sql +39 -2
- package/tests/agent_board_717_tasking_replay_scope.test.sql +21 -2
- package/tests/agent_board_enrollment_audit_content.test.sql +6 -1
- package/tests/agent_board_role_guard.test.sql +3 -1
- package/tests/agent_board_send_outcome.test.sql +39 -1
- package/tests/agent_board_session_identity_approval_revocation.test.sql +5 -0
- package/tests/agent_board_step_up.test.sql +991 -0
- package/tests/agent_board_step_up_helper.test.sql +815 -0
- package/tests/agent_board_thread_key_guards.test.sql +186 -0
- package/tests/agent_board_thread_key_serialize.test.sql +182 -0
- package/tests/agent_board_write_enforcement_class.test.sql +8 -4
- package/tests/catalog/function_execute_privileges.json +72 -0
- package/tests/catalog/p_metadata_parameters.json +32 -0
- package/tests/catalog/step_up_gated_closure.json +24 -0
- package/tests/fixtures/pgtap-unexposed.sql +9 -4
- package/tests/isolation/expected/create_or_find_thread_same_key_race.out +641 -0
- package/tests/isolation/expected/session_identity_links.out +4 -0
- package/tests/isolation/specs/create_or_find_thread_same_key_race.spec +252 -0
- package/tests/isolation/specs/session_identity_links.spec +5 -0
- package/tests/mutations/669/M10_service_role_blanket.sql +10 -0
- package/tests/mutations/669/M12a_no_parse_wrapper.sql +17 -0
- package/tests/mutations/669/M12b_no_session_wrapper.sql +23 -0
- package/tests/mutations/669/M12c_no_claim_type_guard.sql +13 -0
- package/tests/mutations/669/M12d_no_amr_element_guards.sql +15 -0
- package/tests/mutations/669/M14_now_not_clock.sql +9 -0
- package/tests/mutations/669/M15_classifier_reads_metadata.sql +17 -0
- package/tests/mutations/669/M17_skip_ledger.sql +15 -0
- package/tests/mutations/669/M18_service_role_sub_insensitive.sql +9 -0
- package/tests/mutations/669/M18b_drop_uid_conjunct.sql +9 -0
- package/tests/mutations/669/M23_definer_wrapper_over_writer.sql +5 -0
- package/tests/mutations/669/M3_age_from_iat.sql +9 -0
- package/tests/mutations/669/M4_window_900.sql +9 -0
- package/tests/mutations/669/M4b_lt_to_le.sql +9 -0
- package/tests/mutations/669/M5_no_skew_bound.sql +9 -0
- package/tests/mutations/669/M6a_user_metadata_amr.sql +9 -0
- package/tests/mutations/669/M6b_app_metadata_claim.sql +9 -0
- package/tests/mutations/669/M6c_legacy_guc.sql +9 -0
- package/tests/mutations/669/M7_either_claim_any_human.sql +9 -0
- package/tests/mutations/669/M8_any_amr_method.sql +9 -0
- package/tests/mutations/669/M8b_accept_aal2.sql +9 -0
- package/tests/mutations/669/M9_every_kind.sql +9 -0
- package/tests/mutations/669/MAL_allowlist_nonempty.sql +9 -0
- package/tests/mutations/669/MAR_writer_arg_renamed.sql +14 -0
- package/tests/mutations/669/MAU_audit_mode_retyped.sql +2 -0
- package/tests/mutations/669/MA_helper_always_raises.sql +10 -0
- package/tests/mutations/669/MDX_dynamic_execute_definer.sql +5 -0
- package/tests/mutations/669/MD_detail_carries_value.sql +12 -0
- package/tests/mutations/669/MEC0_caller_of_helper.sql +4 -0
- package/tests/mutations/669/MEC0b_overload_caller.sql +4 -0
- package/tests/mutations/669/MG1_helper_granted_authenticated.sql +2 -0
- package/tests/mutations/669/MG2_ledger_insert_authenticated.sql +2 -0
- package/tests/mutations/669/MG3_ledger_policy.sql +2 -0
- package/tests/mutations/669/MR1_writer_no_class_check.sql +9 -0
- package/tests/mutations/669/MR2_writer_wide_window.sql +9 -0
- package/tests/mutations/669/MR3_writer_prunes_everyone.sql +11 -0
- package/tests/mutations/669/MR4_writer_lower_exclusive.sql +9 -0
- package/tests/mutations/669/MR5_writer_upper_exclusive.sql +9 -0
- package/tests/mutations/669/MR6_writer_upper_61.sql +9 -0
- package/tests/mutations/669/MW2_second_ledger_writer.sql +4 -0
- package/tests/mutations/669/MXS_cross_schema_wrapper.sql +4 -0
- package/tests/mutations/669/run.sh +80 -0
- package/tests/mutations/669b/M11_ceil_first.sql +10 -0
- package/tests/mutations/669b/M11_gate_first.sql +10 -0
- package/tests/mutations/669b/M11_grant_first.sql +10 -0
- package/tests/mutations/669b/M11_inv_first.sql +10 -0
- package/tests/mutations/669b/M11_mem_first.sql +10 -0
- package/tests/mutations/669b/M11_readmit_first.sql +10 -0
- package/tests/mutations/669b/M13a_gate_member_preset.sql +12 -0
- package/tests/mutations/669b/M13b_ungate_admin_preset.sql +12 -0
- package/tests/mutations/669b/M16_actor_is_target.sql +14 -0
- package/tests/mutations/669b/M19_drop_readmit.sql +9 -0
- package/tests/mutations/669b/M1_drop_grant.sql +9 -0
- package/tests/mutations/669b/M20_drop_d5_refusal.sql +9 -0
- package/tests/mutations/669b/M21_drop_removal.sql +9 -0
- package/tests/mutations/669b/M21b_only_removed.sql +9 -0
- package/tests/mutations/669b/M21c_gate_members.sql +12 -0
- package/tests/mutations/669b/M21d_requested_state.sql +9 -0
- package/tests/mutations/669b/M23b_api_wrapper_over_audit_writer.sql +6 -0
- package/tests/mutations/669b/M23c_drop_register_type_refusal.sql +9 -0
- package/tests/mutations/669b/M24_drop_kind_refusal.sql +9 -0
- package/tests/mutations/669b/M24b_drop_auth_bind_refusal.sql +9 -0
- package/tests/mutations/669b/M2a_drop_gate.sql +9 -0
- package/tests/mutations/669b/M2b_drop_ceiling.sql +9 -0
- package/tests/mutations/669b/M2c_drop_invite.sql +9 -0
- package/tests/mutations/669b/MAL2_drop_otp.sql +9 -0
- package/tests/mutations/669b/MAL3_admit_passkey.sql +9 -0
- package/tests/mutations/669b/MD7a_no_revocation.sql +2 -0
- package/tests/mutations/669b/MD7b_overwrite_revoked.sql +2 -0
- package/tests/mutations/669b/MD7c_revoke_members.sql +2 -0
- package/tests/mutations/669b/MD7d_revoke_expired.sql +2 -0
- package/tests/mutations/669b/ME1_wrapper_over_gate.sql +7 -0
- package/tests/mutations/669b/MEC2_write_after_helper.sql +10 -0
- package/tests/mutations/669b/MQX_query_to_xml_definer.sql +6 -0
- package/tests/mutations/669b/MQXb_ts_stat_other_schema.sql +4 -0
- package/tests/mutations/669b/MTR_trigger_route.sql +9 -0
- package/tests/mutations/669b/MTRb_new_assign.sql +11 -0
- package/tests/mutations/669b/MTRc_sync_assigns_value.sql +47 -0
- package/tests/mutations/669b/MTX1_tombstone_second_gated_call.sql +15 -0
- package/tests/mutations/669b/MTX2_tombstone_non_delegate_target.sql +10 -0
- package/tests/mutations/669b/MXS_other_schema_names_gated.sql +5 -0
- package/tests/mutations/669b/run.sh +83 -0
- package/tests/mutations/670/M18a_read_operation_default_dropped.sql +11 -0
- package/tests/mutations/670/M18b_read_operation_default_changed.sql +7 -0
- package/tests/mutations/670/M3-DM_helper_on_replay_branch.sql +10 -0
- package/tests/mutations/670/M3-MC_helper_on_replay_branch.sql +10 -0
- package/tests/mutations/s12/M0_identity.sql +56 -0
- package/tests/mutations/s12/M1_serialise_actor_key.sql +57 -0
- package/tests/mutations/s12/M1b_serialise_key_only.sql +57 -0
- package/tests/mutations/s12/M2_unique_index_no_handler.sql +9 -0
- package/tests/mutations/s12/M3_no_trim.sql +56 -0
- package/tests/mutations/s12/M4_casefold.sql +56 -0
- package/tests/mutations/s12/M5_lookup_ignores_actor.sql +56 -0
- package/tests/mutations/s12/M6_reuse_reports_false.sql +56 -0
- package/tests/mutations/s12/run.sh +112 -0
- package/tests/mutations/s12-fix/M0_identity.sql +83 -0
- package/tests/mutations/s12-fix/M10_security_invoker.sql +9 -0
- package/tests/mutations/s12-fix/M11_search_path_changed.sql +9 -0
- package/tests/mutations/s12-fix/M12_reuse_writes_task.sql +84 -0
- package/tests/mutations/s12-fix/M13_dead_inbox_write.sql +84 -0
- package/tests/mutations/s12-fix/M14_metadata_unchecked.sql +78 -0
- package/tests/mutations/s12-fix/M15_metadata_check_after_lock.sql +84 -0
- package/tests/mutations/s12-fix/M16_sql_null_metadata_refused.sql +83 -0
- package/tests/mutations/s12-fix/M17_isolation_unchecked.sql +70 -0
- package/tests/mutations/s12-fix/M18_isolation_check_after_lock.sql +82 -0
- package/tests/mutations/s12-fix/M19_isolation_rr_only.sql +83 -0
- package/tests/mutations/s12-fix/M1_lock_dropped.sql +78 -0
- package/tests/mutations/s12-fix/M1b_lock_key_only.sql +83 -0
- package/tests/mutations/s12-fix/M20_isolation_unsafe_levels_only.sql +83 -0
- package/tests/mutations/s12-fix/M21_isolation_check_miss_path_only.sql +83 -0
- package/tests/mutations/s12-fix/M22_isolation_inverted.sql +83 -0
- package/tests/mutations/s12-fix/M23_isolation_wrong_sqlstate.sql +83 -0
- package/tests/mutations/s12-fix/M24_create_thread_key_unchecked.sql +37 -0
- package/tests/mutations/s12-fix/M25_create_thread_key_nonnull_only.sql +45 -0
- package/tests/mutations/s12-fix/M26_create_thread_key_casefold.sql +45 -0
- package/tests/mutations/s12-fix/M27_create_thread_no_object_guard.sql +45 -0
- package/tests/mutations/s12-fix/M28_create_thread_key_stripped.sql +40 -0
- package/tests/mutations/s12-fix/M29_create_thread_key_nested_refused.sql +45 -0
- package/tests/mutations/s12-fix/M2_lock_actor_only.sql +83 -0
- package/tests/mutations/s12-fix/M30_create_thread_security_invoker.sql +9 -0
- package/tests/mutations/s12-fix/M31_create_thread_search_path_changed.sql +9 -0
- package/tests/mutations/s12-fix/M32_class_new_thread_inserter.sql +14 -0
- package/tests/mutations/s12-fix/M33_class_metadata_update.sql +12 -0
- package/tests/mutations/s12-fix/M33b_class_metadata_update_upper.sql +17 -0
- package/tests/mutations/s12-fix/M33c_class_metadata_update_row_form.sql +16 -0
- package/tests/mutations/s12-fix/M33d_class_metadata_merge_update.sql +16 -0
- package/tests/mutations/s12-fix/M33e_class_metadata_upsert.sql +16 -0
- package/tests/mutations/s12-fix/M34_class_api_role_insert.sql +9 -0
- package/tests/mutations/s12-fix/M3_lock_after_lookup.sql +83 -0
- package/tests/mutations/s12-fix/M4_lock_raw_key.sql +83 -0
- package/tests/mutations/s12-fix/M5_no_trim.sql +83 -0
- package/tests/mutations/s12-fix/M6_casefold.sql +83 -0
- package/tests/mutations/s12-fix/M7_lookup_ignores_actor.sql +82 -0
- package/tests/mutations/s12-fix/M8_reuse_reports_false.sql +83 -0
- package/tests/mutations/s12-fix/M9_blank_key_admitted.sql +83 -0
- package/tests/mutations/s12-fix/run.sh +288 -0
- package/tests/seeds/20261124000100_agent_board_step_up_enforcement.sql.pre.sql +47 -0
|
@@ -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$;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
-- Reuse path reports reused=false.
|
|
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, false;
|
|
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_thread.updated_at, false;$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 M6_reuse_reports_false NOT applied';
|
|
55
|
+
end if;
|
|
56
|
+
end $chk$;
|