Files
ai_academy/internal/modelcontext/leaks.go
T
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

149 lines
4.5 KiB
Go

// leaks.go reports durable identifiers that survive encoding. The registry
// compacts every identifier it knows about, but a raw UUID can still reach the
// model through text it never registered: an error string, a slug, a tool
// result of unknown shape. Each such occurrence is a place where the text
// codec is doing work that the producer should have done. Reporting them at
// the model-call boundary is how those producers are found and fixed, and it
// is the precondition for eventually retiring text-level compaction.
package modelcontext
import (
"fmt"
"regexp"
"sort"
"strings"
"academy/internal/models/chat"
)
var uuidShapeRE = regexp.MustCompile(
`(?i)\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b`,
)
// IdentifierLeak is one message field that still carries UUID-shaped values
// after EncodeMessages.
type IdentifierLeak struct {
MessageIndex int
Role string
// ToolName is the tool that produced a tool message or the tool an
// assistant call targets; empty for plain content.
ToolName string
// Field names the message part: content, reasoning, multi_content, or
// tool_call_arguments.
Field string
// Registered holds leaked values the registry knows a handle for: the
// codec missed a rewrite site. Unregistered holds values that never
// entered the registry: the producer emitted a raw identifier.
Registered []string
Unregistered []string
}
// LeakedIdentifiers scans an encoded message list for UUID-shaped values.
// User-authored messages are skipped: a user may legitimately paste an ID.
// Everything else the application composed, so any UUID there is a leak.
func (r *Registry) LeakedIdentifiers(messages []chat.Message) []IdentifierLeak {
if r == nil {
return nil
}
var leaks []IdentifierLeak
for i := range messages {
msg := &messages[i]
if msg.Role == "user" {
continue
}
leaks = r.appendLeak(leaks, i, msg.Role, msg.Name, "content", msg.Content)
leaks = r.appendLeak(leaks, i, msg.Role, msg.Name, "reasoning", msg.ReasoningContent)
for _, part := range msg.MultiContent {
if part.Type == "text" {
leaks = r.appendLeak(leaks, i, msg.Role, msg.Name, "multi_content", part.Text)
}
}
for _, call := range msg.ToolCalls {
leaks = r.appendLeak(
leaks, i, msg.Role, call.Function.Name, "tool_call_arguments", call.Function.Arguments,
)
}
}
return leaks
}
func (r *Registry) appendLeak(
leaks []IdentifierLeak, index int, role, toolName, field, text string,
) []IdentifierLeak {
if text == "" {
return leaks
}
matches := uuidShapeRE.FindAllString(text, -1)
if len(matches) == 0 {
return leaks
}
seen := make(map[string]struct{}, len(matches))
leak := IdentifierLeak{MessageIndex: index, Role: role, ToolName: toolName, Field: field}
for _, match := range matches {
key := strings.ToLower(match)
if _, dup := seen[key]; dup {
continue
}
seen[key] = struct{}{}
if r.knowsDurable(match) {
leak.Registered = append(leak.Registered, match)
} else {
leak.Unregistered = append(leak.Unregistered, match)
}
}
sort.Strings(leak.Registered)
sort.Strings(leak.Unregistered)
return append(leaks, leak)
}
// knowsDurable reports whether any handle space has registered the value.
func (r *Registry) knowsDurable(value string) bool {
if r.sources != nil && r.sources.handleForDurable(value) != "" {
return true
}
if _, ok := r.issues.Handle(value); ok {
return true
}
if _, ok := r.mcpServers.Handle(value); ok {
return true
}
_, ok := r.mcpTools.Handle(value)
return ok
}
// maxLeakSamples bounds how many identifiers one summary line quotes.
const maxLeakSamples = 3
// SummarizeLeaks renders leaks as one log line per message field so the
// producing tool or role can be identified without dumping the context.
func SummarizeLeaks(leaks []IdentifierLeak) string {
if len(leaks) == 0 {
return ""
}
lines := make([]string, 0, len(leaks))
for _, leak := range leaks {
origin := leak.Role
if leak.ToolName != "" {
origin += "/" + leak.ToolName
}
lines = append(lines, fmt.Sprintf(
"message[%d] %s %s: %d registered %s, %d unregistered %s",
leak.MessageIndex, origin, leak.Field,
len(leak.Registered), sampleIDs(leak.Registered),
len(leak.Unregistered), sampleIDs(leak.Unregistered),
))
}
return strings.Join(lines, "; ")
}
func sampleIDs(ids []string) string {
if len(ids) == 0 {
return "[]"
}
if len(ids) > maxLeakSamples {
return "[" + strings.Join(ids[:maxLeakSamples], " ") + " ...]"
}
return "[" + strings.Join(ids, " ") + "]"
}