降噪A(策略一/二/五): - channelbridge/light.go: 非owner分级门——小事件当系统消息登记(不进loop)、非owner轻响应(她判断回/不回,规则优先mention/关键词+轻LLM兜底fail-closed静默) - backthink 触发降噪: SubjectSystemEvent 低价值不唤醒thinking(仅高价值/owner才think), iot/reminder不受影响 - quiet hours: 安静时段非owner无信号静默,被@才回(非硬性静默); config QUIET_START/END_HOUR 单源共用 限流B(策略三/四): - adapter/flowgate.go: per-adapter 滑窗限流器+总上限+审查剔噪闸门(Admit两连:速率+上限/审查), owner私聊绕过 - channelbridge/flowgate.go: onMessage入站最前挂闸, 超限/被剔不占worker槽; 审查模型NewLLMReviewer复用model.Adapter(仅>80%阈值触发+跨分钟节流,失败回退FIFO,绝不判身份) - config: ChannelAccount 加 GroupMsgPerMin(默认6)/MsgPerMin/MaxMessages(默认50), CHANNELS_JSON每账号, 向后兼容 - 被剔消息走OnRejected审计不静默丢 main wire接线: 加modelAdapter字段+Review注入(reviewFor nil-safe,无LLM回退FIFO) 全仓 go build/vet/test 绿(53包)+交叉编译, 零新依赖, 前端无涉
286 lines
10 KiB
Go
286 lines
10 KiB
Go
package channelbridge
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/session"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/history"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/loop"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/user"
|
|
)
|
|
|
|
// stubLightJudge is a configurable LightJudge recording how often it was consulted.
|
|
type stubLightJudge struct {
|
|
resp LightDecision
|
|
err error
|
|
calls int
|
|
}
|
|
|
|
func (s *stubLightJudge) Judge(context.Context, LightInput) (LightDecision, error) {
|
|
s.calls++
|
|
return s.resp, s.err
|
|
}
|
|
|
|
// nonOwnerGroupMsg returns a third-party group message from a member sender, with
|
|
// the given mentions. Content is left untouched so tests control the signal.
|
|
func nonOwnerGroupMsg(content string, mentions ...adapter.Mention) adapter.UnifiedMessage {
|
|
return adapter.UnifiedMessage{
|
|
Platform: "telegram", ChannelID: "-20", ChannelType: adapter.ChannelGroup,
|
|
GroupID: "-20", GroupName: "Gang", SenderID: "9", SenderName: "张三",
|
|
Sender: adapter.Actor{Role: adapter.RoleMember}, Mentions: mentions,
|
|
Content: content, ContentType: adapter.ContentText,
|
|
}
|
|
}
|
|
|
|
// nonOwnerSession is the restricted (group / stranger) session binding used by the
|
|
// non-owner tests: read-only memory, no owner.
|
|
func nonOwnerSession() session.Session {
|
|
return session.Session{SessionID: "sess-g", IsGroup: true, Owner: false, MemWritable: false}
|
|
}
|
|
|
|
func TestNonOwnerSilencedRegisteredOnly(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chat := &stubChatter{res: &loop.Result{Text: "不该回"}}
|
|
sess := &stubSessions{sess: nonOwnerSession()}
|
|
hist := &captureHistory{}
|
|
b, err := New(Options{
|
|
Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist,
|
|
Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
// A non-owner message with no @/keyword/name signal is silenced (设计/12 §6 策略二):
|
|
// it is registered to the isolated session log but never drives the loop.
|
|
b.process(context.Background(), nonOwnerGroupMsg("hello all"))
|
|
|
|
if chat.sessionID != "" || len(chat.messages) != 0 {
|
|
t.Fatalf("loop ran for a no-signal non-owner message: session=%q messages=%+v", chat.sessionID, chat.messages)
|
|
}
|
|
recs := hist.records()
|
|
if len(recs) != 1 {
|
|
t.Fatalf("records = %d, want 1 (inbound registered, no assistant reply)", len(recs))
|
|
}
|
|
if recs[0].SessionID != "sess-g" || recs[0].Role != history.RoleUser || recs[0].Content != "hello all" {
|
|
t.Fatalf("registered record = %+v, want isolated sess-g user 'hello all'", recs[0])
|
|
}
|
|
}
|
|
|
|
func TestNonOwnerMentionResponds(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chat := &stubChatter{res: &loop.Result{Text: "收到"}}
|
|
sess := &stubSessions{sess: nonOwnerSession()}
|
|
hist := &captureHistory{}
|
|
b, err := New(Options{
|
|
Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist,
|
|
Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
// An addressed (mentioned) non-owner message is worth a response (策略二), so it
|
|
// drives the full loop against the isolated group session.
|
|
b.process(context.Background(), nonOwnerGroupMsg("hello", adapter.Mention{UserID: "bot"}))
|
|
|
|
if chat.sessionID != "sess-g" {
|
|
t.Fatalf("loop session = %q, want isolated sess-g", chat.sessionID)
|
|
}
|
|
// Restricted group context → the third-party boundary system message plus the
|
|
// user message (design/16 §4.6 §1), mirroring TestGroupMessageThirdPartySenderNameAndBoundary.
|
|
if len(chat.messages) != 2 || chat.messages[1].Role != model.RoleUser {
|
|
t.Fatalf("loop messages = %+v, want boundary + user", chat.messages)
|
|
}
|
|
if chat.messages[1].Content[0].Text != "[第三方 · 张三] hello" {
|
|
t.Fatalf("loop user text = %q, want [第三方 · 张三] hello", chat.messages[1].Content[0].Text)
|
|
}
|
|
}
|
|
|
|
func TestNonOwnerHighValueKeywordResponds(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chat := &stubChatter{res: &loop.Result{Text: "我看看"}}
|
|
sess := &stubSessions{sess: nonOwnerSession()}
|
|
b, err := New(Options{
|
|
Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{},
|
|
Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
// A high-value keyword (求助) is worth a response even without an @ (策略二).
|
|
b.process(context.Background(), nonOwnerGroupMsg("有人在吗,紧急求助"))
|
|
if chat.sessionID != "sess-g" {
|
|
t.Fatalf("loop session = %q, want isolated sess-g for a high-value keyword", chat.sessionID)
|
|
}
|
|
}
|
|
|
|
func TestNonOwnerQuietHoursSilenced(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chat := &stubChatter{res: &loop.Result{Text: "不该回"}}
|
|
sess := &stubSessions{sess: nonOwnerSession()}
|
|
hist := &captureHistory{}
|
|
b, err := New(Options{
|
|
Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist,
|
|
QuietStartHour: 23, QuietEndHour: 7,
|
|
Now: func() time.Time { return time.Date(2026, 1, 1, 2, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
// 策略五: during quiet hours a no-signal non-owner message is silenced — it is
|
|
// registered but not answered.
|
|
b.process(context.Background(), nonOwnerGroupMsg("hello all"))
|
|
|
|
if chat.sessionID != "" || len(chat.messages) != 0 {
|
|
t.Fatalf("loop ran during quiet hours for a no-signal message: session=%q messages=%+v", chat.sessionID, chat.messages)
|
|
}
|
|
if len(hist.records()) != 1 {
|
|
t.Fatalf("records = %d, want 1 (registered, no reply)", len(hist.records()))
|
|
}
|
|
}
|
|
|
|
func TestNonOwnerQuietHoursMentionResponds(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chat := &stubChatter{res: &loop.Result{Text: "在的"}}
|
|
sess := &stubSessions{sess: nonOwnerSession()}
|
|
b, err := New(Options{
|
|
Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{},
|
|
QuietStartHour: 23, QuietEndHour: 7,
|
|
Now: func() time.Time { return time.Date(2026, 1, 1, 2, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
// 策略五 is not a hard silence: a directly-addressed message during quiet hours
|
|
// still gets a response.
|
|
b.process(context.Background(), nonOwnerGroupMsg("在吗", adapter.Mention{UserID: "bot"}))
|
|
if chat.sessionID != "sess-g" {
|
|
t.Fatalf("loop session = %q, want sess-g (mention should break quiet-hours silence)", chat.sessionID)
|
|
}
|
|
}
|
|
|
|
func TestOwnerUnaffectedInQuietHours(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
chat := &stubChatter{res: &loop.Result{Text: "主人好"}}
|
|
sess := &stubSessions{sess: session.Session{SessionID: "sess-1", Owner: true, MemWritable: true}}
|
|
ad := &fakeAdapter{name: "telegram"}
|
|
b, err := New(Options{
|
|
Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: &captureHistory{},
|
|
QuietStartHour: 23, QuietEndHour: 7,
|
|
Now: func() time.Time { return time.Date(2026, 1, 1, 2, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
b.Add(ad)
|
|
|
|
// An owner message is never gated, even during quiet hours (策略五 only ever
|
|
// governs non-owner traffic): owner-private chat still routes to the main web
|
|
// session and replies.
|
|
b.process(context.Background(), directMessage())
|
|
|
|
if chat.sessionID != history.MainSessionID {
|
|
t.Fatalf("owner loop session = %q, want %q", chat.sessionID, history.MainSessionID)
|
|
}
|
|
if len(ad.sends) != 1 || ad.sends[0].Content != "主人好" {
|
|
t.Fatalf("owner reply = %+v, want '主人好'", ad.sends)
|
|
}
|
|
}
|
|
|
|
func TestLightJudgeConsultedWhenInconclusive(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// A no-signal daytime message is inconclusive to the rules, so the injected
|
|
// LightJudge gets a chance to decide (设计/12 §6 策略二: 规则不确定才轻 LLM).
|
|
judge := &stubLightJudge{resp: LightDecision{Respond: true, Reason: "值得回"}}
|
|
chat := &stubChatter{res: &loop.Result{Text: "嗯"}}
|
|
sess := &stubSessions{sess: nonOwnerSession()}
|
|
b, err := New(Options{
|
|
Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{},
|
|
Light: judge,
|
|
Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
b.process(context.Background(), nonOwnerGroupMsg("今天天气不错"))
|
|
if judge.calls != 1 {
|
|
t.Fatalf("light judge calls = %d, want 1 (inconclusive rules escalated)", judge.calls)
|
|
}
|
|
if chat.sessionID != "sess-g" {
|
|
t.Fatalf("loop session = %q, want sess-g (judge said respond)", chat.sessionID)
|
|
}
|
|
}
|
|
|
|
func TestLightJudgeSilenceIsRespected(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
judge := &stubLightJudge{resp: LightDecision{Respond: false, Reason: "不值得"}}
|
|
chat := &stubChatter{res: &loop.Result{Text: "不该回"}}
|
|
sess := &stubSessions{sess: nonOwnerSession()}
|
|
hist := &captureHistory{}
|
|
b, err := New(Options{
|
|
Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist,
|
|
Light: judge,
|
|
Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
b.process(context.Background(), nonOwnerGroupMsg("今天天气不错"))
|
|
if judge.calls != 1 {
|
|
t.Fatalf("light judge calls = %d, want 1", judge.calls)
|
|
}
|
|
if chat.sessionID != "" {
|
|
t.Fatalf("loop ran after the judge said silence: session=%q", chat.sessionID)
|
|
}
|
|
if len(hist.records()) != 1 {
|
|
t.Fatalf("records = %d, want 1 (registered, no reply)", len(hist.records()))
|
|
}
|
|
}
|
|
|
|
func TestLightJudgeErrorFailsClosedToSilence(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
judge := &stubLightJudge{err: context.DeadlineExceeded}
|
|
chat := &stubChatter{res: &loop.Result{Text: "不该回"}}
|
|
sess := &stubSessions{sess: nonOwnerSession()}
|
|
hist := &captureHistory{}
|
|
b, err := New(Options{
|
|
Loop: chat, Sessions: sess, Owner: &user.OwnerProfile{}, History: hist,
|
|
Light: judge,
|
|
Now: func() time.Time { return time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) },
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
|
|
// A failing light judge is fail-closed toward silence (不打扰为第一原则).
|
|
b.process(context.Background(), nonOwnerGroupMsg("今天天气不错"))
|
|
if judge.calls != 1 {
|
|
t.Fatalf("light judge calls = %d, want 1", judge.calls)
|
|
}
|
|
if chat.sessionID != "" || len(chat.messages) != 0 {
|
|
t.Fatalf("loop ran after judge error: session=%q messages=%+v", chat.sessionID, chat.messages)
|
|
}
|
|
if len(hist.records()) != 1 {
|
|
t.Fatalf("records = %d, want 1 (registered, no reply)", len(hist.records()))
|
|
}
|
|
}
|