diff --git a/docs/运维陪伴错题本.md b/docs/运维陪伴错题本.md index e235f60..84e6149 100644 --- a/docs/运维陪伴错题本.md +++ b/docs/运维陪伴错题本.md @@ -591,3 +591,13 @@ docs\运维陪伴错题本.md - 修复或规避:新增 `_memory_companion_clock()` 取插件时钟(取不到就退回 None,由 outbox 自己兜底),8 个 outbox 调用点全部带上 `now=`。测试用 2031 年的插件时钟与真实系统时钟拉开距离,时间戳落在 2031 才算通过。 - 预防规则:函数留了 `now`/`clock` 这类可注入参数,就要有一条测试证明调用方真的传了——用一个不可能与系统时钟重合的值来断言,而不是断言「格式是 ISO」。 - 验证证据:`tests/test_p3_key_and_clock.py::OutboxClockTests` 2 项;全量 231 项通过、联合 E2E 135/135。 + +## 2026-07-27 — 「最后写入时间」和「结算到哪个时刻」是两个字段,混用一个就没有衰减 + +- 阶段:P0 生产缺陷清理 +- 类型:功能行为 +- 现象:只要用户在持续对话,Izard 四维与 Plutchik 八维就永不回落,情绪提示词长期停在被最后一次事件推高的状态。实跑对照(recovery=24/h,每 30 秒一条 × 120 次 = 1 小时):修复前四维恒为 `-80/90/80/20`、sadness 恒 80、anger 恒 70,**一格未动**。 +- 根因:`updated_ts` 在两个函数之间被当成两种语义。`_decay_*` 里它是「这组数值结算到哪个时刻」的衰减锚点,上一轮已经改成「真消耗了一格才推进,不足一格保留让时间可累计」;而 `_update_*` 把它当成「最后一次被写入的时间」,写在所有事件分支**之外**无条件执行。主链每条私聊消息都会调 `_update_*`,于是锚点被逐条消息冲成 `now`,`hours` 永远涨不到一格(默认 150 秒),`steps` 恒为 0,衰减分支永不执行——上一轮的修复被完全抵消。更进一步:四维/八维**共用同一个锚点**,任一维被 nudge 就重置整组时钟,所以「亲密对话推高 joy/trust」会连带冻结 anger/sadness/fear。 +- 修复或规避:删掉两个 `_update_*` 里的无条件写入,把锚点所有权交还给 `_decay_*`。不是加「命中分支才推进」的条件——`apology`/`comfort` 这些分支本身在**降**情绪,更没有理由重置衰减时钟。核对过消费面:全仓只有两个 `_decay_*` 读这个字段,页面摘要与前端都不读它,受伤余波的硬闸门走的是另外三个独立时间戳,不受影响。 +- 预防规则:一个时间戳字段只能有一种语义。「最后修改时间」和「结算锚点」看起来都叫 updated_ts,但前者可以随便推进,后者推进一次就丢掉一段没被消耗的时间。共享锚点的多维状态尤其危险:任一维的写入都会连累其余维度,而症状只在「某几维不衰减」上体现,很难联想到锚点。 +- 验证证据:`tests/test_audit_batch1.py::B2EmotionUpdateTimestampTests` 4 项(高频对话仍衰减、分步与静默一小时严格等价、正向事件不冻结负向维度、单次事件不吞掉不足一格的时间)。反证:临时还原成缺陷版,这 4 项全红。全量 235 项通过。 diff --git a/tests/test_audit_batch1.py b/tests/test_audit_batch1.py index 16077e7..fe160bc 100644 --- a/tests/test_audit_batch1.py +++ b/tests/test_audit_batch1.py @@ -627,3 +627,87 @@ class L2ExplicitArgumentTests(unittest.TestCase): if __name__ == "__main__": unittest.main() + + +class B2EmotionUpdateTimestampTests(unittest.TestCase): + """情绪事件不该重置衰减锚点(P0)。 + + `_decay_*` 已经会「衰减了一格才推进锚点,不足一格保留」,但 `_update_*` + 在所有事件分支之外无条件写 `updated_ts = now`。主链每收到一条私聊消息都会调 + `_update_*`,于是锚点被逐条消息冲成 now,`hours` 永远涨不到一格,衰减分支 + 永远不执行——只要用户在持续对话,情绪就永不回落。 + + 四维/八维还共用同一个锚点:亲密对话推高 joy/trust 会连带冻结 anger/sadness/fear。 + recovery=24/h → 一格 = 150 秒;下面用「每 30 秒一条 × 120 次 = 1 小时」构造高频对话。 + """ + + BASE = 1_700_000_000.0 + STEP = 30 + ROUNDS = 120 + + def _izard_state(self): + return {"emotion_dimensions": {"pleasantness": -80, "tension": 90, "arousal": 80, + "certainty": 20, "updated_ts": int(self.BASE)}} + + def _plutchik_state(self): + return {"plutchik_emotions": {"joy": 0, "trust": 10, "fear": 40, "surprise": 20, + "sadness": 80, "disgust": 30, "anger": 70, + "anticipation": 15, "updated_ts": int(self.BASE)}} + + def _chat(self, host, state, which, event="neutral", intensity=0): + for i in range(1, self.ROUNDS + 1): + now = self.BASE + i * self.STEP + if which == "izard": + host._update_izard_emotion_dimensions( + state, event=event, intensity=intensity, target="none", confidence=0.5, + inbound_intent="chat", pressure=0, mode="normal", now=now) + else: + host._update_plutchik_emotions( + state, event=event, intensity=intensity, target="none", + inbound_intent="chat", pressure=0, mode="normal", now=now) + + def test_izard_still_decays_under_high_frequency_chat(self): + host = _EmotionHost() + state = self._izard_state() + self._chat(host, state, "izard") + dims = state["emotion_dimensions"] + # 倍率 1.0 的两维应与「静默一小时」严格相等;另两维有取整下限,只断言确实回落了 + self.assertEqual(dims["tension"], 66) + self.assertEqual(dims["arousal"], 56) + self.assertGreater(dims["pleasantness"], -80) + self.assertGreater(dims["certainty"], 20) + + def test_plutchik_high_frequency_matches_one_hour_of_silence(self): + host = _EmotionHost() + chatty = self._plutchik_state() + self._chat(host, chatty, "plutchik") + silent = self._plutchik_state() + host._decay_plutchik_emotions(silent, now=self.BASE + self.STEP * self.ROUNDS) + keys = ("joy", "trust", "fear", "surprise", "sadness", "disgust", "anger", "anticipation") + self.assertEqual({k: chatty["plutchik_emotions"][k] for k in keys}, + {k: silent["plutchik_emotions"][k] for k in keys}, + "分步高频与一次性静默不等价,说明锚点仍被事件重置") + + def test_positive_events_do_not_freeze_negative_dimensions(self): + """亲密对话推高 joy/trust 时,anger/sadness 也必须继续衰减——八维共用一个锚点。""" + host = _EmotionHost() + state = self._plutchik_state() + self._chat(host, state, "plutchik", event="intimate", intensity=80) + emotions = state["plutchik_emotions"] + self.assertLess(emotions["anger"], 70, "怒气被正向事件冻住了") + self.assertLess(emotions["sadness"], 80, "低落被正向事件冻住了") + + def test_update_never_pushes_the_anchor_past_what_decay_settled(self): + """单次事件:锚点应停在 decay 结算到的位置,而不是被改写成 now。""" + host = _EmotionHost() + state = self._plutchik_state() + host._update_plutchik_emotions(state, event="hurt", intensity=90, target="bot", + inbound_intent="chat", pressure=0, mode="normal", + now=self.BASE + 60) + self.assertEqual(state["plutchik_emotions"]["updated_ts"], int(self.BASE), + "不足一格的事件把已流逝的 60 秒吞掉了") + izard = self._izard_state() + host._update_izard_emotion_dimensions(izard, event="hurt", intensity=90, target="bot", + confidence=0.9, inbound_intent="chat", pressure=0, + mode="normal", now=self.BASE + 60) + self.assertEqual(izard["emotion_dimensions"]["updated_ts"], int(self.BASE)) diff --git a/user_memory.py b/user_memory.py index 6da2c7b..04e3604 100644 --- a/user_memory.py +++ b/user_memory.py @@ -454,7 +454,9 @@ class UserMemoryMixin: self._nudge_emotion_dimension(dims, "tension", -8) self._nudge_emotion_dimension(dims, "arousal", 6) self._nudge_emotion_dimension(dims, "certainty", 8) - dims["updated_ts"] = int(now or _now_ts()) + # updated_ts 是「这组数值结算到哪个时刻」的衰减锚点,只归 _decay_izard_emotion_dimensions 管。 + # 情绪事件只改数值、不消耗时间;在这里覆盖锚点会把不足一格的已流逝时间逐条消息丢掉, + # 高频对话里衰减就永远触发不了。四维共用同一个锚点,任一维被 nudge 都会连累其余三维。 state["emotion_dimensions"] = dims return dims @@ -566,7 +568,9 @@ class UserMemoryMixin: self._nudge_plutchik_emotion(emotions, "joy", 12 if mode == "attached" else 8) self._nudge_plutchik_emotion(emotions, "trust", 14 if mode == "attached" else 9) self._nudge_plutchik_emotion(emotions, "anticipation", 6) - emotions["updated_ts"] = int(now) + # 同 Izard 侧:updated_ts 归 _decay_plutchik_emotions 管,这里只改数值。 + # 八维共用一个锚点,若被 joy / trust 的上调重置,anger / sadness / fear 也会跟着永不衰减 + # (实测亲密对话满一小时,怒气仍停在峰值 70)。 state["plutchik_emotions"] = emotions profile = self._plutchik_profile_from_basic(emotions, now=now) state["plutchik_profile"] = profile