@@ -390,51 +390,42 @@ export async function runKnowledgeSearch({
390390 : undefined
391391 const resultSecretRegistry = preparedRegistry ?? input . resultSecretRegistry
392392 input . signal ?. throwIfAborted ( )
393- /** The embedding is requested the moment admission passes, beside the scope and defaults reads. */
394- const admittedEmbedding = async ( ) => {
395- const billingAttribution = await admit ( )
396- input . signal ?. throwIfAborted ( )
397- const queryEmbedding = hasQuery
398- ? await measureSearchStage ( 'embedding' , ( ) =>
399- runWithKnowledgeModelInputProvenance ( resultSecretRegistry , ( ) =>
400- generateSearchEmbedding (
401- input . query ! ,
402- embeddingTarget ! ,
403- context . workspaceId ,
404- input . signal
405- )
406- )
407- )
408- : null
409- return { billingAttribution, queryEmbedding }
410- }
411- const [ access , searchDefaults , admitted , tagDefinitions , rerankerCredential ] = await Promise . all ( [
412- measureSearchStage ( 'access_scope' , ( ) => context . access . get ( ) ) ,
413- measureSearchStage ( 'defaults' , ( ) =>
414- resolveKnowledgeSearchDefaults ( {
415- workspaceId : context . workspaceId ,
416- organizationId : context . organizationId ,
393+ const [ access , searchDefaults , billingAttribution , tagDefinitions , rerankerCredential ] =
394+ await Promise . all ( [
395+ measureSearchStage ( 'access_scope' , ( ) => context . access . get ( ) ) ,
396+ measureSearchStage ( 'defaults' , ( ) =>
397+ resolveKnowledgeSearchDefaults ( {
398+ workspaceId : context . workspaceId ,
399+ organizationId : context . organizationId ,
417400
418- /** The signed-in person, if any; never the billing owner or a key's creator. */
419- userId : resolvePrincipalSubjectUserId ( principal ) ?? undefined ,
420- requestedMode : input . searchMode ,
421- } )
422- ) ,
423- admittedEmbedding ( ) ,
424- /** The tag names the results are labelled with depend on the bases alone. */
425- filters . length === 0
426- ? measureSearchStage ( 'tag_definitions' , ( ) =>
427- getDocumentTagDefinitionsByKnowledgeBaseIds ( knowledgeBaseIds )
428- )
429- : Promise . resolve ( definitionsByKnowledgeBase ) ,
430- /** A surface may ask to rerank; without a key for the workspace or the platform there is nothing to ask. */
431- input . rerankerEnabled && hasQuery
432- ? hasRerankerCredential ( context . workspaceId , input . rerankerApiKey )
433- : false ,
434- ] )
435- const { billingAttribution, queryEmbedding } = admitted
401+ /** The signed-in person, if any; never the billing owner or a key's creator. */
402+ userId : resolvePrincipalSubjectUserId ( principal ) ?? undefined ,
403+ requestedMode : input . searchMode ,
404+ } )
405+ ) ,
406+ admit ( ) ,
407+ /** The tag names the results are labelled with depend on the bases alone. */
408+ filters . length === 0
409+ ? measureSearchStage ( 'tag_definitions' , ( ) =>
410+ getDocumentTagDefinitionsByKnowledgeBaseIds ( knowledgeBaseIds )
411+ )
412+ : Promise . resolve ( definitionsByKnowledgeBase ) ,
413+ /** A surface may ask to rerank; without a key for the workspace or the platform there is nothing to ask. */
414+ input . rerankerEnabled && hasQuery
415+ ? hasRerankerCredential ( context . workspaceId , input . rerankerApiKey )
416+ : false ,
417+ ] )
436418 definitionsByKnowledgeBase = tagDefinitions
437419 input . signal ?. throwIfAborted ( )
420+ /** Requested only once every prerequisite held: a search refused for any reason spends no model call. */
421+ const queryEmbedding = hasQuery
422+ ? await measureSearchStage ( 'embedding' , ( ) =>
423+ runWithKnowledgeModelInputProvenance ( resultSecretRegistry , ( ) =>
424+ generateSearchEmbedding ( input . query ! , embeddingTarget ! , context . workspaceId , input . signal )
425+ )
426+ )
427+ : null
428+ input . signal ?. throwIfAborted ( )
438429 annotateSearchDiagnostics ( {
439430 accessScopeKind : access . kind ,
440431 searchMode : searchDefaults . searchMode ,
0 commit comments