@consiliency/agent-board-schema 1.83.0 → 1.84.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 (36) hide show
  1. package/README.md +5 -0
  2. package/fixtures/already-applied.json +4 -0
  3. package/migrations/20261203000200_agent_board_signed_seat_gates.sql +695 -0
  4. package/package.json +1 -1
  5. package/schema-manifest.json +12 -3
  6. package/src/tooling.test.mjs +4 -2
  7. package/tests/agent_board_954_signed_seat_gates.test.sql +472 -0
  8. package/tests/catalog/function_execute_privileges.json +36 -0
  9. package/tests/catalog/p_metadata_parameters.json +16 -0
  10. package/tests/mutations/954/MA1_resolver_ignores_signer.sql +15 -0
  11. package/tests/mutations/954/MA2_null_actor_resolves_to_signer.sql +22 -0
  12. package/tests/mutations/954/MB1_no_bind.sql +14 -0
  13. package/tests/mutations/954/MC1_context_any_role.sql +15 -0
  14. package/tests/mutations/954/MG1_has_board_access_service_bypass.sql +18 -0
  15. package/tests/mutations/954/MG2_can_send_to_board_service_bypass.sql +18 -0
  16. package/tests/mutations/954/MG3_can_access_thread_service_bypass.sql +18 -0
  17. package/tests/mutations/954/MG4_can_post_to_thread_service_bypass.sql +13 -0
  18. package/tests/mutations/954/MG5_can_access_task_service_bypass.sql +18 -0
  19. package/tests/mutations/954/MG6_can_mutate_task_service_bypass.sql +18 -0
  20. package/tests/mutations/954/MR1_post_board_message_no_release.sql +14 -0
  21. package/tests/mutations/954/MR2_send_direct_message_no_release.sql +14 -0
  22. package/tests/mutations/954/MR3_send_multicast_message_no_release.sql +14 -0
  23. package/tests/mutations/954/MR4_create_tasking_delivery_no_release.sql +14 -0
  24. package/tests/mutations/954/MR5_append_task_event_no_release.sql +14 -0
  25. package/tests/mutations/954/MR6_grant_board_role_no_release.sql +14 -0
  26. package/tests/mutations/954/MR7_set_board_authority_ceiling_no_release.sql +14 -0
  27. package/tests/mutations/954/MW1_post_board_message_unbound.sql +25 -0
  28. package/tests/mutations/954/MW2_send_direct_message_unbound.sql +25 -0
  29. package/tests/mutations/954/MW3_send_multicast_message_unbound.sql +25 -0
  30. package/tests/mutations/954/MW4_create_tasking_delivery_unbound.sql +25 -0
  31. package/tests/mutations/954/MW5_append_task_event_unbound.sql +25 -0
  32. package/tests/mutations/954/MW6_grant_board_role_unbound.sql +25 -0
  33. package/tests/mutations/954/MW7_set_board_authority_ceiling_unbound.sql +25 -0
  34. package/tests/mutations/954/MX1_bind_session_level.sql +13 -0
  35. package/tests/mutations/954/MX2_release_session_level.sql +13 -0
  36. package/tests/mutations/954/run.sh +93 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@consiliency/agent-board-schema",
3
- "version": "1.83.0",
3
+ "version": "1.84.0",
4
4
  "description": "Canonical, immutable, exact-pinnable agent_board schema bundle: ordered SQL migrations, digest + compatibility manifest, and deterministic materialize/plan/check tooling for adopters.",
5
5
  "repository": "https://github.com/Consiliency/message-board.git",
6
6
  "type": "module",
@@ -2,7 +2,7 @@
2
2
  "schema": "agent_board_schema_manifest.v1",
3
3
  "interface": "IF-0-MBPLANE-BUNDLE-1",
4
4
  "package": "@consiliency/agent-board-schema",
5
- "packageVersion": "1.83.0",
5
+ "packageVersion": "1.84.0",
6
6
  "schemaVersion": 8,
7
7
  "minClientVersion": "1.1.0",
8
8
  "source": {
@@ -46,8 +46,8 @@
46
46
  "usage": "upsert (retention/monitoring defaults; on conflict do nothing)"
47
47
  }
48
48
  ],
49
- "migrationCount": 181,
50
- "aggregateSha256": "be7779d1629fd834b24d4074ee24b9e69d5cf2f15c1cb9974237ecba200741f1",
49
+ "migrationCount": 182,
50
+ "aggregateSha256": "88faf4b474a22adcccb5762b739e90885b40417a0a4b6271f6ee445e9e3cd134",
51
51
  "migrations": [
52
52
  {
53
53
  "order": 0,
@@ -1677,6 +1677,15 @@
1677
1677
  "sourceRef": "message-board",
1678
1678
  "bytes": 23101,
1679
1679
  "sha256": "1c096c0ffc2da4717c8f88e4145b57a756eabe8cdbf6a414f897818c27af60f4"
1680
+ },
1681
+ {
1682
+ "order": 181,
1683
+ "id": "20261203000200_agent_board_signed_seat_gates",
1684
+ "filename": "20261203000200_agent_board_signed_seat_gates.sql",
1685
+ "origin": "message-board-forward",
1686
+ "sourceRef": "message-board",
1687
+ "bytes": 42756,
1688
+ "sha256": "2d4b07e538384e46e6b7dfbae282391db27619f24275e0fa024daa90d1a6a1de"
1680
1689
  }
1681
1690
  ],
1682
1691
  "excludedBridge": {
@@ -41,9 +41,9 @@ describe('manifest integrity', () => {
41
41
  expect(manifest.schemaVersion).toBe(8);
42
42
  expect(manifest.minClientVersion).toMatch(/^\d+\.\d+\.\d+$/);
43
43
  });
44
- it('registers the four MBAUTHZ + four MBSENDAUTHZ + four MBSESSION + four MBDELEG + three MBFED + three MBHARDEN + two MBPORTALGATE + two MBPORTAL-PR32 + two ROOMOWN + two ROOMLINK + one ROOMDELEG + one LOCALAUTH + one ROOMUIAPI + one FEDSESSION + one FEDSESSION-AUTOJOIN + one GATENULL + one MEMBSTATE + one AGENTSDIR + one LEAVEIDEM + one ROLERESET + one MBROWID + one MBHYGIENE + one MBCONSENT-A + one MBCONSENT-B(a) + one MBCONSENT-B(i) + one MBCONSENT-B(ii) + one MBCONSENT-B(iii) + one MBHARDEN-#97 + one MBHARDEN-#118 + one INVITEPOLICY + one OBSGATE + one OBSGATE-2 + one OBSGATE-3 + one OBSGATE-4 + one FLEETMACHINERLS + one RESOLVENOOP + one ROOMDELEG-LANEB + one CLAIMKEY + two MINTHARDEN + one FLEETMACHINEHIJACK + one CLAIMGATE-SHADOW + one CLAIMGATE-ENFORCE + one FLEETMACHINEREACTIVATION + one DELEGCAPCEILING + one FLEETMACHINERECLAIM + one DELEGSOLEWRITER + one FLEETDOMAINRECLAIM + five ATOMICFLEETONBOARDING (onboarding + ceiling-lock + revocation-evidence + fedsession-gate + fedsession-lock-order) + one OUTBOXWAKE + one ENROLLLOCKORDER + one FEDPROVIDERABSENCE + one WAKERECOVERY + one CONSENTRECONCILE + one ENROLLRESUMPTION + one SIGNEDAUTHORITY + one SIGNERLIFECYCLE + one LEDGERAUTHORITY + one CEILINGLOCKCOMMENT + one LEDGERBOARDCREATE + one RECEIPTSUBJECTGATE + one TASKTHREADLOCKORDER + one CLAIMSUBJECTGATE + one FEDSESSIONIDENTITYGATE + one FEDIDENTITYSLUG + one INVITENODOWNGRADE + one SIGNEDWRITEFLAG + one SIGNEDWRITEMODEL + one RTCREDCLAIM + one ORIGINPOSTURE + one BOARDCLAIMPOLICY + one HEARTBEATPRESERVE + one LIVENESSDENOM + one OWNERLESSRENEWTTL + one OWNERLESSQUOTACENSUS + one FLEETDOMAINNODOWNGRADE + one OWNERLESSQUOTACONVERGE + one ADVISORYREVERTRESET + one CLAIMPOLICYRACE + one CLAIMPOLICYLOCKGRANT + one SESSIONCHALLENGESUPERSEDE + one ROOMCONTRACT + one CALLBACKCONSUMER + one ENROLLTOKENSINGLEUSE + one ROOMCONTRACT-ACTIVEFIRST + one ONBOARD + one HOSTED + one HOSTED-FOLLOWUPS + one OWNPOST + one SIDSCHEMA + one SIDOWNER + one GRANTTARGETROLE (#674) + one LASTOWNER (Consiliency/message-board#675) + one SESSIONPROVENANCE (Consiliency/message-board#665) + one REQUESTREMOVED (Consiliency/message-board#680) + one RLSCORRELATION (Consiliency/message-board#589) + one RESOLUTIONAUTHORITY (Consiliency/message-board#668) + one THREADASSOCIATION checkpoint (#667) + one SIDSERVER-MEASUREDMINT (Consiliency/message-board#653) + one REPLYDELIVERY (#661) + one DELIVERYWRITEREXEC (#717 F1) + one REPLAYSCOPE (#717 F2) + one MESSAGEREPLAYSCOPE (#717 F2b) + one SENDOUTCOME (#670) + one STEPUP-PR-A (#669) + one THREADKEYSERIALIZE (S-12) + one THREADKEYGUARDS (S-12 #815/#816) + one STEPUP-PR-B (#669) + one REMOVALSTATE (#906) + one REPLYTO (#655) + one ARCHIVEBOARD (#918) + one ARCHIVEBOARD-READS (#918 PR-2) forward migrations (migrationCount = prior 113 + 5 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 181)', () => {
44
+ it('registers the four MBAUTHZ + four MBSENDAUTHZ + four MBSESSION + four MBDELEG + three MBFED + three MBHARDEN + two MBPORTALGATE + two MBPORTAL-PR32 + two ROOMOWN + two ROOMLINK + one ROOMDELEG + one LOCALAUTH + one ROOMUIAPI + one FEDSESSION + one FEDSESSION-AUTOJOIN + one GATENULL + one MEMBSTATE + one AGENTSDIR + one LEAVEIDEM + one ROLERESET + one MBROWID + one MBHYGIENE + one MBCONSENT-A + one MBCONSENT-B(a) + one MBCONSENT-B(i) + one MBCONSENT-B(ii) + one MBCONSENT-B(iii) + one MBHARDEN-#97 + one MBHARDEN-#118 + one INVITEPOLICY + one OBSGATE + one OBSGATE-2 + one OBSGATE-3 + one OBSGATE-4 + one FLEETMACHINERLS + one RESOLVENOOP + one ROOMDELEG-LANEB + one CLAIMKEY + two MINTHARDEN + one FLEETMACHINEHIJACK + one CLAIMGATE-SHADOW + one CLAIMGATE-ENFORCE + one FLEETMACHINEREACTIVATION + one DELEGCAPCEILING + one FLEETMACHINERECLAIM + one DELEGSOLEWRITER + one FLEETDOMAINRECLAIM + five ATOMICFLEETONBOARDING (onboarding + ceiling-lock + revocation-evidence + fedsession-gate + fedsession-lock-order) + one OUTBOXWAKE + one ENROLLLOCKORDER + one FEDPROVIDERABSENCE + one WAKERECOVERY + one CONSENTRECONCILE + one ENROLLRESUMPTION + one SIGNEDAUTHORITY + one SIGNERLIFECYCLE + one LEDGERAUTHORITY + one CEILINGLOCKCOMMENT + one LEDGERBOARDCREATE + one RECEIPTSUBJECTGATE + one TASKTHREADLOCKORDER + one CLAIMSUBJECTGATE + one FEDSESSIONIDENTITYGATE + one FEDIDENTITYSLUG + one INVITENODOWNGRADE + one SIGNEDWRITEFLAG + one SIGNEDWRITEMODEL + one RTCREDCLAIM + one ORIGINPOSTURE + one BOARDCLAIMPOLICY + one HEARTBEATPRESERVE + one LIVENESSDENOM + one OWNERLESSRENEWTTL + one OWNERLESSQUOTACENSUS + one FLEETDOMAINNODOWNGRADE + one OWNERLESSQUOTACONVERGE + one ADVISORYREVERTRESET + one CLAIMPOLICYRACE + one CLAIMPOLICYLOCKGRANT + one SESSIONCHALLENGESUPERSEDE + one ROOMCONTRACT + one CALLBACKCONSUMER + one ENROLLTOKENSINGLEUSE + one ROOMCONTRACT-ACTIVEFIRST + one ONBOARD + one HOSTED + one HOSTED-FOLLOWUPS + one OWNPOST + one SIDSCHEMA + one SIDOWNER + one GRANTTARGETROLE (#674) + one LASTOWNER (Consiliency/message-board#675) + one SESSIONPROVENANCE (Consiliency/message-board#665) + one REQUESTREMOVED (Consiliency/message-board#680) + one RLSCORRELATION (Consiliency/message-board#589) + one RESOLUTIONAUTHORITY (Consiliency/message-board#668) + one THREADASSOCIATION checkpoint (#667) + one SIDSERVER-MEASUREDMINT (Consiliency/message-board#653) + one REPLYDELIVERY (#661) + one DELIVERYWRITEREXEC (#717 F1) + one REPLAYSCOPE (#717 F2) + one MESSAGEREPLAYSCOPE (#717 F2b) + one SENDOUTCOME (#670) + one STEPUP-PR-A (#669) + one THREADKEYSERIALIZE (S-12) + one THREADKEYGUARDS (S-12 #815/#816) + one STEPUP-PR-B (#669) + one REMOVALSTATE (#906) + one REPLYTO (#655) + one ARCHIVEBOARD (#918) + one ARCHIVEBOARD-READS (#918 PR-2) + one SIGNEDSEATGATES (#954) forward migrations (migrationCount = prior 113 + 5 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 182)', () => {
45
45
  expect(manifest.migrationCount).toBe(manifest.migrations.length);
46
- expect(manifest.migrationCount).toBe(181);
46
+ expect(manifest.migrationCount).toBe(182);
47
47
  // MBSESSION switched enforcement on (scoped-send re-grant): the client-visible
48
48
  // schema version bumped to 8. MBDELEG tightens delegation and MBFED adds only
49
49
  // enrollment-only additive surface (identity_providers registry, enrollment audit,
@@ -391,6 +391,8 @@ describe('manifest integrity', () => {
391
391
  '20261202000100_agent_board_archive_board.sql',
392
392
  // #918 PR-2: archived boards leave lists, inbox and ops counts; board_status.
393
393
  '20261203000100_agent_board_archive_board_reads.sql',
394
+ // #954: signed_* writes are checked against the signer's own board seat and thread access.
395
+ '20261203000200_agent_board_signed_seat_gates.sql',
394
396
  ]) {
395
397
  expect(names.has(f)).toBe(true);
396
398
  }
@@ -0,0 +1,472 @@
1
+ -- #954: a signed write is checked against the SIGNER's board seat and thread access.
2
+ --
3
+ -- Before 20261203000200 every seat gate short-circuited on _is_service_role(), and the signed_*
4
+ -- wrappers run as service_role, so an agent with a signing key and no seat on board X could write
5
+ -- into X through five wrappers (and move its own empty thread onto X). The fix binds the signer
6
+ -- inside _signed_write_actor_id and makes every gate evaluate it; each wrapper releases it before
7
+ -- returning.
8
+ --
9
+ -- What this file pins, and where its populations come from:
10
+ -- W every signed_* wrapper IN pg_proc refuses an unseated signer (42501, with the gate's own
11
+ -- message), accepts a seated one, records evidence only for the seated call, and has released
12
+ -- the signer when it returns. A wrapper with no case row here FAILS AND NAMES ITSELF, and the
13
+ -- canary pins the count, so a new wrapper cannot pass by being absent.
14
+ -- T the thread-move variant and the descriptor/recipient variants of the affected wrappers.
15
+ -- G each of the six gates, called directly under a bound signer: false for the unseated agent,
16
+ -- true for the seated one (one assertion per gate per side, so a mutation of ONE gate is seen
17
+ -- even where another gate would have refused the wrapper call first).
18
+ -- D the service_role callers with no signer (Edge Functions, Portal server-side calls) keep the
19
+ -- bypass, including after a signed wrapper ran in the same transaction.
20
+ -- C structural, catalog-derived over the call closure of every signed_* wrapper and every
21
+ -- agent_board trigger function: every function there that branches on _is_service_role() is
22
+ -- classified, nothing is classified that is no longer there, the reached _current_agent_*
23
+ -- gates all route through the signer-aware helpers, and every wrapper binds and releases.
24
+ -- E the positive claims of the classified service_role branches that are not signer-aware.
25
+ --
26
+ -- Allow-side checks are lives_ok + a separate state read (AGENTS.md rule 3 / #113).
27
+
28
+ create extension if not exists pgtap with schema extensions;
29
+ set search_path = extensions, public, agent_board;
30
+
31
+ begin;
32
+ select plan(75);
33
+
34
+ -- =============================================================================
35
+ -- Fixtures (as postgres)
36
+ -- =============================================================================
37
+ -- O owner of X (signing key) S member of X (signing key) N owner of Y only (signing key)
38
+ -- R member of X (recipient) V observer on X
39
+ create temp table ids(k text primary key, v uuid not null);
40
+ insert into ids values
41
+ ('O', gen_random_uuid()), ('S', gen_random_uuid()), ('N', gen_random_uuid()), ('R', gen_random_uuid()),
42
+ ('V', gen_random_uuid()), ('X', gen_random_uuid()), ('Y', gen_random_uuid()),
43
+ ('TX', gen_random_uuid()), ('TE', gen_random_uuid()), ('TES', gen_random_uuid()), ('TK', gen_random_uuid());
44
+ create function pg_temp.id(text) returns uuid language sql stable as $$ select v from ids where k = $1 $$;
45
+
46
+ insert into agent_board.agents(id, agent_type, handle, display_name) values
47
+ (pg_temp.id('O'), 'agent', 'seat954-owner', 'SEAT954 O'),
48
+ (pg_temp.id('S'), 'agent', 'seat954-seated', 'SEAT954 S'),
49
+ (pg_temp.id('N'), 'agent', 'seat954-unseated', 'SEAT954 N'),
50
+ (pg_temp.id('R'), 'agent', 'seat954-recip', 'SEAT954 R'),
51
+ (pg_temp.id('V'), 'agent', 'seat954-observer', 'SEAT954 V');
52
+
53
+ insert into agent_board.boards(id, slug, name, created_by_agent_id) values
54
+ (pg_temp.id('X'), 'seat954-x', 'SEAT954 X', pg_temp.id('O')),
55
+ (pg_temp.id('Y'), 'seat954-y', 'SEAT954 Y', pg_temp.id('N'));
56
+
57
+ insert into agent_board.board_memberships(board_id, agent_id, membership_state, role, created_by_agent_id) values
58
+ (pg_temp.id('X'), pg_temp.id('O'), 'joined', 'owner', pg_temp.id('O')),
59
+ (pg_temp.id('X'), pg_temp.id('S'), 'joined', 'member', pg_temp.id('O')),
60
+ (pg_temp.id('X'), pg_temp.id('R'), 'joined', 'member', pg_temp.id('O')),
61
+ (pg_temp.id('X'), pg_temp.id('V'), 'joined', 'observer', pg_temp.id('O')),
62
+ (pg_temp.id('Y'), pg_temp.id('N'), 'joined', 'owner', pg_temp.id('N'));
63
+
64
+ insert into agent_board.threads(id, created_by_agent_id, subject, board_id) values
65
+ (pg_temp.id('TX'), pg_temp.id('O'), 'SEAT954 X thread', pg_temp.id('X'));
66
+ insert into agent_board.threads(id, created_by_agent_id, subject) values
67
+ (pg_temp.id('TE'), pg_temp.id('N'), 'SEAT954 N empty thread'),
68
+ (pg_temp.id('TES'), pg_temp.id('S'), 'SEAT954 S empty thread');
69
+ insert into agent_board.tasks(id, thread_id, created_by_agent_id, title) values
70
+ (pg_temp.id('TK'), pg_temp.id('TX'), pg_temp.id('O'), 'SEAT954 X task');
71
+
72
+ -- O's board post on TX, through the unsigned RPC with no signer bound (the server-side shape): it
73
+ -- fans out to X's members, which is what gives S (and V) access to TX on ANY path. N has none.
74
+ select agent_board.post_board_message(pg_temp.id('TX'), pg_temp.id('O'), pg_temp.id('X'), 'SEAT954 opener', 'opens TX');
75
+
76
+ create temp table keys(k text primary key, v uuid not null);
77
+ with ins as (
78
+ insert into agent_board.agent_signing_keys(agent_id, key_id, key_version, public_key, public_key_format, fingerprint, status, proof_state)
79
+ select pg_temp.id(k), 'primary', 1, public.test_pubkey('seat954-' || k), 'raw_ed25519_base64url',
80
+ public.test_key_fingerprint('seat954-' || k), 'active', 'proven'
81
+ from unnest(array['O','S','N']) k
82
+ returning agent_id, id
83
+ )
84
+ insert into keys select i.k, ins.id from ins join ids i on i.v = ins.agent_id;
85
+
86
+ -- The gateway decision as Portal's gateway builds it for a VERIFIED envelope.
87
+ create function pg_temp.decision(p_who text, p_nonce text) returns jsonb language sql stable as $$
88
+ select jsonb_build_object(
89
+ 'verification_status', 'verified', 'failure_class', null,
90
+ 'agent_id', pg_temp.id(p_who), 'signing_key_id', (select v from keys where k = p_who),
91
+ 'key_id', 'primary', 'key_version', 1, 'algorithm', 'Ed25519',
92
+ 'key_fingerprint', public.test_key_fingerprint('seat954-' || p_who),
93
+ 'canonicalization_version', 'agent-provenance-jcs.v1', 'schema_version', 1,
94
+ 'payload_digest', 'sha256:' || md5(p_nonce) || md5(p_nonce),
95
+ 'signature_digest', 'sha256:' || repeat('e', 64),
96
+ 'signed_at', now(), 'verified_at', now(), 'nonce', p_nonce, 'idempotency_key', null,
97
+ 'external_evidence_ref', null, 'enforcement_mode', 'require_for_agent_writes', 'redacted', true)
98
+ $$;
99
+ create function pg_temp.call(p_wrapper text, p_payload jsonb, p_who text, p_nonce text) returns text language sql stable as $$
100
+ select format('select * from agent_board.%I(%L::jsonb, %L::jsonb)', p_wrapper, p_payload, pg_temp.decision(p_who, p_nonce))
101
+ $$;
102
+
103
+ -- One row per wrapper: the unseated call (by N) with the gate message it must hit, and the seated
104
+ -- call (by S for the sends, by owner O for the two authority wrappers). Nonces name the row, so the
105
+ -- evidence read below is per call.
106
+ create temp table cases(wrapper text primary key, allowed text not null, u_sql text not null, u_msg text not null, a_sql text not null);
107
+ insert into cases values
108
+ ('signed_post_board_message', 'S',
109
+ pg_temp.call('signed_post_board_message', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_author_agent_id', pg_temp.id('N'),
110
+ 'p_board_id', pg_temp.id('X'), 'p_subject', 'SEAT954 pbm N', 'p_body', 'unseated'), 'N', 'signed_post_board_message-U'),
111
+ 'thread not accessible to current agent',
112
+ pg_temp.call('signed_post_board_message', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_author_agent_id', pg_temp.id('S'),
113
+ 'p_board_id', pg_temp.id('X'), 'p_subject', 'SEAT954 pbm S', 'p_body', 'seated', 'p_idempotency_key', 'seat954-pbm-S'), 'S', 'signed_post_board_message-A')),
114
+ ('signed_send_direct_message', 'S',
115
+ pg_temp.call('signed_send_direct_message', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_author_agent_id', pg_temp.id('N'),
116
+ 'p_recipient_agent_id', pg_temp.id('R'), 'p_subject', 'SEAT954 sdm N', 'p_body', 'unseated'), 'N', 'signed_send_direct_message-U'),
117
+ 'thread not accessible to current agent',
118
+ pg_temp.call('signed_send_direct_message', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_author_agent_id', pg_temp.id('S'),
119
+ 'p_recipient_agent_id', pg_temp.id('R'), 'p_subject', 'SEAT954 sdm S', 'p_body', 'seated'), 'S', 'signed_send_direct_message-A')),
120
+ ('signed_send_multicast_message', 'S',
121
+ pg_temp.call('signed_send_multicast_message', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_author_agent_id', pg_temp.id('N'),
122
+ 'p_recipient_agent_ids', jsonb_build_array(pg_temp.id('R')), 'p_subject', 'SEAT954 smm N', 'p_body', 'unseated'), 'N', 'signed_send_multicast_message-U'),
123
+ 'thread not accessible to current agent',
124
+ pg_temp.call('signed_send_multicast_message', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_author_agent_id', pg_temp.id('S'),
125
+ 'p_recipient_agent_ids', jsonb_build_array(pg_temp.id('R')), 'p_subject', 'SEAT954 smm S', 'p_body', 'seated'), 'S', 'signed_send_multicast_message-A')),
126
+ ('signed_create_tasking_delivery', 'S',
127
+ pg_temp.call('signed_create_tasking_delivery', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_created_by_agent_id', pg_temp.id('N'),
128
+ 'p_board_id', pg_temp.id('X'), 'p_title', 'SEAT954 ctd N', 'p_details', 'unseated'), 'N', 'signed_create_tasking_delivery-U'),
129
+ 'thread not accessible to current agent',
130
+ pg_temp.call('signed_create_tasking_delivery', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_created_by_agent_id', pg_temp.id('S'),
131
+ 'p_board_id', pg_temp.id('X'), 'p_title', 'SEAT954 ctd S', 'p_details', 'seated'), 'S', 'signed_create_tasking_delivery-A')),
132
+ ('signed_append_task_event', 'S',
133
+ pg_temp.call('signed_append_task_event', jsonb_build_object('p_task_id', pg_temp.id('TK'), 'p_actor_agent_id', pg_temp.id('N'),
134
+ 'p_event_type', 'commented', 'p_payload', jsonb_build_object('seat954', 'N')), 'N', 'signed_append_task_event-U'),
135
+ 'task not accessible to current agent',
136
+ pg_temp.call('signed_append_task_event', jsonb_build_object('p_task_id', pg_temp.id('TK'), 'p_actor_agent_id', pg_temp.id('S'),
137
+ 'p_event_type', 'commented', 'p_payload', jsonb_build_object('seat954', 'S')), 'S', 'signed_append_task_event-A')),
138
+ ('signed_grant_board_role', 'O',
139
+ pg_temp.call('signed_grant_board_role', jsonb_build_object('p_board_id', pg_temp.id('X'), 'p_agent_id', pg_temp.id('R'),
140
+ 'p_grantee_role', 'observer', 'p_grantor_agent_id', pg_temp.id('N')), 'N', 'signed_grant_board_role-U'),
141
+ 'grant exceeds grantor role or board authority ceiling',
142
+ pg_temp.call('signed_grant_board_role', jsonb_build_object('p_board_id', pg_temp.id('X'), 'p_agent_id', pg_temp.id('R'),
143
+ 'p_grantee_role', 'observer', 'p_grantor_agent_id', pg_temp.id('O')), 'O', 'signed_grant_board_role-A')),
144
+ ('signed_set_board_authority_ceiling', 'O',
145
+ pg_temp.call('signed_set_board_authority_ceiling', jsonb_build_object('p_board_id', pg_temp.id('X'),
146
+ 'p_max_grantable_role', 'observer', 'p_actor_agent_id', pg_temp.id('N')), 'N', 'signed_set_board_authority_ceiling-U'),
147
+ 'only a board owner may set the board authority ceiling',
148
+ pg_temp.call('signed_set_board_authority_ceiling', jsonb_build_object('p_board_id', pg_temp.id('X'),
149
+ 'p_max_grantable_role', 'admin', 'p_actor_agent_id', pg_temp.id('O')), 'O', 'signed_set_board_authority_ceiling-A'));
150
+
151
+ -- The population: every signed_* wrapper in pg_proc, matched to its case row (or not).
152
+ create temp table wrappers as
153
+ select p.proname::text as wrapper, p.oid::regprocedure::text as sig, c.u_sql, c.u_msg, c.a_sql, c.allowed
154
+ from pg_proc p
155
+ left join cases c on c.wrapper = p.proname::text
156
+ where p.pronamespace = 'agent_board'::regnamespace and p.proname like 'signed\_%';
157
+
158
+ -- The variants, built now so the service_role session reads only granted temp tables.
159
+ create temp table variants(k text primary key, sql text not null);
160
+ insert into variants values
161
+ ('T1', pg_temp.call('signed_post_board_message', jsonb_build_object('p_thread_id', pg_temp.id('TE'), 'p_author_agent_id', pg_temp.id('N'),
162
+ 'p_board_id', pg_temp.id('X'), 'p_subject', 'SEAT954 move N', 'p_body', 'own empty thread onto X'), 'N', 'T1')),
163
+ ('T3', pg_temp.call('signed_post_board_message', jsonb_build_object('p_thread_id', pg_temp.id('TES'), 'p_author_agent_id', pg_temp.id('S'),
164
+ 'p_board_id', pg_temp.id('X'), 'p_subject', 'SEAT954 move S', 'p_body', 'own empty thread onto X'), 'S', 'T3')),
165
+ ('V1', pg_temp.call('signed_send_multicast_message', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_author_agent_id', pg_temp.id('N'),
166
+ 'p_recipient_descriptors', jsonb_build_array(jsonb_build_object('kind', 'agent_id', 'agentId', pg_temp.id('R'))),
167
+ 'p_subject', 'SEAT954 smm-desc N', 'p_body', 'unseated'), 'N', 'V1')),
168
+ ('V2', pg_temp.call('signed_create_tasking_delivery', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_created_by_agent_id', pg_temp.id('N'),
169
+ 'p_board_slug', 'seat954-x', 'p_recipient_descriptors', jsonb_build_array(jsonb_build_object('kind', 'agent_id', 'agentId', pg_temp.id('R'))),
170
+ 'p_title', 'SEAT954 ctd-desc N', 'p_details', 'unseated'), 'N', 'V2')),
171
+ ('V3', pg_temp.call('signed_create_tasking_delivery', jsonb_build_object('p_thread_id', pg_temp.id('TX'), 'p_created_by_agent_id', pg_temp.id('N'),
172
+ 'p_recipient_agent_ids', jsonb_build_array(pg_temp.id('R')), 'p_title', 'SEAT954 ctd-recip N', 'p_details', 'unseated'), 'N', 'V3')),
173
+ ('D1', format('select * from agent_board.post_board_message(%L::uuid, %L::uuid, %L::uuid, %L, %L)',
174
+ pg_temp.id('TX'), pg_temp.id('N'), pg_temp.id('X'), 'SEAT954 server-side N', 'no signer bound')),
175
+ ('D3', format('select * from agent_board.create_tasking_delivery(%L::uuid, %L::uuid, %L, %L, null::uuid[], %L::uuid)',
176
+ pg_temp.id('TX'), pg_temp.id('N'), 'SEAT954 server-side task N', 'no signer bound', pg_temp.id('X')));
177
+ grant select on cases, wrappers, variants to service_role;
178
+
179
+ -- The class driver: one TAP line per (wrapper, leg), in order. A wrapper with no case row emits a
180
+ -- named failure for each leg it cannot run.
181
+ create function pg_temp.wrapper_legs() returns setof text language plpgsql as $$
182
+ declare r record;
183
+ begin
184
+ for r in select * from wrappers order by sig loop
185
+ if r.u_sql is null then
186
+ return next fail(r.sig || ': no unseated-signer case in agent_board_954_signed_seat_gates.test.sql -- add one');
187
+ return next fail(r.sig || ': no seated-signer case -- add one');
188
+ return next fail(r.sig || ': context-release leg not run');
189
+ continue;
190
+ end if;
191
+ return next throws_ok(r.u_sql, '42501', r.u_msg, 'W-U ' || r.wrapper || ': an unseated signer is refused 42501 by the gate');
192
+ return next lives_ok(r.a_sql, 'W-A ' || r.wrapper || ': a seated signer (' || r.allowed || ') is accepted');
193
+ return next is(nullif(current_setting('agent_board.signed_write_actor', true), ''), null,
194
+ 'W-R ' || r.wrapper || ': the signer context is released when the wrapper returns');
195
+ end loop;
196
+ end $$;
197
+
198
+ -- =============================================================================
199
+ -- P. Preconditions
200
+ -- =============================================================================
201
+ select is((select count(*)::int from agent_board.board_memberships where board_id = pg_temp.id('X') and agent_id = pg_temp.id('N')), 0,
202
+ 'P1 N has no membership row on X in any state');
203
+ select is((select string_agg(a.handle || ':' || bm.role, ',' order by a.handle) from agent_board.board_memberships bm
204
+ join agent_board.agents a on a.id = bm.agent_id where bm.board_id = pg_temp.id('X') and bm.membership_state = 'joined'),
205
+ 'seat954-observer:observer,seat954-owner:owner,seat954-recip:member,seat954-seated:member',
206
+ 'P2 X is seated by O (owner), S and R (member), V (observer)');
207
+ select is(agent_board._agent_can_access_thread(pg_temp.id('TX'), pg_temp.id('S')), true, 'P3 S reaches TX through O''s board post');
208
+ select is(agent_board._agent_can_access_thread(pg_temp.id('TX'), pg_temp.id('N')), false, 'P4 N does not reach TX');
209
+
210
+ -- =============================================================================
211
+ -- G. The gates one by one, under a bound signer (postgres session, service_role claims)
212
+ -- =============================================================================
213
+ create function pg_temp.bind(p_who text) returns uuid language sql as $$
214
+ select agent_board._signed_write_actor_id('{}'::jsonb, jsonb_build_object('agent_id', pg_temp.id(p_who)), 'p_actor_agent_id')
215
+ $$;
216
+ -- Resolution as text, or the SQLSTATE it raised: a bare select of a raising call would abort the
217
+ -- file instead of failing one assertion (#113).
218
+ create function pg_temp.resolve(p_supplied uuid) returns text language plpgsql as $$
219
+ begin
220
+ return agent_board._resolved_actor_agent_id(p_supplied, 'p_author_agent_id')::text;
221
+ exception when others then
222
+ return 'SQLSTATE ' || sqlstate;
223
+ end $$;
224
+ select set_config('request.jwt.claims', '{"role":"service_role","iss":"supabase"}', true);
225
+
226
+ select pg_temp.bind('N');
227
+ select is(agent_board._current_agent_has_board_access(pg_temp.id('X')), false, 'G1n _current_agent_has_board_access: unseated signer false');
228
+ select is(agent_board._current_agent_can_send_to_board(pg_temp.id('X')), false, 'G2n _current_agent_can_send_to_board: unseated signer false');
229
+ select is(agent_board._current_agent_can_access_thread(pg_temp.id('TX')), false, 'G3n _current_agent_can_access_thread: unseated signer false');
230
+ select is(agent_board._current_agent_can_post_to_thread(pg_temp.id('TX')), false, 'G4n _current_agent_can_post_to_thread: unseated signer false');
231
+ select is(agent_board._current_agent_can_access_task(pg_temp.id('TK')), false, 'G5n _current_agent_can_access_task: unseated signer false');
232
+ select is(agent_board._current_agent_can_mutate_task(pg_temp.id('TK')), false, 'G6n _current_agent_can_mutate_task: unseated signer false');
233
+
234
+ select pg_temp.bind('S');
235
+ select is(agent_board._current_agent_has_board_access(pg_temp.id('X')), true, 'G1s _current_agent_has_board_access: seated signer true');
236
+ select is(agent_board._current_agent_can_send_to_board(pg_temp.id('X')), true, 'G2s _current_agent_can_send_to_board: seated signer true');
237
+ select is(agent_board._current_agent_can_access_thread(pg_temp.id('TX')), true, 'G3s _current_agent_can_access_thread: seated signer true');
238
+ select is(agent_board._current_agent_can_post_to_thread(pg_temp.id('TX')), true, 'G4s _current_agent_can_post_to_thread: seated signer true');
239
+ select is(agent_board._current_agent_can_access_task(pg_temp.id('TK')), true, 'G5s _current_agent_can_access_task: seated signer true');
240
+ select is(agent_board._current_agent_can_mutate_task(pg_temp.id('TK')), true, 'G6s _current_agent_can_mutate_task: seated signer true');
241
+
242
+ select pg_temp.bind('V');
243
+ select is(agent_board._current_agent_has_board_access(pg_temp.id('X')), true, 'G7 an observer signer has board access');
244
+ select is(agent_board._current_agent_can_send_to_board(pg_temp.id('X')), false, 'G8 an observer signer may not send to the board');
245
+
246
+ -- Actor resolution under a bound signer: only the signer may act, and it must be named.
247
+ select pg_temp.bind('S');
248
+ select throws_ok(format('select agent_board._resolved_actor_agent_id(%L::uuid, %L)', pg_temp.id('N'), 'p_author_agent_id'),
249
+ '42501', 'p_author_agent_id must match the signed write actor',
250
+ 'G9 _resolved_actor_agent_id: a supplied actor other than the bound signer is refused');
251
+ select is(pg_temp.resolve(null), 'SQLSTATE 22023',
252
+ 'G10 _resolved_actor_agent_id: no supplied actor is refused 22023 under a bound signer (the signer is never assumed)');
253
+ -- A context left set (a wrapper that forgot to release) must only narrow a later unsigned call:
254
+ -- an unsigned post with no author must not be written as the stale signer (#956 review, finding 3).
255
+ select throws_ok(format('select * from agent_board.post_board_message(%L::uuid, null::uuid, %L::uuid, %L, %L)',
256
+ pg_temp.id('TX'), pg_temp.id('X'), 'SEAT954 stale', 'no author named'),
257
+ '22023', 'p_author_agent_id is required for service_role callers',
258
+ 'G10b a stale signer context does not author an unsigned post that names no author');
259
+
260
+ -- The binding is TRANSACTION-local: a session-level set_config would outlive the transaction and
261
+ -- evaluate every later call on a pooled backend as the last signer. One transaction cannot observe
262
+ -- that, so the third argument of both writers is pinned (#956 review, finding 2; mutant MX1/MX2).
263
+ select ok(agent_board_test.stripped_body('agent_board._signed_write_actor_id(jsonb,jsonb,text)'::regprocedure)
264
+ ~ 'set_config\(\s*,\s*v_actor::text\s*,\s*true\s*\)',
265
+ 'L1 _signed_write_actor_id binds the signer with set_config(..., true): transaction-local');
266
+ select ok(agent_board_test.stripped_body('agent_board._release_signed_write_actor()'::regprocedure)
267
+ ~ 'set_config\(\s*,\s*,\s*true\s*\)',
268
+ 'L2 _release_signed_write_actor clears it with set_config(..., true): transaction-local');
269
+
270
+ -- Released: the bypass is back for a service_role caller with no signer. N (unseated) is bound
271
+ -- first, so a release that cleared nothing would leave G11 false.
272
+ select pg_temp.bind('N');
273
+ select agent_board._release_signed_write_actor();
274
+ select is(
275
+ agent_board._current_agent_has_board_access(pg_temp.id('X')) and agent_board._current_agent_can_send_to_board(pg_temp.id('X'))
276
+ and agent_board._current_agent_can_access_thread(pg_temp.id('TX')) and agent_board._current_agent_can_post_to_thread(pg_temp.id('TX'))
277
+ and agent_board._current_agent_can_access_task(pg_temp.id('TK')) and agent_board._current_agent_can_mutate_task(pg_temp.id('TK')),
278
+ true,
279
+ 'G11 released: all six gates short-circuit for service_role with no signer, as before');
280
+ select throws_ok(format('select agent_board._resolved_actor_agent_id(null::uuid, %L)', 'p_author_agent_id'),
281
+ '22023', 'p_author_agent_id is required for service_role callers',
282
+ 'G12 released: _resolved_actor_agent_id requires a supplied actor from service_role, as before');
283
+ select is(pg_temp.resolve(pg_temp.id('N')), pg_temp.id('N')::text,
284
+ 'G13 released: _resolved_actor_agent_id returns the supplied actor for service_role, as before');
285
+
286
+ -- The context is honoured only under service_role: in an authenticated session it is ignored.
287
+ select set_config('request.jwt.claims', json_build_object('role', 'authenticated', 'sub', gen_random_uuid())::text, true);
288
+ select set_config('agent_board.signed_write_actor', pg_temp.id('S')::text, true);
289
+ select is(agent_board._signed_write_actor_context(), null::uuid,
290
+ 'G14 an authenticated session reads no signer context, whatever the setting holds');
291
+ select is(agent_board._current_agent_has_board_access(pg_temp.id('X')), false,
292
+ 'G15 an authenticated session with no agent is not lent the seated agent named in the setting');
293
+ select set_config('agent_board.signed_write_actor', '', true);
294
+ select set_config('request.jwt.claims', '{"role":"service_role","iss":"supabase"}', true);
295
+
296
+ -- =============================================================================
297
+ -- W. Every signed_* wrapper, from pg_proc (as the gateway: PostgREST >= 12, service_role key)
298
+ -- =============================================================================
299
+ select is((select count(*)::int from wrappers), 7,
300
+ 'W0 canary: 7 signed_* wrappers in pg_proc (a new one must also get a case row, or W-U fails naming it)');
301
+
302
+ select set_config('request.jwt.claims', '{"role":"service_role","iss":"supabase"}', true);
303
+ set local role service_role;
304
+
305
+ select * from pg_temp.wrapper_legs();
306
+
307
+ -- T/V: the thread-move variant and the variant branches.
308
+ select throws_ok((select sql from variants where k = 'T1'), '42501', 'board not accessible to current agent',
309
+ 'T1 an unseated signer cannot post its own empty thread onto X (it would move the thread)');
310
+ select lives_ok((select sql from variants where k = 'T3'), 'T3 a seated signer can (the legitimate move still works)');
311
+ select throws_ok((select sql from variants where k = 'V1'), '42501', 'thread not accessible to current agent',
312
+ 'V1 signed_send_multicast_message by descriptors: unseated signer refused');
313
+ select throws_ok((select sql from variants where k = 'V2'), '42501', 'thread not accessible to current agent',
314
+ 'V2 signed_create_tasking_delivery by board slug + descriptors: unseated signer refused');
315
+ select throws_ok((select sql from variants where k = 'V3'), '42501', 'thread not accessible to current agent',
316
+ 'V3 signed_create_tasking_delivery by recipient ids, no board: unseated signer refused');
317
+
318
+ -- D: a service_role caller with no signer -- Portal's server-side post, an Edge Function -- keeps
319
+ -- the bypass, in the same transaction the signed wrappers just ran in.
320
+ select lives_ok((select sql from variants where k = 'D1'),
321
+ 'D1 unsigned post_board_message as service_role, no signer bound: unseated author still accepted (unchanged)');
322
+ select lives_ok((select sql from variants where k = 'D3'),
323
+ 'D3 unsigned create_tasking_delivery as service_role, no signer bound: still accepted (unchanged)');
324
+
325
+ reset role;
326
+
327
+ -- State, as postgres.
328
+ select is(
329
+ (select string_agg(w.wrapper || '=' || (select count(*) from agent_board.signature_evidence se
330
+ where se.agent_id = pg_temp.id('N') and se.nonce = w.wrapper || '-U'), ',' order by w.wrapper)
331
+ from wrappers w),
332
+ (select string_agg(w.wrapper || '=0', ',' order by w.wrapper) from wrappers w),
333
+ 'W-SU no wrapper recorded evidence for the unseated signer''s call');
334
+ select is(
335
+ (select string_agg(w.wrapper || '=' || (select count(*) from agent_board.signature_evidence se
336
+ where se.agent_id = pg_temp.id(w.allowed) and se.nonce = w.wrapper || '-A'
337
+ and se.operation = substr(w.wrapper, 8)), ',' order by w.wrapper)
338
+ from wrappers w where w.allowed is not null),
339
+ (select string_agg(w.wrapper || '=1', ',' order by w.wrapper) from wrappers w where w.allowed is not null),
340
+ 'W-SA every wrapper recorded exactly one evidence row for the seated signer''s call');
341
+
342
+ select is(
343
+ (select (select count(*) from agent_board.messages m where m.author_agent_id = pg_temp.id('N') and m.metadata ? 'signed_write')
344
+ + (select count(*) from agent_board.tasks t where t.created_by_agent_id = pg_temp.id('N') and t.title <> 'SEAT954 server-side task N')
345
+ + (select count(*) from agent_board.task_events e where e.actor_agent_id = pg_temp.id('N') and e.task_id = pg_temp.id('TK'))
346
+ + (select count(*) from agent_board.inbox_items i join agent_board.messages m on m.id = i.source_message_id
347
+ where m.author_agent_id = pg_temp.id('N') and m.metadata ? 'signed_write'))::int,
348
+ 0,
349
+ 'X1 the unseated signer wrote no signed message, task, task event or inbox item anywhere');
350
+ select is(
351
+ (select string_agg(m.metadata ->> 'rpcName', ',' order by m.metadata ->> 'rpcName') from agent_board.messages m
352
+ where m.author_agent_id = pg_temp.id('S') and m.thread_id = pg_temp.id('TX') and m.metadata ? 'signed_write'),
353
+ 'post_board_message,send_direct_message,send_multicast_message',
354
+ 'X2 the seated signer''s three signed messages landed on TX');
355
+ select is(
356
+ (select count(*)::int from agent_board.tasks t where t.created_by_agent_id = pg_temp.id('S') and t.thread_id = pg_temp.id('TX'))
357
+ + (select count(*)::int from agent_board.task_events e where e.actor_agent_id = pg_temp.id('S') and e.task_id = pg_temp.id('TK')),
358
+ 2,
359
+ 'X3 the seated signer''s signed task and task event landed');
360
+ select is((select board_id from agent_board.threads where id = pg_temp.id('TE')), null::uuid,
361
+ 'T2 N''s own thread was NOT moved onto X');
362
+ select is((select board_id from agent_board.threads where id = pg_temp.id('TES')), pg_temp.id('X'),
363
+ 'T4 S''s own thread was moved onto X');
364
+ select is((select count(*)::int from agent_board.messages m where m.author_agent_id = pg_temp.id('N') and m.thread_id = pg_temp.id('TX')
365
+ and m.metadata ->> 'subject' = 'SEAT954 server-side N'), 1,
366
+ 'D2 the server-side post by the unseated author landed (service_role bypass unchanged)');
367
+ select is((select count(*)::int from agent_board.tasks t where t.created_by_agent_id = pg_temp.id('N') and t.thread_id = pg_temp.id('TX')
368
+ and t.title = 'SEAT954 server-side task N'), 1,
369
+ 'D4 the server-side tasking delivery landed (service_role bypass unchanged)');
370
+
371
+ -- =============================================================================
372
+ -- C. Structural, from the catalog
373
+ -- =============================================================================
374
+ -- regprocedure output is schema-qualified only when agent_board is off the search_path.
375
+ set local search_path = extensions, public;
376
+ create temp table closure as select * from agent_board_test.fn_call_closure();
377
+
378
+ -- Roots: every signed_* wrapper, and every agent_board trigger function (a write in the closure
379
+ -- fires them, and a call graph does not see that edge).
380
+ create temp table population as
381
+ with roots as (
382
+ select p.oid from pg_proc p where p.pronamespace = 'agent_board'::regnamespace and p.proname like 'signed\_%'
383
+ union
384
+ select t.tgfoid from pg_trigger t join pg_class c on c.oid = t.tgrelid
385
+ where c.relnamespace = 'agent_board'::regnamespace and not t.tgisinternal
386
+ )
387
+ select distinct c.reached_oid as oid, c.reached_oid::regprocedure::text as sig, agent_board_test.stripped_body(c.reached_oid) as body
388
+ from closure c join roots r on r.oid = c.root_oid;
389
+
390
+ -- Every function in the population that branches on _is_service_role(), with what that branch is.
391
+ -- signer signer-aware: reads the bound signer itself
392
+ -- telemetry the branch records send_authz_telemetry; access is decided by the gates (E2)
393
+ -- no-receipt the branch records nothing (E1)
394
+ -- step-up human step-up freshness, not a seat; reached only through grant_board_role and
395
+ -- set_board_authority_ceiling, whose own owner/grantor gates refuse the unseated
396
+ -- signer (W-U for both)
397
+ -- row-guard a trigger that refuses or passes a row write; it grants no access and reads no seat
398
+ create temp table classified(sig text primary key, kind text not null);
399
+ insert into classified values
400
+ ('agent_board._signed_write_actor_context()', 'signer'),
401
+ ('agent_board._seat_gate_bypass()', 'signer'),
402
+ ('agent_board._seat_gate_agent_id()', 'signer'),
403
+ ('agent_board._resolved_actor_agent_id(uuid,text)', 'signer'),
404
+ ('agent_board.post_board_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid)', 'telemetry'),
405
+ ('agent_board.send_direct_message(uuid,uuid,uuid,text,text,text,text,text,jsonb,uuid)', 'telemetry'),
406
+ ('agent_board.send_multicast_message(uuid,uuid,uuid[],text,text,text,text,text,jsonb,uuid)', 'telemetry'),
407
+ ('agent_board.create_tasking_delivery(uuid,uuid,text,text,uuid[],uuid,text,text,text,jsonb)', 'telemetry'),
408
+ ('agent_board._record_send_authz_telemetry(text,text,uuid,uuid,text,text,text,jsonb)', 'telemetry'),
409
+ ('agent_board._record_send_outcome(text,uuid,text,uuid)', 'no-receipt'),
410
+ ('agent_board._require_human_step_up(text)', 'step-up'),
411
+ ('agent_board._archived_board_guard()', 'row-guard'),
412
+ ('agent_board._protect_federation_contract()', 'row-guard');
413
+
414
+ select is((select count(*)::int from population where sig like 'agent_board.signed\_%'), 7,
415
+ 'C0 canary: the population is drawn from pg_proc and holds all 7 wrappers');
416
+ select is(
417
+ (select string_agg(p.sig, '; ' order by p.sig) from population p
418
+ where p.body ~ agent_board_test.call_re('_is_service_role')
419
+ and not exists (select 1 from classified c where c.sig = p.sig)),
420
+ null,
421
+ 'C1 every function reachable from a signed_* wrapper (or a trigger) that branches on _is_service_role() is classified');
422
+ select is(
423
+ (select string_agg(c.sig, '; ' order by c.sig) from classified c
424
+ where not exists (select 1 from population p where p.sig = c.sig and p.body ~ agent_board_test.call_re('_is_service_role'))),
425
+ null,
426
+ 'C2 nothing is classified that is no longer reachable or no longer branches on _is_service_role()');
427
+ select is(
428
+ (select string_agg(p.sig, '; ' order by p.sig) from population p join classified c on c.sig = p.sig
429
+ where c.kind = 'signer' and p.sig <> 'agent_board._signed_write_actor_context()'
430
+ and p.body !~ agent_board_test.call_re('_signed_write_actor_context')),
431
+ null,
432
+ 'C3 every signer-kind function reads _signed_write_actor_context()');
433
+ select is(
434
+ (select string_agg(p.sig, ', ' order by p.sig) from population p
435
+ where p.sig like 'agent\_board.\_current\_agent\_%'),
436
+ 'agent_board._current_agent_can_access_task(uuid), agent_board._current_agent_can_access_thread(uuid), agent_board._current_agent_can_mutate_task(uuid), agent_board._current_agent_can_post_to_thread(uuid), agent_board._current_agent_can_send_to_board(uuid), agent_board._current_agent_has_board_access(uuid)',
437
+ 'C4 canary: the six seat gates are reached from the signed wrappers');
438
+ select is(
439
+ (select string_agg(p.sig, '; ' order by p.sig) from population p
440
+ where p.sig like 'agent\_board.\_current\_agent\_%'
441
+ and (p.body !~ agent_board_test.call_re('_seat_gate_bypass')
442
+ or p.body ~ agent_board_test.call_re('_is_service_role')
443
+ or p.body ~ agent_board_test.call_re('current_agent_id'))),
444
+ null,
445
+ 'C5 every reached _current_agent_* gate bypasses only through _seat_gate_bypass() and reads no session subject directly');
446
+ select is(
447
+ (select string_agg(p.sig, '; ' order by p.sig) from population p
448
+ where p.sig like 'agent\_board.signed\_%'
449
+ and (p.body !~ agent_board_test.call_re('_signed_write_actor_id')
450
+ or p.body !~ agent_board_test.call_re('_release_signed_write_actor'))),
451
+ null,
452
+ 'C6 every signed_* wrapper binds its signer (_signed_write_actor_id) and releases it (_release_signed_write_actor)');
453
+
454
+ -- =============================================================================
455
+ -- E. The classified branches' positive claims
456
+ -- =============================================================================
457
+ select is(
458
+ (select count(*)::int from agent_board.send_outcomes so
459
+ where so.author_agent_id = pg_temp.id('S') and so.idempotency_key = 'seat954-pbm-S'),
460
+ 0,
461
+ 'E1 no-receipt: the seated signer''s KEYED signed post minted no send_outcomes row');
462
+ select is(
463
+ (select string_agg(x.label, ',' order by x.label)
464
+ from (select t.operation || ':' || case t.actor_agent_id when pg_temp.id('N') then 'N' else 'S' end as label
465
+ from agent_board.send_authz_telemetry t
466
+ where t.kind = 'service_role_bypass' and t.actor_agent_id in (pg_temp.id('S'), pg_temp.id('N'))
467
+ and t.operation in ('send_direct_message', 'send_multicast_message', 'create_tasking_delivery')) x),
468
+ 'create_tasking_delivery:N,create_tasking_delivery:S,send_direct_message:S,send_multicast_message:S',
469
+ 'E2 telemetry: the service_role branch records one row per write that passed the gates (S''s signed sends, N''s unsigned D3) and none for a refused one');
470
+
471
+ select * from finish();
472
+ rollback;
@@ -811,6 +811,15 @@
811
811
  "authenticated": false
812
812
  }
813
813
  },
814
+ {
815
+ "name": "_release_signed_write_actor",
816
+ "signature": "_release_signed_write_actor()",
817
+ "executable_by": {
818
+ "anon": false,
819
+ "service_role": false,
820
+ "authenticated": false
821
+ }
822
+ },
814
823
  {
815
824
  "name": "_require_human_step_up",
816
825
  "signature": "_require_human_step_up(p_operation text)",
@@ -883,6 +892,24 @@
883
892
  "authenticated": true
884
893
  }
885
894
  },
895
+ {
896
+ "name": "_seat_gate_agent_id",
897
+ "signature": "_seat_gate_agent_id()",
898
+ "executable_by": {
899
+ "anon": false,
900
+ "service_role": false,
901
+ "authenticated": false
902
+ }
903
+ },
904
+ {
905
+ "name": "_seat_gate_bypass",
906
+ "signature": "_seat_gate_bypass()",
907
+ "executable_by": {
908
+ "anon": false,
909
+ "service_role": false,
910
+ "authenticated": false
911
+ }
912
+ },
886
913
  {
887
914
  "name": "_send_fleet_scope_mode",
888
915
  "signature": "_send_fleet_scope_mode(p_scope_key text)",
@@ -991,6 +1018,15 @@
991
1018
  "authenticated": false
992
1019
  }
993
1020
  },
1021
+ {
1022
+ "name": "_signed_write_actor_context",
1023
+ "signature": "_signed_write_actor_context()",
1024
+ "executable_by": {
1025
+ "anon": false,
1026
+ "service_role": false,
1027
+ "authenticated": false
1028
+ }
1029
+ },
994
1030
  {
995
1031
  "name": "_signed_write_actor_id",
996
1032
  "signature": "_signed_write_actor_id(p_payload jsonb, p_gateway_decision jsonb, p_actor_field text)",