@consiliency/agent-board-schema 1.76.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.
Files changed (121) hide show
  1. package/fixtures/already-applied.json +12 -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/package.json +1 -1
  6. package/schema-manifest.json +30 -3
  7. package/src/deployment-drift-main.test.mjs +89 -6
  8. package/src/deployment-drift.test.mjs +20 -1
  9. package/src/tooling.test.mjs +9 -2
  10. package/tests/agent_board_717_message_replay_scope.test.sql +39 -2
  11. package/tests/agent_board_717_tasking_replay_scope.test.sql +21 -2
  12. package/tests/agent_board_role_guard.test.sql +3 -1
  13. package/tests/agent_board_send_outcome.test.sql +39 -1
  14. package/tests/agent_board_session_identity_approval_revocation.test.sql +5 -0
  15. package/tests/agent_board_step_up.test.sql +991 -0
  16. package/tests/agent_board_step_up_helper.test.sql +132 -28
  17. package/tests/agent_board_thread_key_guards.test.sql +186 -0
  18. package/tests/agent_board_thread_key_serialize.test.sql +182 -0
  19. package/tests/agent_board_write_enforcement_class.test.sql +3 -2
  20. package/tests/catalog/step_up_gated_closure.json +24 -0
  21. package/tests/fixtures/pgtap-unexposed.sql +9 -4
  22. package/tests/isolation/expected/create_or_find_thread_same_key_race.out +641 -0
  23. package/tests/isolation/expected/session_identity_links.out +4 -0
  24. package/tests/isolation/specs/create_or_find_thread_same_key_race.spec +252 -0
  25. package/tests/isolation/specs/session_identity_links.spec +5 -0
  26. package/tests/mutations/669/run.sh +12 -4
  27. package/tests/mutations/669b/M11_ceil_first.sql +10 -0
  28. package/tests/mutations/669b/M11_gate_first.sql +10 -0
  29. package/tests/mutations/669b/M11_grant_first.sql +10 -0
  30. package/tests/mutations/669b/M11_inv_first.sql +10 -0
  31. package/tests/mutations/669b/M11_mem_first.sql +10 -0
  32. package/tests/mutations/669b/M11_readmit_first.sql +10 -0
  33. package/tests/mutations/669b/M13a_gate_member_preset.sql +12 -0
  34. package/tests/mutations/669b/M13b_ungate_admin_preset.sql +12 -0
  35. package/tests/mutations/669b/M16_actor_is_target.sql +14 -0
  36. package/tests/mutations/669b/M19_drop_readmit.sql +9 -0
  37. package/tests/mutations/669b/M1_drop_grant.sql +9 -0
  38. package/tests/mutations/669b/M20_drop_d5_refusal.sql +9 -0
  39. package/tests/mutations/669b/M21_drop_removal.sql +9 -0
  40. package/tests/mutations/669b/M21b_only_removed.sql +9 -0
  41. package/tests/mutations/669b/M21c_gate_members.sql +12 -0
  42. package/tests/mutations/669b/M21d_requested_state.sql +9 -0
  43. package/tests/mutations/669b/M23b_api_wrapper_over_audit_writer.sql +6 -0
  44. package/tests/mutations/669b/M23c_drop_register_type_refusal.sql +9 -0
  45. package/tests/mutations/669b/M24_drop_kind_refusal.sql +9 -0
  46. package/tests/mutations/669b/M24b_drop_auth_bind_refusal.sql +9 -0
  47. package/tests/mutations/669b/M2a_drop_gate.sql +9 -0
  48. package/tests/mutations/669b/M2b_drop_ceiling.sql +9 -0
  49. package/tests/mutations/669b/M2c_drop_invite.sql +9 -0
  50. package/tests/mutations/669b/MAL2_drop_otp.sql +9 -0
  51. package/tests/mutations/669b/MAL3_admit_passkey.sql +9 -0
  52. package/tests/mutations/669b/MD7a_no_revocation.sql +2 -0
  53. package/tests/mutations/669b/MD7b_overwrite_revoked.sql +2 -0
  54. package/tests/mutations/669b/MD7c_revoke_members.sql +2 -0
  55. package/tests/mutations/669b/MD7d_revoke_expired.sql +2 -0
  56. package/tests/mutations/669b/ME1_wrapper_over_gate.sql +7 -0
  57. package/tests/mutations/669b/MEC2_write_after_helper.sql +10 -0
  58. package/tests/mutations/669b/MQX_query_to_xml_definer.sql +6 -0
  59. package/tests/mutations/669b/MQXb_ts_stat_other_schema.sql +4 -0
  60. package/tests/mutations/669b/MTR_trigger_route.sql +9 -0
  61. package/tests/mutations/669b/MTRb_new_assign.sql +11 -0
  62. package/tests/mutations/669b/MTRc_sync_assigns_value.sql +47 -0
  63. package/tests/mutations/669b/MTX1_tombstone_second_gated_call.sql +15 -0
  64. package/tests/mutations/669b/MTX2_tombstone_non_delegate_target.sql +10 -0
  65. package/tests/mutations/669b/MXS_other_schema_names_gated.sql +5 -0
  66. package/tests/mutations/669b/run.sh +83 -0
  67. package/tests/mutations/670/M18a_read_operation_default_dropped.sql +11 -0
  68. package/tests/mutations/670/M18b_read_operation_default_changed.sql +7 -0
  69. package/tests/mutations/670/M3-DM_helper_on_replay_branch.sql +10 -0
  70. package/tests/mutations/670/M3-MC_helper_on_replay_branch.sql +10 -0
  71. package/tests/mutations/s12/M0_identity.sql +56 -0
  72. package/tests/mutations/s12/M1_serialise_actor_key.sql +57 -0
  73. package/tests/mutations/s12/M1b_serialise_key_only.sql +57 -0
  74. package/tests/mutations/s12/M2_unique_index_no_handler.sql +9 -0
  75. package/tests/mutations/s12/M3_no_trim.sql +56 -0
  76. package/tests/mutations/s12/M4_casefold.sql +56 -0
  77. package/tests/mutations/s12/M5_lookup_ignores_actor.sql +56 -0
  78. package/tests/mutations/s12/M6_reuse_reports_false.sql +56 -0
  79. package/tests/mutations/s12/run.sh +112 -0
  80. package/tests/mutations/s12-fix/M0_identity.sql +83 -0
  81. package/tests/mutations/s12-fix/M10_security_invoker.sql +9 -0
  82. package/tests/mutations/s12-fix/M11_search_path_changed.sql +9 -0
  83. package/tests/mutations/s12-fix/M12_reuse_writes_task.sql +84 -0
  84. package/tests/mutations/s12-fix/M13_dead_inbox_write.sql +84 -0
  85. package/tests/mutations/s12-fix/M14_metadata_unchecked.sql +78 -0
  86. package/tests/mutations/s12-fix/M15_metadata_check_after_lock.sql +84 -0
  87. package/tests/mutations/s12-fix/M16_sql_null_metadata_refused.sql +83 -0
  88. package/tests/mutations/s12-fix/M17_isolation_unchecked.sql +70 -0
  89. package/tests/mutations/s12-fix/M18_isolation_check_after_lock.sql +82 -0
  90. package/tests/mutations/s12-fix/M19_isolation_rr_only.sql +83 -0
  91. package/tests/mutations/s12-fix/M1_lock_dropped.sql +78 -0
  92. package/tests/mutations/s12-fix/M1b_lock_key_only.sql +83 -0
  93. package/tests/mutations/s12-fix/M20_isolation_unsafe_levels_only.sql +83 -0
  94. package/tests/mutations/s12-fix/M21_isolation_check_miss_path_only.sql +83 -0
  95. package/tests/mutations/s12-fix/M22_isolation_inverted.sql +83 -0
  96. package/tests/mutations/s12-fix/M23_isolation_wrong_sqlstate.sql +83 -0
  97. package/tests/mutations/s12-fix/M24_create_thread_key_unchecked.sql +37 -0
  98. package/tests/mutations/s12-fix/M25_create_thread_key_nonnull_only.sql +45 -0
  99. package/tests/mutations/s12-fix/M26_create_thread_key_casefold.sql +45 -0
  100. package/tests/mutations/s12-fix/M27_create_thread_no_object_guard.sql +45 -0
  101. package/tests/mutations/s12-fix/M28_create_thread_key_stripped.sql +40 -0
  102. package/tests/mutations/s12-fix/M29_create_thread_key_nested_refused.sql +45 -0
  103. package/tests/mutations/s12-fix/M2_lock_actor_only.sql +83 -0
  104. package/tests/mutations/s12-fix/M30_create_thread_security_invoker.sql +9 -0
  105. package/tests/mutations/s12-fix/M31_create_thread_search_path_changed.sql +9 -0
  106. package/tests/mutations/s12-fix/M32_class_new_thread_inserter.sql +14 -0
  107. package/tests/mutations/s12-fix/M33_class_metadata_update.sql +12 -0
  108. package/tests/mutations/s12-fix/M33b_class_metadata_update_upper.sql +17 -0
  109. package/tests/mutations/s12-fix/M33c_class_metadata_update_row_form.sql +16 -0
  110. package/tests/mutations/s12-fix/M33d_class_metadata_merge_update.sql +16 -0
  111. package/tests/mutations/s12-fix/M33e_class_metadata_upsert.sql +16 -0
  112. package/tests/mutations/s12-fix/M34_class_api_role_insert.sql +9 -0
  113. package/tests/mutations/s12-fix/M3_lock_after_lookup.sql +83 -0
  114. package/tests/mutations/s12-fix/M4_lock_raw_key.sql +83 -0
  115. package/tests/mutations/s12-fix/M5_no_trim.sql +83 -0
  116. package/tests/mutations/s12-fix/M6_casefold.sql +83 -0
  117. package/tests/mutations/s12-fix/M7_lookup_ignores_actor.sql +82 -0
  118. package/tests/mutations/s12-fix/M8_reuse_reports_false.sql +83 -0
  119. package/tests/mutations/s12-fix/M9_blank_key_admitted.sql +83 -0
  120. package/tests/mutations/s12-fix/run.sh +288 -0
  121. package/tests/seeds/20261124000100_agent_board_step_up_enforcement.sql.pre.sql +47 -0
@@ -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$;
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env bash
2
+ # S-12 mutation receipts for tests/isolation/specs/create_or_find_thread_same_key_race.spec.
3
+ #
4
+ # For every mutation beside this script: copy this package and the REAL isolation runner into a
5
+ # throwaway directory that holds ONLY the S-12 spec and its golden, append the mutation to that
6
+ # copy's operator-hardening fixture (applied after every migration, ON_ERROR_STOP, so a mutation
7
+ # whose self-check raises aborts the template build rather than running unmutated), run the
8
+ # runner at ITER=1, and record WHICH permutations changed against the golden.
9
+ #
10
+ # It is a check, not a log: each mutation declares the permutations it must change (EXPECT
11
+ # below), and the run exits 1 if any mutation changes a different set, including the empty set
12
+ # when it should have changed something. M0 re-creates the shipped body unchanged and must
13
+ # change nothing; that row is what proves the copy-and-append machinery is itself neutral.
14
+ #
15
+ # Connection: DATABASE_URL in the ENVIRONMENT (never argv), naming a CLUSTER on which the role
16
+ # can CREATE DATABASE -- the runner builds and drops its own template and per-spec clones.
17
+ # Usage: DATABASE_URL=... bash run.sh <receipt-path> [mutation.sql ...]
18
+ set -uo pipefail
19
+ HERE="$(cd "$(dirname "$0")" && pwd)"
20
+ PKG="$(cd "$HERE/../../.." && pwd)"
21
+ ROOT="$(cd "$PKG/../.." && pwd)"
22
+ NAME="create_or_find_thread_same_key_race"
23
+ # This harness's EXPECT table is relative to #808's golden, which PINNED THE DEFECT. Migration
24
+ # 20261120000200 fixed the defect and regenerated that golden, so against a tree that carries it
25
+ # every row below would mismatch for a reason that is not the mutation. Refuse rather than report
26
+ # noise. The fixed body's receipts come from tests/mutations/s12-fix/run.sh; this receipt
27
+ # (plans/evidence/s12/mutation-receipt.txt) reproduces from 18354aa8, the #808 head.
28
+ if [ -f "$PKG/migrations/20261120000200_agent_board_thread_key_serialize.sql" ]; then
29
+ echo "REFUSED: pre-fix S-12 harness; this tree carries the fix (20261120000200). Use tests/mutations/s12-fix/run.sh." >&2
30
+ exit 3
31
+ fi
32
+ OUT="${1:?receipt path}"; shift
33
+ [ -n "${DATABASE_URL:-}" ] || { echo "FATAL: DATABASE_URL unset (environment, not argv)" >&2; exit 3; }
34
+ if [ "$#" -gt 0 ]; then MUTS=("$@"); else mapfile -t MUTS < <(find "$HERE" -maxdepth 1 -name 'M*.sql' | sort -V); fi
35
+ [ "${#MUTS[@]}" -gt 0 ] || { echo "FATAL: no mutations found" >&2; exit 3; }
36
+
37
+ # Permutations (1-based, spec order) each mutation must change. Anything else is a failure.
38
+ declare -A EXPECT=(
39
+ [M0_identity]=""
40
+ [M1_serialise_actor_key]="1 2"
41
+ [M1b_serialise_key_only]="1 2 5"
42
+ [M2_unique_index_no_handler]="1 2"
43
+ [M3_no_trim]="2 3"
44
+ [M4_casefold]="4"
45
+ [M5_lookup_ignores_actor]="6"
46
+ [M6_reuse_reports_false]="3"
47
+ )
48
+
49
+ WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT
50
+ mkdir -p "$WORK/scripts" "$WORK/packages"
51
+ cp "$ROOT/scripts/run-agent-board-isolation-tests.sh" "$WORK/scripts/"
52
+ cp -r "$PKG" "$WORK/packages/"
53
+ C="$WORK/packages/$(basename "$PKG")"
54
+ find "$C/tests/isolation/specs" -name '*.spec' ! -name "$NAME.spec" -delete
55
+ find "$C/tests/isolation/expected" -name '*.out' ! -name "$NAME.out" -delete
56
+ find "$C" -maxdepth 1 -name node_modules -prune -exec rm -rf {} +
57
+ [ -d "$C/tests/isolation/output" ] && find "$C/tests/isolation/output" -mindepth 1 -delete
58
+ FIX="$C/tests/fixtures/operator-hardening.sql"
59
+ cp "$FIX" "$WORK/operator-hardening.orig.sql"
60
+ GOLDEN="$C/tests/isolation/expected/$NAME.out"
61
+ DECLARED="$(grep -c '^permutation ' "$C/tests/isolation/specs/$NAME.spec")"
62
+
63
+ # Which permutations differ between the golden and a transcript. Prints "n m ..." (maybe empty).
64
+ changed_perms() {
65
+ python3 -I - "$1" "$2" <<'PY'
66
+ import re, sys
67
+ def perms(p):
68
+ t = open(p).read()
69
+ return [x for x in re.split(r'\n(?=starting permutation: )', t) if x.startswith('starting permutation')]
70
+ a, b = perms(sys.argv[1]), perms(sys.argv[2])
71
+ if len(a) != len(b):
72
+ print(f"COUNT {len(a)}!={len(b)}"); sys.exit(0)
73
+ print(' '.join(str(i) for i, (x, y) in enumerate(zip(a, b), 1) if x != y))
74
+ PY
75
+ }
76
+
77
+ bad=0; ran=0
78
+ {
79
+ echo "# S-12 mutation receipt -- $(date -u +%FT%TZ)"
80
+ echo "# tree $(git -C "$ROOT" rev-parse HEAD)$(git -C "$ROOT" diff --quiet HEAD -- "$PKG" || echo ' + uncommitted changes')"
81
+ echo "# spec $NAME: $DECLARED permutations declared; golden $(grep -c '^starting permutation' "$GOLDEN") permutations"
82
+ for m in "${MUTS[@]}"; do
83
+ name="$(basename "$m" .sql)"
84
+ want="${EXPECT[$name]-UNDECLARED}"
85
+ echo "-- $name: $(head -1 "$m" | sed 's/^-- //')"
86
+ cp "$WORK/operator-hardening.orig.sql" "$FIX"
87
+ { echo; cat "$m"; echo; } >> "$FIX"
88
+ log="$WORK/$name.log"
89
+ (cd "$WORK" && ITER=1 bash scripts/run-agent-board-isolation-tests.sh > "$log" 2>&1); rc=$?
90
+ applied="$(sed -nE 's/^ *applied ([0-9]+) migrations into .*/\1/p' "$log")"
91
+ verdict="$(grep -cE "^ (ok|FAIL) +$NAME" "$log")"
92
+ if [ -z "$applied" ] || [ "$verdict" -ne 1 ]; then
93
+ echo " BROKEN: runner exit $rc, template applied='${applied:-none}', verdicts=$verdict (mutation self-check raised, or the run truncated)"
94
+ grep -E 'FATAL|ERROR|NOT applied' "$log" | head -5 | sed 's/^/ /'
95
+ bad=$((bad+1)); continue
96
+ fi
97
+ ran=$((ran+1))
98
+ tdir="$(sed -n 's/^== per-run transcripts: \(.*\) ==$/\1/p' "$log")"
99
+ if grep -qE "^ ok +$NAME" "$log"; then got=""
100
+ elif [ -f "$tdir/$NAME.out" ]; then got="$(changed_perms "$GOLDEN" "$tdir/$NAME.out")"
101
+ else got="NO-TRANSCRIPT"; fi
102
+ if [ "$got" = "$want" ]; then mark="as declared"; else mark="MISMATCH (declared: '${want}')"; bad=$((bad+1)); fi
103
+ echo " runner exit $rc; migrations $applied; changed permutations: '${got}' -- $mark"
104
+ if [ -n "$got" ] && [ -f "$tdir/$NAME.out" ]; then
105
+ diff "$GOLDEN" "$tdir/$NAME.out" | grep -E '^[<>] .*(ERROR|waiting|[0-9]+\|)' | head -8 | sed 's/^/ /'
106
+ fi
107
+ done
108
+ cp "$WORK/operator-hardening.orig.sql" "$FIX"
109
+ echo "# $ran of ${#MUTS[@]} mutations reached a verdict; $bad problem(s)"
110
+ } | tee "$OUT"
111
+ # The block above runs in a pipeline subshell, so re-derive the verdict from the receipt.
112
+ grep -qE "^# ${#MUTS[@]} of ${#MUTS[@]} mutations reached a verdict; 0 problem\(s\)$" "$OUT"
@@ -0,0 +1,83 @@
1
+ -- CONTROL: re-create the FIXED body unchanged. The spec must stay byte-identical to its golden and the pgTAP files all ok.
2
+ -- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
3
+ -- transaction (pgTAP), never to a shipped migration. Derived from the body in
4
+ -- migrations/20261121000100_agent_board_thread_key_guards.sql.
5
+ create or replace function agent_board.create_or_find_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_idempotency_key text,
9
+ p_thread_type text default 'coordination',
10
+ p_metadata jsonb default '{}'::jsonb
11
+ )
12
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
13
+ language plpgsql
14
+ security definer
15
+ set search_path = agent_board, public
16
+ as $$
17
+ declare
18
+ v_actor_agent_id uuid;
19
+ v_key text := nullif(trim(p_idempotency_key), '');
20
+ v_thread agent_board.threads%rowtype;
21
+ v_isolation text := current_setting('transaction_isolation');
22
+ begin
23
+ -- S12FIX mutation M0_identity
24
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
25
+ if nullif(trim(p_subject), '') is null or v_key is null then
26
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
27
+ end if;
28
+ -- S-12: a non-object would make the stored metadata an array or scalar with no top-level
29
+ -- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
30
+ if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
31
+ raise exception 'metadata must be a JSON object' using errcode = '22023';
32
+ end if;
33
+ -- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
34
+ -- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
35
+ -- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
36
+ -- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
37
+ if v_isolation <> 'read committed' then
38
+ raise exception
39
+ 'create_or_find_thread requires transaction isolation level read committed (got %)',
40
+ v_isolation
41
+ using errcode = '0A000',
42
+ hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
43
+ 'where each statement takes a fresh snapshot. Retry this call in a READ '
44
+ 'COMMITTED transaction.';
45
+ end if;
46
+
47
+ -- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
48
+ -- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
49
+ -- never wait on each other.
50
+ perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
51
+
52
+ select *
53
+ into v_thread
54
+ from agent_board.threads t
55
+ where t.created_by_agent_id = v_actor_agent_id
56
+ and t.metadata ->> 'addressingThreadKey' = v_key
57
+ order by t.created_at desc
58
+ limit 1
59
+ for update;
60
+
61
+ if v_thread.id is not null then
62
+ 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;
63
+ return;
64
+ end if;
65
+
66
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
67
+ values (
68
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
69
+ v_actor_agent_id,
70
+ trim(p_subject),
71
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
72
+ )
73
+ returning * into v_thread;
74
+
75
+ 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;
76
+ end;
77
+ $$;
78
+
79
+ do $chk$ begin
80
+ if position('S12FIX mutation M0_identity' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
81
+ raise exception 'S-12 fix mutation M0_identity NOT applied';
82
+ end if;
83
+ end $chk$;
@@ -0,0 +1,9 @@
1
+ -- Make the function SECURITY INVOKER: the redefinition must keep SECURITY DEFINER.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ alter function agent_board.create_or_find_thread(uuid,text,text,text,jsonb) security invoker;
4
+
5
+ do $chk$ begin
6
+ if (select prosecdef from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure) then
7
+ raise exception 'S-12 fix mutation M10_security_invoker NOT applied';
8
+ end if;
9
+ end $chk$;
@@ -0,0 +1,9 @@
1
+ -- Change the pinned search_path: the redefinition must keep `agent_board, public`.
2
+ -- SCRATCH ONLY: applied by run.sh, never to a shipped migration.
3
+ alter function agent_board.create_or_find_thread(uuid,text,text,text,jsonb) set search_path = agent_board, public, pg_temp;
4
+
5
+ do $chk$ begin
6
+ if (select proconfig from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure) = array['search_path=agent_board, public'] then
7
+ raise exception 'S-12 fix mutation M11_search_path_changed NOT applied';
8
+ end if;
9
+ end $chk$;
@@ -0,0 +1,84 @@
1
+ -- The reuse path also writes a TASK: the thread-only disposition must fail in both replay-class files.
2
+ -- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
3
+ -- transaction (pgTAP), never to a shipped migration. Derived from the body in
4
+ -- migrations/20261121000100_agent_board_thread_key_guards.sql.
5
+ create or replace function agent_board.create_or_find_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_idempotency_key text,
9
+ p_thread_type text default 'coordination',
10
+ p_metadata jsonb default '{}'::jsonb
11
+ )
12
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
13
+ language plpgsql
14
+ security definer
15
+ set search_path = agent_board, public
16
+ as $$
17
+ declare
18
+ v_actor_agent_id uuid;
19
+ v_key text := nullif(trim(p_idempotency_key), '');
20
+ v_thread agent_board.threads%rowtype;
21
+ v_isolation text := current_setting('transaction_isolation');
22
+ begin
23
+ -- S12FIX mutation M12_reuse_writes_task
24
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
25
+ if nullif(trim(p_subject), '') is null or v_key is null then
26
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
27
+ end if;
28
+ -- S-12: a non-object would make the stored metadata an array or scalar with no top-level
29
+ -- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
30
+ if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
31
+ raise exception 'metadata must be a JSON object' using errcode = '22023';
32
+ end if;
33
+ -- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
34
+ -- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
35
+ -- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
36
+ -- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
37
+ if v_isolation <> 'read committed' then
38
+ raise exception
39
+ 'create_or_find_thread requires transaction isolation level read committed (got %)',
40
+ v_isolation
41
+ using errcode = '0A000',
42
+ hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
43
+ 'where each statement takes a fresh snapshot. Retry this call in a READ '
44
+ 'COMMITTED transaction.';
45
+ end if;
46
+
47
+ -- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
48
+ -- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
49
+ -- never wait on each other.
50
+ perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
51
+
52
+ select *
53
+ into v_thread
54
+ from agent_board.threads t
55
+ where t.created_by_agent_id = v_actor_agent_id
56
+ and t.metadata ->> 'addressingThreadKey' = v_key
57
+ order by t.created_at desc
58
+ limit 1
59
+ for update;
60
+
61
+ if v_thread.id is not null then
62
+ insert into agent_board.tasks(thread_id, created_by_agent_id, title) values (v_thread.id, v_actor_agent_id, 'S12FIX side task');
63
+ 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;
64
+ return;
65
+ end if;
66
+
67
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
68
+ values (
69
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
70
+ v_actor_agent_id,
71
+ trim(p_subject),
72
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
73
+ )
74
+ returning * into v_thread;
75
+
76
+ 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;
77
+ end;
78
+ $$;
79
+
80
+ do $chk$ begin
81
+ if position('S12FIX mutation M12_reuse_writes_task' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
82
+ raise exception 'S-12 fix mutation M12_reuse_writes_task NOT applied';
83
+ end if;
84
+ end $chk$;
@@ -0,0 +1,84 @@
1
+ -- The body gains an inbox write that never runs: only the catalog half of the thread-only disposition can see it.
2
+ -- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
3
+ -- transaction (pgTAP), never to a shipped migration. Derived from the body in
4
+ -- migrations/20261121000100_agent_board_thread_key_guards.sql.
5
+ create or replace function agent_board.create_or_find_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_idempotency_key text,
9
+ p_thread_type text default 'coordination',
10
+ p_metadata jsonb default '{}'::jsonb
11
+ )
12
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
13
+ language plpgsql
14
+ security definer
15
+ set search_path = agent_board, public
16
+ as $$
17
+ declare
18
+ v_actor_agent_id uuid;
19
+ v_key text := nullif(trim(p_idempotency_key), '');
20
+ v_thread agent_board.threads%rowtype;
21
+ v_isolation text := current_setting('transaction_isolation');
22
+ begin
23
+ -- S12FIX mutation M13_dead_inbox_write
24
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
25
+ if nullif(trim(p_subject), '') is null or v_key is null then
26
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
27
+ end if;
28
+ -- S-12: a non-object would make the stored metadata an array or scalar with no top-level
29
+ -- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
30
+ if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
31
+ raise exception 'metadata must be a JSON object' using errcode = '22023';
32
+ end if;
33
+ -- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
34
+ -- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
35
+ -- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
36
+ -- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
37
+ if v_isolation <> 'read committed' then
38
+ raise exception
39
+ 'create_or_find_thread requires transaction isolation level read committed (got %)',
40
+ v_isolation
41
+ using errcode = '0A000',
42
+ hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
43
+ 'where each statement takes a fresh snapshot. Retry this call in a READ '
44
+ 'COMMITTED transaction.';
45
+ end if;
46
+
47
+ -- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
48
+ -- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
49
+ -- never wait on each other.
50
+ perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
51
+
52
+ select *
53
+ into v_thread
54
+ from agent_board.threads t
55
+ where t.created_by_agent_id = v_actor_agent_id
56
+ and t.metadata ->> 'addressingThreadKey' = v_key
57
+ order by t.created_at desc
58
+ limit 1
59
+ for update;
60
+
61
+ if v_thread.id is not null then
62
+ if false then update agent_board.inbox_items set subject = subject where false; end if;
63
+ 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;
64
+ return;
65
+ end if;
66
+
67
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
68
+ values (
69
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
70
+ v_actor_agent_id,
71
+ trim(p_subject),
72
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
73
+ )
74
+ returning * into v_thread;
75
+
76
+ 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;
77
+ end;
78
+ $$;
79
+
80
+ do $chk$ begin
81
+ if position('S12FIX mutation M13_dead_inbox_write' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
82
+ raise exception 'S-12 fix mutation M13_dead_inbox_write NOT applied';
83
+ end if;
84
+ end $chk$;
@@ -0,0 +1,78 @@
1
+ -- Drop the non-object metadata refusal: an array/scalar/JSON-null is stored where the lookup cannot find it.
2
+ -- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
3
+ -- transaction (pgTAP), never to a shipped migration. Derived from the body in
4
+ -- migrations/20261121000100_agent_board_thread_key_guards.sql.
5
+ create or replace function agent_board.create_or_find_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_idempotency_key text,
9
+ p_thread_type text default 'coordination',
10
+ p_metadata jsonb default '{}'::jsonb
11
+ )
12
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
13
+ language plpgsql
14
+ security definer
15
+ set search_path = agent_board, public
16
+ as $$
17
+ declare
18
+ v_actor_agent_id uuid;
19
+ v_key text := nullif(trim(p_idempotency_key), '');
20
+ v_thread agent_board.threads%rowtype;
21
+ v_isolation text := current_setting('transaction_isolation');
22
+ begin
23
+ -- S12FIX mutation M14_metadata_unchecked
24
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
25
+ if nullif(trim(p_subject), '') is null or v_key is null then
26
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
27
+ end if;
28
+ -- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
29
+ -- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
30
+ -- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
31
+ -- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
32
+ if v_isolation <> 'read committed' then
33
+ raise exception
34
+ 'create_or_find_thread requires transaction isolation level read committed (got %)',
35
+ v_isolation
36
+ using errcode = '0A000',
37
+ hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
38
+ 'where each statement takes a fresh snapshot. Retry this call in a READ '
39
+ 'COMMITTED transaction.';
40
+ end if;
41
+
42
+ -- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
43
+ -- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
44
+ -- never wait on each other.
45
+ perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
46
+
47
+ select *
48
+ into v_thread
49
+ from agent_board.threads t
50
+ where t.created_by_agent_id = v_actor_agent_id
51
+ and t.metadata ->> 'addressingThreadKey' = v_key
52
+ order by t.created_at desc
53
+ limit 1
54
+ for update;
55
+
56
+ if v_thread.id is not null then
57
+ 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;
58
+ return;
59
+ end if;
60
+
61
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
62
+ values (
63
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
64
+ v_actor_agent_id,
65
+ trim(p_subject),
66
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
67
+ )
68
+ returning * into v_thread;
69
+
70
+ 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;
71
+ end;
72
+ $$;
73
+
74
+ do $chk$ begin
75
+ if position('S12FIX mutation M14_metadata_unchecked' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
76
+ raise exception 'S-12 fix mutation M14_metadata_unchecked NOT applied';
77
+ end if;
78
+ end $chk$;
@@ -0,0 +1,84 @@
1
+ -- Move the metadata refusal AFTER the lock: a refused caller now waits on the lock first.
2
+ -- SCRATCH ONLY: applied by run.sh to a throwaway template (isolation) or inside a rolled-back
3
+ -- transaction (pgTAP), never to a shipped migration. Derived from the body in
4
+ -- migrations/20261121000100_agent_board_thread_key_guards.sql.
5
+ create or replace function agent_board.create_or_find_thread(
6
+ p_created_by_agent_id uuid,
7
+ p_subject text,
8
+ p_idempotency_key text,
9
+ p_thread_type text default 'coordination',
10
+ p_metadata jsonb default '{}'::jsonb
11
+ )
12
+ returns table(thread_id uuid, thread_type text, subject text, status text, last_message_sequence bigint, created_at timestamptz, updated_at timestamptz, reused boolean)
13
+ language plpgsql
14
+ security definer
15
+ set search_path = agent_board, public
16
+ as $$
17
+ declare
18
+ v_actor_agent_id uuid;
19
+ v_key text := nullif(trim(p_idempotency_key), '');
20
+ v_thread agent_board.threads%rowtype;
21
+ v_isolation text := current_setting('transaction_isolation');
22
+ begin
23
+ -- S12FIX mutation M15_metadata_check_after_lock
24
+ v_actor_agent_id := agent_board._resolved_actor_agent_id(p_created_by_agent_id, 'p_created_by_agent_id');
25
+ if nullif(trim(p_subject), '') is null or v_key is null then
26
+ raise exception 'subject and idempotency key are required' using errcode = '22023';
27
+ end if;
28
+ -- S-12 (#815): READ COMMITTED IS A PRECONDITION, NOT A PREFERENCE. The lock below only helps if
29
+ -- the lookup after it sees the winner's COMMITTED row, and that needs a fresh snapshot per
30
+ -- statement. Under repeatable read / serializable the snapshot is fixed, the wait does not
31
+ -- advance it, and this caller would insert a duplicate. Refuse, before the lock and the lookup.
32
+ if v_isolation <> 'read committed' then
33
+ raise exception
34
+ 'create_or_find_thread requires transaction isolation level read committed (got %)',
35
+ v_isolation
36
+ using errcode = '0A000',
37
+ hint = 'The (actor, key) lock only prevents a duplicate thread under READ COMMITTED, '
38
+ 'where each statement takes a fresh snapshot. Retry this call in a READ '
39
+ 'COMMITTED transaction.';
40
+ end if;
41
+
42
+ -- S-12: serialise the lookup-then-insert on (actor, key) so a concurrent caller with the same
43
+ -- pair waits for this transaction and then reuses its row. Scoped by actor: different actors
44
+ -- never wait on each other.
45
+ perform agent_board._lock_idempotency_key('create_or_find_thread:' || v_actor_agent_id::text, v_key);
46
+
47
+ -- S-12: a non-object would make the stored metadata an array or scalar with no top-level
48
+ -- addressingThreadKey, so the lookup below could never find it again. SQL NULL means "none".
49
+ if p_metadata is not null and jsonb_typeof(p_metadata) <> 'object' then
50
+ raise exception 'metadata must be a JSON object' using errcode = '22023';
51
+ end if;
52
+
53
+ select *
54
+ into v_thread
55
+ from agent_board.threads t
56
+ where t.created_by_agent_id = v_actor_agent_id
57
+ and t.metadata ->> 'addressingThreadKey' = v_key
58
+ order by t.created_at desc
59
+ limit 1
60
+ for update;
61
+
62
+ if v_thread.id is not null then
63
+ 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;
64
+ return;
65
+ end if;
66
+
67
+ insert into agent_board.threads(thread_type, created_by_agent_id, subject, metadata)
68
+ values (
69
+ coalesce(nullif(trim(p_thread_type), ''), 'coordination'),
70
+ v_actor_agent_id,
71
+ trim(p_subject),
72
+ coalesce(p_metadata, '{}'::jsonb) || jsonb_build_object('addressingThreadKey', v_key)
73
+ )
74
+ returning * into v_thread;
75
+
76
+ 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;
77
+ end;
78
+ $$;
79
+
80
+ do $chk$ begin
81
+ if position('S12FIX mutation M15_metadata_check_after_lock' in (select prosrc from pg_proc where oid = 'agent_board.create_or_find_thread(uuid,text,text,text,jsonb)'::regprocedure)) = 0 then
82
+ raise exception 'S-12 fix mutation M15_metadata_check_after_lock NOT applied';
83
+ end if;
84
+ end $chk$;