WeKnora/internal/agent/tools/wiki_source_refs_test.go

179 lines
6.8 KiB
Go
Raw Normal View History

refactor(modelcontext): consolidate source_refs parsing, fixed data_analysis table, and identifier leak report (#3372) * refactor(wiki): consolidate source_refs parsing and resolution source_refs entries ("uuid" or "uuid|title") were parsed by ten independent implementations that disagreed on whitespace handling, on whether a model-supplied title could be trusted, and on how a leading pipe was treated. The scope-enforced and legacy agent resolvers also duplicated the lookup and the routing step looked every document up a second time. - types.ParseWikiSourceRef / FormatWikiSourceRef / WikiSourceRefMatchesKnowledge are now the only readers and writer of the format. - resolveWikiSourceDocuments replaces resolveAuthorizedSourceRefs, resolveSourceRefs and wikiKnowledgeBasesForSourceRefs: one lookup per document, titles always rebuilt from server data, dedupe by canonical ID, unknown documents fail closed in every mode. - removeSourceRef now matches padded or titled refs of a deleted document. * refactor(data_analysis): expose the selected document as a fixed table name The tool required the document ID inside the SQL text and blindly string-replaced it with the physical DuckDB table name, which forced the model-context codec to rewrite handles inside quoted SQL and let the UUID-derived k_<uuid> table name reach the model through stored schema summaries. The document is now selected only by knowledge_id. Every query runs on a dedicated connection where "dataset" is a temporary view over the physical table (DuckDB temporary objects are connection-local, so concurrent sessions stay isolated). The physical name remains accepted for internal callers; any other table fails validation with an error naming the table the model should use. Schema summaries, the schema tool, the analyst prompt and the tool schema all name the same table, so data_analysis.sql no longer carries source handles and its text codec policy is removed. * feat(modelcontext): report raw identifiers that survive encoding Text-level compaction exists because raw UUIDs reach model context through producers the registry never sees: error strings, slugs, results of unknown shape. Retiring it requires knowing where those producers are. Registry.LeakedIdentifiers scans an encoded message list for UUID-shaped values in every application-composed field (user messages are skipped), classifies each as registered (codec missed a rewrite) or unregistered (producer emitted a raw ID), and SummarizeLeaks renders one line per field. The agent engine and the three RAG pipeline call sites log the report as a warning after EncodeMessages. * fix(chat_pipeline): align RAG data analysis with the fixed table contract The RAG data-analysis plugin still asked the planning model to fill in knowledge_id and to write SQL from a prompt that exposed the raw knowledge UUID, then passed the model's JSON straight to Execute. Under the new data_analysis contract a query that names anything but the dataset table is rejected, and the plugin logged that rejection at error level and moved on, so the analysis vanished without any signal. A "no analysis needed" plan took the same error path. The pipeline now owns document selection: the model returns only needs_analysis and sql, the prompt names the dataset table and never shows the knowledge ID or physical table, and the plugin builds the tool input itself. A rejected query is fed back to the model once for repair; a query that still fails is reported through pipelineWarn with the SQL and the tool error, and "not needed" is logged as such instead of as a failure. Covered by unit tests with a scripted model and executor: not-needed, pipeline-owned knowledge_id, one-shot repair, persistent failure surfaced, unparseable plan rejected.
2026-09-17 14:08:16 +00:00
package tools
import (
"context"
"fmt"
"strings"
"testing"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
// mapKnowledgeService resolves documents from a fixed map and counts lookups
// so tests can assert that each ref is resolved exactly once.
type mapKnowledgeService struct {
interfaces.KnowledgeService
docs map[string]*types.Knowledge
lookups int
}
func (s *mapKnowledgeService) GetKnowledgeByIDOnly(_ context.Context, id string) (*types.Knowledge, error) {
s.lookups++
if doc, ok := s.docs[id]; ok {
return doc, nil
}
return nil, fmt.Errorf("record not found")
}
func TestResolveWikiSourceDocumentsRebuildsTitlesAndDedupes(t *testing.T) {
service := &mapKnowledgeService{docs: map[string]*types.Knowledge{
"doc-1": {ID: "doc-1", KnowledgeBaseID: "kb-1", Title: "Real title"},
"doc-2": {ID: "doc-2", KnowledgeBaseID: "kb-1", FileName: "fallback.pdf"},
"doc-3": {ID: "doc-3", KnowledgeBaseID: "kb-2"},
}}
targets := types.SearchTargets{{Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-1"}}
docs, err := resolveWikiSourceDocuments(
context.Background(),
[]string{"doc-1|model-supplied title", " doc-1 ", "doc-2", "", "|orphan title"},
service, targets, true,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
refs := wikiSourceRefs(docs)
want := []string{"doc-1|Real title", "doc-2|fallback.pdf"}
if strings.Join(refs, ",") != strings.Join(want, ",") {
t.Fatalf("refs = %v, want %v", refs, want)
}
if got := wikiSourceKnowledgeIDs(docs); strings.Join(got, ",") != "doc-1,doc-2" {
t.Fatalf("ids = %v", got)
}
// Two distinct documents, each looked up once; the duplicate and the
// empty entries never reach the service.
if service.lookups != 3 {
t.Fatalf("lookups = %d, want 3 (duplicate doc-1 is looked up before dedupe, empties skipped)", service.lookups)
}
}
func TestResolveWikiSourceDocumentsFailsClosedOutsideScope(t *testing.T) {
service := &mapKnowledgeService{docs: map[string]*types.Knowledge{
"doc-3": {ID: "doc-3", KnowledgeBaseID: "kb-2", Title: "Elsewhere"},
}}
targets := types.SearchTargets{{Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-1"}}
if _, err := resolveWikiSourceDocuments(
context.Background(), []string{"doc-3"}, service, targets, true,
); err == nil {
t.Fatal("document outside the Agent scope must be rejected")
}
if _, err := resolveWikiSourceDocuments(
context.Background(), []string{"doc-missing"}, service, targets, false,
); err == nil {
t.Fatal("unknown document must be rejected even without scope enforcement")
}
}
func TestResolveWikiSourceDocumentsWithoutKnowledgeServiceKeepsBareIDs(t *testing.T) {
docs, err := resolveWikiSourceDocuments(
context.Background(), []string{" doc-1|typed title ", "doc-1"}, nil, nil, false,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if refs := wikiSourceRefs(docs); len(refs) != 1 || refs[0] != "doc-1" {
t.Fatalf("refs = %v, want a single bare doc-1 (model title is never trusted)", refs)
}
hints, err := wikiKnowledgeBasesForSourceDocuments(docs, []string{"kb-1"})
if err != nil || len(hints) != 0 {
t.Fatalf("documents with unknown KB must contribute no routing hint: hints=%v err=%v", hints, err)
}
}
func TestWikiKnowledgeBasesForSourceDocuments(t *testing.T) {
docs := []wikiSourceDocument{
{ID: "doc-1", KnowledgeBaseID: "kb-1"},
{ID: "doc-2", KnowledgeBaseID: "kb-1"},
{ID: "doc-3", KnowledgeBaseID: "kb-2"},
}
hints, err := wikiKnowledgeBasesForSourceDocuments(docs, []string{"kb-1", "kb-2"})
if err != nil || strings.Join(hints, ",") != "kb-1,kb-2" {
t.Fatalf("hints = %v err = %v", hints, err)
}
if _, err := wikiKnowledgeBasesForSourceDocuments(docs, []string{"kb-1"}); err == nil {
t.Fatal("a document owned by a KB outside the Wiki scope must fail routing")
}
if hints, err := wikiKnowledgeBasesForSourceDocuments(nil, []string{"kb-1"}); err != nil || hints != nil {
t.Fatalf("no documents must yield no hints: %v %v", hints, err)
}
}
func TestWikiReplaceTextRebuildsSourceRefsFromServer(t *testing.T) {
service := &sourceRefWikiService{page: &types.WikiPage{
KnowledgeBaseID: "kb-1",
Slug: "concept/a",
Content: "old body",
SourceRefs: types.StringArray{"doc-old|Old"},
}}
knowledgeService := &mapKnowledgeService{docs: map[string]*types.Knowledge{
"doc-2": {ID: "doc-2", KnowledgeBaseID: "kb-1", Title: "Server title"},
}}
targets := types.SearchTargets{{Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-1"}}
tool := NewWikiReplaceTextTool(service, []string{"kb-1"}, knowledgeService, NewWikiRouteResolver()).
WithSearchTargets(targets)
result, err := tool.Execute(context.Background(), []byte(
`{"slug":"concept/a","old_text":"old","new_text":"new","source_refs":["doc-2|hallucinated"]}`,
))
if err != nil || result == nil || !result.Success {
t.Fatalf("replace failed: result=%+v err=%v", result, err)
}
if got := strings.Join(service.page.SourceRefs, ","); got != "doc-2|Server title" {
t.Fatalf("source_refs = %q, want server-rebuilt title", got)
}
}
func TestWikiFlagIssueStoresBareAuthorizedKnowledgeIDs(t *testing.T) {
service := &flagIssueWikiService{page: &types.WikiPage{
TenantID: 1, KnowledgeBaseID: "kb-1", Slug: "concept/a",
}}
knowledgeService := &mapKnowledgeService{docs: map[string]*types.Knowledge{
"doc-2": {ID: "doc-2", KnowledgeBaseID: "kb-1", Title: "T"},
}}
targets := types.SearchTargets{{Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-1"}}
tool := NewWikiFlagIssueTool(service, []string{"kb-1"}, NewWikiRouteResolver()).
WithKnowledgeScope(knowledgeService, targets)
result, err := tool.Execute(context.Background(), []byte(
`{"slug":"concept/a","issue_type":"contradiction","description":"d",`+
`"suspected_knowledge_ids":["doc-2|title","doc-2"]}`,
))
if err != nil || result == nil || !result.Success {
t.Fatalf("flag failed: result=%+v err=%v", result, err)
}
if service.issue == nil || strings.Join(service.issue.SuspectedKnowledgeIDs, ",") != "doc-2" {
t.Fatalf("issue = %+v, want deduped bare doc-2", service.issue)
}
result, err = tool.Execute(context.Background(), []byte(
`{"slug":"concept/a","issue_type":"contradiction","description":"d","suspected_knowledge_ids":["doc-unknown"]}`,
))
if err != nil || result == nil || result.Success {
t.Fatalf("unknown suspected document must fail closed: result=%+v err=%v", result, err)
}
}
// flagIssueWikiService is the minimal WikiPageService for wiki_flag_issue.
type flagIssueWikiService struct {
interfaces.WikiPageService
page *types.WikiPage
issue *types.WikiPageIssue
}
func (s *flagIssueWikiService) GetPageBySlug(context.Context, string, string) (*types.WikiPage, error) {
return s.page, nil
}
func (s *flagIssueWikiService) CreateIssue(
_ context.Context, issue *types.WikiPageIssue,
) (*types.WikiPageIssue, error) {
s.issue = issue
return issue, nil
}