2026-08-27 02:30:32 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// resolvingCatalog answers Resolve from a fixed handle → tenant map and records
|
|
|
|
|
// every Bind, which is all bindContentResources exercises.
|
|
|
|
|
type resolvingCatalog struct {
|
|
|
|
|
tenantByRef map[string]uint64
|
|
|
|
|
binds []bindCall
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *resolvingCatalog) Resolve(_ context.Context, ref string) (*types.StoredResource, error) {
|
|
|
|
|
tenantID, ok := c.tenantByRef[ref]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, errors.New("resource not found")
|
|
|
|
|
}
|
|
|
|
|
handle, _ := types.ParseResourcePath(ref)
|
|
|
|
|
return &types.StoredResource{ID: handle, Handle: handle, TenantID: tenantID}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *resolvingCatalog) Bind(_ context.Context, ref, ownerType, ownerID, relation string) error {
|
|
|
|
|
c.binds = append(c.binds, bindCall{ref, ownerType, ownerID, relation})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *resolvingCatalog) Register(
|
|
|
|
|
context.Context, uint64, string, interfaces.ResourceRegistration,
|
|
|
|
|
) (string, error) {
|
|
|
|
|
return "", errors.New("unused")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *resolvingCatalog) ResolvePath(
|
|
|
|
|
_ context.Context, value string,
|
|
|
|
|
) (string, *types.StoredResource, error) {
|
|
|
|
|
return value, nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *resolvingCatalog) Release(context.Context, string, string, string) (int64, error) {
|
|
|
|
|
return -1, nil
|
|
|
|
|
}
|
|
|
|
|
|
fix(knowledge): claim cloned and released document images in the resource catalog (#3752)
* fix(knowledge): claim cloned and released document images in the catalog
#3493 binds images at parse time (bindStoredImages), which covers ingestion
but not the two paths that move image ownership:
Clone. A copied image is written as a new object, yet nothing claimed it for
the destination knowledge, so the KB file proxy answered 403 and cloned
documents rendered without images. The copy now goes through SaveBytes into
the servable exports/ namespace (CopyFile wrote to the knowledge-scoped
upload layout the proxy rejects), is claimed by the destination knowledge,
and CloneChunk / FAQ clone bind the handles left in chunk content and
image_info.
Delete. Release only looked at multimodal ImageInfo, which stays empty for
documents whose images live in markdown alone, so those objects outlived the
knowledge that owned them. The release set is now the union of ImageInfo URLs
and the bindings recorded for the knowledge being removed, with source_file
excluded so reparse and cleanup keep the original document.
The parse-path claim from the earlier attempt is dropped: ProcessDocument
already gets it from bindStoredImages, and the removed test only string
matched that call in the source. Every claim path stays best-effort,
including the clone path, which used to fail the whole clone and delete the
fresh copy when a single Bind errored.
* fix(knowledge): keep markdown-only images out of the pre-reindex cleanup
cleanupKnowledgeResources runs before a knowledge is re-indexed -- a manual
update, a reparse, a move with reparse -- not before it is removed. Unioning
in the catalog bindings there released a solely owned markdown-only
attachment, deleteExtractedImages deleted the file, and the catalog file
service marked the resource deleted. The re-claim that runs after the cleanup
resolves through a state=active lookup, so it could never bind that handle
again: the republished body was left pointing at a deleted image.
Cleanup collects ImageInfo URLs alone again. The paths that do remove a
knowledge entry (executeKnowledgeDelete, ProcessKBDelete) keep the union, which
is what lets markdown-only images be released when the knowledge actually goes
away.
Reported by @lyingbug in review on #3752.
---------
Co-authored-by: tayadwx <tayadwx@users.noreply.github.com>
2026-10-09 05:01:26 +00:00
|
|
|
func (c *resolvingCatalog) ListReferencesByOwner(context.Context, string, ...string) ([]string, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 02:30:32 +00:00
|
|
|
func (c *resolvingCatalog) MarkDeleted(context.Context, string) error { return nil }
|
|
|
|
|
|
|
|
|
|
func (c *resolvingCatalog) CreateAccessGrant(
|
|
|
|
|
context.Context, string, time.Duration,
|
|
|
|
|
) (string, error) {
|
|
|
|
|
return "", errors.New("unused")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *resolvingCatalog) ResolveAccessGrant(
|
|
|
|
|
context.Context, string,
|
|
|
|
|
) (*types.StoredResource, error) {
|
|
|
|
|
return nil, errors.New("unused")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func contentRef(char string) string {
|
|
|
|
|
return types.BuildResourcePath(strings.Repeat(char, types.ResourceHandleLength))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Saving a chat answer into the knowledge base must claim the files it shows
|
|
|
|
|
// without copying them: the assistant message keeps its own claim, so either
|
|
|
|
|
// side can be deleted without breaking the other.
|
|
|
|
|
func TestBindContentResourcesClaimsEveryReferencedFile(t *testing.T) {
|
|
|
|
|
chart := contentRef("a")
|
|
|
|
|
table := contentRef("b")
|
|
|
|
|
catalog := &resolvingCatalog{tenantByRef: map[string]uint64{chart: 7, table: 7}}
|
|
|
|
|
svc := &knowledgeService{resourceCatalog: catalog}
|
|
|
|
|
|
|
|
|
|
content := "## 结论\n\n\n\n数据见 [表格](" + table + ")"
|
|
|
|
|
svc.bindContentResources(context.Background(), 7, "kn-1", content)
|
|
|
|
|
|
|
|
|
|
if len(catalog.binds) != 2 {
|
|
|
|
|
t.Fatalf("binds = %v, want one per reference", catalog.binds)
|
|
|
|
|
}
|
|
|
|
|
for i, want := range []string{chart, table} {
|
|
|
|
|
got := catalog.binds[i]
|
|
|
|
|
if got.ref != want {
|
|
|
|
|
t.Fatalf("binds[%d].ref = %q, want %q", i, got.ref, want)
|
|
|
|
|
}
|
|
|
|
|
if got.ownerType != types.ResourceOwnerKnowledge || got.ownerID != "kn-1" {
|
|
|
|
|
t.Fatalf("binds[%d] owner = (%q, %q), want (%q, kn-1)",
|
|
|
|
|
i, got.ownerType, got.ownerID, types.ResourceOwnerKnowledge)
|
|
|
|
|
}
|
|
|
|
|
if got.relation != types.ResourceRelationAttachment {
|
|
|
|
|
t.Fatalf("binds[%d].relation = %q, want %q",
|
|
|
|
|
i, got.relation, types.ResourceRelationAttachment)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A handle pasted from another workspace must not be claimed: binding it would
|
|
|
|
|
// hand the caller a file it may not read.
|
|
|
|
|
func TestBindContentResourcesSkipsForeignAndUnknownHandles(t *testing.T) {
|
|
|
|
|
mine := contentRef("c")
|
|
|
|
|
theirs := contentRef("d")
|
|
|
|
|
unknown := contentRef("e")
|
|
|
|
|
catalog := &resolvingCatalog{tenantByRef: map[string]uint64{mine: 7, theirs: 9}}
|
|
|
|
|
svc := &knowledgeService{resourceCatalog: catalog}
|
|
|
|
|
|
|
|
|
|
content := "  "
|
|
|
|
|
svc.bindContentResources(context.Background(), 7, "kn-1", content)
|
|
|
|
|
|
|
|
|
|
if len(catalog.binds) != 1 || catalog.binds[0].ref != mine {
|
|
|
|
|
t.Fatalf("binds = %v, want only %q", catalog.binds, mine)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBindContentResourcesIsInertWithoutCatalog(t *testing.T) {
|
|
|
|
|
svc := &knowledgeService{}
|
|
|
|
|
// Must not panic; a deployment without a resource registry has nothing to
|
|
|
|
|
// claim and keeps the pre-binding behaviour.
|
|
|
|
|
svc.bindContentResources(context.Background(), 7, "kn-1", "+")")
|
|
|
|
|
}
|
fix(knowledge): claim cloned and released document images in the resource catalog (#3752)
* fix(knowledge): claim cloned and released document images in the catalog
#3493 binds images at parse time (bindStoredImages), which covers ingestion
but not the two paths that move image ownership:
Clone. A copied image is written as a new object, yet nothing claimed it for
the destination knowledge, so the KB file proxy answered 403 and cloned
documents rendered without images. The copy now goes through SaveBytes into
the servable exports/ namespace (CopyFile wrote to the knowledge-scoped
upload layout the proxy rejects), is claimed by the destination knowledge,
and CloneChunk / FAQ clone bind the handles left in chunk content and
image_info.
Delete. Release only looked at multimodal ImageInfo, which stays empty for
documents whose images live in markdown alone, so those objects outlived the
knowledge that owned them. The release set is now the union of ImageInfo URLs
and the bindings recorded for the knowledge being removed, with source_file
excluded so reparse and cleanup keep the original document.
The parse-path claim from the earlier attempt is dropped: ProcessDocument
already gets it from bindStoredImages, and the removed test only string
matched that call in the source. Every claim path stays best-effort,
including the clone path, which used to fail the whole clone and delete the
fresh copy when a single Bind errored.
* fix(knowledge): keep markdown-only images out of the pre-reindex cleanup
cleanupKnowledgeResources runs before a knowledge is re-indexed -- a manual
update, a reparse, a move with reparse -- not before it is removed. Unioning
in the catalog bindings there released a solely owned markdown-only
attachment, deleteExtractedImages deleted the file, and the catalog file
service marked the resource deleted. The re-claim that runs after the cleanup
resolves through a state=active lookup, so it could never bind that handle
again: the republished body was left pointing at a deleted image.
Cleanup collects ImageInfo URLs alone again. The paths that do remove a
knowledge entry (executeKnowledgeDelete, ProcessKBDelete) keep the union, which
is what lets markdown-only images be released when the knowledge actually goes
away.
Reported by @lyingbug in review on #3752.
---------
Co-authored-by: tayadwx <tayadwx@users.noreply.github.com>
2026-10-09 05:01:26 +00:00
|
|
|
|
|
|
|
|
func TestBindChunkResourcesClaimsContentAndImageInfo(t *testing.T) {
|
|
|
|
|
fromContent := contentRef("p")
|
|
|
|
|
fromInfo := contentRef("q")
|
|
|
|
|
catalog := &resolvingCatalog{tenantByRef: map[string]uint64{fromContent: 7, fromInfo: 7}}
|
|
|
|
|
svc := &knowledgeService{resourceCatalog: catalog}
|
|
|
|
|
|
|
|
|
|
svc.bindChunkResources(context.Background(), 7, "kn-clone", []*types.Chunk{
|
|
|
|
|
{Content: ""},
|
|
|
|
|
{ImageInfo: `[{"url":"` + fromInfo + `"}]`},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if len(catalog.binds) != 2 {
|
|
|
|
|
t.Fatalf("binds = %v, want content and image_info handles", catalog.binds)
|
|
|
|
|
}
|
|
|
|
|
}
|