fix(server): case-insensitive doc keyword search + not-found classification

Make document keyword search case-insensitive (LOWER(...) LIKE LOWER(?)) in the
list filter and SearchKnowledge / SearchKnowledgeInScopes paths so a query and
its differently-cased variant return the same results.

Classify not-found consistently as HTTP 404 (not 500) so clients read a
permanent resource.not_found instead of a retryable 5xx: map the document /
chunk not-found sentinels in the KB-access middleware (typed ErrChunkNotFound
via errors.Is), and return 404 for a missing message on the continue-stream and
message-delete endpoints.
This commit is contained in:
nullkey
2026-06-26 20:40:59 +08:00
committed by lyingbug
parent 8f36ea2229
commit 445a4330fa
8 changed files with 77 additions and 20 deletions
+10 -3
View File
@@ -14,6 +14,13 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
// ErrChunkNotFound is returned when a chunk lookup finds no row. A typed
// sentinel (matching the ErrXNotFound convention used by the other repos)
// so callers can errors.Is it safely through wrapping — replacing the
// previous bare errors.New("chunk not found") that forced fragile
// string-equality matching in the service layer.
var ErrChunkNotFound = errors.New("chunk not found")
// chunkRepository implements the ChunkRepository interface // chunkRepository implements the ChunkRepository interface
type chunkRepository struct { type chunkRepository struct {
db *gorm.DB db *gorm.DB
@@ -58,7 +65,7 @@ func (r *chunkRepository) GetChunkByID(ctx context.Context, tenantID uint64, id
var chunk types.Chunk var chunk types.Chunk
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND id = ?", tenantID, id).First(&chunk).Error; err != nil { if err := r.db.WithContext(ctx).Where("tenant_id = ? AND id = ?", tenantID, id).First(&chunk).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("chunk not found") return nil, ErrChunkNotFound
} }
return nil, err return nil, err
} }
@@ -70,7 +77,7 @@ func (r *chunkRepository) GetChunkByIDOnly(ctx context.Context, id string) (*typ
var chunk types.Chunk var chunk types.Chunk
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&chunk).Error; err != nil { if err := r.db.WithContext(ctx).Where("id = ?", id).First(&chunk).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("chunk not found") return nil, ErrChunkNotFound
} }
return nil, err return nil, err
} }
@@ -82,7 +89,7 @@ func (r *chunkRepository) GetChunkBySeqID(ctx context.Context, tenantID uint64,
var chunk types.Chunk var chunk types.Chunk
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND seq_id = ?", tenantID, seqID).First(&chunk).Error; err != nil { if err := r.db.WithContext(ctx).Where("tenant_id = ? AND seq_id = ?", tenantID, seqID).First(&chunk).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("chunk not found") return nil, ErrChunkNotFound
} }
return nil, err return nil, err
} }
+12 -7
View File
@@ -105,8 +105,13 @@ func applyKnowledgeListFilter(query *gorm.DB, filter types.KnowledgeListFilter)
) )
} }
if filter.Keyword != "" { if filter.Keyword != "" {
escaped := escapeLikeKeyword(filter.Keyword) // Case-insensitive (LOWER … LIKE LOWER) so keyword search matches
query = query.Where("(file_name LIKE ? OR title LIKE ?)", "%"+escaped+"%", "%"+escaped+"%") // regardless of the stored casing — consistent with the sibling
// LOWER() filters in this file and with the client-side `search kb`
// / `search sessions` filters. Plain LIKE is case-sensitive in
// Postgres, which surprised callers searching with lowercase.
escaped := strings.ToLower(escapeLikeKeyword(filter.Keyword))
query = query.Where("(LOWER(file_name) LIKE ? OR LOWER(title) LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
} }
// FileType and Source share the same special-case routing onto `type` for // FileType and Source share the same special-case routing onto `type` for
// the "manual" / "url" values, so callers can pick either control. // the "manual" / "url" values, so callers can pick either control.
@@ -530,10 +535,10 @@ func (r *knowledgeRepository) SearchKnowledge(
Where("knowledge_bases.type = ?", types.KnowledgeBaseTypeDocument). Where("knowledge_bases.type = ?", types.KnowledgeBaseTypeDocument).
Where("knowledges.deleted_at IS NULL") Where("knowledges.deleted_at IS NULL")
// If keyword is provided, filter by file_name or title // If keyword is provided, filter by file_name or title (case-insensitive).
if keyword != "" { if keyword != "" {
escaped := escapeLikeKeyword(keyword) escaped := strings.ToLower(escapeLikeKeyword(keyword))
query = query.Where("(knowledges.file_name LIKE ? OR knowledges.title LIKE ?)", "%"+escaped+"%", "%"+escaped+"%") query = query.Where("(LOWER(knowledges.file_name) LIKE ? OR LOWER(knowledges.title) LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
} }
// If fileTypes is provided, filter by file extension or type // If fileTypes is provided, filter by file extension or type
@@ -651,8 +656,8 @@ func (r *knowledgeRepository) SearchKnowledgeInScopes(
Where("knowledges.deleted_at IS NULL") Where("knowledges.deleted_at IS NULL")
if keyword != "" { if keyword != "" {
escaped := escapeLikeKeyword(keyword) escaped := strings.ToLower(escapeLikeKeyword(keyword))
query = query.Where("(knowledges.file_name LIKE ? OR knowledges.title LIKE ?)", "%"+escaped+"%", "%"+escaped+"%") query = query.Where("(LOWER(knowledges.file_name) LIKE ? OR LOWER(knowledges.title) LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
} }
if len(fileTypes) > 0 { if len(fileTypes) > 0 {
+4 -1
View File
@@ -5,6 +5,7 @@ package service
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"github.com/Tencent/WeKnora/internal/application/service/retriever" "github.com/Tencent/WeKnora/internal/application/service/retriever"
@@ -107,7 +108,9 @@ func (s *chunkService) GetChunkByID(ctx context.Context, id string) (*types.Chun
func (s *chunkService) GetChunkByIDOnly(ctx context.Context, id string) (*types.Chunk, error) { func (s *chunkService) GetChunkByIDOnly(ctx context.Context, id string) (*types.Chunk, error) {
chunk, err := s.chunkRepository.GetChunkByIDOnly(ctx, id) chunk, err := s.chunkRepository.GetChunkByIDOnly(ctx, id)
if err != nil { if err != nil {
if err != nil && err.Error() == "chunk not found" { // errors.Is (not string equality) so the sentinel survives wrapping.
// ErrChunkNotFound aliases the repo sentinel, so this matches directly.
if errors.Is(err, ErrChunkNotFound) {
return nil, ErrChunkNotFound return nil, ErrChunkNotFound
} }
logger.ErrorWithFields(ctx, err, map[string]interface{}{"chunk_id": id}) logger.ErrorWithFields(ctx, err, map[string]interface{}{"chunk_id": id})
+5 -2
View File
@@ -26,8 +26,11 @@ var (
ErrInvalidFileType = errors.New("unsupported file type") ErrInvalidFileType = errors.New("unsupported file type")
// ErrInvalidURL is returned when an invalid URL is provided // ErrInvalidURL is returned when an invalid URL is provided
ErrInvalidURL = errors.New("invalid URL") ErrInvalidURL = errors.New("invalid URL")
// ErrChunkNotFound is returned when a requested chunk cannot be found // ErrChunkNotFound is returned when a requested chunk cannot be found.
ErrChunkNotFound = errors.New("chunk not found") // Aliases the repository sentinel so a chunk-not-found from the repo
// errors.Is-matches at the service and middleware layers (a single
// identity instead of two string-equal-but-distinct errors).
ErrChunkNotFound = repository.ErrChunkNotFound
// ErrDuplicateFile is returned when trying to add a file that already exists // ErrDuplicateFile is returned when trying to add a file that already exists
ErrDuplicateFile = errors.New("file already exists") ErrDuplicateFile = errors.New("file already exists")
// ErrDuplicateURL is returned when trying to add a URL that already exists // ErrDuplicateURL is returned when trying to add a URL that already exists
+10
View File
@@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/gorm"
"github.com/Tencent/WeKnora/internal/errors" "github.com/Tencent/WeKnora/internal/errors"
"github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/logger"
@@ -171,6 +172,15 @@ func (h *MessageHandler) DeleteMessage(c *gin.Context) {
c.Error(errors.NewNotFoundError(err.Error())) c.Error(errors.NewNotFoundError(err.Error()))
return return
} }
if stderrors.Is(err, gorm.ErrRecordNotFound) {
// The message_id doesn't exist under this session — a client error,
// not a server fault. 404 so callers read resource.not_found (a
// permanent condition, not retryable) instead of a 5xx. Mirrors the
// ContinueStream / kb / doc / chunk not-found handling.
logger.Warnf(ctx, "Message not found, session ID: %s, message ID: %s", sessionID, messageID)
c.Error(errors.NewNotFoundError(err.Error()))
return
}
logger.ErrorWithFields(ctx, err, nil) logger.ErrorWithFields(ctx, err, nil)
c.Error(errors.NewInternalServerError(err.Error())) c.Error(errors.NewInternalServerError(err.Error()))
return return
+11
View File
@@ -14,6 +14,7 @@ import (
"github.com/Tencent/WeKnora/internal/types/interfaces" "github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils" secutils "github.com/Tencent/WeKnora/internal/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/gorm"
) )
// ContinueStream godoc // ContinueStream godoc
@@ -77,6 +78,16 @@ func (h *Handler) ContinueStream(c *gin.Context) {
c.Error(errors.NewNotFoundError(err.Error())) c.Error(errors.NewNotFoundError(err.Error()))
return return
} }
if stderrors.Is(err, gorm.ErrRecordNotFound) {
// The message_id doesn't exist (e.g. a wrong / non-persisted id, or an
// expired replay buffer). That is a client error, not a server fault:
// return 404 so callers read resource.not_found (a permanent condition
// they must not retry) instead of a retryable 5xx. Mirrors the
// ErrSessionNotFound branch above and the kb/doc/chunk not-found fix.
logger.Warnf(ctx, "Message not found, session ID: %s, message ID: %s", sessionID, messageID)
c.Error(errors.NewNotFoundError(err.Error()))
return
}
logger.ErrorWithFields(ctx, err, nil) logger.ErrorWithFields(ctx, err, nil)
c.Error(errors.NewInternalServerError(err.Error())) c.Error(errors.NewInternalServerError(err.Error()))
return return
+6 -3
View File
@@ -3,7 +3,6 @@ package middleware
import ( import (
"context" "context"
stderrors "errors" stderrors "errors"
"strings"
apprepo "github.com/Tencent/WeKnora/internal/application/repository" apprepo "github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/config" "github.com/Tencent/WeKnora/internal/config"
@@ -190,9 +189,13 @@ func KBIDFromChunkIDParam(param string, chunkService ChunkLookup) KBIDResolver {
// from forcing every service to standardise on a single error type // from forcing every service to standardise on a single error type
// before this refactor is useful. // before this refactor is useful.
func isResourceNotFound(err error) bool { func isResourceNotFound(err error) bool {
// ErrChunkNotFound is defined in the repository layer and aliased by the
// service; match the canonical repo sentinel so this predicate depends
// only on the repository package (KB / Knowledge / Chunk are all here).
return stderrors.Is(err, apprepo.ErrKnowledgeBaseNotFound) || return stderrors.Is(err, apprepo.ErrKnowledgeBaseNotFound) ||
stderrors.Is(err, ErrResourceNotFound) || stderrors.Is(err, apprepo.ErrKnowledgeNotFound) ||
strings.TrimSpace(err.Error()) == "chunk not found" stderrors.Is(err, apprepo.ErrChunkNotFound) ||
stderrors.Is(err, ErrResourceNotFound)
} }
// RequireKBAccess returns a gin.HandlerFunc that resolves KB access // RequireKBAccess returns a gin.HandlerFunc that resolves KB access
+19 -4
View File
@@ -237,6 +237,25 @@ func TestRequireKBAccess_OwnKB(t *testing.T) {
require.Equal(t, uint64(100), got) require.Equal(t, uint64(100), got)
} }
// TestIsResourceNotFound_RecognisesKnowledgeSentinel pins that a missing
// *document* (knowledge) is treated as not-found, not a transient error.
// Regression: ErrKnowledgeNotFound was absent from the predicate, so
// GET/DELETE /knowledge/:id and chunk list resolved a missing doc into a
// raw 500 instead of a 404 — which the CLI then surfaced as a retryable
// server.error (exit 7), looping agents on a permanently-absent doc.
func TestIsResourceNotFound_RecognisesKnowledgeSentinel(t *testing.T) {
require.True(t, isResourceNotFound(apprepo.ErrKnowledgeNotFound),
"missing document (ErrKnowledgeNotFound) must classify as not-found")
require.True(t, isResourceNotFound(apprepo.ErrKnowledgeBaseNotFound),
"missing KB must still classify as not-found")
require.True(t, isResourceNotFound(apprepo.ErrChunkNotFound),
"missing chunk (ErrChunkNotFound) must classify as not-found — chunk view/by-id resolved a missing chunk into a raw 500 (exit 7) otherwise")
require.True(t, isResourceNotFound(ErrResourceNotFound),
"generic resource-not-found sentinel must still classify as not-found")
require.False(t, isResourceNotFound(errors.New("connection refused")),
"a genuine transient error must NOT be classified as not-found")
}
func TestRequireKBAccess_NotFound_Aborts(t *testing.T) { func TestRequireKBAccess_NotFound_Aborts(t *testing.T) {
_, c := runGuard(t, 100, "kb-missing", types.OrgRoleViewer, nil, nil, guardOpts{}) _, c := runGuard(t, 100, "kb-missing", types.OrgRoleViewer, nil, nil, guardOpts{})
require.True(t, c.IsAborted(), "missing KB must abort") require.True(t, c.IsAborted(), "missing KB must abort")
@@ -245,10 +264,6 @@ func TestRequireKBAccess_NotFound_Aborts(t *testing.T) {
require.False(t, ok, "no access should be stashed on failure") require.False(t, ok, "no access should be stashed on failure")
} }
func TestIsResourceNotFound_ChunkNotFound(t *testing.T) {
require.True(t, isResourceNotFound(errors.New("chunk not found")))
}
func TestRequireKBAccess_SharedKB_RewritesTenantContext(t *testing.T) { func TestRequireKBAccess_SharedKB_RewritesTenantContext(t *testing.T) {
share := &stubKBShareForGuard{ share := &stubKBShareForGuard{
permission: map[string]types.OrgMemberRole{"kb-shared": types.OrgRoleEditor}, permission: map[string]types.OrgMemberRole{"kb-shared": types.OrgRoleEditor},