主人 2026-10-03 拍板「注释一律中文」;本模块按长期分批协议(一个模块一次派工)执行。 注释行数 1329 → 1329 逐行 1:1(含中文 33 → 1304);行尾注释 5 条一并译。 校验:机械校验(非注释改动行)= 空;独立 Go AST 程序(剥注释复渲)证明 44/44 文件代码全等; 全树 go test ./... 58 包 ok / 0 FAIL;交叉编译绿。 gofmt:capacity_loop_test.go / flow.go 改动前即未格式化,按铁律保留原样未动。
245 lines
9.0 KiB
Go
245 lines
9.0 KiB
Go
package loop
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/capability/registry"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/capability/restrict"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/tool"
|
|
)
|
|
|
|
// newPolicyDispatcher 构建全新的 registry + dispatcher,并为每个
|
|
// 名字注册一个 mock 工具,两者都返回,使测试能在演练
|
|
// design/11 §3 安全门之前在 registry 上设置群聊/陌生人标记。
|
|
func newPolicyDispatcher(t *testing.T, names []string) (*registry.Registry, *tool.Dispatcher) {
|
|
t.Helper()
|
|
reg := registry.New()
|
|
d, err := tool.New(reg)
|
|
if err != nil {
|
|
t.Fatalf("tool.New: %v", err)
|
|
}
|
|
for _, name := range names {
|
|
if err := d.Register(&mockTool{name: name}); err != nil {
|
|
t.Fatalf("Register %q: %v", name, err)
|
|
}
|
|
}
|
|
return reg, d
|
|
}
|
|
|
|
// expectToolNames 断言 got 恰好携带 want 中的工具名,顺序不限。
|
|
// registry 的 List 遍历 map,因此注册顺序不保留;
|
|
// 集合比较使这些断言与顺序无关。
|
|
func expectToolNames(t *testing.T, got []model.ToolSpec, want ...string) {
|
|
t.Helper()
|
|
if len(got) != len(want) {
|
|
t.Fatalf("tool schema count = %d, want %d (%v)", len(got), len(want), want)
|
|
}
|
|
wantSet := make(map[string]bool, len(want))
|
|
for _, n := range want {
|
|
wantSet[n] = true
|
|
}
|
|
for _, s := range got {
|
|
if !wantSet[s.Name] {
|
|
t.Fatalf("unexpected tool %q in schemas %v", s.Name, toolNames(got))
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestToolSchemas_RestrictedEnv_FiltersByMarkers 证明群聊/陌生人会话
|
|
// 只被展示其可编辑标记允许该环境的工具,而
|
|
// owner 会话保留全集(design/11 §3)。
|
|
func TestToolSchemas_RestrictedEnv_FiltersByMarkers(t *testing.T) {
|
|
t.Parallel()
|
|
reg, d := newPolicyDispatcher(t, []string{"groupOnly", "strangerOnly", "both", "ownerOnly"})
|
|
mustSetPolicy(t, reg, "groupOnly", registry.ToolPolicy{AllowInGroup: true})
|
|
mustSetPolicy(t, reg, "strangerOnly", registry.ToolPolicy{AllowInStranger: true})
|
|
mustSetPolicy(t, reg, "both", registry.ToolPolicy{AllowInGroup: true, AllowInStranger: true})
|
|
// ownerOnly 保持默认关闭(零标记)。
|
|
|
|
l := New(Options{Tools: d})
|
|
|
|
group := l.toolSchemas(WithSessionEnv(context.Background(),
|
|
SessionEnv{ActorEnv: restrict.ActorGroupMember, MemWritable: false}), nil)
|
|
expectToolNames(t, group, "groupOnly", "both")
|
|
|
|
stranger := l.toolSchemas(WithSessionEnv(context.Background(),
|
|
SessionEnv{ActorEnv: restrict.ActorStranger, MemWritable: false}), nil)
|
|
expectToolNames(t, stranger, "strangerOnly", "both")
|
|
|
|
owner := l.toolSchemas(WithSessionEnv(context.Background(), OwnerEnv()), nil)
|
|
expectToolNames(t, owner, "groupOnly", "strangerOnly", "both", "ownerOnly")
|
|
}
|
|
|
|
// TestToolSchemas_DefaultClosed_UnmarkedToolHidden 证明默认关闭
|
|
// 标记:未标记的工具不向群聊/陌生人会话广告,因此
|
|
// 会话连看都看不到(更别说调用)主人专属能力。
|
|
func TestToolSchemas_DefaultClosed_UnmarkedToolHidden(t *testing.T) {
|
|
t.Parallel()
|
|
reg, d := newPolicyDispatcher(t, []string{"secret", "shared"})
|
|
// 只有 shared 对群聊开放;secret 默认关闭。
|
|
mustSetPolicy(t, reg, "shared", registry.ToolPolicy{AllowInGroup: true})
|
|
l := New(Options{Tools: d})
|
|
|
|
ctx := WithSessionEnv(context.Background(),
|
|
SessionEnv{ActorEnv: restrict.ActorGroupMember, MemWritable: false})
|
|
expectToolNames(t, l.toolSchemas(ctx, nil), "shared")
|
|
}
|
|
|
|
// TestRun_Restricted_NotAllowed_BlockedAndAudited 证明群聊会话调用
|
|
// 主人专属工具绝不执行它:该调用作为软 not_allowed
|
|
// 结果回灌(不 panic/崩溃),并为该拒绝记录一个
|
|
// AuditEvent。
|
|
func TestRun_Restricted_NotAllowed_BlockedAndAudited(t *testing.T) {
|
|
t.Parallel()
|
|
call := `{"tool_calls":[{"name":"ownerOnly","args":{}}]}`
|
|
ad := &mockAdapter{chat: []mgResp{
|
|
{resp: model.Response{Content: call}},
|
|
{resp: model.Response{Content: "done"}},
|
|
}}
|
|
reg, d := newPolicyDispatcher(t, []string{"ownerOnly", "shared"})
|
|
mustSetPolicy(t, reg, "shared", registry.ToolPolicy{AllowInGroup: true})
|
|
|
|
a := newTestAgent(t, ad, d)
|
|
var mu sync.Mutex
|
|
var audited []AuditEvent
|
|
l := New(Options{Agent: a, MaxRounds: 8, OnAudit: func(ev AuditEvent) {
|
|
mu.Lock()
|
|
audited = append(audited, ev)
|
|
mu.Unlock()
|
|
}})
|
|
|
|
ctx := WithSessionEnv(context.Background(),
|
|
SessionEnv{ActorEnv: restrict.ActorGroupMember, MemWritable: false})
|
|
res, err := l.Run(ctx, "sess", []model.Message{model.TextMessage(model.RoleUser, "go")})
|
|
if err != nil {
|
|
t.Fatalf("Run: %v", err)
|
|
}
|
|
if res.Text != "done" {
|
|
t.Fatalf("Text = %q, want done", res.Text)
|
|
}
|
|
if res.ToolCallsExecuted != 0 {
|
|
t.Fatalf("ToolCallsExecuted = %d, want 0 (ownerOnly blocked in group)", res.ToolCallsExecuted)
|
|
}
|
|
|
|
last := ad.reqs[len(ad.reqs)-1].Messages
|
|
lastMsg := last[len(last)-1]
|
|
if !strings.Contains(lastMsg.Content[0].Text, "not_allowed") ||
|
|
!strings.Contains(lastMsg.Content[0].Text, "ownerOnly") {
|
|
t.Fatalf("expected not_allowed soft result, got %q", lastMsg.Content[0].Text)
|
|
}
|
|
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if len(audited) != 1 {
|
|
t.Fatalf("audit events = %d, want 1", len(audited))
|
|
}
|
|
if audited[0].What != auditToolRejectNotAllowed {
|
|
t.Fatalf("audit what = %q, want %q", audited[0].What, auditToolRejectNotAllowed)
|
|
}
|
|
if audited[0].Who != string(restrict.ActorGroupMember) {
|
|
t.Fatalf("audit who = %q, want %q", audited[0].Who, restrict.ActorGroupMember)
|
|
}
|
|
if !strings.Contains(audited[0].Result, "ownerOnly") {
|
|
t.Fatalf("audit result = %q, want to name ownerOnly", audited[0].Result)
|
|
}
|
|
}
|
|
|
|
// TestRun_Restricted_AllowedTool_Executes 证明群聊会话可以调用
|
|
// 设置了 AllowInGroup 标记的工具,且它正常运行。
|
|
func TestRun_Restricted_AllowedTool_Executes(t *testing.T) {
|
|
t.Parallel()
|
|
call := `{"tool_calls":[{"name":"shared","args":{"q":"hi"}}]}`
|
|
ad := &mockAdapter{chat: []mgResp{
|
|
{resp: model.Response{Content: call}},
|
|
{resp: model.Response{Content: "done"}},
|
|
}}
|
|
reg, d := newPolicyDispatcher(t, []string{"shared", "ownerOnly"})
|
|
mustSetPolicy(t, reg, "shared", registry.ToolPolicy{AllowInGroup: true})
|
|
|
|
a := newTestAgent(t, ad, d)
|
|
l := New(Options{Agent: a, MaxRounds: 8})
|
|
|
|
ctx := WithSessionEnv(context.Background(),
|
|
SessionEnv{ActorEnv: restrict.ActorGroupMember, MemWritable: false})
|
|
res, err := l.Run(ctx, "sess", []model.Message{model.TextMessage(model.RoleUser, "go")})
|
|
if err != nil {
|
|
t.Fatalf("Run: %v", err)
|
|
}
|
|
if res.ToolCallsExecuted != 1 {
|
|
t.Fatalf("ToolCallsExecuted = %d, want 1 (shared allowed in group)", res.ToolCallsExecuted)
|
|
}
|
|
if res.Text != "done" {
|
|
t.Fatalf("Text = %q, want done", res.Text)
|
|
}
|
|
}
|
|
|
|
// TestRun_Owner_AllToolsExecute 证明 owner 环境不受
|
|
// 默认关闭标记影响:主人可以调用主人专属(未标记)工具。
|
|
func TestRun_Owner_AllToolsExecute(t *testing.T) {
|
|
t.Parallel()
|
|
call := `{"tool_calls":[{"name":"ownerOnly","args":{}}]}`
|
|
ad := &mockAdapter{chat: []mgResp{
|
|
{resp: model.Response{Content: call}},
|
|
{resp: model.Response{Content: "done"}},
|
|
}}
|
|
_, d := newPolicyDispatcher(t, []string{"ownerOnly"})
|
|
// 默认关闭标记(从未开放):主人仍必须能调用它。
|
|
a := newTestAgent(t, ad, d)
|
|
l := New(Options{Agent: a, MaxRounds: 8})
|
|
|
|
res, err := l.Run(WithSessionEnv(context.Background(), OwnerEnv()), "sess",
|
|
[]model.Message{model.TextMessage(model.RoleUser, "go")})
|
|
if err != nil {
|
|
t.Fatalf("Run: %v", err)
|
|
}
|
|
if res.ToolCallsExecuted != 1 {
|
|
t.Fatalf("ToolCallsExecuted = %d, want 1 (owner unaffected by default-closed)", res.ToolCallsExecuted)
|
|
}
|
|
if res.Text != "done" {
|
|
t.Fatalf("Text = %q, want done", res.Text)
|
|
}
|
|
}
|
|
|
|
// TestRun_Stranger_NotAllowed_Blocked 证明该门同样适用于
|
|
// 陌生人私聊环境,且仅群聊的工具在那里被拒。
|
|
func TestRun_Stranger_NotAllowed_Blocked(t *testing.T) {
|
|
t.Parallel()
|
|
call := `{"tool_calls":[{"name":"groupOnly","args":{}}]}`
|
|
ad := &mockAdapter{chat: []mgResp{
|
|
{resp: model.Response{Content: call}},
|
|
{resp: model.Response{Content: "done"}},
|
|
}}
|
|
reg, d := newPolicyDispatcher(t, []string{"groupOnly", "strangerOk"})
|
|
mustSetPolicy(t, reg, "groupOnly", registry.ToolPolicy{AllowInGroup: true})
|
|
mustSetPolicy(t, reg, "strangerOk", registry.ToolPolicy{AllowInStranger: true})
|
|
|
|
a := newTestAgent(t, ad, d)
|
|
l := New(Options{Agent: a, MaxRounds: 8})
|
|
|
|
ctx := WithSessionEnv(context.Background(),
|
|
SessionEnv{ActorEnv: restrict.ActorStranger, MemWritable: false})
|
|
res, err := l.Run(ctx, "sess", []model.Message{model.TextMessage(model.RoleUser, "go")})
|
|
if err != nil {
|
|
t.Fatalf("Run: %v", err)
|
|
}
|
|
if res.ToolCallsExecuted != 0 {
|
|
t.Fatalf("ToolCallsExecuted = %d, want 0 (groupOnly denied to stranger)", res.ToolCallsExecuted)
|
|
}
|
|
last := ad.reqs[len(ad.reqs)-1].Messages
|
|
if !strings.Contains(last[len(last)-1].Content[0].Text, "not_allowed") {
|
|
t.Fatalf("expected not_allowed soft result, got %q", last[len(last)-1].Content[0].Text)
|
|
}
|
|
}
|
|
|
|
// mustSetPolicy 把 ToolPolicy 应用到具名工具,出错即让测试失败。
|
|
func mustSetPolicy(t *testing.T, reg *registry.Registry, name string, pol registry.ToolPolicy) {
|
|
t.Helper()
|
|
if err := reg.SetPolicy(name, pol); err != nil {
|
|
t.Fatalf("SetPolicy %q: %v", name, err)
|
|
}
|
|
}
|