@@ -469,25 +469,31 @@ async def sync_entity_vectors_batch(
469469 * (self ._clear_entity_vectors (entity_id ) for entity_id in opted_out_ids )
470470 )
471471
472- repository_results : list [VectorSyncBatchResult ] = []
473- if unknown_ids :
474- # Trigger: a caller passes entity IDs that were deleted after the batch was built.
475- # Why: repository sync still owns stale chunk cleanup for IDs with no source rows.
476- # Outcome: deleted entities do not silently keep orphaned vector rows forever.
477- repository_results .append (await self .repository .sync_entity_vectors_batch (unknown_ids ))
478-
479472 eligible_entity_ids = [
480473 entity_id
481474 for entity_id in entity_ids
482475 if entity_id in entities_by_id and entity_id not in opted_out_ids
483476 ]
484- if eligible_entity_ids :
485- repository_results .append (
486- await self .repository .sync_entity_vectors_batch (
487- eligible_entity_ids ,
488- progress_callback = progress_callback ,
489- )
477+
478+ cleanup_task = (
479+ self .repository .sync_entity_vectors_batch (unknown_ids ) if unknown_ids else None
480+ )
481+ eligible_task = (
482+ self .repository .sync_entity_vectors_batch (
483+ eligible_entity_ids ,
484+ progress_callback = progress_callback ,
490485 )
486+ if eligible_entity_ids
487+ else None
488+ )
489+ repository_results = [
490+ result
491+ for result in await asyncio .gather (
492+ cleanup_task if cleanup_task is not None else asyncio .sleep (0 , result = None ),
493+ eligible_task if eligible_task is not None else asyncio .sleep (0 , result = None ),
494+ )
495+ if result is not None
496+ ]
491497
492498 if not repository_results :
493499 return VectorSyncBatchResult (
@@ -503,7 +509,9 @@ async def sync_entity_vectors_batch(
503509 entities_failed = sum (result .entities_failed for result in repository_results ),
504510 entities_deferred = sum (result .entities_deferred for result in repository_results ),
505511 entities_skipped = (
506- len (opted_out_ids ) + sum (result .entities_skipped for result in repository_results )
512+ len (opted_out_ids )
513+ + sum (result .entities_skipped for result in repository_results )
514+ - len (unknown_ids )
507515 ),
508516 failed_entity_ids = [
509517 failed_entity_id
0 commit comments