diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go index e3942c99..31624ce4 100644 --- a/internal/application/service/knowledge_process.go +++ b/internal/application/service/knowledge_process.go @@ -943,7 +943,11 @@ func (s *knowledgeService) ProcessSummaryGeneration(ctx context.Context, t *asyn return nil } if payload.Refresh { - _, err := s.RegenerateKnowledgeSummary(ctx, payload.KnowledgeID) + var err error + ctx, err = restoreSummaryRefreshTenantInfo(ctx, s.tenantRepo, payload.TenantID) + if err == nil { + _, err = s.RegenerateKnowledgeSummary(ctx, payload.KnowledgeID) + } if err != nil { // A retry will move the row back to processing. Keeping failed here // ensures both Redis workers and the Lite executor have a terminal, diff --git a/internal/application/service/knowledge_summary_refresh.go b/internal/application/service/knowledge_summary_refresh.go index dc20da8d..01c910cf 100644 --- a/internal/application/service/knowledge_summary_refresh.go +++ b/internal/application/service/knowledge_summary_refresh.go @@ -16,6 +16,28 @@ type summaryKnowledgeBaseReader interface { GetKnowledgeBaseByID(ctx context.Context, id string) (*types.KnowledgeBase, error) } +// restoreSummaryRefreshTenantInfo rebuilds the tenant context normally added +// by the HTTP authentication middleware. Summary refreshes run in an Asynq +// worker, and the retrieve-engine factory needs the full tenant configuration, +// not only TenantIDContextKey, when it reindexes the summary chunk. +func restoreSummaryRefreshTenantInfo( + ctx context.Context, + tenantRepo interfaces.TenantRepository, + tenantID uint64, +) (context.Context, error) { + if tenantRepo == nil { + return ctx, fmt.Errorf("tenant repository is unavailable") + } + tenant, err := tenantRepo.GetTenantByID(ctx, tenantID) + if err != nil { + return ctx, fmt.Errorf("get tenant %d for summary refresh: %w", tenantID, err) + } + if tenant == nil { + return ctx, fmt.Errorf("tenant %d not found for summary refresh", tenantID) + } + return context.WithValue(ctx, types.TenantInfoContextKey, tenant), nil +} + // enqueueSummaryRefresh marks an existing summary as queued and enqueues an // independent refresh task. The pending status is written before Enqueue // because the Lite executor may run the task synchronously inside Enqueue. diff --git a/internal/application/service/knowledge_summary_refresh_test.go b/internal/application/service/knowledge_summary_refresh_test.go new file mode 100644 index 00000000..9ea6277d --- /dev/null +++ b/internal/application/service/knowledge_summary_refresh_test.go @@ -0,0 +1,60 @@ +package service + +import ( + "context" + "errors" + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/Tencent/WeKnora/internal/types/interfaces" +) + +type summaryRefreshTenantRepo struct { + interfaces.TenantRepository + tenant *types.Tenant + err error + requestedID uint64 +} + +func (r *summaryRefreshTenantRepo) GetTenantByID(_ context.Context, id uint64) (*types.Tenant, error) { + r.requestedID = id + return r.tenant, r.err +} + +func TestRestoreSummaryRefreshTenantInfo(t *testing.T) { + repo := &summaryRefreshTenantRepo{tenant: &types.Tenant{ID: 42}} + ctx, err := restoreSummaryRefreshTenantInfo(context.Background(), repo, 42) + if err != nil { + t.Fatalf("restore tenant context: %v", err) + } + if repo.requestedID != 42 { + t.Fatalf("requested tenant ID = %d, want 42", repo.requestedID) + } + tenant, ok := types.TenantInfoFromContext(ctx) + if !ok || tenant.ID != 42 { + t.Fatalf("tenant context = %#v, %v; want tenant 42", tenant, ok) + } +} + +func TestRestoreSummaryRefreshTenantInfoRejectsMissingTenant(t *testing.T) { + tests := []struct { + name string + repo interfaces.TenantRepository + }{ + {name: "repository unavailable"}, + {name: "tenant lookup failed", repo: &summaryRefreshTenantRepo{err: errors.New("lookup failed")}}, + {name: "tenant not found", repo: &summaryRefreshTenantRepo{}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx, err := restoreSummaryRefreshTenantInfo(context.Background(), tt.repo, 42) + if err == nil { + t.Fatal("expected an error") + } + if _, ok := types.TenantInfoFromContext(ctx); ok { + t.Fatal("tenant info should not be added after a failed lookup") + } + }) + } +}