feat(backthink): 安全域穿透安静时段 + 长静默不拦心跳 + 启动即思考(裁决 1.8/07.4.2/07.4.3/07.4.4/07.4.5)
执行方案阶段 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:
1 parent
a35bcb8aa9
commit
afef75f640
7 files changed
+468
-176
No files matched your search
@@ -71,17 +71,31 @@ func TestBackthink_Heartbeat_TriggersThink(t *testing.T) {
|
||||
b.Start(context.Background())
|
||||
defer func() { _ = b.Stop(context.Background()) }()
|
||||
|
||||
eventually(t, 2*time.Second, func() bool { return thinker.count() >= 1 })
|
||||
// 启动轮(07.4.4)会先落一条 startup 记录,这里等真正的周期心跳记录。
|
||||
eventually(t, 2*time.Second, func() bool {
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
return len(recs) >= 1
|
||||
for _, r := range recs {
|
||||
if r.Trigger == "heartbeat" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
if got := thinker.count(); got < 1 {
|
||||
t.Fatalf("thinker calls = %d, want at least 1", got)
|
||||
}
|
||||
found := false
|
||||
recs, err := b.List(context.Background(), 10)
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if recs[0].Trigger != "heartbeat" {
|
||||
t.Fatalf("Trigger = %q, want heartbeat", recs[0].Trigger)
|
||||
for _, r := range recs {
|
||||
if r.Trigger == "heartbeat" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("no heartbeat record in %d records", len(recs))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,22 +119,29 @@ func TestBackthink_EventTrigger_Push(t *testing.T) {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
|
||||
// 启动轮(07.4.4)同样会思考并推送,因此按触发源过滤出事件记录再断言。
|
||||
var rec Record
|
||||
eventually(t, 2*time.Second, func() bool {
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
return len(recs) >= 1
|
||||
for _, r := range recs {
|
||||
if r.Trigger == SubjectReminderTrigger {
|
||||
rec = r
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
if got := thinker.count(); got != 1 {
|
||||
t.Fatalf("thinker calls = %d, want 1", got)
|
||||
if got := thinker.count(); got < 1 {
|
||||
t.Fatalf("thinker calls = %d, want at least 1", got)
|
||||
}
|
||||
if got := pusher.count(); got != 1 {
|
||||
t.Fatalf("pusher calls = %d, want 1 (important + daytime)", got)
|
||||
if got := pusher.count(); got < 1 {
|
||||
t.Fatalf("pusher calls = %d, want at least 1 (important + daytime)", got)
|
||||
}
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
if !recs[0].ShouldPush {
|
||||
if !rec.ShouldPush {
|
||||
t.Fatalf("ShouldPush = false, want true")
|
||||
}
|
||||
if !recs[0].PushResult.Pushed || recs[0].PushResult.Route != RouteActiveClient {
|
||||
t.Fatalf("PushResult = %+v, want pushed active_client", recs[0].PushResult)
|
||||
if !rec.PushResult.Pushed || rec.PushResult.Route != RouteActiveClient {
|
||||
t.Fatalf("PushResult = %+v, want pushed active_client", rec.PushResult)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,22 +168,25 @@ func TestBackthink_EventTrigger_NoPush(t *testing.T) {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
|
||||
var rec Record
|
||||
eventually(t, 2*time.Second, func() bool {
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
return len(recs) >= 1
|
||||
for _, r := range recs {
|
||||
if r.Trigger == "iot.device_update" {
|
||||
rec = r
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
if got := pusher.count(); got != 0 {
|
||||
t.Fatalf("pusher calls = %d, want 0 (asleep + night)", got)
|
||||
}
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
if recs[0].ShouldPush {
|
||||
if rec.ShouldPush {
|
||||
t.Fatalf("ShouldPush = true, want false")
|
||||
}
|
||||
if recs[0].PushResult.Route != RouteNone {
|
||||
t.Fatalf("PushResult.Route = %q, want %q", recs[0].PushResult.Route, RouteNone)
|
||||
}
|
||||
if recs[0].Trigger != "iot.device_update" {
|
||||
t.Fatalf("Trigger = %q, want iot.device_update", recs[0].Trigger)
|
||||
if rec.PushResult.Route != RouteNone {
|
||||
t.Fatalf("PushResult.Route = %q, want %q", rec.PushResult.Route, RouteNone)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,23 +205,36 @@ func TestBackthink_SystemEventLowValueDowngraded(t *testing.T) {
|
||||
b.Start(context.Background())
|
||||
defer func() { _ = b.Stop(context.Background()) }()
|
||||
|
||||
// 先等启动轮(07.4.4)落库并记下基线,再发布低价值系统事件。
|
||||
eventually(t, 2*time.Second, func() bool {
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
return len(recs) >= 1 && recs[0].Trigger == "startup"
|
||||
})
|
||||
before := thinker.count()
|
||||
|
||||
if err := bs.Publish(context.Background(), SubjectSystemEvent, SystemEvent{Summary: "member joined"}); err != nil {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
|
||||
var rec Record
|
||||
eventually(t, 2*time.Second, func() bool {
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
return len(recs) >= 1 && recs[0].Trigger == SubjectSystemEvent
|
||||
for _, r := range recs {
|
||||
if r.Trigger == SubjectSystemEvent {
|
||||
rec = r
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
if got := thinker.count(); got != 0 {
|
||||
t.Fatalf("thinker calls = %d, want 0 (low-value system event downgraded)", got)
|
||||
if got := thinker.count(); got != before {
|
||||
t.Fatalf("thinker calls = %d, want %d (low-value system event downgraded)", got, before)
|
||||
}
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
if recs[0].ShouldPush {
|
||||
if rec.ShouldPush {
|
||||
t.Fatalf("ShouldPush = true for a downgraded system event")
|
||||
}
|
||||
if recs[0].PushResult.Route != RouteNone {
|
||||
t.Fatalf("PushResult.Route = %q, want %q", recs[0].PushResult.Route, RouteNone)
|
||||
if rec.PushResult.Route != RouteNone {
|
||||
t.Fatalf("PushResult.Route = %q, want %q", rec.PushResult.Route, RouteNone)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,11 +253,17 @@ func TestBackthink_SystemEventHighValueThinks(t *testing.T) {
|
||||
b.Start(context.Background())
|
||||
defer func() { _ = b.Stop(context.Background()) }()
|
||||
|
||||
eventually(t, 2*time.Second, func() bool {
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
return len(recs) >= 1 && recs[0].Trigger == "startup"
|
||||
})
|
||||
before := thinker.count()
|
||||
|
||||
if err := bs.Publish(context.Background(), SubjectSystemEvent, SystemEvent{Summary: "owner tagged", Import: ImportanceHigh}); err != nil {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
|
||||
eventually(t, 2*time.Second, func() bool { return thinker.count() >= 1 })
|
||||
eventually(t, 2*time.Second, func() bool { return thinker.count() >= before+1 })
|
||||
}
|
||||
|
||||
func TestBackthink_List_NewestFirst(t *testing.T) {
|
||||
@@ -288,11 +331,21 @@ func TestBackthink_Stop_UnsubscribesBus(t *testing.T) {
|
||||
b, _ := newBackthink(t, Config{Interval: 25 * time.Millisecond}, Deps{Bus: bs, Thinker: thinker})
|
||||
|
||||
b.Start(context.Background())
|
||||
// Confirm the subscription is live by triggering one think.
|
||||
// Confirm the subscription is live by triggering one think (identified by its
|
||||
// persisted record, so the 07.4.4 startup round and heartbeat ticks with their
|
||||
// own trigger strings cannot mask or overwrite it).
|
||||
if err := bs.Publish(context.Background(), SubjectReminderTrigger, "first"); err != nil {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
eventually(t, 2*time.Second, func() bool { return thinker.count() >= 1 })
|
||||
eventually(t, 2*time.Second, func() bool {
|
||||
recs, _ := b.List(context.Background(), 10)
|
||||
for _, r := range recs {
|
||||
if r.Trigger == SubjectReminderTrigger {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
if err := b.Stop(context.Background()); err != nil {
|
||||
t.Fatalf("Stop: %v", err)
|
||||
|
||||
Reference in new issue
Block a user