Skip to content

Commit 2f3634c

Browse files
committed
improvement(knowledge): keep billing effects awaited and treat a capped pool as exhausted
1 parent a7bdc75 commit 2f3634c

2 files changed

Lines changed: 5 additions & 7 deletions

File tree

‎apps/sim/lib/knowledge/application/search.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { checkAndBillPayerOverageThreshold } from '@/lib/billing/threshold-billi
1111
import { OrchestrationError } from '@/lib/core/orchestration/types'
1212
import { resourceScopeFromOwner, resourceScopeKey } from '@/lib/core/resource-scope'
1313
import { PlatformEvents } from '@/lib/core/telemetry'
14-
import { runDetached } from '@/lib/core/utils/background'
1514
import { generateRequestId } from '@/lib/core/utils/request'
1615
import { importDurableSecretProvenance } from '@/lib/execution/durable-secret-provenance'
1716
import { reportDurableSecretProvenanceRefusal } from '@/lib/execution/durable-secret-provenance-telemetry'
@@ -620,8 +619,7 @@ const searchKnowledgeUseCase = defineAuthorizedKnowledgeUseCase({
620619
],
621620
})
622621
)
623-
/** Whether the payer crossed a billing threshold is settled after the results are on their way. */
624-
runDetached('knowledge search overage billing', () =>
622+
await measureSearchStage('overage_billing', () =>
625623
checkAndBillPayerOverageThreshold(billingAttribution.billingEntity)
626624
)
627625
} catch (error) {
@@ -738,11 +736,10 @@ const searchKnowledgeUseCase = defineAuthorizedKnowledgeUseCase({
738736
},
739737
afterSuccess: async ({ principal, context, input, result }) => {
740738
const actorUserId = resolvePrincipalSubjectUserId(principal)
741-
/** The record is for the organization's activity view; the caller's results never wait on it. */
742739
if (context.organizationId && actorUserId) {
743-
runDetached('organization search activity', () =>
740+
await measureSearchStage('activity_recording', () =>
744741
recordOrganizationSearchActivity({
745-
organizationId: context.organizationId!,
742+
organizationId: context.organizationId,
746743
userId: actorUserId,
747744
surface: input.surface ?? 'other',
748745
results: result.results,

‎apps/sim/lib/knowledge/search/queries.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1891,11 +1891,12 @@ async function selectVectorResults(params: SearchParams): Promise<SearchResult[]
18911891
}
18921892
}
18931893
}
1894+
/** A pool the walk could not fill, or one at the ceiling, is all the pages will ever get. */
18941895
candidatePool = {
18951896
excludedKey,
18961897
ids: selected,
18971898
limit: candidateLimit,
1898-
exhausted: selected.length < candidateLimit,
1899+
exhausted: selected.length < candidateLimit || candidateLimit >= MAX_VECTOR_CANDIDATES,
18991900
}
19001901
annotateSearchDiagnostics({
19011902
vectorCandidateCount: selected.length,

0 commit comments

Comments
 (0)