fix(vault): suppress stale error toast on stop + count unenriched docs in scan

- AddError() now skips broadcast after Finish() to prevent cancelled
  goroutines from emitting error events to UI after user stops enrichment
- batchSummarize skips AddError when context is cancelled (expected on stop)
- Rescan always re-enqueues unenriched docs alongside new/updated files,
  worker-level dedup prevents double-processing
This commit is contained in:
viettranx committed 2026-04-13 21:42:17 +07:00
1 parent 2fee42dc32
commit 2e0f3a5a19
3 files changed
+11 -5

No files matched your search

+5 -4
View File
@@ -204,14 +204,15 @@ func (h *VaultHandler) handleRescan(w http.ResponseWriter, r *http.Request) {
// and AddDone calls are not dropped by the !running guard.
total := result.New + result.Updated
// If no new/updated files, check for unenriched docs and re-enqueue them.
// This handles the case where previous enrichment failed (e.g. provider timeout).
if total == 0 && h.enrichWorker != nil {
// Always re-enqueue docs that lack summaries (failed previous enrichment).
// Worker-level dedup (DocID+ContentHash) prevents double-processing docs
// that are also in PendingEvents from the current scan.
if h.enrichWorker != nil {
enqueued, err := h.enrichWorker.EnqueueUnenriched(ctx, tenantID, wsPath, h.eventBus, 0)
if err != nil {
slog.Warn("vault.rescan: enqueue_unenriched failed", "tenant", tenantID, "error", err)
} else if enqueued > 0 {
total = enqueued
total += enqueued
result.Reenqueued = enqueued
slog.Info("vault.rescan: re-enqueued unenriched docs", "tenant", tenantID, "count", enqueued)
}
+4
View File
@@ -78,9 +78,13 @@ func (p *EnrichProgress) Start(total int, tenantID uuid.UUID) {
// AddError increments error count and broadcasts an error event.
// Used when enrichment fails for a document (e.g., LLM retries exhausted).
// Suppressed after Finish() to prevent stale errors from cancelled goroutines.
func (p *EnrichProgress) AddError(errMsg string) {
p.mu.Lock()
defer p.mu.Unlock()
if !p.running {
return
}
p.errorCount++
p.lastError = errMsg
p.broadcast(EnrichEvent{
+2 -1
View File
@@ -460,7 +460,8 @@ func (w *EnrichWorker) batchSummarize(ctx context.Context, provider providers.Pr
})
if err != nil {
slog.Warn("vault.enrich: batch_summarize", "count", len(paths), "err", err)
if w.progress != nil {
// Don't report context cancellation as enrichment error — expected during stop.
if w.progress != nil && ctx.Err() == nil {
w.progress.AddError(fmt.Sprintf("batch summarize failed: %v", err))
}
return nil