feat(backthink): 安全域穿透安静时段 + 长静默不拦心跳 + 启动即思考(裁决 1.8/07.4.2/07.4.3/07.4.4/07.4.5)
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

执行方案阶段 2 · P2-2(`DevDocs/audit/_plan.md`)。四条裁决:
- 1.8/07.4.2:`think()` 不再走忽略 importance 的 `disturbAllowed`(已删),**接回 `Decide` 级联**;
  安静时段只挡常规推送、**critical 安全域穿透**;`DecisionInput` 增 `ConclusionPush`/`Heartbeat`。
  睡眠仍绝对不打扰(裁决只豁免安静时段,此点已在设计与报告写明)。
- 07.4.3:长静默(>2h)分支对**心跳类**放行(常规事件仍需 ≥high),刚聊过天 10min 内扣留不变。
- 07.4.4:`tickLoop` **启动立即先思考一轮** + 拟人化启动消息;启动轮与心跳**同门同限频**
  (重启风暴不会刷屏),ctx 已取消则跳过。
- 07.4.5:核对一致(`THINK_INTERVAL_SEC` 默认 900s 与 design/20 §0 权威口径一致)⇒ **代码零改动**。

验证:go build 绿;backthink 包 vet/测试绿;`-count=20`(抗 flake)+ `-race` 绿;
linux/amd64 + darwin/arm64 交叉编译绿;gofmt 干净;文件 ≤500 行、无 >80 行函数。
变异验证(三条,均带还原证明):① 安静时段改回"全拦" → 安全域穿透断言 FAIL(首次只删 critical
分支时仍绿——`critical >= high` 会从 high 分支漏过,故真实变异必须是"全拦");
② 删心跳豁免 → 长静默心跳断言 FAIL;③ 删启动思考 → 启动即思考断言 FAIL。

未验证:无真实模型凭据 ⇒ 启动轮问候与 `[我决定:…]` 解析仅经 mock + 真 loop 验证,端到端 LLM 未验。
This commit is contained in:
AskaEth committed 2026-10-04 11:21:44 +08:00
1 parent a35bcb8aa9
commit afef75f640
7 files changed
+468 -176

No files matched your search

+71 -92
View File
@@ -6,9 +6,11 @@ import (
"time"
)
// Importance is the estimated severity of an event that woke a thinking pass. It
// is the primary positive signal in the interrupt decision: a low-importance
// heartbeat almost never interrupts, while a critical safety event normally does.
// Importance is the estimated severity of an event that woke a thinking pass.
// Under design/20 §2 P2 it no longer decides *whether* she wants to speak (her
// conclusion does); it decides the hard-gate exemptions: a critical safety event
// punches through work/busy and quiet hours (裁决 07.4.2), and a high event still
// passes quiet hours (the pre-existing cascade semantics).
type Importance int
const (
@@ -18,7 +20,8 @@ const (
ImportanceNormal
// ImportanceHigh is a notable event (a reminder the user set firing).
ImportanceHigh
// ImportanceCritical is a safety-critical event that may override work/busy.
// ImportanceCritical is a safety-critical event that punches through
// work/busy and quiet hours (裁决 07.4.2 安全域豁免).
ImportanceCritical
)
@@ -38,14 +41,10 @@ func (i Importance) String() string {
}
}
// highBoredomThreshold is the boredom (0..1) above which a normal daytime event
// is worth interrupting a bored, otherwise idle user. It is a const so no magic
// 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.
// activity gate only lets a genuinely important event through — except heartbeat
// passes, which are never held by a long-silent gap (裁决 07.4.3). Compile-time
// so no magic number appears.
const longIdleThreshold = 2 * time.Hour
// UserState describes the user's current condition as observed from device
@@ -62,7 +61,9 @@ type UserState struct {
// Busy reports whether the user is marked busy (calendar / do-not-disturb).
Busy bool
// Boredom ranges 0..1. Higher boredom means the user is idle and more open
// to a gentle interruption.
// to a gentle interruption. Under design/20 §2 P2 the push intent is her own
// conclusion, so this dial no longer gates the decision; it is kept as a
// telemetry signal her judgment already absorbs.
Boredom float64
}
@@ -90,6 +91,12 @@ type DecisionInput struct {
LastActivity time.Time
// HasActivity reports whether LastActivity is meaningful.
HasActivity bool
// ConclusionPush 是她自己思考结论的推送意愿(design/20 §2 P2):「想不想说」
// 由她的结论决定,本决策只回答「此刻能不能说」。
ConclusionPush bool
// Heartbeat 标记本轮是否为心跳类触发(周期心跳或启动轮)。裁决 07.4.3:
// 长期静默段不拦心跳——主人久未回应时,主动关心仍可照常发出。
Heartbeat bool
}
// Decision is the outcome of Decide.
@@ -100,36 +107,37 @@ type Decision struct {
Reason string
}
// Decide applies the interrupt rule from design/12 §2 (and design/11 §6) with
// "不打扰 (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.
// Decide 是生产路径的打扰决策(design/12 §2 + design/20 §2 P2 + 裁决 07.4.2/07.4.3)。
// 「想不想说」交给她的结论(ConclusionPush),本方法只判「此刻能不能说」,评估顺序
// 刻意 fail-closed(宁可不打扰):
//
// baseDecide's evaluation order is deliberately fail-closed (prefer not
// interrupting):
// 0. 她的结论要求不推送 → 不推送。
// 1. 用户在睡 → 绝不打扰。
// 2. 上班/忙碌 → 仅 critical 安全事件可打扰。
// 3. 安静时段(深夜窗口)→ 挡常规推送;critical 安全事件穿透(裁决 07.4.2,
// 与 design/12 §6 策略五同口径),high 沿袭原级联口径照常放行。
// 4. 其余时段 → 放行(是否值得说已由她的结论回答)。
//
// 1. A sleeping user is never interrupted.
// 2. A working or busy user is interrupted only by a critical event.
// 3. Inside the quiet-hours (deep-night) window only high/critical events
// 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.
// 通过硬门后再过活动软门:刚聊过天或主人久未回应时酌情扣下;心跳类触发在
// 长期静默段不拦(裁决 07.4.3,主动关心照常发出)。频率门(PushGate)在推送器
// 内部单独生效,不在此处。
func (b *Backthink) Decide(in DecisionInput) Decision {
d := b.baseDecide(in)
if !d.ShouldPush {
if !in.ConclusionPush {
return Decision{ShouldPush: false, Reason: "conclusion requested no push"}
}
if d := b.baseDecide(in); !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}
if held, why := b.activityHeld(in); held {
return Decision{ShouldPush: false, Reason: "proactive push held: " + why}
}
return d
return Decision{ShouldPush: true, Reason: "proactive push approved"}
}
// baseDecide is the do-not-interrupt cascade of design/12 §2.
// baseDecide 是不打扰硬门级联(design/12 §2)。critical 安全事件走安全域分支:
// 工作忙碌与安静时段都放行(裁决 07.4.2 安全域豁免);睡眠仍是绝对不打扰。
// 白天是否「值得说」不再由重要性/无聊度判定——那是她的结论(design/20 §2 P2)
// 已经回答的问题。
func (b *Backthink) baseDecide(in DecisionInput) Decision {
s := in.UserState
if s.Sleeping {
@@ -139,78 +147,49 @@ func (b *Backthink) baseDecide(in DecisionInput) Decision {
return Decision{ShouldPush: false, Reason: "user is working/busy; not interrupting"}
}
if b.inQuietHours(in.At) {
if in.Import >= ImportanceCritical {
return Decision{ShouldPush: true, Reason: "quiet hours but critical safety event bypasses"}
}
if in.Import >= ImportanceHigh {
return Decision{ShouldPush: true, Reason: "quiet hours but event is important"}
}
return Decision{ShouldPush: false, Reason: "quiet hours; not interrupting"}
}
switch in.Import {
case ImportanceCritical, ImportanceHigh:
return Decision{ShouldPush: true, Reason: "high/critical importance during daytime"}
case ImportanceNormal:
if s.Boredom >= highBoredomThreshold {
return Decision{ShouldPush: true, Reason: "normal importance and user is bored"}
}
return Decision{ShouldPush: false, Reason: "normal importance; not worth interrupting"}
default:
return Decision{ShouldPush: false, Reason: "low importance; not interrupting"}
}
return Decision{ShouldPush: true, Reason: "worth interrupting"}
}
// activityAllowed is the activity-aware soft gate (design/12 §6). It widens the
// do-not-interrupt principle to the conversation's own rhythm:
// activityHeld 是活动感知软门(design/12 §6 + 裁决 07.4.3)。它把「不打扰」延伸到
// 对话自身的节奏,返回是否扣下与简短理由:
//
// - 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).
// - 最近 RecentChatCooldown 内主人刚说过话 → 对话正热,非 critical 扣下,
// 不在聊天中途插话;
// - 超过 longIdleThreshold 未说话 → 主人久未回应,仅真正重要的事件值得唤醒;
// 心跳类触发(含启动轮)例外照常放行(裁决 07.4.3:长期静默段不拦心跳,
// 主动关心仍可发出);
// - 其间与活动状态未知 → 不软化,照硬门结论通过。
//
// 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
// 活动是软信号:硬门(baseDecide)独立生效,频率门(PushGate)在推送器内部生效。
func (b *Backthink) activityHeld(in DecisionInput) (bool, string) {
if !in.HasActivity {
return false, ""
}
since := at.Sub(lastActivity)
since := in.At.Sub(in.LastActivity)
if since <= b.cfg.RecentChatCooldown {
return imp >= ImportanceCritical
if in.Import >= ImportanceCritical {
return false, ""
}
return true, "owner just chatted"
}
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
// when it is genuinely inappropriate to interrupt: the user is asleep, working or
// busy, or it is inside the quiet-hours window. The frequency gate (PushGate)
// applies separately inside the pusher.
func (b *Backthink) disturbAllowed(at time.Time, s UserState) bool {
if s.Sleeping || s.Working || s.Busy {
return false
}
return !b.inQuietHours(at)
}
// 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, 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"
if in.Heartbeat {
return false, ""
}
if in.Import >= ImportanceHigh {
return false, ""
}
return true, "owner away a long while"
}
return false, ""
}
// inQuietHours reports whether at falls inside the configured do-not-disturb