Files
Matt Rossman 4bb36b944f feat(studio): let High Compliance projects opt-in to Assistant data access (#50548)
Orgs with the HIPAA add-on had the Assistant's opt-in level forced to
`disabled` on any project marked High Compliance, regardless of what the
org picked in its AI settings. The restriction predated our AI provider
BAAs. The consequence is those users see the Assistant failing to answer
questions about their data w/ no clear path how to fix it, even though
the LLM provider supports this use case.

This PR removes these Assistant restrictions on the server and client so
those projects honor the org's chosen level. Braintrust conversation
tracing is unchanged and still blocked for these projects, see [this
test
case](https://github.com/supabase/supabase/blob/b9800ccf16/apps/studio/lib/ai/braintrust-logger.test.ts#L16-L20).
See
[comments](https://linear.app/supabase/issue/AI-1153/allow-hipaa-orgs-to-opt-in-to-assistant-data-access-for-high#comment-485a0d46)
for legal approval and conditions.

The client-side changes enable features like "Debug with AI" on SQL
query failures, “Generate/Rename with AI” for snippet titles, and
generated Assistant chat titles for these customers.

The AI opt-in copy now adds a reminder to obtain consent from data
subjects, linking the [shared responsibility
model](https://supabase.com/docs/guides/deployment/shared-responsibility-model)
based also on [this
comment](https://linear.app/supabase/issue/AI-1153/allow-hipaa-orgs-to-opt-in-to-assistant-data-access-for-high#comment-f81ee610).

<img width="400" alt="CleanShot 2026-09-17 at 5 01 02 PM@2x"
src="https://github.com/user-attachments/assets/d02123f2-3e32-4d83-9f98-7d15e59222ef"
/>

To test with a HIPAA-enabled project in staging, you can use this [Plan
Change
[Staging]](https://app.hex.tech/supabase/app/Plan-Change-Staging-032BD32jo1EaisCS85qunf/latest)
Hex to add the HIPAA add-on. Once the add-on is present, you can turn on
High Compliance from a project's settings. Also in org settings, crank
up the Assistant data opt-in level and verify the Assistant is able to
answer questions about the project's data.

My results testing with opt-in level "Schema, Logs & Database Data":

| High compliance setting | Data opt-in working |
|--------|--------|
| <img width="1302" height="422" alt="CleanShot 2026-09-17 at 5 03 36
PM@2x"
src="https://github.com/user-attachments/assets/c416371b-2eb8-49df-9c07-6d8eababb443"
/> | <img width="1566" height="1516" alt="CleanShot 2026-09-17 at 5 05
14 PM@2x"
src="https://github.com/user-attachments/assets/39624355-7f8f-46ce-9f08-a8acfb9da830"
/> |

Closes AI-1153


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## New Features

- AI-assisted query renaming, snippet title generation, debugging, and
tools now follow organization AI opt-in settings rather than project
HIPAA status.
- Debugging assistance and AI actions remain available for eligible
users without additional HIPAA-based blocking.
- AI metadata warnings consistently show standard opt-in messaging and
permission settings.
- AI settings remind users to obtain consent before entering personal
data and link to shared responsibility guidance.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-09-18 08:21:50 -04:00

264 lines
8.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { getAIDetails } from './ai-details'
vi.mock('@/data/organizations/organizations-query', () => ({
getOrganizations: vi.fn(),
}))
vi.mock('@/data/projects/project-detail-query', () => ({
getProjectDetail: vi.fn(),
}))
vi.mock('@/data/subscriptions/org-subscription-query', () => ({
getOrgSubscription: vi.fn(),
}))
vi.mock('@/data/config/project-settings-v2-query', () => ({
getProjectSettings: vi.fn(),
}))
vi.mock('@/hooks/misc/useOrgOptedIntoAi', () => ({
getAiOptInLevel: vi.fn(),
}))
vi.mock('@/components/interfaces/Billing/Subscription/Subscription.utils', () => ({
subscriptionHasHipaaAddon: vi.fn(),
}))
vi.mock('@/data/entitlements/entitlements-query', () => ({
checkEntitlement: vi.fn(),
}))
const AUTH = 'Bearer token'
const HEADERS = { 'Content-Type': 'application/json', Authorization: AUTH }
const ORG_SLUG = 'test-org'
const PROJECT_REF = 'test-project'
describe('getAIDetails', () => {
let mockGetOrganizations: ReturnType<typeof vi.fn>
let mockGetOrgSubscription: ReturnType<typeof vi.fn>
let mockGetProjectDetail: ReturnType<typeof vi.fn>
let mockGetProjectSettings: ReturnType<typeof vi.fn>
let mockGetAiOptInLevel: ReturnType<typeof vi.fn>
let mockSubscriptionHasHipaaAddon: ReturnType<typeof vi.fn>
let mockCheckEntitlement: ReturnType<typeof vi.fn>
beforeEach(async () => {
const orgsQuery = await import('@/data/organizations/organizations-query')
const subscriptionQuery = await import('@/data/subscriptions/org-subscription-query')
const projectQuery = await import('@/data/projects/project-detail-query')
const settingsQuery = await import('@/data/config/project-settings-v2-query')
const aiHook = await import('@/hooks/misc/useOrgOptedIntoAi')
const subscriptionUtils =
await import('@/components/interfaces/Billing/Subscription/Subscription.utils')
const entitlementsQuery = await import('@/data/entitlements/entitlements-query')
mockGetOrganizations = vi.mocked(orgsQuery.getOrganizations)
mockGetOrgSubscription = vi.mocked(subscriptionQuery.getOrgSubscription)
mockGetProjectDetail = vi.mocked(projectQuery.getProjectDetail)
mockGetProjectSettings = vi.mocked(settingsQuery.getProjectSettings)
mockGetAiOptInLevel = vi.mocked(aiHook.getAiOptInLevel)
mockSubscriptionHasHipaaAddon = vi.mocked(subscriptionUtils.subscriptionHasHipaaAddon)
mockCheckEntitlement = vi.mocked(entitlementsQuery.checkEntitlement)
mockGetOrganizations.mockResolvedValue([
{ id: 1, slug: ORG_SLUG, plan: { id: 'pro' }, opt_in_tags: [] },
])
mockGetProjectDetail.mockResolvedValue({
ref: PROJECT_REF,
region: 'us-east-1',
organization_id: 1,
})
mockGetProjectSettings.mockResolvedValue({ is_sensitive: false })
mockGetOrgSubscription.mockResolvedValue({ addons: [] })
mockSubscriptionHasHipaaAddon.mockReturnValue(false)
mockCheckEntitlement.mockResolvedValue({ hasAccess: false })
mockGetAiOptInLevel.mockReturnValue('schema')
})
it('returns the resolved posture when the project belongs to the org', async () => {
const result = await getAIDetails({
orgSlug: ORG_SLUG,
projectRef: PROJECT_REF,
authorization: AUTH,
})
expect(result).toEqual({
aiOptInLevel: 'schema',
hasAccessToAdvanceModel: false,
hasHipaaAddon: false,
orgId: 1,
orgSlug: ORG_SLUG,
planId: 'pro',
region: 'us-east-1',
isSensitive: false,
})
})
it('calls getAiOptInLevel with the matched org opt_in_tags', async () => {
const opt_in_tags = ['AI_SQL_GENERATOR_OPT_IN']
mockGetOrganizations.mockResolvedValue([
{ id: 1, slug: ORG_SLUG, plan: { id: 'pro' }, opt_in_tags },
])
await getAIDetails({ orgSlug: ORG_SLUG, projectRef: PROJECT_REF, authorization: AUTH })
expect(mockGetAiOptInLevel).toHaveBeenCalledWith(opt_in_tags)
})
it('returns hasAccessToAdvanceModel true when the entitlement grants access', async () => {
mockCheckEntitlement.mockResolvedValue({ hasAccess: true })
const result = await getAIDetails({
orgSlug: ORG_SLUG,
projectRef: PROJECT_REF,
authorization: AUTH,
})
expect(result.hasAccessToAdvanceModel).toBe(true)
})
it('finds the correct org when multiple orgs are returned', async () => {
mockGetOrganizations.mockResolvedValue([
{ id: 1, slug: 'org-1', plan: { id: 'free' }, opt_in_tags: [] },
{ id: 2, slug: ORG_SLUG, plan: { id: 'pro' }, opt_in_tags: [] },
])
mockGetProjectDetail.mockResolvedValue({
ref: PROJECT_REF,
region: 'us-east-1',
organization_id: 2,
})
const result = await getAIDetails({
orgSlug: ORG_SLUG,
projectRef: PROJECT_REF,
authorization: AUTH,
})
expect(result.orgId).toBe(2)
expect(result.planId).toBe('pro')
})
it('forwards authorization headers to all fetches', async () => {
await getAIDetails({ orgSlug: ORG_SLUG, projectRef: PROJECT_REF, authorization: AUTH })
expect(mockGetOrganizations).toHaveBeenCalledWith({ headers: HEADERS })
expect(mockGetOrgSubscription).toHaveBeenCalledWith({ orgSlug: ORG_SLUG }, undefined, HEADERS)
expect(mockCheckEntitlement).toHaveBeenCalledWith(
ORG_SLUG,
'assistant.advance_model',
undefined,
HEADERS
)
expect(mockGetProjectDetail).toHaveBeenCalledWith(
{ ref: PROJECT_REF, skipWake: true },
undefined,
HEADERS
)
expect(mockGetProjectSettings).toHaveBeenCalledWith(
{ projectRef: PROJECT_REF },
undefined,
HEADERS
)
})
describe('when the project does not belong to the org', () => {
beforeEach(() => {
mockGetOrganizations.mockResolvedValue([
{ id: 1, slug: ORG_SLUG, plan: { id: 'pro' }, opt_in_tags: ['AI_SQL_GENERATOR_OPT_IN'] },
{ id: 2, slug: 'other-org', plan: { id: 'free' }, opt_in_tags: [] },
])
mockGetProjectDetail.mockResolvedValue({
ref: PROJECT_REF,
region: 'us-east-1',
organization_id: 2,
})
mockGetAiOptInLevel.mockReturnValue('schema_and_log_and_data')
mockCheckEntitlement.mockResolvedValue({ hasAccess: true })
})
it('falls back to the most restrictive posture', async () => {
const result = await getAIDetails({
orgSlug: ORG_SLUG,
projectRef: PROJECT_REF,
authorization: AUTH,
})
expect(result.aiOptInLevel).toBe('disabled')
expect(result.hasAccessToAdvanceModel).toBe(false)
expect(result.orgId).toBeUndefined()
expect(result.orgSlug).toBeUndefined()
expect(result.planId).toBeUndefined()
})
it('leaves hasHipaaAddon undefined so tracing checks fail closed', async () => {
mockSubscriptionHasHipaaAddon.mockReturnValue(false)
const result = await getAIDetails({
orgSlug: ORG_SLUG,
projectRef: PROJECT_REF,
authorization: AUTH,
})
expect(result.hasHipaaAddon).toBeUndefined()
})
})
it('falls back to the most restrictive posture when the org slug matches no org', async () => {
mockGetOrganizations.mockResolvedValue([])
const result = await getAIDetails({
orgSlug: ORG_SLUG,
projectRef: PROJECT_REF,
authorization: AUTH,
})
expect(result.aiOptInLevel).toBe('disabled')
expect(result.orgId).toBeUndefined()
expect(result.orgSlug).toBeUndefined()
})
it('falls back to the most restrictive posture when project detail is unavailable', async () => {
mockGetProjectDetail.mockResolvedValue(undefined)
const result = await getAIDetails({
orgSlug: ORG_SLUG,
projectRef: PROJECT_REF,
authorization: AUTH,
})
expect(result.aiOptInLevel).toBe('disabled')
expect(result.region).toBeUndefined()
})
it('keeps the opt-in level for a sensitive project in a HIPAA org', async () => {
mockSubscriptionHasHipaaAddon.mockReturnValue(true)
mockGetAiOptInLevel.mockReturnValue('schema_and_log_and_data')
mockGetProjectSettings.mockResolvedValue({ is_sensitive: true })
const result = await getAIDetails({
orgSlug: ORG_SLUG,
projectRef: PROJECT_REF,
authorization: AUTH,
})
expect(result.aiOptInLevel).toBe('schema_and_log_and_data')
expect(result.hasHipaaAddon).toBe(true)
expect(result.isSensitive).toBe(true)
})
it('keeps the opt-in level for a sensitive project outside a HIPAA org', async () => {
mockSubscriptionHasHipaaAddon.mockReturnValue(false)
mockGetProjectSettings.mockResolvedValue({ is_sensitive: true })
const result = await getAIDetails({
orgSlug: ORG_SLUG,
projectRef: PROJECT_REF,
authorization: AUTH,
})
expect(result.aiOptInLevel).toBe('schema')
})
})