feat(backthink): 事件驱动主动消息绕过PushGate——IoT等事件该发就发(heartbeat仍受60min节流)
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

- trigger.go: 新增 TriggerKind(TriggerHeartbeat/TriggerEvent) + Trigger.Kind 标记触发来源
- push.go: PushRequest.TriggerKind 分流——event 跳过 gate.Allow/RecordPush(该发就发),heartbeat 仍受 60min≤1/冷却/分类限频;PushResult.Bypassed 标记 + detail=event_bypass 区分
- backthink.go: think() 传 TriggerKind + 过同事件去重门
- event_dedup.go(新): 同事件(Source+Summary)去重防刷,无 60min 节流;不同事件/心跳后重置链
- 测试: event 连续 3 次全推 bypass / heartbeat 第2次 rate_limited / event 不消耗心跳限 / 同事件去重
- build/vet/test 全绿 + windows/darwin 交叉编译通过
This commit is contained in:
Aska committed 2026-09-08 13:46:43 +08:00
1 parent 3e4efa0e41
commit d09b25071d
6 files changed
+266 -22

No files matched your search

+73
View File
@@ -237,3 +237,76 @@ func TestRateGate_KindWindowLimitsSameFlavor(t *testing.T) {
t.Fatal("same flavor after the kind window should be allowed")
}
}
// TestPusher_EventBypassesGate verifies an event-driven push skips the PushGate:
// with a limit of 1 push per window, three consecutive event pushes are ALL
// delivered (owner mandate "该发就发"), each marked Bypassed and each advancing
// no cooldown/kind window.
func TestPusher_EventBypassesGate(t *testing.T) {
t.Parallel()
active := &mockActive{n: 1}
p := newPusher(active, nil, newRateGate(1, time.Minute, 0, 0, discardLog()), discardLog())
req := PushRequest{Message: adapter.UnifiedMessage{Content: "洗衣机洗好了"}, TriggerKind: TriggerEvent}
first := p.Push(context.Background(), req)
second := p.Push(context.Background(), req)
third := p.Push(context.Background(), req)
if !first.Pushed || !first.Bypassed {
t.Fatalf("first event push = %+v, want delivered + Bypassed", first)
}
if !second.Pushed || !second.Bypassed {
t.Fatalf("second event push = %+v, want delivered + Bypassed (gate bypassed)", second)
}
if !third.Pushed || !third.Bypassed {
t.Fatalf("third event push = %+v, want delivered + Bypassed (gate bypassed)", third)
}
if active.calls != 3 {
t.Fatalf("active calls = %d, want 3 (events are not rate-capped)", active.calls)
}
}
// TestPusher_HeartbeatStillRateLimited verifies a heartbeat-driven push stays
// gate-governed: with a limit of 1 push per window, the first passes and the
// second is refused as rate_limited, never Bypassed.
func TestPusher_HeartbeatStillRateLimited(t *testing.T) {
t.Parallel()
active := &mockActive{n: 1}
p := newPusher(active, nil, newRateGate(1, time.Minute, 0, 0, discardLog()), discardLog())
req := PushRequest{Message: adapter.UnifiedMessage{Content: "想跟你说声晚安"}, TriggerKind: TriggerHeartbeat}
first := p.Push(context.Background(), req)
second := p.Push(context.Background(), req)
if !first.Pushed || first.Bypassed {
t.Fatalf("first heartbeat push = %+v, want delivered non-bypassed", first)
}
if second.Pushed || second.Route != RouteRateLimited || second.Bypassed {
t.Fatalf("second heartbeat push = %+v, want rate_limited non-bypassed", second)
}
if active.calls != 1 {
t.Fatalf("active calls = %d, want 1 (heartbeat still gated)", active.calls)
}
}
// TestPusher_EventBypass_DoesNotAdvanceGate verifies an event-driven push does not
// consume the heartbeat limit: after an event push, a following heartbeat push
// still gets its own full allowance.
func TestPusher_EventBypass_DoesNotAdvanceGate(t *testing.T) {
t.Parallel()
active := &mockActive{n: 1}
p := newPusher(active, nil, newRateGate(1, time.Minute, 0, 0, discardLog()), discardLog())
ev := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{Content: "洗衣机洗好了"}, TriggerKind: TriggerEvent})
hb := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{Content: "想跟你说声晚安"}, TriggerKind: TriggerHeartbeat})
if !ev.Pushed || !ev.Bypassed {
t.Fatalf("event push = %+v, want delivered + Bypassed", ev)
}
if !hb.Pushed || hb.Bypassed {
t.Fatalf("heartbeat push after an event = %+v, want delivered non-bypassed (event did not consume the gate)", hb)
}
if active.calls != 2 {
t.Fatalf("active calls = %d, want 2", active.calls)
}
}