feat(v1.2): 阶段 0 —— 情绪双字段 + 立绘联动的地基 + 死亡规则(v2.0.55)

按 v1.2 需求开工。情绪双字段是所有新功能的**地基**:立绘联动按 preset 切图、
peer_emotions 注入 preset、状态脚本改情绪,三个都压在它上面,所以先做它。

## 数据层

`emotions` 表:`emotion`(单字段自由文本)→ `preset` + `text`。

- `preset` 五档(喜/怒/哀/平常/死亡)**驱动系统行为**;`text` 只管扮演质感。
  合成一个做不到:系统要按 preset 分支,而自然语言没法当分支条件。
- **迁移**:老的自由文本搬进 `text`,preset 给「平常」。旧列留着不动
  (SQLite 删列要重建整张表,不值当)。
- 新增 `emotion_state` / `emotion_from_export` / `is_dead`;
  `emotion_set` 改成"只写传进来的那些"(preset 和 text 分开可选)。
- `emotion_from_export` **两种格式都认**:v1.1 的字符串、v1.2 的对象。
  只认新格式的话,用户拿老文件导入会在情绪这一步抛异常,
  而前半截记忆已经写进去了 —— 留下一个"记忆进来了、情绪没进来"的半成品。

## 死亡规则(需求 3.5)

- 死亡的智能体**不能对话**:内核在**发起前**拦(和 Token 限额同一层,
  不是生成时才拒绝,那时候钱已经花了)
- **AI 不能把自己从死亡改活** —— 这条限制放在 **AI 的工具通道**上
  (`agent_tools.emotion_set`),**不在数据层**:数据层拦了的话,
  界面就没法复活了,而那正是需求写的"用户手动改回是兜底主权"
- 死亡状态**照常播报**给同世界观伙伴

## 注入

- `<emotion>` 两字段都带(附录 C 的格式);「平常 + 空文本」整段省掉
- `<peer_emotions>` 注入 **preset 不是 text** —— text 是那个角色的内心独白,
  读给别的角色听等于扒开他的脑子

## 界面

- 记忆页情绪区:五档选择器(「死亡」标红)+ 自由文本;选中死亡时多一行
  红字说明(不说的话用户点了死亡发现发不出消息,会以为坏了)
- 聊天页顶栏显示 preset;**死亡时输入条换成提示条**,点「去复活 ›」直达情绪区

## ★ 真机验证抓到一个探针抓不到的 bug

改完情绪后界面上**什么都没发生** —— `setEmotion` 只刷新了记忆页,
没刷新全局状态,而顶栏标签和死亡禁言读的都是 `state.emotions`。
这种"数据存进去了、界面不知道"的漏,探针(测内核)一个都抓不到。

## 验证

探针新增两节 14 项:
- **17 节 老库迁移**:用**内存库**测(换文件的做法被 WAL 坑过一次——
  连着的连接 + `-wal` 里的新数据会把复制进去的老库盖掉,测出来是假阴性)。
  覆盖:能搬、preset 默认、幂等、新库跑它是空操作。
- **18 节 死亡规则**:AI 能把自己改成死亡、**不能自愈**、用户能改活、
  死亡照常播报、不认识的 preset 被拒且理由列出合法值、只写 text 时 preset 不动。

五个探针全绿;装机实测五档选择器 + 迁移过来的老文本 + 死亡禁言提示条。
This commit is contained in:
l
2026-10-16 17:43:38 +08:00
parent e11e178810
commit 1a9229aa75
11 changed files with 435 additions and 64 deletions

View File

@@ -150,8 +150,9 @@ def main():
check("memory_add 返回 id", bool(r.get("id")))
r = agent_tools.memory_search({"keyword": "猫"})
check("memory_search 找到刚记的", r["count"] >= 1, str(r["count"]))
agent_tools.emotion_set({"emotion": "刚被夸了,有点得意"})
check("情绪写进去了", "得意" in agentdb.emotion_get(a1["id"]))
agent_tools.emotion_set({"preset": "喜", "text": "刚被夸了,有点得意"})
check("情绪写进去了(双字段)", "得意" in agentdb.emotion_state(a1["id"])["text"])
check("preset 也写进去了", agentdb.emotion_state(a1["id"])["preset"] == "喜")
agentdb.token_add(a1["id"], "doubao-pro", 1200, 800)
agentdb.token_add(a1["id"], "doubao-pro", 300, 100)
agentdb.token_add(a1["id"], "deepseek-r1", 500, 200)
@@ -213,7 +214,7 @@ def main():
# ---------------------------------------------------------------- 8
section("8. system prompt 注入(附录 C)")
tools_runtime.push_context(agentId=a1["id"], conversationId="c_test")
agentdb.emotion_set(a2["id"], "有点烦")
agentdb.emotion_set(a2["id"], preset="哀", text="有点烦")
store.update_agent(a2["id"], {"emotionEnabled": True})
# ★ a1 在这一节之前已经绑了世界观 → 它的记忆空间是**共享**的那个,
# 所以验证"记忆进提示词"要往当前 scope 里写(这正是绑定世界观的正确行为)
@@ -244,7 +245,9 @@ def main():
check("名单里也没有 agent 的 JSON", "'builtin'" not in prompt and "worldviewId'" not in prompt)
store.update_agent(a1["id"], {"emotionEnabled": True})
prompt2 = agent_context.build_system_prompt(store.read_settings(), store.get_agent(a1["id"]))
check("开了感知后有同伴情绪", "<peer_emotions>" in prompt2 and "仙辰-有点烦" in prompt2, prompt2[-200:])
check("开了感知后看到的是**同伴的 preset**(不是他的内心独白)",
"<peer_emotions>" in prompt2 and "仙辰-哀" in prompt2, prompt2[-200:])
check("**同伴的 text 不外泄**", "有点烦" not in prompt2.split("<peer_emotions>")[-1], prompt2[-200:])
# 空段要整段删掉
a3 = store.create_agent({"name": "空白的"})
p3 = agent_context.build_system_prompt(store.read_settings(), a3)
@@ -267,8 +270,9 @@ def main():
check("GET 记忆列表", r.get("ok") and r.get("shared") is True)
r = h.call(f"/api/agents/{a1['id']}/usage", "GET")
check("GET 用量", r.get("ok") and r.get("total", 0) > 0)
r = h.call(f"/api/agents/{a1['id']}/emotion", "PUT", {"emotion": "挺开心的"})
check("PUT 情绪", r.get("emotion") == "挺开心的")
r = h.call(f"/api/agents/{a1['id']}/emotion", "PUT", {"preset": "喜", "text": "挺开心的"})
check("PUT 情绪(双字段)", (r.get("emotion") or {}).get("text") == "挺开心的", str(r.get("emotion")))
check("preset 也存了", (r.get("emotion") or {}).get("preset") == "喜")
r = h.call("/api/worldviews", "GET")
check("GET 世界观列表", r.get("ok") and len(r["items"]) == 1)
r = h.call("/api/model-entries", "POST", {"name": "豆包 Pro", "model": "doubao-pro", "pricePerK": 0.8})
@@ -312,11 +316,12 @@ def main():
r = h.call(f"/api/agents/{a1['id']}/memories/import", "POST", {"data": exported, "mode": "merge"})
check("合并模式:新增了 1 条", r.get("added") == 1, str(r))
check("合并模式:同名的跳过了", r.get("skipped", 0) >= 1, str(r))
check("合并模式:保留当前情绪", agentdb.emotion_get(a1["id"]) == "挺开心的")
check("合并模式:保留当前情绪", agentdb.emotion_state(a1["id"])["text"] == "挺开心的")
r = h.call(f"/api/agents/{a1['id']}/memories/import", "POST",
{"data": {"type": "agent-memory", "version": 1, "smartMemory": [], "Emotion": "换掉了"},
"mode": "replace"})
check("覆盖模式:情绪也换了", agentdb.emotion_get(a1["id"]) == "换掉了", agentdb.emotion_get(a1["id"]))
check("覆盖模式:情绪也换了(v1.1 的字符串格式也认)",
agentdb.emotion_state(a1["id"])["text"] == "换掉了", str(agentdb.emotion_state(a1["id"])))
# ---------------------------------------------------------------- 11
section("11. 多模型:引用了就真的用(需求 3.3)")
@@ -473,6 +478,84 @@ def main():
m2 = speech.voice_for(st, store.get_agent(half["id"]))
check("只设音色时引擎回落全局", m2.get("ttsVoice") == "only-voice", str(m2.get("ttsVoice")))
# ---------------------------------------------------------------- 17
section("17. v1.2 情绪双字段:老库迁移(**最容易把老用户数据搞丢的地方**)")
#
# ★ 直接在**内存库**上测 `_migrate`,而不是"造一个老库文件再换过去":
# 换文件的做法被 WAL 坑过一次 —— SQLite 连着旧文件的连接、
# 外加 -wal 里的新数据会把复制进去的老库盖掉,测出来是假阴性。
# 内存库没有这个问题,而且不碰任何全局状态。
import sqlite3
def old_db():
"""造一个 v1.1 的 emotions 表(只有 emotion 一列)"""
c = sqlite3.connect(":memory:")
c.row_factory = sqlite3.Row
c.executescript(
"CREATE TABLE emotions (agent_id TEXT PRIMARY KEY, "
"emotion TEXT NOT NULL DEFAULT '', updated_at TEXT NOT NULL);"
)
c.execute(
"INSERT INTO emotions VALUES ('a_old', '刚被师父夸了,心里偷着乐', '2026-01-01T00:00:00Z')"
)
c.commit()
return c
conn_old = old_db()
agentdb._migrate(conn_old)
row = conn_old.execute("SELECT preset, text, emotion FROM emotions WHERE agent_id='a_old'").fetchone()
check("迁移没炸(新列都加上了)", row is not None, str(row))
check("**老的自由文本搬进了 text**", row["text"] == "刚被师父夸了,心里偷着乐", str(tuple(row)))
check("preset 给了默认的「平常」", row["preset"] == "平常", str(tuple(row)))
check("老的 emotion 列留着(SQLite 删列要重建表,不值当)",
row["emotion"] == "刚被师父夸了,心里偷着乐")
# 幂等:再迁移一次不能把 text 冲掉
agentdb._migrate(conn_old)
row2 = conn_old.execute("SELECT text FROM emotions WHERE agent_id='a_old'").fetchone()
check("再迁移一次不会冲掉已有数据(幂等)", row2["text"] == row["text"], str(tuple(row2)))
# 全新的库(表已经是新结构)跑迁移应当什么都不做
conn_new = sqlite3.connect(":memory:")
conn_new.row_factory = sqlite3.Row
conn_new.executescript(agentdb.SCHEMA)
agentdb._migrate(conn_new)
check("新结构的库跑迁移是空操作(不报错、不动数据)", True)
conn_old.close()
conn_new.close()
section("18. v1.2 死亡规则:不能对话、不能自己复活、但用户能复活")
dead = store.create_agent({"name": "会死的"})
did = dead["id"]
check("默认不是死亡", not agentdb.is_dead(did))
tools_runtime.push_context(agentId=did, conversationId="c_probe")
agent_tools.emotion_set({"preset": "死亡", "text": "胸口一凉,倒了下去"})
check("AI 能把自己改成死亡", agentdb.is_dead(did))
# ★ AI 不能自愈(需求 3.5 明文)
try:
agent_tools.emotion_set({"preset": "喜", "text": "我又活了"})
check("**AI 不能把自己从死亡改活**", False, "居然改成功了")
except ValueError as exc:
check("**AI 不能把自己从死亡改活**", "不能自己复活" in str(exc), str(exc))
check("死亡状态没被改掉", agentdb.is_dead(did))
# 死亡**照常播报**给同伴(需求 3.5 明文)
rows = agentdb.peer_emotions("w", "x", [did])
check("死亡也播报给同伴", bool(rows) and rows[0][1] == "死亡", str(rows))
# ★ 用户通道能改活(兜底主权)
r = h.call(f"/api/agents/{did}/emotion", "PUT", {"preset": "喜", "text": "醒来"})
check("**用户能把它改活**(兜底主权)", not agentdb.is_dead(did), str(r.get("emotion")))
# 不认识的 preset 要拒绝,而且理由里要列出合法值
try:
agent_tools.emotion_set({"preset": "暴躁", "text": ""})
check("不认识的 preset 被拒", False, "居然通过了")
except ValueError as exc:
check("不认识的 preset 被拒", "五个之一" in str(exc), str(exc))
# 只给 text 不给 preset:保留当前 preset(emotion_set 是"只写传进来的")
agent_tools.emotion_set({"preset": "怒", "text": "火了"})
agentdb.emotion_set(did, text="气消了一半")
check("只写 text 时 preset 不动",
agentdb.emotion_state(did)["preset"] == "怒"
and agentdb.emotion_state(did)["text"] == "气消了一半",
str(agentdb.emotion_state(did)))
# ---------------------------------------------------------------- 汇总
print()
if fails: