fix(backthink): 主动推送降频(15min心跳+60min≤1次+推后30min冷却+刚聊过10min冷避) + 情绪约束(分类限频/活跃感知) + 剥离'我决定:push'内部标记
This commit is contained in:
1 parent
03d7050e1e
commit
12031bd13c
14 files changed
+662
-149
No files matched your search
@@ -2,6 +2,7 @@ package backthink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -42,6 +43,11 @@ func (i Importance) String() string {
|
||||
// number appears in the decision path.
|
||||
const highBoredomThreshold = 0.7
|
||||
|
||||
// longIdleThreshold is the gap after the owner's last message beyond which the
|
||||
// activity gate only lets a genuinely important event through (a long-away owner
|
||||
// should not be woken for trivia). Compile-time so no magic number appears.
|
||||
const longIdleThreshold = 2 * time.Hour
|
||||
|
||||
// UserState describes the user's current condition as observed from device
|
||||
// telemetry (heart rate, sleep) and schedule (work, busy). It is the negative
|
||||
// signal in the interrupt decision: a sleeping, working or busy user is left
|
||||
@@ -78,6 +84,12 @@ type DecisionInput struct {
|
||||
At time.Time
|
||||
// UserState carries the user's physiological/schedule condition.
|
||||
UserState UserState
|
||||
// LastActivity is the wall-clock time of the owner's most recent message.
|
||||
// HasActivity reports whether a user message was found; when false the
|
||||
// activity gate falls through without guessing.
|
||||
LastActivity time.Time
|
||||
// HasActivity reports whether LastActivity is meaningful.
|
||||
HasActivity bool
|
||||
}
|
||||
|
||||
// Decision is the outcome of Decide.
|
||||
@@ -89,11 +101,12 @@ type Decision struct {
|
||||
}
|
||||
|
||||
// Decide applies the interrupt rule from design/12 §2 (and design/11 §6) with
|
||||
// "不打扰 (do not interrupt) as the first principle" as its bias. It is a pure
|
||||
// method on Backthink only so it can read the configured quiet-hours window; it
|
||||
// does not touch state.
|
||||
// "不打扰 (do not interrupt) as the first principle" as its bias, then softens it
|
||||
// with the activity-aware gate (design/12 §6). It is a pure method on Backthink
|
||||
// only so it can read the configured quiet-hours window; it does not touch state.
|
||||
//
|
||||
// The evaluation order is deliberately fail-closed (prefer not interrupting):
|
||||
// baseDecide's evaluation order is deliberately fail-closed (prefer not
|
||||
// interrupting):
|
||||
//
|
||||
// 1. A sleeping user is never interrupted.
|
||||
// 2. A working or busy user is interrupted only by a critical event.
|
||||
@@ -101,7 +114,23 @@ type Decision struct {
|
||||
// interrupt; low/normal events are deferred.
|
||||
// 4. In the daytime, high/critical events always interrupt; a normal event
|
||||
// interrupts only when the user is quite bored; a low event never does.
|
||||
//
|
||||
// A would-push decision is then run through the activity gate, which holds a push
|
||||
// when the owner just chatted (fresh conversation) or has been away a long while
|
||||
// and the event is not genuinely important.
|
||||
func (b *Backthink) Decide(in DecisionInput) Decision {
|
||||
d := b.baseDecide(in)
|
||||
if !d.ShouldPush {
|
||||
return d
|
||||
}
|
||||
if !b.activityAllowed(in.Import, in.LastActivity, in.HasActivity, in.At) {
|
||||
return Decision{ShouldPush: false, Reason: "proactive push held: " + d.Reason}
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// baseDecide is the do-not-interrupt cascade of design/12 §2.
|
||||
func (b *Backthink) baseDecide(in DecisionInput) Decision {
|
||||
s := in.UserState
|
||||
if s.Sleeping {
|
||||
return Decision{ShouldPush: false, Reason: "user is asleep; not interrupting"}
|
||||
@@ -128,6 +157,32 @@ func (b *Backthink) Decide(in DecisionInput) Decision {
|
||||
}
|
||||
}
|
||||
|
||||
// activityAllowed is the activity-aware soft gate (design/12 §6). It widens the
|
||||
// do-not-interrupt principle to the conversation's own rhythm:
|
||||
//
|
||||
// - Within RecentChatCooldown of the owner's last message, the conversation is
|
||||
// fresh — a non-critical push is held so she is not interrupted mid-chat.
|
||||
// - After longIdleThreshold without a message, the owner has been away — only a
|
||||
// genuinely important event is worth waking them for.
|
||||
// - Anywhere in between, and when activity is unknown, the decision is not
|
||||
// softened (fall through).
|
||||
//
|
||||
// Activity is a soft signal: the hard do-not-disturb gate is separate, and the
|
||||
// frequency gate (PushGate) applies separately inside the pusher.
|
||||
func (b *Backthink) activityAllowed(imp Importance, lastActivity time.Time, hasActivity bool, at time.Time) bool {
|
||||
if !hasActivity {
|
||||
return true
|
||||
}
|
||||
since := at.Sub(lastActivity)
|
||||
if since <= b.cfg.RecentChatCooldown {
|
||||
return imp >= ImportanceCritical
|
||||
}
|
||||
if since >= longIdleThreshold {
|
||||
return imp >= ImportanceHigh
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// disturbAllowed is the hard "do not disturb" safety gate for an autonomous push
|
||||
// (design/12 §2 first principle). Unlike Decide it deliberately ignores trigger
|
||||
// importance — which design/20 P2 replaces with her own judgment — and only blocks
|
||||
@@ -143,14 +198,16 @@ func (b *Backthink) disturbAllowed(at time.Time, s UserState) bool {
|
||||
|
||||
// interruptReason is the short human-readable note recorded/logged when no push
|
||||
// goes out (or as the pre-push intent). It keeps the panel self-explanatory:
|
||||
// either she concluded no push, or the do-not-disturb gate refused a push she
|
||||
// wanted.
|
||||
func (b *Backthink) interruptReason(push bool, at time.Time, s UserState) string {
|
||||
// either she concluded no push, the do-not-disturb gate refused a push she
|
||||
// wanted, or the activity gate held it.
|
||||
func (b *Backthink) interruptReason(push bool, at time.Time, s UserState, imp Importance, lastActivity time.Time, hasActivity bool) string {
|
||||
switch {
|
||||
case !push:
|
||||
return "conclusion requested no push"
|
||||
case !b.disturbAllowed(at, s):
|
||||
return "proactive push blocked by do-not-disturb"
|
||||
case !b.activityAllowed(imp, lastActivity, hasActivity, at):
|
||||
return "proactive push held by activity gate"
|
||||
default:
|
||||
return "proactive push approved"
|
||||
}
|
||||
@@ -167,3 +224,76 @@ func (b *Backthink) inQuietHours(at time.Time) bool {
|
||||
}
|
||||
return h >= start || h < end
|
||||
}
|
||||
|
||||
// PushKind classifies a proactive message into one of a few flavors so the
|
||||
// frequency gate can limit each flavor within a window, avoiding same-style spam
|
||||
// (design/12 §6 策略二). It is a soft tag derived from the message body's
|
||||
// keywords — it affects pacing, never identity or permission.
|
||||
type PushKind int
|
||||
|
||||
const (
|
||||
// KindGreeting is a warm hello / good-morning / good-night opener.
|
||||
KindGreeting PushKind = iota
|
||||
// KindReminder is a nudge about a schedule, task or deadline.
|
||||
KindReminder
|
||||
// KindShare is a discovery, observation or interesting find.
|
||||
KindShare
|
||||
// KindCare is a warm, caring check-in about the owner's wellbeing.
|
||||
KindCare
|
||||
)
|
||||
|
||||
// String returns a stable label for logs and records.
|
||||
func (k PushKind) String() string {
|
||||
switch k {
|
||||
case KindGreeting:
|
||||
return "greeting"
|
||||
case KindReminder:
|
||||
return "reminder"
|
||||
case KindShare:
|
||||
return "share"
|
||||
case KindCare:
|
||||
return "care"
|
||||
default:
|
||||
return fmt.Sprintf("pushkind(%d)", int(k))
|
||||
}
|
||||
}
|
||||
|
||||
// classifyPush assigns a PushKind to a proactive message from its keyword
|
||||
// content. A message that matches no flavor defaults to KindCare (the warm,
|
||||
// generic intent of an unsolicited proactive note), which is the least
|
||||
// spam-prone grouping. Priority is reminder > care > greeting > share so the
|
||||
// dominant intent wins when several keywords co-occur.
|
||||
func classifyPush(message string) PushKind {
|
||||
switch {
|
||||
case hasAnyKeyword(message, kindReminderKeywords...):
|
||||
return KindReminder
|
||||
case hasAnyKeyword(message, kindCareKeywords...):
|
||||
return KindCare
|
||||
case hasAnyKeyword(message, kindGreetingKeywords...):
|
||||
return KindGreeting
|
||||
case hasAnyKeyword(message, kindShareKeywords...):
|
||||
return KindShare
|
||||
default:
|
||||
return KindCare
|
||||
}
|
||||
}
|
||||
|
||||
// keyword sets for classifyPush. They are package vars (not consts) so the
|
||||
// classification stays flexible and testable, but they are immutable by
|
||||
// convention and never mutated at run time.
|
||||
var (
|
||||
kindReminderKeywords = []string{"提醒", "记得", "别忘了", "该去", "日程", "会议", "体检", "吃药", "喝药", "到点", "几点", "还有", "迟到", "明天", "马上", "最后", "截止"}
|
||||
kindCareKeywords = []string{"关心", "最近", "辛苦", "注意身体", "照顾好", "好好休息", "心情", "还好", "想你了", "想我", "担心", "保重", "你还好", "陪你", "累"}
|
||||
kindGreetingKeywords = []string{"早安", "晚安", "早上好", "中午好", "晚上好", "午安", "回来", "吃饭", "睡了"}
|
||||
kindShareKeywords = []string{"发现", "分享", "看到", "听说", "注意到", "告诉你", "有个", "想到", "有意思", "有趣", "你知道吗"}
|
||||
)
|
||||
|
||||
// hasAnyKeyword reports whether s contains any of the given substrings.
|
||||
func hasAnyKeyword(s string, subs ...string) bool {
|
||||
for _, sub := range subs {
|
||||
if strings.Contains(s, sub) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user