feat(backthink): 主动推送每次只发一个平台——决策渠道/最近活跃优先,平台含微信
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

主动推送目标不再取第一个白名单渠道(kook),也不再双目标 web+kook;改为每次只发一个平台,范围=全部已连接白名单渠道(含微信 weixin-ilink)。

backthink/push.go: pusher 单目标路由——web hint 一次 pushActive;im/auto/实体平台 hint 一次性 pushIM (msgs 置哨兵/实体平台),无 IM sink 时诚实降级 pushActive;保留 PushGate 降频与成功 RecordPush。
channelbridge/proactive.go: proactiveTarget 三级选择——①决策 [渠道:xxx] 实体平台且白名单+已连接优先;②最近活跃(主会话最近20条 newest-first 找白名单+已连接平台,web 会话自动跳过);③回退 NextConnectedAccount 用旋转游标避免永远 kook。微信不再排除、可被选中;决策 hint 指定非白名单渠道回退(不报错)。Bridge 新增 proactiveCursor 字段(mu 保护)。
context.go: guidancePrompt 描述改为每次挑一个合适渠道(含微信)。
测试: 决策优先/最近活跃/微信可选中/无信号回退旋转/单目标 等,go build/vet/test 全绿 + windows/darwin 交叉编译。stash@{0} 保留被取代的'web+kook双目标'WIP以备回滚。
This commit is contained in:
Aska committed 2026-09-08 13:34:22 +08:00
1 parent 12031bd13c
commit 3e4efa0e41
8 files changed
+419 -114

No files matched your search

+34 -47
View File
@@ -96,10 +96,11 @@ type PushGate interface {
// PushRequest carries one proactive push attempt: the outbound message plus the
// channel target the autonomous conclusion asked for (design/20 §2 P2).
// ChannelHint is the lowercased [渠道:...] token: ""/auto keep the default
// active-client-first, IM-fallback route; "web" targets the active client; "im"
// targets the IM bridge; a concrete platform id (kook/telegram/...) targets the IM
// bridge with the message tagged for that platform.
// ChannelHint is the lowercased [渠道:...] token: ""/auto pick the default (the
// owner mandate: ONE channel, chosen by her / her most recent platform / the
// whitelist default); "web" targets the active client; "im" targets the IM bridge;
// a concrete platform id (kook/telegram/weixin-ilink/...) targets the IM bridge
// with the message tagged for that platform.
type PushRequest struct {
// Message is the normalized outbound proactive message.
Message adapter.UnifiedMessage
@@ -107,17 +108,19 @@ type PushRequest struct {
ChannelHint string
}
// Pusher delivers a proactive message under the active-client-first, IM-fallback
// policy (design/12 §3), bounded by the rate gate, honoring the channel hint the
// autonomous conclusion selected.
// Pusher delivers a proactive message to ONE target channel. It is bounded by the
// rate gate and honors the channel hint the autonomous conclusion selected: "web"
// goes to the active client, everything else goes over the IM bridge a single time,
// where the bridge picks the one channel (her choice → her most recent platform →
// whitelist default, WeChat included).
type Pusher interface {
// Push delivers req preferring the active client and falling back to the IM
// bridge, per the channel hint. It never blocks on a slow target.
// Push delivers req to a single target channel, per the channel hint. It never
// blocks on a slow target.
Push(ctx context.Context, req PushRequest) PushResult
}
// pusher is the concrete Pusher: it consults the gate, then tries the active
// client and falls back to IM, honoring the requested channel hint.
// pusher is the concrete Pusher: it consults the gate, then routes the message to
// exactly one target — the active client for a "web" hint, otherwise the IM bridge.
type pusher struct {
active ActiveClient
im IMAdapter
@@ -132,10 +135,14 @@ func newPusher(active ActiveClient, im IMAdapter, gate PushGate, log *slog.Logge
return &pusher{active: active, im: im, gate: gate, log: log}
}
// Push implements Pusher: it applies the rate gate, then resolves the channel
// hint to a route. A specific platform targets the IM bridge (tagged on the
// message) and falls back to the default route when no IM sink is wired, so the
// owner still receives the message rather than having it dropped.
// Push implements Pusher: it applies the rate gate, then routes the message to a
// single target. A "web" hint reaches the active web/native client; every other
// hint ("im", "auto", or a concrete platform) goes over the IM bridge once, where
// the bridge selects one owner channel (her [渠道:...] choice, then the platform she
// used most recently, then the whitelist default). The IM bridge is primary, so a
// push never fans out to several channels. When no IM sink is wired the active
// client is the honest fallback, so the owner is still reached rather than the
// message being dropped.
func (p *pusher) Push(ctx context.Context, req PushRequest) PushResult {
if err := ctx.Err(); err != nil {
return PushResult{Pushed: false, Route: RouteNone, Err: err.Error()}
@@ -148,27 +155,23 @@ func (p *pusher) Push(ctx context.Context, req PushRequest) PushResult {
hint := normalizeChannelHint(req.ChannelHint)
msg := req.Message
// A specific platform names the requested channel on the message so a
// platform-aware IM sink can honor it; auto/web/im keep the default platform.
if isSpecificChannel(hint) {
msg.Platform = hint
}
var res PushResult
switch hint {
case channelWeb:
if hint == channelWeb {
res = p.pushActive(ctx, msg)
case channelIM:
res = p.pushIM(ctx, msg)
case channelAuto:
res = p.pushDefault(ctx, msg)
default:
// A specific platform targets the IM bridge for that channel. With no IM
// sink wired (the backthink IM seam is single-sink), fall back to the
// default route so the owner is still reached; the hint is recorded.
} else {
// "im"/"auto"/a concrete platform all travel over the IM bridge once. A
// generic hint hands the sentinel platform so the bridge falls through to its
// recent-active / default selection; a concrete hint is honored when the
// channel is whitelisted and connected.
if hint == channelIM || hint == channelAuto {
msg.Platform = defaultProactivePlatform
} else {
msg.Platform = hint
}
res = p.pushIM(ctx, msg)
if !res.Pushed {
res = p.pushDefault(ctx, msg)
res = p.pushActive(ctx, msg)
}
}
// Only a real delivery advances the cooldown/per-kind windows, so an
@@ -179,22 +182,6 @@ func (p *pusher) Push(ctx context.Context, req PushRequest) PushResult {
return annotateHint(res, req.ChannelHint)
}
// pushDefault tries the active client first and falls back to the IM bridge.
func (p *pusher) pushDefault(ctx context.Context, msg adapter.UnifiedMessage) PushResult {
if p.active != nil {
n, err := p.active.SendProactive(ctx, msg)
if err != nil {
p.log.Warn("backthink active client push failed", "error", err)
} else if n > 0 {
return PushResult{Pushed: true, Route: RouteActiveClient, Detail: fmt.Sprintf("delivered to %d active client(s)", n)}
}
}
if p.im == nil {
return PushResult{Pushed: false, Route: RouteNone, Detail: "no active client or IM target"}
}
return p.pushIM(ctx, msg)
}
// pushActive delivers only to the active web/native client.
func (p *pusher) pushActive(ctx context.Context, msg adapter.UnifiedMessage) PushResult {
if p.active != nil {