From 969305fe8951c4c30881038815068fd941952a8d Mon Sep 17 00:00:00 2001 From: msukkari Date: Thu, 13 Nov 2025 21:07:41 -0800 Subject: [PATCH 1/6] clear search contexts on init --- packages/web/src/actions.ts | 16 ++++++++++++++++ packages/web/src/initialize.ts | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/web/src/actions.ts b/packages/web/src/actions.ts index e194f808..16ce61a5 100644 --- a/packages/web/src/actions.ts +++ b/packages/web/src/actions.ts @@ -1669,6 +1669,22 @@ export const getSearchContexts = async (domain: string) => sew(() => }, /* minRequiredRole = */ OrgRole.GUEST), /* allowAnonymousAccess = */ true )); +export const clearSearchContexts = async (domain: string) => sew(() => + withAuth((userId) => + withOrgMembership(userId, domain, async ({ org }) => { + await prisma.searchContext.deleteMany({ + where: { + orgId: org.id, + }, + }); + + return { + success: true, + }; + }, /* minRequiredRole = */ OrgRole.OWNER) + ) +) + export const getRepoImage = async (repoId: number): Promise => sew(async () => { return await withOptionalAuthV2(async ({ org, prisma }) => { const repo = await prisma.repo.findUnique({ diff --git a/packages/web/src/initialize.ts b/packages/web/src/initialize.ts index fa27febd..a0f95325 100644 --- a/packages/web/src/initialize.ts +++ b/packages/web/src/initialize.ts @@ -7,6 +7,7 @@ import { getOrgFromDomain } from './data/org'; import { SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_ID, SOURCEBOT_GUEST_USER_ID } from './lib/constants'; import { ServiceErrorException } from './lib/serviceError'; import { getOrgMetadata, isServiceError } from './lib/utils'; +import { clearSearchContexts, getSearchContexts } from './actions'; const logger = createLogger('web-initialize'); @@ -62,6 +63,16 @@ const initSingleTenancy = async () => { } } + // If we don't have the search context entitlement then wipe any existing + // search contexts that may be present in the DB + const hasSearchContextEntitlement = hasEntitlement("search-contexts") + if(!hasSearchContextEntitlement) { + const searchContexts = await getSearchContexts(SINGLE_TENANT_ORG_DOMAIN); + if (!isServiceError(searchContexts) && searchContexts.length > 0) { + clearSearchContexts(SINGLE_TENANT_ORG_DOMAIN) + } + } + // Sync anonymous access config from the config file const config = await loadConfig(env.CONFIG_PATH); const forceEnableAnonymousAccess = config.settings?.enablePublicAccess ?? env.FORCE_ENABLE_ANONYMOUS_ACCESS === 'true'; From 4a65d77dcfb7d67f22b6778ca17e818991cae60b Mon Sep 17 00:00:00 2001 From: msukkari Date: Thu, 13 Nov 2025 21:09:25 -0800 Subject: [PATCH 2/6] simplify --- packages/web/src/initialize.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/web/src/initialize.ts b/packages/web/src/initialize.ts index a0f95325..67d9db23 100644 --- a/packages/web/src/initialize.ts +++ b/packages/web/src/initialize.ts @@ -7,7 +7,7 @@ import { getOrgFromDomain } from './data/org'; import { SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_ID, SOURCEBOT_GUEST_USER_ID } from './lib/constants'; import { ServiceErrorException } from './lib/serviceError'; import { getOrgMetadata, isServiceError } from './lib/utils'; -import { clearSearchContexts, getSearchContexts } from './actions'; +import { clearSearchContexts } from './actions'; const logger = createLogger('web-initialize'); @@ -67,10 +67,7 @@ const initSingleTenancy = async () => { // search contexts that may be present in the DB const hasSearchContextEntitlement = hasEntitlement("search-contexts") if(!hasSearchContextEntitlement) { - const searchContexts = await getSearchContexts(SINGLE_TENANT_ORG_DOMAIN); - if (!isServiceError(searchContexts) && searchContexts.length > 0) { - clearSearchContexts(SINGLE_TENANT_ORG_DOMAIN) - } + clearSearchContexts(SINGLE_TENANT_ORG_DOMAIN) } // Sync anonymous access config from the config file From 29659031adb26c107f02ed206566e5cd25ffedee Mon Sep 17 00:00:00 2001 From: msukkari Date: Thu, 13 Nov 2025 21:11:03 -0800 Subject: [PATCH 3/6] comments --- packages/web/src/initialize.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/web/src/initialize.ts b/packages/web/src/initialize.ts index 67d9db23..bd598ec5 100644 --- a/packages/web/src/initialize.ts +++ b/packages/web/src/initialize.ts @@ -64,7 +64,8 @@ const initSingleTenancy = async () => { } // If we don't have the search context entitlement then wipe any existing - // search contexts that may be present in the DB + // search contexts that may be present in the DB. This could happen if a deployment had + // the entitlement, synced search contexts, and then no longer had the entitlement const hasSearchContextEntitlement = hasEntitlement("search-contexts") if(!hasSearchContextEntitlement) { clearSearchContexts(SINGLE_TENANT_ORG_DOMAIN) From 945f0a282fb3ff600ba0bac084a883ca4ae56f4c Mon Sep 17 00:00:00 2001 From: msukkari Date: Thu, 13 Nov 2025 21:12:56 -0800 Subject: [PATCH 4/6] simplify --- packages/web/src/actions.ts | 16 ---------------- packages/web/src/initialize.ts | 6 +++++- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/packages/web/src/actions.ts b/packages/web/src/actions.ts index 16ce61a5..e194f808 100644 --- a/packages/web/src/actions.ts +++ b/packages/web/src/actions.ts @@ -1669,22 +1669,6 @@ export const getSearchContexts = async (domain: string) => sew(() => }, /* minRequiredRole = */ OrgRole.GUEST), /* allowAnonymousAccess = */ true )); -export const clearSearchContexts = async (domain: string) => sew(() => - withAuth((userId) => - withOrgMembership(userId, domain, async ({ org }) => { - await prisma.searchContext.deleteMany({ - where: { - orgId: org.id, - }, - }); - - return { - success: true, - }; - }, /* minRequiredRole = */ OrgRole.OWNER) - ) -) - export const getRepoImage = async (repoId: number): Promise => sew(async () => { return await withOptionalAuthV2(async ({ org, prisma }) => { const repo = await prisma.repo.findUnique({ diff --git a/packages/web/src/initialize.ts b/packages/web/src/initialize.ts index bd598ec5..cf2fbdc4 100644 --- a/packages/web/src/initialize.ts +++ b/packages/web/src/initialize.ts @@ -68,7 +68,11 @@ const initSingleTenancy = async () => { // the entitlement, synced search contexts, and then no longer had the entitlement const hasSearchContextEntitlement = hasEntitlement("search-contexts") if(!hasSearchContextEntitlement) { - clearSearchContexts(SINGLE_TENANT_ORG_DOMAIN) + await prisma.searchContext.deleteMany({ + where: { + orgId: SINGLE_TENANT_ORG_ID, + }, + }); } // Sync anonymous access config from the config file From 2d461baf2ee94fffbbe68bbbccbac89ce710dedd Mon Sep 17 00:00:00 2001 From: msukkari Date: Thu, 13 Nov 2025 21:13:57 -0800 Subject: [PATCH 5/6] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index db775061..99163978 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed spurious infinite loads with explore panel, file tree, and file search command. [#617](https://github.com/sourcebot-dev/sourcebot/pull/617) +- Wipe search context on init if entitlement no longer exists [#618](https://github.com/sourcebot-dev/sourcebot/pull/618) ## [4.9.2] - 2025-11-13 From 50ef9326b4abef33b225507892879fd56be1649c Mon Sep 17 00:00:00 2001 From: msukkari Date: Thu, 13 Nov 2025 21:14:27 -0800 Subject: [PATCH 6/6] remove dead ref --- packages/web/src/initialize.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/web/src/initialize.ts b/packages/web/src/initialize.ts index cf2fbdc4..97efc9ea 100644 --- a/packages/web/src/initialize.ts +++ b/packages/web/src/initialize.ts @@ -7,7 +7,6 @@ import { getOrgFromDomain } from './data/org'; import { SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_ID, SOURCEBOT_GUEST_USER_ID } from './lib/constants'; import { ServiceErrorException } from './lib/serviceError'; import { getOrgMetadata, isServiceError } from './lib/utils'; -import { clearSearchContexts } from './actions'; const logger = createLogger('web-initialize');