feat(knowledge): add scoped activity audit trail

This commit is contained in:
wizardchen
2026-07-22 19:15:27 +08:00
committed by lyingbug
parent f195e0494c
commit a3a0308b62
44 changed files with 2429 additions and 81 deletions
@@ -903,7 +903,6 @@ func (s *knowledgeService) ProcessSummaryGeneration(ctx context.Context, t *asyn
logger.Errorf(ctx, "Failed to unmarshal summary generation payload: %v", err)
return nil // Don't retry on unmarshal error
}
logger.Infof(ctx, "Processing summary generation for knowledge: %s", payload.KnowledgeID)
// Set tenant and language context
@@ -2030,6 +2029,11 @@ func (s *knowledgeService) ReparseKnowledge(
if kb != nil && kb.IsWikiEnabled() {
s.prepareWikiForReparse(ctx, existing)
}
recordReparseStarted := func() {
recordKBActivity(ctx, s.audit, tenantID, existing.KnowledgeBaseID, types.AuditActionKnowledgeReparseStarted,
"knowledge", existing.ID, types.AuditOutcomeAccepted,
map[string]any{"title": existing.Title, "type": existing.Type, "attempt": reparseAttempt})
}
// For manual knowledge, use async manual processing (cleanup + re-indexing in worker)
if existing.IsManual() {
@@ -2061,11 +2065,13 @@ func (s *knowledgeService) ReparseKnowledge(
return nil, err
}
if err := s.enqueueManualProcessing(ctx, existing, meta.Content, true); err != nil {
if _, err := s.enqueueManualProcessing(ctx, existing, meta.Content, true); err != nil {
logger.Errorf(ctx, "Failed to enqueue manual reparse task: %v", err)
existing.ParseStatus = "failed"
existing.ErrorMessage = "Failed to enqueue processing task"
s.repo.UpdateKnowledge(ctx, existing)
} else {
recordReparseStarted()
}
return existing, nil
}
@@ -2148,6 +2154,7 @@ func (s *knowledgeService) ReparseKnowledge(
return existing, nil
}
logger.Infof(ctx, "Enqueued reparse task: id=%s queue=%s knowledge_id=%s", info.ID, info.Queue, existing.ID)
recordReparseStarted()
// For data tables (csv, xlsx, xls), also enqueue summary task
if slices.Contains([]string{"csv", "xlsx", "xls"}, getFileType(existing.FileName)) {
@@ -2201,6 +2208,7 @@ func (s *knowledgeService) ReparseKnowledge(
return existing, nil
}
logger.Infof(ctx, "Enqueued file URL reparse task: id=%s queue=%s knowledge_id=%s", info.ID, info.Queue, existing.ID)
recordReparseStarted()
return existing, nil
}
@@ -2247,6 +2255,7 @@ func (s *knowledgeService) ReparseKnowledge(
return existing, nil
}
logger.Infof(ctx, "Enqueued URL reparse task: id=%s queue=%s knowledge_id=%s", info.ID, info.Queue, existing.ID)
recordReparseStarted()
return existing, nil
}
@@ -2351,6 +2360,9 @@ func (s *knowledgeService) CancelKnowledgeParse(
// at isWikiKnowledgeAborted anyway, but scrubbing avoids waking the
// batch in the first place.
s.scrubWikiPendingIngest(ctx, existing.KnowledgeBaseID, knowledgeID, "cancel")
recordKBActivity(ctx, s.audit, tenantID, existing.KnowledgeBaseID, types.AuditActionKnowledgeParseCanceled,
"knowledge", existing.ID, types.AuditOutcomeCanceled,
map[string]any{"title": existing.Title, "type": existing.Type})
return existing, nil
}
@@ -3405,6 +3417,9 @@ func (s *knowledgeService) ProcessKnowledgeListReparse(ctx context.Context, t *a
logger.Errorf(ctx, "Failed to unmarshal knowledge list reparse payload: %v", err)
return err
}
ctx = payload.Initiator.Apply(ctx)
taskID, _ := asynq.GetTaskID(ctx)
ctx = withKBActivityTask(ctx, taskID, "user")
logger.Infof(ctx, "Processing knowledge list reparse task for %d knowledge items", len(payload.KnowledgeIDs))