@consiliency/agent-board-schema 1.9.0 → 1.12.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.
@@ -0,0 +1,364 @@
1
+ -- FEDSESSION auto-join: give a freshly provisioned federated human BASELINE board standing.
2
+ --
3
+ -- WHY: a Portal human who signs in for the first time gets a kind='human' fed-agent via
4
+ -- enroll_federated_human, but with NO board_memberships — so the Portal room-ops UI renders
5
+ -- over an empty dashboard and every owner-loop write 403s until they create/join a board.
6
+ -- The self-service path (create_board self-seeds the caller as owner) covers OWNED boards;
7
+ -- this migration adds the complementary default: an OPERATOR-DESIGNATED, per-issuer default
8
+ -- board that fresh fed-humans auto-join at provision time so they land with participation
9
+ -- standing.
10
+ --
11
+ -- MODEL (chosen over per-human manual grants): the default board is designated PER ISSUER on
12
+ -- the identity_providers row (new nullable default_board_id). NULL => no auto-join (the exact
13
+ -- current behavior; this migration is inert until an operator designates a board). The
14
+ -- auto-join is:
15
+ -- * BASELINE role only ('member' — never owner/admin): merely landing in the default board
16
+ -- confers no board authority beyond participation. Owner authority stays with create_board
17
+ -- / grant_board_role.
18
+ -- * FIRST-TIME-ONLY & idempotent: it inserts only when the fed-human has NO membership row
19
+ -- for that board AT ALL (any state). A human who deliberately LEFT the default board is
20
+ -- NOT silently re-joined, and re-provisioning never duplicates a membership. The NOT EXISTS
21
+ -- guard is intentionally BROADER than the live partial unique index
22
+ -- idx_agent_board_board_memberships_active_unique ((board_id, agent_id) WHERE
23
+ -- membership_state in ('joined','subscribed'), 20260505000000): the guard also covers
24
+ -- 'left'/'removed' rows (which the partial index does NOT), which is what gives the
25
+ -- first-time-only / left-respect semantics. That partial index is the CONCURRENCY backstop:
26
+ -- two concurrent re-provisions of the same already-provisioned human can both pass NOT
27
+ -- EXISTS, and the second insert then trips the index — the helper catches that
28
+ -- unique_violation to a no-op (mirroring enroll_federated_human's step-8 agent-insert race
29
+ -- handling) so auto-join stays non-fatal (never duplicates AND never aborts the mint).
30
+ -- * NEVER fatal: it runs only when the designated board EXISTS and is status='active'. A
31
+ -- missing/inactive/mis-designated board silently skips the join so provisioning (and the
32
+ -- Option A session mint) still succeeds. Auto-join is a convenience, never a gate.
33
+ --
34
+ -- SCOPE: additive + forward-only. Does NOT change the enroll_federated_human signature, the
35
+ -- IF-0-FEDSESSION-1 session response, the synthetic-uid/IDLINK derivation, the provider/kind
36
+ -- gates, the PII handling, or the audit. Only NEW: the default_board_id column + the auto-join
37
+ -- block. The /portal-session Edge Function is unchanged (it calls the same RPC). Writes no
38
+ -- agent_board.schema_version (stays 8).
39
+
40
+ -- (A) Per-issuer default board designation. NULL => no auto-join (inert). ON DELETE SET NULL so
41
+ -- retiring the board gracefully disables auto-join instead of blocking the delete.
42
+ alter table agent_board.identity_providers
43
+ add column if not exists default_board_id uuid
44
+ references agent_board.boards(id) on delete set null;
45
+
46
+ comment on column agent_board.identity_providers.default_board_id is
47
+ 'FEDSESSION auto-join target: the board a freshly provisioned federated human is auto-joined '
48
+ 'to (baseline member role) at enroll_federated_human time. NULL disables auto-join for this '
49
+ 'issuer (default). Operator/service_role-only (authenticated has no INSERT/UPDATE on '
50
+ 'identity_providers); the board must exist and be active for the join to fire. OPERATOR '
51
+ 'CAVEAT: member = RLS-gated read AND post, and designation grants that standing to EVERY '
52
+ 'future federated human of this issuer WITHOUT the board owner''s consent (a compromised/'
53
+ 'high-volume approved issuer thus inflates membership, bounded by /portal-session''s '
54
+ 'per-source rate cap). Designate ONLY a board intended as a shared landing/commons for this '
55
+ 'issuer and whose owner you control.';
56
+
57
+ -- (B) Recreate enroll_federated_human with the auto-join block. Body is byte-identical to
58
+ -- 20260728000100 EXCEPT: step (7) sets v_agent := v_existing to unify the return, and a new
59
+ -- step (auto-join) runs before BOTH return paths.
60
+ create or replace function agent_board.enroll_federated_human(
61
+ p_issuer text,
62
+ p_audience text,
63
+ p_subject text,
64
+ p_human_identity_claim jsonb,
65
+ p_display_name text default null
66
+ )
67
+ returns table(
68
+ agent_id uuid,
69
+ auth_user_id uuid,
70
+ handle text,
71
+ kind text,
72
+ status text,
73
+ provider_id text,
74
+ created_at timestamptz
75
+ )
76
+ language plpgsql
77
+ security definer
78
+ set search_path = agent_board, public
79
+ as $$
80
+ declare
81
+ v_provider agent_board.identity_providers%rowtype;
82
+ v_handle text;
83
+ v_uid uuid;
84
+ v_agent agent_board.agents%rowtype;
85
+ v_existing agent_board.agents%rowtype;
86
+ v_email text := nullif(trim(coalesce(p_human_identity_claim->>'email', '')), '');
87
+ v_email_verified boolean;
88
+ begin
89
+ -- Derive the verified-email boolean from the claim (never trust it as board-proven
90
+ -- possession; it only records what the Portal asserted). A non-boolean/absent value
91
+ -- collapses to false.
92
+ begin
93
+ v_email_verified := coalesce((p_human_identity_claim->>'email_verified')::boolean, false);
94
+ exception when others then
95
+ v_email_verified := false;
96
+ end;
97
+
98
+ -- (1) service_role-only (defense in depth on top of the execute-grant revocation).
99
+ if not agent_board._is_service_role() then
100
+ raise exception 'service_role required for enroll_federated_human' using errcode = '42501';
101
+ end if;
102
+
103
+ -- (2) A verified subject + human identity claim are always present from the EF.
104
+ if p_subject is null then
105
+ raise exception 'verified subject is required' using errcode = '22023';
106
+ end if;
107
+ if p_human_identity_claim is null then
108
+ raise exception 'human identity claim is required' using errcode = '22023';
109
+ end if;
110
+
111
+ -- (3) F-11: the synthetic-uid preimage is issuer||'|'||subject. Guard the separator so a
112
+ -- subject cannot inject a '|' to collide two distinct (issuer, subject) pairs onto one
113
+ -- owner uid. ALSO reject an email-shaped subject ('@'): the subject is stored VERBATIM in
114
+ -- agents.metadata.federation.subject, and the agents SELECT RLS is not row-scoped, so an
115
+ -- email `sub` would be a fleet-harvestable PII leak — the exact hazard the email is kept
116
+ -- out of agents.metadata to avoid. The operator's ENROLL_SUBJECT_CLAIM must resolve to an
117
+ -- opaque id; this is the fail-closed backstop, and the pgTAP PII guard asserts BOTH the
118
+ -- '@'-rejection and metadata carrying no '@'.
119
+ if position('|' in p_subject) > 0 then
120
+ raise exception 'verified subject must not contain a pipe separator character'
121
+ using errcode = '22023';
122
+ end if;
123
+ if position('@' in p_subject) > 0 then
124
+ raise exception 'verified subject must be an opaque id, not an email/@-bearing value'
125
+ using errcode = '22023';
126
+ end if;
127
+
128
+ -- (4) provider gate on (issuer, audience): must exist + supported + policy_approved.
129
+ -- Defense in depth atop the EF's signature verify (mirrors 20260719001200:154-181).
130
+ select *
131
+ into v_provider
132
+ from agent_board.identity_providers
133
+ where issuer = p_issuer
134
+ and audience = p_audience
135
+ limit 1;
136
+ if v_provider.id is null then
137
+ raise exception 'issuer/audience not in the identity-provider allowlist' using errcode = '42501';
138
+ end if;
139
+ if not v_provider.supported then
140
+ raise exception 'identity provider is not supported' using errcode = '42501';
141
+ end if;
142
+ if not v_provider.policy_approved then
143
+ raise exception 'identity provider is not policy-approved' using errcode = '42501';
144
+ end if;
145
+
146
+ -- (5) HUMAN-ONLY ceiling. allowed_agent_kinds is permissive on empty/null (the ceiling
147
+ -- is "unset"); reject ONLY when it is non-empty AND does not include 'human'.
148
+ if array_length(v_provider.allowed_agent_kinds, 1) is not null
149
+ and not ('human' = any (v_provider.allowed_agent_kinds)) then
150
+ raise exception 'provider ceiling does not permit the human kind' using errcode = '42501';
151
+ end if;
152
+
153
+ -- (6) SYNTHETIC uid — byte-identical derivation to enroll_federated_identity:198-199
154
+ -- (extensions.digest, since the pinned search_path lacks extensions). Idempotent per
155
+ -- (issuer, sub) so re-provisioning converges to ONE owner auth.users row = the shared
156
+ -- IDLINK uid. Pass issuer/subject VERBATIM (no trim/normalize) to stay byte-identical.
157
+ v_handle := 'fed-human-' || substr(
158
+ encode(extensions.digest(convert_to(p_issuer || '|' || p_subject, 'UTF8'), 'sha256'), 'hex'),
159
+ 1, 32);
160
+ v_uid := agent_board._ensure_agent_auth_user(v_handle);
161
+
162
+ -- (7) Idempotent + no-self-reactivation, keyed on auth_user_id = the synthetic uid (ANY
163
+ -- status). An ACTIVE row -> return it (idempotent). A NON-active row -> fail closed
164
+ -- (42501): a suspended/revoked human must NOT re-mint a session (mirrors
165
+ -- 20260726000100:78-81 and reinforces the F-13 revocation lever). F-5: assert the row is
166
+ -- human before returning it as a human session (a non-human agent on this owner uid must
167
+ -- never yield a "human" provision).
168
+ select *
169
+ into v_existing
170
+ from agent_board.agents a
171
+ where a.auth_user_id = v_uid
172
+ order by (a.status = 'active') desc, a.created_at asc
173
+ limit 1;
174
+ if found then
175
+ if v_existing.status = 'active' then
176
+ if v_existing.kind <> 'human' then
177
+ raise exception 'existing agent on this identity is not human; refusing human session'
178
+ using errcode = '42501';
179
+ end if;
180
+ v_agent := v_existing;
181
+ -- Refresh the verified-email snapshot on re-provision (MERGE, see step 9).
182
+ if v_email is not null then
183
+ update auth.users u
184
+ set raw_user_meta_data = coalesce(u.raw_user_meta_data, '{}'::jsonb)
185
+ || jsonb_build_object(
186
+ 'fedsession_email', v_email,
187
+ 'fedsession_email_verified', v_email_verified,
188
+ 'fedsession_email_provenance', 'portal-claim-unverified')
189
+ where u.id = v_uid;
190
+ end if;
191
+ -- (auto-join before the idempotent return — see step 11 for the rationale).
192
+ perform agent_board._fedsession_autojoin_default_board(v_provider, v_agent.id);
193
+ insert into agent_board.enrollment_federation_audit(
194
+ issuer, audience, subject, human_identity_claim, provider_id,
195
+ agent_id, auth_user_id, requested_scopes, granted_scopes, mode)
196
+ values (
197
+ p_issuer, p_audience, p_subject, p_human_identity_claim, v_provider.id,
198
+ v_agent.id, v_uid, '{}', '{}', 'fedsession');
199
+ return query
200
+ select v_agent.id, v_agent.auth_user_id, v_agent.handle,
201
+ v_agent.kind::text, v_agent.status, v_provider.id, v_agent.created_at;
202
+ return;
203
+ else
204
+ raise exception 'a non-active identity already exists for this human; contact an operator'
205
+ using errcode = '42501';
206
+ end if;
207
+ end if;
208
+
209
+ -- (8) Create the kind='human' agent (self-owned: auth_user_id = owner_user_id = uid).
210
+ -- No agent_signing_keys row (humans hold no key). metadata.federation carries ONLY the
211
+ -- boolean email_verified — never the address (PII / fleet-readable RLS).
212
+ begin
213
+ insert into agent_board.agents(
214
+ agent_type,
215
+ kind,
216
+ handle,
217
+ display_name,
218
+ status,
219
+ auth_user_id,
220
+ owner_user_id,
221
+ metadata
222
+ )
223
+ values (
224
+ 'human',
225
+ 'human'::agent_board.agent_kind,
226
+ v_handle,
227
+ coalesce(nullif(trim(p_display_name), ''), 'Human'),
228
+ 'active',
229
+ v_uid,
230
+ v_uid,
231
+ jsonb_build_object(
232
+ 'federation', jsonb_build_object(
233
+ 'mode', 'fedsession',
234
+ 'issuer', p_issuer,
235
+ 'audience', p_audience,
236
+ 'subject', p_subject,
237
+ 'providerId', v_provider.id,
238
+ 'email_verified', v_email_verified,
239
+ 'enrolledAt', now()
240
+ )
241
+ )
242
+ )
243
+ returning * into v_agent;
244
+ exception when unique_violation then
245
+ -- Concurrent first-provision race on the synthetic uid. F-5: re-select the ACTIVE row
246
+ -- and ASSERT it is human (fail closed otherwise) — never return a non-human agent as a
247
+ -- human session.
248
+ select *
249
+ into v_existing
250
+ from agent_board.agents a
251
+ where a.auth_user_id = v_uid
252
+ and a.status = 'active'
253
+ and a.kind = 'human'
254
+ limit 1;
255
+ if found then
256
+ v_agent := v_existing;
257
+ else
258
+ raise exception 'could not provision federated human (race or non-human on owner uid), please retry'
259
+ using errcode = '40001';
260
+ end if;
261
+ end;
262
+
263
+ -- (9) F-12/F-4: store the verified email ONLY in auth.users.raw_user_meta_data
264
+ -- (service-role-only), MERGED so _ensure_agent_auth_user's {slug, issuer} is preserved,
265
+ -- tagged provenance='portal-claim-unverified'. The definer owner (postgres) owns auth.*.
266
+ if v_email is not null then
267
+ update auth.users u
268
+ set raw_user_meta_data = coalesce(u.raw_user_meta_data, '{}'::jsonb)
269
+ || jsonb_build_object(
270
+ 'fedsession_email', v_email,
271
+ 'fedsession_email_verified', v_email_verified,
272
+ 'fedsession_email_provenance', 'portal-claim-unverified')
273
+ where u.id = v_uid;
274
+ end if;
275
+
276
+ -- (10) FEDSESSION auto-join (new). Baseline default-board membership for a fresh fed-human.
277
+ perform agent_board._fedsession_autojoin_default_board(v_provider, v_agent.id);
278
+
279
+ -- (11) F-6: append the audit row (mode='fedsession'). No requested/granted scopes on the
280
+ -- human path (authority is membership/role, not enrollment scopes) -> empty arrays.
281
+ insert into agent_board.enrollment_federation_audit(
282
+ issuer, audience, subject, human_identity_claim, provider_id,
283
+ agent_id, auth_user_id, requested_scopes, granted_scopes, mode)
284
+ values (
285
+ p_issuer, p_audience, p_subject, p_human_identity_claim, v_provider.id,
286
+ v_agent.id, v_uid, '{}', '{}', 'fedsession');
287
+
288
+ return query
289
+ select v_agent.id, v_agent.auth_user_id, v_agent.handle,
290
+ v_agent.kind::text, v_agent.status, v_provider.id, v_agent.created_at;
291
+ end;
292
+ $$;
293
+
294
+ -- (C) The auto-join helper. SECURITY DEFINER internal (service_role/postgres only — it is
295
+ -- only ever called from enroll_federated_human, itself service_role-gated). Idempotent,
296
+ -- first-time-only, active-board-only, non-fatal. Kept as a helper so BOTH provision paths
297
+ -- (idempotent re-provision + first insert) share one byte-identical implementation.
298
+ create or replace function agent_board._fedsession_autojoin_default_board(
299
+ p_provider agent_board.identity_providers,
300
+ p_agent_id uuid
301
+ )
302
+ returns void
303
+ language plpgsql
304
+ security definer
305
+ set search_path = agent_board, public
306
+ as $$
307
+ begin
308
+ if p_provider.default_board_id is null then
309
+ return;
310
+ end if;
311
+ -- The NOT EXISTS below is FIRST-TIME-ONLY (any existing membership row, incl. 'left'/
312
+ -- 'removed', suppresses the auto-join so a human who deliberately left is not re-joined).
313
+ -- Under concurrency it is NOT sufficient on its own: two concurrent re-provisions of the
314
+ -- same already-provisioned human can both pass NOT EXISTS, and the second insert then trips
315
+ -- the partial unique index idx_agent_board_board_memberships_active_unique. Catch that
316
+ -- unique_violation to a no-op (mirrors enroll_federated_human's step-8 agent-insert race
317
+ -- handling) so auto-join is NEVER fatal to the session mint and never duplicates.
318
+ begin
319
+ insert into agent_board.board_memberships(
320
+ board_id, agent_id, role, membership_state, created_by_agent_id, metadata)
321
+ select p_provider.default_board_id, p_agent_id, 'member', 'joined', p_agent_id,
322
+ jsonb_build_object('source', 'fedsession-autojoin', 'providerId', p_provider.id)
323
+ where exists (
324
+ select 1 from agent_board.boards b
325
+ where b.id = p_provider.default_board_id
326
+ and b.status = 'active')
327
+ and not exists (
328
+ select 1 from agent_board.board_memberships m
329
+ where m.board_id = p_provider.default_board_id
330
+ and m.agent_id = p_agent_id);
331
+ exception when unique_violation then
332
+ null; -- concurrent auto-join won the race; its membership stands. Non-fatal.
333
+ end;
334
+ end;
335
+ $$;
336
+
337
+ comment on function agent_board._fedsession_autojoin_default_board(agent_board.identity_providers, uuid) is
338
+ 'FEDSESSION internal: idempotently auto-join a freshly provisioned federated human to the '
339
+ 'provider''s designated default_board_id (baseline member role, joined). First-time-only '
340
+ '(any existing membership row suppresses it, so a human who left is not re-joined), '
341
+ 'active-board-only, non-fatal. service_role/postgres only (called from enroll_federated_human).';
342
+
343
+ revoke all on function agent_board._fedsession_autojoin_default_board(agent_board.identity_providers, uuid)
344
+ from public, anon, authenticated;
345
+ grant execute on function agent_board._fedsession_autojoin_default_board(agent_board.identity_providers, uuid)
346
+ to service_role;
347
+
348
+ comment on function agent_board.enroll_federated_human(text, text, text, jsonb, text) is
349
+ 'FEDSESSION (Option A): service_role no-PoP federated-HUMAN provisioning. Re-gates the '
350
+ '(issuer,audience) provider allowlist/policy + the human-only ceiling, establishes a '
351
+ 'kind=human self-owned agent (auth_user_id = owner_user_id = a SYNTHETIC uid derived '
352
+ 'byte-identically to enroll_federated_identity per (issuer,sub) = the IDLINK substrate), '
353
+ 'stores the verified email ONLY in auth.users.raw_user_meta_data (provenance '
354
+ 'portal-claim-unverified) with only the boolean email_verified in agents.metadata, '
355
+ 'auto-joins the provider''s designated default board (baseline member; first-time-only, '
356
+ 'non-fatal), and writes a mode=fedsession audit row. No signing key. Idempotent for an '
357
+ 'active identity; a non-active identity fails closed (42501, no self-reactivation) — the '
358
+ 'F-13 revocation lever (suspend the agent -> current_agent_id null -> RLS denies + re-mint '
359
+ 'blocked). service_role-only; never returns or touches a Portal token.';
360
+
361
+ revoke all on function agent_board.enroll_federated_human(text, text, text, jsonb, text)
362
+ from public, anon, authenticated;
363
+ grant execute on function agent_board.enroll_federated_human(text, text, text, jsonb, text)
364
+ to service_role;