Files
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

136 lines
6.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package modelcontext
import (
"fmt"
"strings"
"testing"
"academy/internal/models/chat"
"academy/internal/types"
"github.com/stretchr/testify/require"
)
func TestMCPAnswerCannotBorrowDirectoryOrHistoricalKnowledgeCitations(t *testing.T) {
r := NewRegistry(true)
r.RegisterContextChunk(ChunkReference{ChunkID: "faq-1", DocumentTitle: "什么是 Academy?"})
r.RegisterContextChunk(ChunkReference{ChunkID: "faq-2", DocumentTitle: "如何创建知识库?"})
history := []chat.Message{{Role: "assistant", Content: `物业工作 <kb doc="9月13日周报.docx" chunk_id="weekly-report" />`}}
r.EncodeMessages(history)
const article = "https://km.woa.com/articles/show/669504?jumpfrom=kmmcp"
result := &types.ToolResult{Success: true, Output: "标题: AI 玩法\nAI摘要: Computer Use 案例\n链接: " + article}
modelOutput := r.ModelToolResultForTool("call_mcp_tool", result)
require.True(t, strings.HasPrefix(modelOutput, result.Output), "external payload is preserved")
require.Contains(t, modelOutput, `<source id="url://1" url="`+article+`"/>`)
require.Equal(t, modelOutput, r.ModelToolResultForTool("call_mcp_tool", result),
"rebuilding context keeps stable handles")
r.EncodeMessages(history) // Replaying history must not promote its citations.
raw := `玩法<ref id="chunk://3"/> 动画<ref id="chunk://1"/> 对话<ref id="chunk://2"/> 来源<ref id="url://1"/>`
want := `玩法 动画 对话 来源<web url="` + article + `" title="" />`
require.Equal(t, want, r.DecodeOutputText(raw))
decoder := r.StreamDecoder()
var streamed strings.Builder
for _, char := range raw {
streamed.WriteString(decoder.Feed(string(char)))
}
streamed.WriteString(decoder.Flush())
require.Equal(t, want, streamed.String(), "SSE and final text apply identical evidence checks")
// Directory handles remain usable to retrieve the actual FAQ.
calls := []types.LLMToolCall{{Function: types.FunctionCall{
Name: "list_knowledge_chunks", Arguments: `{"faq_id":"chunk://1"}`,
}}}
r.DecodeToolCalls(calls)
require.JSONEq(t, `{"faq_id":"faq-1"}`, calls[0].Function.Arguments)
r.ModelToolResultForTool("knowledge_search", &types.ToolResult{Success: true, Data: map[string]interface{}{
"display_type": "search_results",
"results": []map[string]interface{}{{
"chunk_id": "faq-1", "knowledge_title": "什么是 Academy?", "content": "知识库管理系统",
}},
}})
r.RegisterContextChunk(ChunkReference{ChunkID: "faq-1"})
r.EncodeMessages(history)
require.Contains(t, r.DecodeOutputText(`<ref id="chunk://1"/>`), `chunk_id="faq-1"`,
"fresh KB evidence is still citable in a mixed-source answer")
require.Empty(t, r.DecodeOutputText(`<ref id="chunk://2"/><ref id="chunk://3"/>`))
}
func TestToolArgumentsAloneDoNotAuthorizeCitations(t *testing.T) {
r := NewRegistry(true)
r.EncodeMessages([]chat.Message{{Role: "assistant", ToolCalls: []chat.ToolCall{
{Function: chat.FunctionCall{Name: "list_knowledge_chunks", Arguments: `{"faq_id":"faq-real"}`}},
{Function: chat.FunctionCall{Name: "web_fetch", Arguments: `{"items":[{"url":"https://example.com/unread"}]}`}},
}}})
require.Empty(t, r.DecodeOutputText(`<ref id="chunk://1"/><ref id="url://1"/>`))
r.ModelToolResultForTool("call_mcp_tool", &types.ToolResult{Success: false, Error: "https://example.com/error"})
require.Empty(t, r.DecodeOutputText(`<ref id="url://2"/>`))
}
func TestCurrentLegacySourceResultsRemainCitableButHistoryDoesNot(t *testing.T) {
for _, output := range []string{
`<chunk chunk_id="chunk-real" knowledge_title="Guide">Evidence</chunk>`,
`Evidence <kb doc="Guide" chunk_id="chunk-real" />`,
} {
r := NewRegistry(true)
r.EncodeMessages([]chat.Message{{Role: "tool", Name: "wiki_read_page", Content: output}})
require.Empty(t, r.DecodeOutputText(`<ref id="chunk://1"/>`))
result := &types.ToolResult{Success: true, Output: output}
modelOutput := r.ModelToolResultForTool("wiki_read_page", result)
require.Contains(t, modelOutput, "chunk://1")
require.Equal(t, output, result.Output)
require.Contains(t, r.DecodeOutputText(`<ref id="chunk://1"/>`), `chunk_id="chunk-real"`)
}
}
func TestMCPSourceCandidatesPreserveExternalLinksAndPayload(t *testing.T) {
for _, tool := range []string{"call_mcp_tool", "mcp_external_search"} {
t.Run(tool, func(t *testing.T) {
r := NewRegistry(true)
body := "[MCP result]\n" + `{"url":"https:\/\/example.com\/article?a=1\u0026b=2","chunk_id":"foreign-id"}` +
"\n[Article](https://example.com/article?a=1&b=2)\n[Wiki](https://example.com/Function_(math))。\n" +
"[HTML](https://example.com/page?x=1&amp;y=2)\n" +
"https://user:pass@example.com/private javascript:alert(1) https://"
result := &types.ToolResult{Success: true, Output: body}
got := r.ModelToolResultForTool(tool, result)
require.Equal(t, body, result.Output)
require.True(t, strings.HasPrefix(got, body))
sidecar := strings.TrimPrefix(got, body)
require.Equal(t, 3, strings.Count(sidecar, "<source "))
require.Contains(t, sidecar, `url="https://example.com/article?a=1&amp;b=2"`)
require.Contains(t, sidecar, `url="https://example.com/Function_(math)"`)
require.Contains(t, sidecar, `url="https://example.com/page?x=1&amp;y=2"`)
require.NotContains(t, sidecar, "user:pass")
require.Empty(t, r.ChunkHandle("foreign-id"), "external IDs never become KB references")
})
}
}
func TestMCPSourceCandidatesAreBoundedAndOnlyForSuccessfulExternalResults(t *testing.T) {
var body strings.Builder
for i := 0; i < maxMCPSourceCandidates+10; i++ {
fmt.Fprintf(&body, "https://example.com/%d\n", i)
}
r := NewRegistry(true)
got := r.ModelToolResultForTool("call_mcp_tool", &types.ToolResult{Success: true, Output: body.String()})
require.Equal(t, maxMCPSourceCandidates, strings.Count(got, "<source "))
for _, tc := range []struct {
tool string
enabled, success bool
}{
{"discover_mcp_tools", true, true},
{"read_file", true, true},
{"call_mcp_tool", false, true},
{"call_mcp_tool", true, false},
} {
r := NewRegistry(tc.enabled)
output := r.ModelToolResultForTool(tc.tool, &types.ToolResult{
Success: tc.success, Output: "https://example.com/page",
})
require.NotContains(t, output, "<external_source_candidates>")
require.Empty(t, r.DecodeOutputText(`<ref id="url://1"/>`))
}
r = NewRegistry(true)
require.Equal(t, "No links", r.ModelToolResultForTool("call_mcp_tool", &types.ToolResult{
Success: true, Output: "No links",
}))
}