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:
@@ -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
|
||||
|
||||
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user