@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.
@@ -312,6 +312,22 @@
312
312
  {
313
313
  "id": "20260727000100_agent_board_roomuiapi",
314
314
  "sha256": "9c938c40115aa25f464bbb9d177f9f85214fb84f7b37abe5ae9df5b6a2485141"
315
+ },
316
+ {
317
+ "id": "20260728000100_agent_board_enroll_federated_human",
318
+ "sha256": "5f300449c29ec05c2cd3c2cd7eca622ca41e8b32b8856c5bc0695b5750a196e7"
319
+ },
320
+ {
321
+ "id": "20260729000100_agent_board_fedsession_autojoin_default_board",
322
+ "sha256": "6f3a2db6c7349119b3893fe530788236ef5ccf4e6b0f3e21235497b8b7b232c0"
323
+ },
324
+ {
325
+ "id": "20260730000100_agent_board_gatenull_service_role_bypass",
326
+ "sha256": "db8b2a93996c1d61bd34fc58a5e4b3056563c53bb2eb20cbb4a3b0b799a0d91e"
327
+ },
328
+ {
329
+ "id": "20260730000200_agent_board_membstate_authz",
330
+ "sha256": "a4d935614f970a946f27a5925c0166e1d41510a82078e559fb769f4ea39afc2e"
315
331
  }
316
332
  ]
317
333
  }
@@ -0,0 +1,313 @@
1
+ -- FEDSESSION (Option A): service_role no-PoP federated-HUMAN provisioning substrate.
2
+ --
3
+ -- WHAT: enroll_federated_human() is the DB half of the /portal-session Edge Function
4
+ -- minter. A browser human who holds a live Portal OIDC session hits /portal-session; the
5
+ -- EF RS256-verifies the Portal token (aud/exp/sig against the issuer JWKS), then calls
6
+ -- THIS RPC (service_role) to establish a kind='human' agent keyed on (issuer, sub), and
7
+ -- finally self-signs a bounded (<=15 min) board-native access token (the same HS256
8
+ -- mechanism as /session — SESSION_SIGNING_SECRET == the project PostgREST JWT secret).
9
+ --
10
+ -- Option A = ACCESS-ONLY. There is NO GoTrue session and therefore NO refresh_token and
11
+ -- NO revocable auth.sessions row. The browser re-mints from /portal-session on expiry
12
+ -- (it holds a live Portal session throughout). This RPC never returns a token — the EF
13
+ -- mints it — and never touches a Portal token itself.
14
+ --
15
+ -- WHY a new RPC (cannot reuse the existing federation paths):
16
+ -- * enroll_federated_identity (20260719001200) HARD-REQUIRES p_pop_verified=true + a
17
+ -- raw Ed25519 key. A human holds no signing key, so that path cannot apply.
18
+ -- * self_enroll_human (20260726000100) binds the REAL auth.uid() (authenticated-only).
19
+ -- The Portal human is not authenticated to THIS project, so there is no auth.uid().
20
+ -- => a service_role, no-PoP, human-only RPC that mints a SYNTHETIC per-identity uid.
21
+ --
22
+ -- IDENTITY MODEL: the human is its OWN principal — auth_user_id = owner_user_id = a
23
+ -- SYNTHETIC uid derived idempotently from (issuer, sub). This mirrors self_enroll_human's
24
+ -- self-ownership, NOT enroll_federated_identity (where the human is only owner_user_id of
25
+ -- a separate key-identified agent). The synthetic-uid handle uses the SAME byte-exact
26
+ -- derivation as enroll_federated_identity:198-199, so a fed-human here and a federated
27
+ -- IDENTITY enrollment for the SAME (issuer, sub) converge on ONE shared owner uid (the
28
+ -- IDLINK substrate). L-4b (one agent per real auth user) is not violated: this uid is
29
+ -- synthetic, and enroll_federated_identity never puts an agent ON that owner uid.
30
+ --
31
+ -- KIND ENFORCEMENT IS STRUCTURAL: there is NO p_agent_kind argument — this RPC ONLY ever
32
+ -- inserts kind='human'/agent_type='human'. No agent_signing_keys row is created (humans
33
+ -- hold no key). Authority stays membership/role-gated: merely holding a session grants no
34
+ -- board access; current_agent_id() resolves auth.uid() -> the active human agent (with NO
35
+ -- agent_credential_id claim, the null-credential branch), and RLS then gates by
36
+ -- membership/role exactly as for any other principal.
37
+ --
38
+ -- F-13 REVOCATION LEVER (Option A's substitute for a revocable session row): suspend or
39
+ -- revoke the fed-human agent (agents.status <> 'active'). current_agent_id() then resolves
40
+ -- to NULL, so every RLS-gated read/write denies — any outstanding <=15-min token is inert
41
+ -- at the AUTHORITY layer within one token lifetime. The non-active guard below ALSO blocks
42
+ -- re-mint (no self-reactivation), so a suspended human cannot re-provision a fresh session.
43
+ --
44
+ -- F-10 TRUST BOUNDARY (named): Portal `sub`-issuance is the takeover boundary. A
45
+ -- compromised or mis-issued Portal `sub` for a human = full takeover of that human on the
46
+ -- board, INCLUDING ownership of any agent subtree the human owns (this fed-human is the
47
+ -- owner_user_id of it). The board delegates that decision to the Portal by design.
48
+ --
49
+ -- F-14 FLAG: because the fed-human is owner_user_id of its subtree, a FUTURE "act-as-owned"
50
+ -- RPC would turn this bounded session into act-as-fleet. No such RPC exists today; if one
51
+ -- is ever added it MUST NOT be reachable from a fedsession-minted token.
52
+ --
53
+ -- PII (F-12): the verified email is written ONLY to auth.users.raw_user_meta_data
54
+ -- (service-role-only), tagged provenance='portal-claim-unverified' (the board has NOT
55
+ -- independently verified possession — an IDLINK link-time flow MUST re-verify before
56
+ -- trusting it). agents.metadata.federation stores ONLY the boolean email_verified, NEVER
57
+ -- the address: the agents SELECT RLS is not row-scoped, so a stored email would be a
58
+ -- fleet-wide harvestable directory (the LOCALAUTH lesson, 20260726000100:15-22).
59
+ --
60
+ -- Additive, forward-only. Writes no agent_board.schema_version (stays 8).
61
+
62
+ -- F-6: tag each federation-audit row with its provisioning mode. Additive nullable column
63
+ -- (enroll_federated_identity's insert leaves it null; the audit-content pgTAP uses
64
+ -- has_column, not a strict column-set pin, so this is safe). enroll_federated_human sets
65
+ -- mode='fedsession' for accountability — Option A has no revocable session row, so the
66
+ -- audit trail is the durable per-provision record.
67
+ alter table agent_board.enrollment_federation_audit
68
+ add column if not exists mode text;
69
+
70
+ create or replace function agent_board.enroll_federated_human(
71
+ p_issuer text,
72
+ p_audience text,
73
+ p_subject text,
74
+ p_human_identity_claim jsonb,
75
+ p_display_name text default null
76
+ )
77
+ returns table(
78
+ agent_id uuid,
79
+ auth_user_id uuid,
80
+ handle text,
81
+ kind text,
82
+ status text,
83
+ provider_id text,
84
+ created_at timestamptz
85
+ )
86
+ language plpgsql
87
+ security definer
88
+ set search_path = agent_board, public
89
+ as $$
90
+ declare
91
+ v_provider agent_board.identity_providers%rowtype;
92
+ v_handle text;
93
+ v_uid uuid;
94
+ v_agent agent_board.agents%rowtype;
95
+ v_existing agent_board.agents%rowtype;
96
+ v_email text := nullif(trim(coalesce(p_human_identity_claim->>'email', '')), '');
97
+ v_email_verified boolean;
98
+ begin
99
+ -- Derive the verified-email boolean from the claim (never trust it as board-proven
100
+ -- possession; it only records what the Portal asserted). A non-boolean/absent value
101
+ -- collapses to false.
102
+ begin
103
+ v_email_verified := coalesce((p_human_identity_claim->>'email_verified')::boolean, false);
104
+ exception when others then
105
+ v_email_verified := false;
106
+ end;
107
+
108
+ -- (1) service_role-only (defense in depth on top of the execute-grant revocation).
109
+ if not agent_board._is_service_role() then
110
+ raise exception 'service_role required for enroll_federated_human' using errcode = '42501';
111
+ end if;
112
+
113
+ -- (2) A verified subject + human identity claim are always present from the EF.
114
+ if p_subject is null then
115
+ raise exception 'verified subject is required' using errcode = '22023';
116
+ end if;
117
+ if p_human_identity_claim is null then
118
+ raise exception 'human identity claim is required' using errcode = '22023';
119
+ end if;
120
+
121
+ -- (3) F-11: the synthetic-uid preimage is issuer||'|'||subject. Guard the separator so a
122
+ -- subject cannot inject a '|' to collide two distinct (issuer, subject) pairs onto one
123
+ -- owner uid. ALSO reject an email-shaped subject ('@'): the subject is stored VERBATIM in
124
+ -- agents.metadata.federation.subject, and the agents SELECT RLS is not row-scoped, so an
125
+ -- email `sub` would be a fleet-harvestable PII leak — the exact hazard the email is kept
126
+ -- out of agents.metadata to avoid. The operator's ENROLL_SUBJECT_CLAIM must resolve to an
127
+ -- opaque id; this is the fail-closed backstop, and the pgTAP PII guard asserts BOTH the
128
+ -- '@'-rejection and metadata carrying no '@'.
129
+ if position('|' in p_subject) > 0 then
130
+ raise exception 'verified subject must not contain a pipe separator character'
131
+ using errcode = '22023';
132
+ end if;
133
+ if position('@' in p_subject) > 0 then
134
+ raise exception 'verified subject must be an opaque id, not an email/@-bearing value'
135
+ using errcode = '22023';
136
+ end if;
137
+
138
+ -- (4) provider gate on (issuer, audience): must exist + supported + policy_approved.
139
+ -- Defense in depth atop the EF's signature verify (mirrors 20260719001200:154-181).
140
+ select *
141
+ into v_provider
142
+ from agent_board.identity_providers
143
+ where issuer = p_issuer
144
+ and audience = p_audience
145
+ limit 1;
146
+ if v_provider.id is null then
147
+ raise exception 'issuer/audience not in the identity-provider allowlist' using errcode = '42501';
148
+ end if;
149
+ if not v_provider.supported then
150
+ raise exception 'identity provider is not supported' using errcode = '42501';
151
+ end if;
152
+ if not v_provider.policy_approved then
153
+ raise exception 'identity provider is not policy-approved' using errcode = '42501';
154
+ end if;
155
+
156
+ -- (5) HUMAN-ONLY ceiling. allowed_agent_kinds is permissive on empty/null (the ceiling
157
+ -- is "unset"); reject ONLY when it is non-empty AND does not include 'human'.
158
+ if array_length(v_provider.allowed_agent_kinds, 1) is not null
159
+ and not ('human' = any (v_provider.allowed_agent_kinds)) then
160
+ raise exception 'provider ceiling does not permit the human kind' using errcode = '42501';
161
+ end if;
162
+
163
+ -- (6) SYNTHETIC uid — byte-identical derivation to enroll_federated_identity:198-199
164
+ -- (extensions.digest, since the pinned search_path lacks extensions). Idempotent per
165
+ -- (issuer, sub) so re-provisioning converges to ONE owner auth.users row = the shared
166
+ -- IDLINK uid. Pass issuer/subject VERBATIM (no trim/normalize) to stay byte-identical.
167
+ v_handle := 'fed-human-' || substr(
168
+ encode(extensions.digest(convert_to(p_issuer || '|' || p_subject, 'UTF8'), 'sha256'), 'hex'),
169
+ 1, 32);
170
+ v_uid := agent_board._ensure_agent_auth_user(v_handle);
171
+
172
+ -- (7) Idempotent + no-self-reactivation, keyed on auth_user_id = the synthetic uid (ANY
173
+ -- status). An ACTIVE row -> return it (idempotent). A NON-active row -> fail closed
174
+ -- (42501): a suspended/revoked human must NOT re-mint a session (mirrors
175
+ -- 20260726000100:78-81 and reinforces the F-13 revocation lever). F-5: assert the row is
176
+ -- human before returning it as a human session (a non-human agent on this owner uid must
177
+ -- never yield a "human" provision).
178
+ select *
179
+ into v_existing
180
+ from agent_board.agents a
181
+ where a.auth_user_id = v_uid
182
+ order by (a.status = 'active') desc, a.created_at asc
183
+ limit 1;
184
+ if found then
185
+ if v_existing.status = 'active' then
186
+ if v_existing.kind <> 'human' then
187
+ raise exception 'existing agent on this identity is not human; refusing human session'
188
+ using errcode = '42501';
189
+ end if;
190
+ -- Refresh the verified-email snapshot on re-provision (MERGE, see step 9).
191
+ if v_email is not null then
192
+ update auth.users u
193
+ set raw_user_meta_data = coalesce(u.raw_user_meta_data, '{}'::jsonb)
194
+ || jsonb_build_object(
195
+ 'fedsession_email', v_email,
196
+ 'fedsession_email_verified', v_email_verified,
197
+ 'fedsession_email_provenance', 'portal-claim-unverified')
198
+ where u.id = v_uid;
199
+ end if;
200
+ insert into agent_board.enrollment_federation_audit(
201
+ issuer, audience, subject, human_identity_claim, provider_id,
202
+ agent_id, auth_user_id, requested_scopes, granted_scopes, mode)
203
+ values (
204
+ p_issuer, p_audience, p_subject, p_human_identity_claim, v_provider.id,
205
+ v_existing.id, v_uid, '{}', '{}', 'fedsession');
206
+ return query
207
+ select v_existing.id, v_existing.auth_user_id, v_existing.handle,
208
+ v_existing.kind::text, v_existing.status, v_provider.id, v_existing.created_at;
209
+ return;
210
+ else
211
+ raise exception 'a non-active identity already exists for this human; contact an operator'
212
+ using errcode = '42501';
213
+ end if;
214
+ end if;
215
+
216
+ -- (8) Create the kind='human' agent (self-owned: auth_user_id = owner_user_id = uid).
217
+ -- No agent_signing_keys row (humans hold no key). metadata.federation carries ONLY the
218
+ -- boolean email_verified — never the address (PII / fleet-readable RLS).
219
+ begin
220
+ insert into agent_board.agents(
221
+ agent_type,
222
+ kind,
223
+ handle,
224
+ display_name,
225
+ status,
226
+ auth_user_id,
227
+ owner_user_id,
228
+ metadata
229
+ )
230
+ values (
231
+ 'human',
232
+ 'human'::agent_board.agent_kind,
233
+ v_handle,
234
+ coalesce(nullif(trim(p_display_name), ''), 'Human'),
235
+ 'active',
236
+ v_uid,
237
+ v_uid,
238
+ jsonb_build_object(
239
+ 'federation', jsonb_build_object(
240
+ 'mode', 'fedsession',
241
+ 'issuer', p_issuer,
242
+ 'audience', p_audience,
243
+ 'subject', p_subject,
244
+ 'providerId', v_provider.id,
245
+ 'email_verified', v_email_verified,
246
+ 'enrolledAt', now()
247
+ )
248
+ )
249
+ )
250
+ returning * into v_agent;
251
+ exception when unique_violation then
252
+ -- Concurrent first-provision race on the synthetic uid. F-5: re-select the ACTIVE row
253
+ -- and ASSERT it is human (fail closed otherwise) — never return a non-human agent as a
254
+ -- human session.
255
+ select *
256
+ into v_existing
257
+ from agent_board.agents a
258
+ where a.auth_user_id = v_uid
259
+ and a.status = 'active'
260
+ and a.kind = 'human'
261
+ limit 1;
262
+ if found then
263
+ v_agent := v_existing;
264
+ else
265
+ raise exception 'could not provision federated human (race or non-human on owner uid), please retry'
266
+ using errcode = '40001';
267
+ end if;
268
+ end;
269
+
270
+ -- (9) F-12/F-4: store the verified email ONLY in auth.users.raw_user_meta_data
271
+ -- (service-role-only), MERGED so _ensure_agent_auth_user's {slug, issuer} is preserved,
272
+ -- tagged provenance='portal-claim-unverified'. The definer owner (postgres) owns auth.*.
273
+ if v_email is not null then
274
+ update auth.users u
275
+ set raw_user_meta_data = coalesce(u.raw_user_meta_data, '{}'::jsonb)
276
+ || jsonb_build_object(
277
+ 'fedsession_email', v_email,
278
+ 'fedsession_email_verified', v_email_verified,
279
+ 'fedsession_email_provenance', 'portal-claim-unverified')
280
+ where u.id = v_uid;
281
+ end if;
282
+
283
+ -- (10) F-6: append the audit row (mode='fedsession'). No requested/granted scopes on the
284
+ -- human path (authority is membership/role, not enrollment scopes) -> empty arrays.
285
+ insert into agent_board.enrollment_federation_audit(
286
+ issuer, audience, subject, human_identity_claim, provider_id,
287
+ agent_id, auth_user_id, requested_scopes, granted_scopes, mode)
288
+ values (
289
+ p_issuer, p_audience, p_subject, p_human_identity_claim, v_provider.id,
290
+ v_agent.id, v_uid, '{}', '{}', 'fedsession');
291
+
292
+ return query
293
+ select v_agent.id, v_agent.auth_user_id, v_agent.handle,
294
+ v_agent.kind::text, v_agent.status, v_provider.id, v_agent.created_at;
295
+ end;
296
+ $$;
297
+
298
+ comment on function agent_board.enroll_federated_human(text, text, text, jsonb, text) is
299
+ 'FEDSESSION (Option A): service_role no-PoP federated-HUMAN provisioning. Re-gates the '
300
+ '(issuer,audience) provider allowlist/policy + the human-only ceiling, establishes a '
301
+ 'kind=human self-owned agent (auth_user_id = owner_user_id = a SYNTHETIC uid derived '
302
+ 'byte-identically to enroll_federated_identity per (issuer,sub) = the IDLINK substrate), '
303
+ 'stores the verified email ONLY in auth.users.raw_user_meta_data (provenance '
304
+ 'portal-claim-unverified) with only the boolean email_verified in agents.metadata, and '
305
+ 'writes a mode=fedsession audit row. No signing key. Idempotent for an active identity; '
306
+ 'a non-active identity fails closed (42501, no self-reactivation) — the F-13 revocation '
307
+ 'lever (suspend the agent -> current_agent_id null -> RLS denies + re-mint blocked). '
308
+ 'service_role-only; never returns or touches a Portal token.';
309
+
310
+ revoke all on function agent_board.enroll_federated_human(text, text, text, jsonb, text)
311
+ from public, anon, authenticated;
312
+ grant execute on function agent_board.enroll_federated_human(text, text, text, jsonb, text)
313
+ to service_role;