{"record":{"id":"5300fe4c1ba15924","repo":"Tencent/WeKnora","slug":"custom-agent-configuration-is-required-for-agent-q","errorCode":null,"errorMessage":"custom agent configuration is required for agent QA","messagePattern":"custom agent configuration is required for agent QA","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/service/session_agent_qa.go","lineNumber":38,"sourceCode":"func (s *sessionService) AgentQA(\n\tctx context.Context,\n\treq *types.QARequest,\n\teventBus *event.EventBus,\n) error {\n\tsessionID := req.Session.ID\n\t// Propagate the session ID so stateful sandbox backends (CubeSandbox) can\n\t// bind script execution to a per-session MicroVM instance.\n\tctx = types.WithSessionID(ctx, sessionID)\n\tsessionJSON, err := json.Marshal(req.Session)\n\tif err != nil {\n\t\tlogger.Errorf(ctx, \"Failed to marshal session, session ID: %s, error: %v\", sessionID, err)\n\t\treturn fmt.Errorf(\"failed to marshal session: %w\", err)\n\t}\n\n\t// customAgent is required for AgentQA (handler has already done permission check for shared agent)\n\tif req.CustomAgent == nil {\n\t\tlogger.Warnf(ctx, \"Custom agent not provided for session: %s\", sessionID)\n\t\treturn errors.New(\"custom agent configuration is required for agent QA\")\n\t}\n\n\t// Resolve retrieval tenant using shared helper\n\tagentTenantID := s.resolveRetrievalTenantID(ctx, req)\n\tlogger.Infof(ctx, \"Start agent-based question answering, session ID: %s, agent tenant ID: %d, query: %s, session: %s\",\n\t\tsessionID, agentTenantID, req.Query, string(sessionJSON))\n\n\tvar tenantInfo *types.Tenant\n\tif v := ctx.Value(types.TenantInfoContextKey); v != nil {\n\t\ttenantInfo, _ = v.(*types.Tenant)\n\t}\n\t// When agent belongs to another tenant (shared agent), use agent's tenant for KB/model scope; load tenantInfo if needed\n\tif tenantInfo == nil || tenantInfo.ID != agentTenantID {\n\t\tif s.tenantService != nil {\n\t\t\tif agentTenant, err := s.tenantService.GetTenantByID(ctx, agentTenantID); err == nil && agentTenant != nil {\n\t\t\t\ttenantInfo = agentTenant\n\t\t\t\tlogger.Infof(ctx, \"Using agent tenant info for retrieval scope, tenant ID: %d\", agentTenantID)\n\t\t\t}","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/service/session_agent_qa.go#L20-L56","documentation":"AgentQA requires req.CustomAgent to be set; the handler is expected to have resolved the custom agent and done permission checks for shared agents. If CustomAgent is nil, it logs a warning and returns this error instead of proceeding with retrieval-augmented QA.","triggerScenarios":"Invoking AgentQA on a session whose request lacks a CustomAgent configuration — e.g. calling the service directly, or a handler path that skips agent resolution for non-agent sessions.","commonSituations":"Calling the application service directly in tests or scripts without populating CustomAgent, sessions created without an agent binding, or API clients omitting the custom_agent field.","solutions":["Populate req.CustomAgent with the agent configuration before calling AgentQA.","Verify the handler resolved the agent (including shared-agent permission checks) before invoking the service.","For sessions not bound to an agent, use the non-agent QA path instead."],"exampleFix":"// before\nreq := &types.AgentQARequest{Query: \"what is this doc about?\"}\nans, err := svc.AgentQA(ctx, sessionID, req)\n// after\nagent, err := agentService.GetCustomAgent(ctx, agentID)\nif err != nil { return err }\nreq := &types.AgentQARequest{Query: \"what is this doc about?\", CustomAgent: agent}\nans, err := svc.AgentQA(ctx, sessionID, req)","handlingStrategy":"validation","validationCode":"if req.CustomAgent == nil {\n    return fmt.Errorf(\"custom agent must be resolved before calling AgentQA\")\n}","typeGuard":"func hasCustomAgent(r *types.AgentQARequest) bool { return r != nil && r.CustomAgent != nil }","tryCatchPattern":null,"preventionTips":["Resolve the custom agent (with shared-agent permission checks) in the handler before AgentQA.","Use the non-agent QA path for sessions without an agent binding.","Assert req.CustomAgent != nil in tests covering this service."],"tags":["configuration","agent","qa","go"],"backgroundTag":"missing-required-config","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}