{"record":{"id":"8fad31c854494ec3","repo":"Tencent/WeKnora","slug":"knowledge-service-returned-an-empty-result-8fad31","errorCode":null,"errorMessage":"knowledge service returned an empty result","messagePattern":"knowledge service returned an empty result","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/agent/tools/wiki_read_source_doc.go","lineNumber":148,"sourceCode":"\tif err := json.Unmarshal(args, &params); err != nil {\n\t\treturn &types.ToolResult{Success: false, Error: \"Invalid parameters: \" + err.Error()}, nil\n\t}\n\n\tknowledgeID := strings.TrimSpace(params.KnowledgeID)\n\tif knowledgeID == \"\" {\n\t\treturn &types.ToolResult{Success: false, Error: \"knowledge_id is required\"}, nil\n\t}\n\n\tvar knowledge *types.Knowledge\n\tvar err error\n\tif t.scopeEnforced {\n\t\tknowledge, err = authorizeKnowledgeInSearchTargets(ctx, t.searchTargets, knowledgeID, t.knowledgeService)\n\t} else {\n\t\tknowledge, err = t.knowledgeService.GetKnowledgeByIDOnly(ctx, knowledgeID)\n\t}\n\tif err != nil || knowledge == nil {\n\t\tif err == nil {\n\t\t\terr = fmt.Errorf(\"knowledge service returned an empty result\")\n\t\t}\n\t\treturn &types.ToolResult{Success: false, Error: fmt.Sprintf(\"Document not found: %v\", err)}, nil\n\t}\n\n\tvar sb strings.Builder\n\tsb.WriteString(\"<source_document>\\n<metadata>\\n\")\n\tsb.WriteString(fmt.Sprintf(\"<title>%s</title>\\n\", knowledge.Title))\n\tsb.WriteString(fmt.Sprintf(\"<knowledge_id>%s</knowledge_id>\\n\", knowledgeID))\n\n\thasRange := params.StartChunkIndex > 0\n\tvar re *regexp.Regexp\n\n\tif hasRange {\n\t\tif params.EndChunkIndex < params.StartChunkIndex {\n\t\t\tparams.EndChunkIndex = params.StartChunkIndex + 10 // Default window\n\t\t}\n\t\tif params.EndChunkIndex-params.StartChunkIndex > 50 {\n\t\t\tparams.EndChunkIndex = params.StartChunkIndex + 50 // Max 50 chunks to prevent bloat","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/agent/tools/wiki_read_source_doc.go#L130-L166","documentation":"This error is synthesized inside the wiki read-source-document tool when the knowledge service call (either authorizeKnowledgeInSearchTargets or GetKnowledgeByIDOnly) returns a nil knowledge object with a nil error. Since the code cannot distinguish 'missing' from 'malformed response', it fabricates this message and surfaces it to the agent as 'Document not found'. It indicates the service layer silently returned nothing instead of a typed not-found error.","triggerScenarios":"Calling Execute on the wiki_read_source_doc tool with a knowledgeID the knowledge service cannot return: the service finds no record and returns (nil, nil) rather than a not-found sentinel error.","commonSituations":"Stale knowledge ID cached in conversation history after the document was deleted; ID pointing to a different environment/index; a knowledge service implementation bug that swallows repository.ErrNotFound and returns nil without an error.","solutions":["Verify the knowledgeID exists via the knowledge service/admin API before invoking the tool","Check the knowledgeService implementation returns an explicit not-found error (e.g. repository.ErrKnowledgeNotFound) instead of (nil, nil)","Re-sync or refresh the knowledge base if the document was recently deleted or moved","If this is a legitimate miss, treat it as user-facing 'Document not found' and stop retrying with the same ID"],"exampleFix":"// before (service)\nfunc (s *service) GetKnowledgeByIDOnly(ctx, id) (*Knowledge, error) {\n    k, err := s.repo.Get(ctx, id)\n    if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } ... }\n// after\nfunc (s *service) GetKnowledgeByIDOnly(ctx, id) (*Knowledge, error) {\n    k, err := s.repo.Get(ctx, id)\n    if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, ErrKnowledgeNotFound } ... }","handlingStrategy":"validation","validationCode":"func validateKnowledgeID(id string) error {\n    if strings.TrimSpace(id) == \"\" { return errors.New(\"knowledge id is empty\") }\n    return nil\n}\n// call before Execute; also pre-check existence via the admin/list API when possible","typeGuard":"func knowledgeExists(k *types.Knowledge) bool { return k != nil && k.ID != \"\" }","tryCatchPattern":"res, err := tool.Execute(ctx, args)\nif err != nil || (res != nil && !res.Success) {\n    if strings.Contains(res.Error, \"Document not found\") {\n        // treat as permanent miss: refresh ID or inform user; do not retry same ID\n    }\n}","preventionTips":["Always pass knowledge IDs obtained from a fresh list/search call, not stale conversation memory","Make the knowledge service return a typed not-found error instead of (nil, nil)","Log the knowledgeID with the failure to aid correlation","Validate ID format before invoking the tool"],"tags":["go","knowledge-service","not-found","agent-tools"],"backgroundTag":"empty-result-from-service","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}