AOSSIE-Org / AOSSIE-Org/Ell-ena

Critical Asynchronous Race Condition Bypasses Embedding Generation in Semantic Meeting Query

Aperta
#272 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Dart
Stelle
54
Fork
110
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

### Description
A critical asynchronous race condition exists in the `getRelevantMeetingSummaries` method of `AIService` (`lib/services/ai_service.dart`). The service attempts to query semantic meeting summaries immediately after queuing an embedding request, without waiting for the asynchronous background embedding generation to complete. This causes semantic/vector search to consistently return empty or invalid results.

### Technical Details
In `lib/services/ai_service.dart`, the `getRelevantMeetingSummaries` method is defined as follows:
```dart
555: final respIdResponse = await _supabaseService.client.rpc(
556: 'queue_embedding',
557: params: {
558: 'query_text': query,
559: },
560: );
561:
562: final respId = respIdResponse as int;
563: print("👉 Embedding queued with response ID: $respId");
564:
565: // Step 2: Fetch meetings using the resp_id
566: final response = await _supabaseService.client.rpc(
567: 'search_meeting_summaries_by_resp_id',
568: params: {
569: 'resp_id': respId,
570: 'match_count': 2,
571: },
572: );
```

1. The client invokes the Supabase RPC `queue_embedding`, which adds a row containing the `query_text` to an embedding queue table in the database and returns a generated `resp_id`.
2. A background trigger or database webhook (usually calling an external LLM/embedding API) is responsible for asynchronously computing the embedding vector and updating the row in the database.
3. The client immediately (within a few milliseconds) calls the RPC `search_meeting_summaries_by_resp_id` passing the `respId`.
4. Because the background vector generation process takes hundreds of milliseconds (or more) to execute the HTTP request to the embedding model and write back to the database, the embedding vector in the database is still `NULL` or unitialized when `search_meeting_summaries_by_resp_id` runs.
5. Consequently, the similarity search fails or returns zero matches.

### Impact
The semantic search feature is broken. Users querying the AI agent for information discussed in meetings (e.g. "what did we discuss in the last meeting?") will receive responses stating that no relevant meeting summaries could be found, or the model will generate hallucinated answers due to missing context.

### Recommended Mitigation
Implement a polling mechanism or wait loop on the client side to wait until the embedding vector has been generated before executing the similarity search:
1. Create a helper RPC or query to check if the embedding vector for `resp_id` has been populated (i.e. is not null).
2. Poll this endpoint with a short delay (e.g., every 200ms up to a timeout of 3-5 seconds) before proceeding to step 2:
```dart
bool isReady = false;
int retries = 0;
while (!isReady && retries < 15) {
final check = await _supabaseService.client
.from('embedding_responses') // assuming the table name
.select('embedding')
.eq('id', respId)
.maybeSingle();
if (check != null && check['embedding'] != null) {
isReady = true;
break;
}
await Future.delayed(Duration(milliseconds: 200));
retries++;
}

if (!isReady) {
print("Timed out waiting for embedding vector generation");
return [];
}
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.