2026-07-26 17:01:19 +00:00
|
|
|
package modelcontext
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/models/chat"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestRegistryRoundTripAndDeduplicate(t *testing.T) {
|
2026-07-26 17:01:19 +00:00
|
|
|
r := newResourceRegistry()
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
ref := "resource://AbCdEfGhIjKlMnOpQrStUv"
|
|
|
|
|
encoded := r.EncodeText(" and " + ref)
|
2026-07-15 08:55:39 +00:00
|
|
|
require.Equal(t, " and res://0001", encoded)
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
require.Equal(t, " and "+ref, r.DecodeText(encoded))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRegistryAliasesLegacyPhysicalReferencesDuringRollout(t *testing.T) {
|
2026-07-26 17:01:19 +00:00
|
|
|
r := newResourceRegistry()
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
ref := "storage://c0d93536-702c-4977-aa5e-fe670073c3cb/local://10000/exports/image.png"
|
|
|
|
|
encoded := r.EncodeText("")
|
2026-07-15 08:55:39 +00:00
|
|
|
require.Equal(t, "", encoded)
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
require.Equal(t, "", r.DecodeText(encoded))
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-24 07:06:49 +00:00
|
|
|
func TestRegistryAliasesWikiSummarySlug(t *testing.T) {
|
2026-07-26 17:01:19 +00:00
|
|
|
r := newResourceRegistry()
|
2026-07-24 07:06:49 +00:00
|
|
|
slug := "summary/07a20bb1-a662-47cf-9929-06fb5d5b5b5e"
|
|
|
|
|
// Inside a [[slug|display]] link the model must copy verbatim.
|
|
|
|
|
encoded := r.EncodeText("see [[" + slug + "|Foo.md - Summary]]")
|
|
|
|
|
require.Equal(t, "see [[res://0001|Foo.md - Summary]]", encoded)
|
|
|
|
|
require.Equal(t, "see [["+slug+"|Foo.md - Summary]]", r.DecodeText(encoded))
|
|
|
|
|
|
2026-07-26 17:17:36 +00:00
|
|
|
// The same slug as a tool-call argument value resolves to one handle.
|
2026-07-24 07:06:49 +00:00
|
|
|
require.Equal(t, "res://0001", r.EncodeText(slug))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRegistryLeavesEntitySlugUntouched(t *testing.T) {
|
2026-07-26 17:01:19 +00:00
|
|
|
r := newResourceRegistry()
|
2026-07-24 07:06:49 +00:00
|
|
|
// Entity slugs are low-entropy and semantically meaningful; not aliased.
|
|
|
|
|
entity := "[[entity/weknora-error-log|WeKnora 试错记录]]"
|
|
|
|
|
require.Equal(t, entity, r.EncodeText(entity))
|
|
|
|
|
}
|
|
|
|
|
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
func TestRegistryEncodesMessageCopies(t *testing.T) {
|
2026-07-26 17:01:19 +00:00
|
|
|
r := newResourceRegistry()
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
ref := "resource://AbCdEfGhIjKlMnOpQrStUv"
|
|
|
|
|
original := []chat.Message{{Role: "tool", Content: ref}}
|
|
|
|
|
encoded := r.EncodeMessages(original)
|
|
|
|
|
require.Equal(t, ref, original[0].Content)
|
2026-07-15 08:55:39 +00:00
|
|
|
require.Equal(t, "res://0001", encoded[0].Content)
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStreamDecoderRestoresSplitAlias(t *testing.T) {
|
2026-07-26 17:01:19 +00:00
|
|
|
r := newResourceRegistry()
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
ref := "resource://AbCdEfGhIjKlMnOpQrStUv"
|
2026-07-15 08:55:39 +00:00
|
|
|
require.Equal(t, "res://0001", r.EncodeText(ref))
|
2026-07-26 17:01:19 +00:00
|
|
|
d := newResourceStreamDecoder(r)
|
2026-07-15 08:55:39 +00:00
|
|
|
require.Equal(t, "before ", d.Feed("before res://0"))
|
feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored
objects, decoupling API responses, persisted rich text, and LLM prompts from
physical storage paths. Adds a resource catalog service (register/resolve/bind,
revocable expiring access grants), a request-local alias registry that compacts
resource references while in the LLM context and restores them (including across
streaming chunks), plus router, IM, chat-pipeline, and multimodal integration.
Includes schema migrations (resources, resource_bindings, resource_access_grants)
for sqlite, and hardens URL/output security handling.
2026-07-15 08:31:21 +00:00
|
|
|
require.Equal(t, ref+" afte", d.Feed("001 after"))
|
|
|
|
|
require.Equal(t, "r", d.Flush())
|
|
|
|
|
}
|
2026-07-15 08:49:43 +00:00
|
|
|
|
2026-07-26 16:59:38 +00:00
|
|
|
func TestStreamDecoderRestoresAliasSplitBeforeScheme(t *testing.T) {
|
|
|
|
|
const ref = "resource://AbCdEfGhIjKlMnOpQrStUv"
|
2026-07-26 17:01:19 +00:00
|
|
|
r := newResourceRegistry()
|
2026-07-26 16:59:38 +00:00
|
|
|
require.Equal(t, "res://0001", r.EncodeText(ref))
|
2026-07-26 17:01:19 +00:00
|
|
|
d := newResourceStreamDecoder(r)
|
2026-07-26 16:59:38 +00:00
|
|
|
require.Equal(t, "see ", d.Feed("see re"))
|
|
|
|
|
require.Equal(t, ref, d.Feed("s://0001"))
|
|
|
|
|
require.Empty(t, d.Flush())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 08:49:43 +00:00
|
|
|
func TestOrphanAliasesReportsUnresolvableTokens(t *testing.T) {
|
2026-07-26 17:01:19 +00:00
|
|
|
r := newResourceRegistry()
|
2026-07-15 08:49:43 +00:00
|
|
|
ref := "resource://AbCdEfGhIjKlMnOpQrStUv"
|
2026-07-15 08:55:39 +00:00
|
|
|
require.Equal(t, "res://0001", r.EncodeText(ref))
|
2026-07-15 08:49:43 +00:00
|
|
|
|
2026-07-26 17:17:36 +00:00
|
|
|
// Known handle resolves and leaves no orphan once decoded.
|
|
|
|
|
require.Nil(t, r.OrphanHandles(r.DecodeText("see res://0001")))
|
2026-07-15 08:49:43 +00:00
|
|
|
|
|
|
|
|
// A reference the registry never assigned is reported (deduplicated).
|
2026-07-26 17:17:36 +00:00
|
|
|
require.Equal(t, []string{"res://0099"}, r.OrphanHandles("look at res://0099 and res://0099"))
|
2026-07-15 08:49:43 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 16:59:38 +00:00
|
|
|
func TestStripOrphanAliasesRemovesIncompleteNumericHandle(t *testing.T) {
|
2026-07-26 17:01:19 +00:00
|
|
|
r := newResourceRegistry()
|
2026-07-26 17:17:36 +00:00
|
|
|
require.Equal(t, "broken ", r.StripOrphanHandles("broken res://1"))
|
2026-07-26 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-15 08:49:43 +00:00
|
|
|
func TestOrphanAliasesNilRegistry(t *testing.T) {
|
2026-07-26 17:01:19 +00:00
|
|
|
var r *resourceRegistry
|
2026-07-26 17:17:36 +00:00
|
|
|
require.Equal(t, []string{"res://0001"}, r.OrphanHandles("res://0001"))
|
2026-07-15 08:49:43 +00:00
|
|
|
}
|