/** Inert fixed source SQL. Both captures still execute current bound values. */ import { createHash } from "node:crypto" export const principalBytes = 8 * 1024 * 1024, grantBytes = 2 * 1024 * 1024 export const sourceAliases = ["__ps_exists", "__ps_oversize", "__ps_bytes", "__ps_guard", "__ps_isolation", "__ps_credential", "__ps_credential_current", "__ps_credential_expires", "__ps_credential_revoked", "__ps_owner_until", "__ps_owner_issued", "__ps_cutoff_owner", "__ps_cutoff", "__ps_cutoff_text", "__ps_settings", "__ps_enabled", "__ps_grant_id", "__ps_grant_oversize", "__ps_grant_payload", "__ps_policy_sha256", "__ps_grant_sha256"] as const const utc = (column: string) => `to_char(${column} AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"')` export const principalSourceSql = `/* mcp-principal:coherent-source-v1 */ WITH candidate AS MATERIALIZED ( SELECT c AS source_row,octet_length(to_jsonb(c)::text) AS source_bytes FROM mcp_connections c WHERE c.id=$2::uuid ), eligible AS MATERIALIZED (SELECT (source_row).* FROM candidate WHERE source_bytes<=${principalBytes}) SELECT c.*,flags.source_exists AS __ps_exists,flags.oversize AS __ps_oversize,flags.source_bytes AS __ps_bytes, NOT(COALESCE(to_jsonb(c) ?| ARRAY[${sourceAliases.map(name => "'" + name + "'").join(",")}],false)) AS __ps_guard, current_setting('transaction_isolation') AS __ps_isolation,k.id AS __ps_credential, pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(c.policy::text,'UTF8')),'hex') AS __ps_policy_sha256, (k.id IS NOT NULL AND k.revoked_at IS NULL AND k.expires_at>now()) AS __ps_credential_current, ${utc("k.expires_at")} AS __ps_credential_expires,${utc("k.revoked_at")} AS __ps_credential_revoked, ${utc("c.owner_valid_until")} AS __ps_owner_until,${utc("c.owner_claims_issued_at")} AS __ps_owner_issued, r.owner_id AS __ps_cutoff_owner,r.invalid_before AS __ps_cutoff,${utc("r.invalid_before")} AS __ps_cutoff_text, s.id AS __ps_settings,s.enabled AS __ps_enabled,g.id AS __ps_grant_id,g.oversize AS __ps_grant_oversize,g.payload AS __ps_grant_payload,g.payload_sha256 AS __ps_grant_sha256 FROM (SELECT EXISTS(SELECT 1 FROM candidate) AS source_exists, COALESCE((SELECT source_bytes>${principalBytes} FROM candidate),false) AS oversize,(SELECT source_bytes FROM candidate) AS source_bytes) flags LEFT JOIN eligible c ON true LEFT JOIN mcp_credentials k ON k.id=$1::uuid AND k.connection_id=c.id LEFT JOIN mcp_owner_revocations r ON r.owner_id=c.owner_id LEFT JOIN mcp_settings s ON s.id=1 LEFT JOIN LATERAL (WITH raw_grants AS MATERIALIZED ( SELECT id,payload,octet_length(payload::text) AS payload_bytes FROM mcp_grants WHERE $3::boolean AND connection_id=c.id AND revoked_at IS NULL AND payload->>'credentialId'=$1::text ORDER BY id LIMIT 2) SELECT id,payload_bytes>${grantBytes} AS oversize, CASE WHEN payload_bytes<=${grantBytes} THEN payload ELSE NULL END AS payload, CASE WHEN payload_bytes<=${grantBytes} THEN pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(payload::text,'UTF8')),'hex') ELSE NULL END AS payload_sha256 FROM raw_grants ORDER BY id) g ON true` /** One bounded protocol statement per physical backend; never a result cache. */ export const principalSourceStatementName = "mm_ps_v1_" + createHash("sha256").update(principalSourceSql).digest("base64url")