修主人报的硬故障「后台思考的主动推送一个都没有推出来」(根因两条,缺一不可): - 后端 gateway/proactive.go 只广播 proactive_message 帧、**从不写会话历史** ⇒ 刷新补不回来 - 前端**零消费点** ⇒ 帧被静默丢弃(既无气泡也无通知) 后端:新增 proactive_persist.go(**先落库、再发帧**;web/assistant/mode=chat;未接线时空操作且不谎称已落库) 载荷新增 id/createdAt(落库行权威 id);PushResult.Detail 三态如实分开报告「投递到几个连接」与「是否落库」 前端:handlers/proactive.ts(气泡**恒落** + 按落库 id 去重 + **非前台才补通知** + 拒绝授权/畸形载荷可见降级) + lib/notify/proactive.ts(纯逻辑 isBackground/decideProactiveNotice/有界去重表) + handlers/task.ts(另三个被丢的帧 task_done/automation_result/subagent_result) + ws/resync.ts + useWsResync.ts(**重连重跑本页引导读取**,首次连接不跑) 双份核查(带三层证据):现状不双份 —— 路由分叉「一次 Push 至多一个 sink 成功」,且 IM 路由本来就把主动消息镜像回主会话恰好一次 契约 v0.72;变异实测:只发帧不落库 / 写成 user 角色 / 去重失效 / 前台也弹 / IM 成功再打活跃 ⇒ 全部打红
145 lines
3.4 KiB
Go
145 lines
3.4 KiB
Go
package backthink
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"io"
|
|
"log/slog"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/db"
|
|
)
|
|
|
|
// mockThinker 记录每次 Think 调用并返回固定结论。它
|
|
// 尊重 ctx 取消,好让 Stop 能排空挂住的轮次。
|
|
type mockThinker struct {
|
|
mu sync.Mutex
|
|
text string
|
|
err error
|
|
sleep time.Duration
|
|
calls int
|
|
last string
|
|
}
|
|
|
|
func (m *mockThinker) Think(ctx context.Context, sessionID, trigger string) (string, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return "", err
|
|
}
|
|
if m.sleep > 0 {
|
|
select {
|
|
case <-ctx.Done():
|
|
return "", ctx.Err()
|
|
case <-time.After(m.sleep):
|
|
}
|
|
}
|
|
m.mu.Lock()
|
|
m.calls++
|
|
m.last = trigger
|
|
m.mu.Unlock()
|
|
return m.text, m.err
|
|
}
|
|
|
|
func (m *mockThinker) count() int { m.mu.Lock(); defer m.mu.Unlock(); return m.calls }
|
|
func (m *mockThinker) lastTrigger() string { m.mu.Lock(); defer m.mu.Unlock(); return m.last }
|
|
|
|
// mockState 返回固定的 UserState。
|
|
type mockState struct{ state UserState }
|
|
|
|
func (m *mockState) Current(context.Context) (UserState, error) { return m.state, nil }
|
|
|
|
// mockPusher 记录 Push 调用并返回固定结果。
|
|
type mockPusher struct {
|
|
mu sync.Mutex
|
|
result PushResult
|
|
calls int
|
|
lastMsg adapter.UnifiedMessage
|
|
lastHint string
|
|
}
|
|
|
|
func (p *mockPusher) Push(ctx context.Context, req PushRequest) PushResult {
|
|
p.mu.Lock()
|
|
p.calls++
|
|
p.lastMsg = req.Message
|
|
p.lastHint = req.ChannelHint
|
|
p.mu.Unlock()
|
|
return p.result
|
|
}
|
|
|
|
func (p *mockPusher) count() int { p.mu.Lock(); defer p.mu.Unlock(); return p.calls }
|
|
|
|
// mockActive 实现 ActiveClient。
|
|
type mockActive struct {
|
|
mu sync.Mutex
|
|
n int
|
|
persisted bool
|
|
err error
|
|
calls int
|
|
last adapter.UnifiedMessage
|
|
}
|
|
|
|
func (m *mockActive) SendProactive(ctx context.Context, msg adapter.UnifiedMessage) (ProactiveDelivery, error) {
|
|
m.mu.Lock()
|
|
m.calls++
|
|
m.last = msg
|
|
n, persisted, err := m.n, m.persisted, m.err
|
|
m.mu.Unlock()
|
|
return ProactiveDelivery{Connections: n, Persisted: persisted}, err
|
|
}
|
|
|
|
// mockIMAdapter 实现 IMAdapter。
|
|
type mockIMAdapter struct {
|
|
mu sync.Mutex
|
|
err error
|
|
calls int
|
|
last adapter.UnifiedMessage
|
|
}
|
|
|
|
func (m *mockIMAdapter) SendProactive(ctx context.Context, msg adapter.UnifiedMessage) error {
|
|
m.mu.Lock()
|
|
m.calls++
|
|
m.last = msg
|
|
m.mu.Unlock()
|
|
return m.err
|
|
}
|
|
|
|
func (m *mockIMAdapter) count() int { m.mu.Lock(); defer m.mu.Unlock(); return m.calls }
|
|
|
|
// mockGate 实现 PushGate。
|
|
type mockGate struct{ allow bool }
|
|
|
|
func (g *mockGate) Allow(context.Context, PushKind) bool { return g.allow }
|
|
func (g *mockGate) RecordPush(PushKind) {}
|
|
|
|
// newTestDB 为单个测试打开一个隔离的内存 SQLite 句柄,并在
|
|
// 清理时关闭它。
|
|
func newTestDB(t *testing.T) *sql.DB {
|
|
t.Helper()
|
|
h, err := db.Open(context.Background(), ":memory:", nil)
|
|
if err != nil {
|
|
t.Fatalf("open test db: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = h.Close() })
|
|
return h
|
|
}
|
|
|
|
// discardLog 返回一个什么都不写的日志器。
|
|
func discardLog() *slog.Logger {
|
|
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
}
|
|
|
|
// eventually 轮询 cond 直到它为真或超过期限。
|
|
func eventually(t *testing.T, timeout time.Duration, cond func() bool) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(timeout)
|
|
for time.Now().Before(deadline) {
|
|
if cond() {
|
|
return
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
t.Fatalf("condition not met within %v", timeout)
|
|
}
|