Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

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

nullkey445a4330fa3e
diff --git a/internal/application/repository/chunk.go b/internal/application/repository/chunk.go
index c326f39f4..2ae8ce654 100644
--- a/internal/application/repository/chunk.go
+++ b/internal/application/repository/chunk.go
@@ -14,6 +14,13 @@ import (
"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
type chunkRepository struct {
db *gorm.DB
@@ -58,7 +65,7 @@ func (r *chunkRepository) GetChunkByID(ctx context.Context, tenantID uint64, id
var chunk types.Chunk
if err := r.db.WithContext(ctx).Where("tenant_id = ? AND id = ?", tenantID, id).First(&chunk).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
- return nil, errors.New("chunk not found")
+ return nil, ErrChunkNotFound
}
return nil, err
}
@@ -70,7 +77,7 @@ func (r *chunkRepository) GetChunkByIDOnly(ctx context.Context, id string) (*typ
var chunk types.Chunk
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&chunk).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
- return nil, errors.New("chunk not found")
+ return nil, ErrChunkNotFound
}
return nil, err
}
@@ -82,7 +89,7 @@ func (r *chunkRepository) GetChunkBySeqID(ctx context.Context, tenantID uint64,
var chunk types.Chunk
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) {
- return nil, errors.New("chunk not found")
+ return nil, ErrChunkNotFound
}
return nil, err
}
diff --git a/internal/application/repository/knowledge.go b/internal/application/repository/knowledge.go
index 93838530e..79b6ecf00 100644
--- a/internal/application/repository/knowledge.go
+++ b/internal/application/repository/knowledge.go
@@ -105,8 +105,13 @@ func applyKnowledgeListFilter(query *gorm.DB, filter types.KnowledgeListFilter)
)
}
if filter.Keyword != "" {
- escaped := escapeLikeKeyword(filter.Keyword)
- query = query.Where("(file_name LIKE ? OR title LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
+ // Case-insensitive (LOWER … LIKE LOWER) so keyword search matches
+ // 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
// 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("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 != "" {
- escaped := escapeLikeKeyword(keyword)
- query = query.Where("(knowledges.file_name LIKE ? OR knowledges.title LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
+ escaped := strings.ToLower(escapeLikeKeyword(keyword))
+ 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
@@ -651,8 +656,8 @@ func (r *knowledgeRepository) SearchKnowledgeInScopes(
Where("knowledges.deleted_at IS NULL")
if keyword != "" {
- escaped := escapeLikeKeyword(keyword)
- query = query.Where("(knowledges.file_name LIKE ? OR knowledges.title LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
+ escaped := strings.ToLower(escapeLikeKeyword(keyword))
+ query = query.Where("(LOWER(knowledges.file_name) LIKE ? OR LOWER(knowledges.title) LIKE ?)", "%"+escaped+"%", "%"+escaped+"%")
}
if len(fileTypes) > 0 {
diff --git a/internal/application/service/chunk.go b/internal/application/service/chunk.go
index 4f532a125..e93e16b26 100644
--- a/internal/application/service/chunk.go
+++ b/internal/application/service/chunk.go
@@ -5,6 +5,7 @@ package service
import (
"context"
+ "errors"
"fmt"
"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) {
chunk, err := s.chunkRepository.GetChunkByIDOnly(ctx, id)
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
}
logger.ErrorWithFields(ctx, err, map[string]interface{}{"chunk_id": id})
diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go
index 7fe4529c0..4e3d78fef 100644
--- a/internal/application/service/knowledge.go
+++ b/internal/application/service/knowledge.go
@@ -26,8 +26,11 @@ var (
ErrInvalidFileType = errors.New("unsupported file type")
// ErrInvalidURL is returned when an invalid URL is provided
ErrInvalidURL = errors.New("invalid URL")
- // ErrChunkNotFound is returned when a requested chunk cannot be found
- ErrChunkNotFound = errors.New("chunk not found")
+ // ErrChunkNotFound is returned when a requested chunk cannot be 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 = errors.New("file already exists")
// ErrDuplicateURL is returned when trying to add a URL that already exists
diff --git a/internal/handler/message.go b/internal/handler/message.go
index df444fc2e..c7a34c9a2 100644
--- a/internal/handler/message.go
+++ b/internal/handler/message.go
@@ -8,6 +8,7 @@ import (
"time"
"github.com/gin-gonic/gin"
+ "gorm.io/gorm"
"github.com/Tencent/WeKnora/internal/errors"
"github.com/Tencent/WeKnora/internal/logger"
@@ -171,6 +172,15 @@ func (h *MessageHandler) DeleteMessage(c *gin.Context) {
c.Error(errors.NewNotFoundError(err.Error()))
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)
c.Error(errors.NewInternalServerError(err.Error()))
return
diff --git a/internal/handler/session/stream.go b/internal/handler/session/stream.go
index 2269ecd45..553ee3045 100644
--- a/internal/handler/session/stream.go
+++ b/internal/handler/session/stream.go
@@ -14,6 +14,7 @@ import (
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
"github.com/gin-gonic/gin"
+ "gorm.io/gorm"
)
// ContinueStream godoc
@@ -77,6 +78,16 @@ func (h *Handler) ContinueStream(c *gin.Context) {
c.Error(errors.NewNotFoundError(err.Error()))
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)
c.Error(errors.NewInternalServerError(err.Error()))
return
diff --git a/internal/middleware/kb_access.go b/internal/middleware/kb_access.go
index 95ae514ed..a42cc08a2 100644
--- a/internal/middleware/kb_access.go
+++ b/internal/middleware/kb_access.go
@@ -3,7 +3,6 @@ package middleware
import (
"context"
stderrors "errors"
- "strings"
apprepo "github.com/Tencent/WeKnora/internal/application/repository"
"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
// before this refactor is useful.
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) ||
- stderrors.Is(err, ErrResourceNotFound) ||
- strings.TrimSpace(err.Error()) == "chunk not found"
+ stderrors.Is(err, apprepo.ErrKnowledgeNotFound) ||
+ stderrors.Is(err, apprepo.ErrChunkNotFound) ||
+ stderrors.Is(err, ErrResourceNotFound)
}
// RequireKBAccess returns a gin.HandlerFunc that resolves KB access
diff --git a/internal/middleware/kb_access_test.go b/internal/middleware/kb_access_test.go
index 3cf878b55..6e5d2ec94 100644
--- a/internal/middleware/kb_access_test.go
+++ b/internal/middleware/kb_access_test.go
@@ -237,6 +237,25 @@ func TestRequireKBAccess_OwnKB(t *testing.T) {
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) {
_, c := runGuard(t, 100, "kb-missing", types.OrgRoleViewer, nil, nil, guardOpts{})
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")
}
-func TestIsResourceNotFound_ChunkNotFound(t *testing.T) {
- require.True(t, isResourceNotFound(errors.New("chunk not found")))
-}
-
func TestRequireKBAccess_SharedKB_RewritesTenantContext(t *testing.T) {
share := &stubKBShareForGuard{
permission: map[string]types.OrgMemberRole{"kb-shared": types.OrgRoleEditor},