{"record":{"id":"f1eb2deb7276f6a8","repo":"Tencent/WeKnora","slug":"failed-to-list-existing-faq-chunks-w","errorCode":null,"errorMessage":"failed to list existing FAQ chunks: %w","messagePattern":"failed to list existing FAQ chunks: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/service/knowledge_faq_import.go","lineNumber":1009,"sourceCode":"\n// calculateAppendOperations 计算 Append 模式下的操作（支持智能合并）。\n// 如果 entry 的标准问在 KB 中已存在，则视为合并操作（相似问 / 反例并集，\n// 答案 / 策略以新条目为准）；否则视为新建。无变化（hash 一致且操作位\n// 也未变）的合并目标会被 skip 掉，避免无效写库 / 重建索引。\n//\n// 内部 master 行为，对应开源版仅 \"重复 = 失败\" 的简化逻辑。FAQ 导入的\n// 常见使用方式是\"导出修改后重新 append\"，需要这种合并语义才能正确叠加\n// 新的相似问而不丢历史数据。\nfunc (s *knowledgeService) calculateAppendOperations(ctx context.Context,\n\ttenantID uint64, kbID string, entries []types.FAQEntryPayload,\n) (newEntries []types.FAQEntryPayload, mergeOps []faqMergeOperation, skippedCount int, err error) {\n\tif len(entries) == 0 {\n\t\treturn nil, nil, 0, nil\n\t}\n\n\texistingChunks, err := s.chunkRepo.ListAllFAQChunksWithMetadataByKnowledgeBaseID(ctx, tenantID, kbID)\n\tif err != nil {\n\t\treturn nil, nil, 0, fmt.Errorf(\"failed to list existing FAQ chunks: %w\", err)\n\t}\n\n\texistingStdQToChunk := make(map[string]*types.Chunk)\n\texistingQuestions := make(map[string]bool)\n\tfor _, chunk := range existingChunks {\n\t\tmeta, cErr := chunk.FAQMetadata()\n\t\tif cErr != nil || meta == nil {\n\t\t\tcontinue\n\t\t}\n\t\tif meta.StandardQuestion != \"\" {\n\t\t\texistingStdQToChunk[meta.StandardQuestion] = chunk\n\t\t\texistingQuestions[meta.StandardQuestion] = true\n\t\t}\n\t\tfor _, q := range meta.SimilarQuestions {\n\t\t\tif q != \"\" {\n\t\t\t\texistingQuestions[q] = true\n\t\t\t}\n\t\t}","sourceCodeStart":991,"sourceCodeEnd":1027,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/service/knowledge_faq_import.go#L991-L1027","documentation":"calculateAppendOperations wraps errors from chunkRepo.ListAllFAQChunksWithMetadataByKnowledgeBaseID when loading all existing FAQ chunks for a knowledge base before computing append/merge operations. It means the append flow could not even determine the current chunk state, so no merge plan was produced. The underlying cause (DB connectivity, table issues, context cancellation) is preserved via %w.","triggerScenarios":"executeFAQImport runs in append mode with non-empty entries; the ListAllFAQChunksWithMetadataByKnowledgeBaseID query fails due to database outage, connection pool exhaustion, query timeout, or a canceled request context.","commonSituations":"Postgres/MySQL restart or failover during an import; long-running import whose context times out; misconfigured DSN or read-replica lag causing connection errors; schema mismatch after migration.","solutions":["Inspect the wrapped cause in logs to identify the DB error","Verify database connectivity and that the chunks table exists with expected schema","Check for context deadline/cancellation; increase the import timeout or DB query timeout","Retry the FAQ import once the database is healthy"],"exampleFix":"// before\nexistingChunks, err := s.chunkRepo.ListAllFAQChunksWithMetadataByKnowledgeBaseID(ctx, tenantID, kbID)\nif err != nil {\n\treturn nil, nil, 0, fmt.Errorf(\"failed to list existing FAQ chunks: %w\", err)\n}\n// after\nexistingChunks, err := s.chunkRepo.ListAllFAQChunksWithMetadataByKnowledgeBaseID(ctx, tenantID, kbID)\nif err != nil {\n\tlogger.Errorf(ctx, \"list existing FAQ chunks kb=%s: %v\", kbID, err)\n\treturn nil, nil, 0, fmt.Errorf(\"failed to list existing FAQ chunks: %w\", err)\n}","handlingStrategy":"retry","validationCode":"// Pre-check DB reachability and chunk repo before starting an append import\nif err := db.PingContext(ctx); err != nil {\n\treturn fmt.Errorf(\"database unavailable, abort FAQ import: %w\", err)\n}\ncount, err := chunkRepo.CountFAQChunks(ctx, tenantID, kbID)\nif err != nil {\n\treturn fmt.Errorf(\"cannot read FAQ chunks, abort import: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"err := executeAppendImport(ctx, payload)\nvar appErr *AppError\nif errors.As(err, &appErr) && strings.Contains(appErr.Error(), \"failed to list existing FAQ chunks\") {\n\t// transient DB issue: retry with backoff\n\treturn retryWithBackoff(3, executeAppendImport, ctx, payload)\n}\nreturn err","preventionTips":["Set generous but bounded DB statement timeouts for import transactions","Health-check the database before enqueueing import tasks","Monitor connection pool saturation during bulk imports","Wrap chunk listing in a retry helper for transient errors"],"tags":["database","faq-import","repository"],"backgroundTag":"faq-import-chunk-list-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}