{"record":{"id":"eedf9d51da769dff","repo":"Tencent/WeKnora","slug":"knowledge-service-is-unavailable","errorCode":null,"errorMessage":"knowledge service is unavailable","messagePattern":"knowledge service is unavailable","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/agent/tools/scope_authorization.go","lineNumber":70,"sourceCode":"\t\tlen(knowledgeIDs) == 0 && len(tagIDs) == 0\n}\n\n// authorizeKnowledgeInSearchTargets is the shared authorization boundary for\n// every Agent tool that accepts a model-visible dN/knowledge_id. Handle\n// decoding is necessary but never sufficient: the durable document must also\n// belong to the server-owned search scope for this Agent execution.\nfunc authorizeKnowledgeInSearchTargets(\n\tctx context.Context,\n\tsearchTargets types.SearchTargets,\n\tknowledgeID string,\n\tknowledgeService interfaces.KnowledgeService,\n) (*types.Knowledge, error) {\n\tknowledgeID = strings.TrimSpace(knowledgeID)\n\tif knowledgeID == \"\" {\n\t\treturn nil, fmt.Errorf(\"knowledge_id is required\")\n\t}\n\tif knowledgeService == nil {\n\t\treturn nil, fmt.Errorf(\"knowledge service is unavailable\")\n\t}\n\tknowledge, err := knowledgeService.GetKnowledgeByIDOnly(ctx, knowledgeID)\n\tif err != nil || knowledge == nil {\n\t\tif err == nil {\n\t\t\terr = fmt.Errorf(\"empty result\")\n\t\t}\n\t\treturn nil, fmt.Errorf(\"document %s not found: %w\", knowledgeID, err)\n\t}\n\tif !searchTargets.ContainsKB(knowledge.KnowledgeBaseID) {\n\t\treturn nil, fmt.Errorf(\"knowledge base %s is not within the current Agent scope\", knowledge.KnowledgeBaseID)\n\t}\n\tallowed, err := searchTargetsAllowKnowledgeID(\n\t\tctx, searchTargets, knowledge.ID, knowledge.KnowledgeBaseID, knowledgeService,\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to validate document scope: %w\", err)\n\t}\n\tif !allowed {","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/agent/tools/scope_authorization.go#L52-L88","documentation":"authorizeKnowledgeInSearchTargets throws this when the injected interfaces.KnowledgeService dependency is nil. Without a configured knowledge service the tool cannot fetch or authorize the requested knowledge item, so it fails fast instead of nil-pointer panicking.","triggerScenarios":"Constructing the tool (or the agent toolset) without wiring a KnowledgeService implementation — knowledgeService parameter is nil when Execute / resolveAuthorizedSourceRefs reaches authorizeKnowledgeInSearchTargets.","commonSituations":"Knowledge feature not enabled in config so the service was never registered in DI; running in an environment/deployment mode where the knowledge store isn't initialized; a refactor changed the constructor and the dependency was dropped.","solutions":["Initialize and inject a KnowledgeService implementation when constructing the tool/agent.","Enable the knowledge feature in configuration so the service is registered with the DI container.","Guard callers with a nil check on the service and return a clear configuration error at startup rather than at call time."],"exampleFix":"// before\ntool := NewSearchTool(logger) // KnowledgeService never wired\n// after\ntool := NewSearchTool(logger, WithKnowledgeService(knowledgeService))","handlingStrategy":"validation","validationCode":"if knowledgeService == nil {\n    return fmt.Errorf(\"knowledge service not configured; check DI wiring/config\")\n}","typeGuard":null,"tryCatchPattern":"knowledge, err := authorizeKnowledgeInSearchTargets(ctx, targets, kbID, svc)\nif err != nil && strings.Contains(err.Error(), \"knowledge service is unavailable\") {\n    return nil, fmt.Errorf(\"knowledge feature disabled or misconfigured: %w\", err)\n}","preventionTips":["Fail fast at startup with a health check that asserts required services are non-nil.","Wire KnowledgeService explicitly in DI/container setup and cover it with a wiring test.","Gate knowledge-dependent features behind a config flag so they aren't reachable when the service is absent."],"tags":["knowledge-base","dependency-injection","nil-dependency","go"],"backgroundTag":"nil-service-dependency","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}