feat(v1.2): 阶段4 下半 —— 私聊安全网 + 仲裁接进对话循环
## 私聊(§3.9 形态 B) 新增 `kernel/private_chat.py` + `POST /api/private-chat`。 **三个必须想清楚的点**(都写进了模块头注释): **① "安全网"防的是什么** 两个 AI 互相对话最大的风险是**停不下来**:A 说一句、B 接一句、A 再接回来…… 它们没有"该结束了"的内在信号(用户不在场,没人喊停)。 所以轮数上限(默认 10,可配)不是优化,是**防失控**。 另加硬顶 60 轮 —— 一轮两次 LLM 调用,100 轮就是 200 次,那不是"聊天"。 **② "不撑独角戏"** 一方 Token 触顶时,**不能**让另一方继续自言自语。需求的做法是: 另一方**单方面收尾**(说一句结语)然后立刻结束。 不收尾直接断的话对话会停在一句没头没尾的话上;继续说的话就成了独白, 读起来怪而且白花钱。 **③ 每轮的"用户"是谁** 对 A 来说 B 说的话就是**用户消息**,对 B 来说反过来。 这是私聊能跑起来的关键 —— 两个 AI 各自都在"跟对方聊天", 而不是"在同一个上下文里各写各的"。探针专门钉了这条视角转换。 **摘要写进共享记忆**:写到两人的**共享空间**(`world:<id>`), 这样以后两人聊天时都"记得"这次私聊。没有共同世界观时**不写** —— 硬塞进某一个人的私有空间,等于替他记了一件他没参与的事。 ## 仲裁接进对话循环 `_perform_chat` 里:群聊时按 `group_chat.resolve()` 换掉 `agent`, 并 emit 一个 `speaker` 事件(界面据此显示"这一轮谁在说、为什么是他")。 单聊时这是**恒等操作**(判断收在 `is_group` 里),所以不用加 if。 ★ 这一步是必须的 —— 不接的话仲裁模块就是个孤岛, 群聊行为跟单聊一模一样("数据接上了但没挂上去"是这个项目反复出现的失败类)。 ## 验证 `probe-group-chat.py` 扩到 11 节:私聊 20+ 项。 ★ 关键的一节是**用 mock 替掉大模型**测编排逻辑: 真跑的话一次测试要烧 20 次 API 调用,而且测的是模型输出而不是我的逻辑。 mock 之后能精确断言"说到 3 轮就停(6 句)"、 "一方触顶时另一方收尾后立刻结束"、"触顶那个没有继续说话"。 ★ 探针自己又错了一次:设 `tokenLimit: 1` 但那个智能体**用量是 0**, 0 >= 1 是 False,根本没触顶。加了"前置条件:b 确实触顶了"这条断言之后, 这种"测试本身没生效"的情况下次会立刻暴露 —— 这是写安全网测试时的 通用教训:**先断言前置条件成立**。 八个探针全绿。
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""群聊仲裁的探针(v1.2 §3.9)。
|
||||
"""群聊仲裁的探针(v1.2 §3.9)。
|
||||
|
||||
点名制的分支有五种(用户 @ / AI 移交 / 兜底 / 被点的人死亡 / 一轮内重复移交),
|
||||
每一种都有明确的期望结果,而且**都不能报错**(报错的话用户看到的是
|
||||
@@ -184,6 +184,102 @@ def main():
|
||||
check("一共 11 个(附录 B 的 10 个 + 建议机制)", len(agent_tools.AGENT_TOOLS) == 11,
|
||||
str(sorted(agent_tools.AGENT_TOOLS)))
|
||||
|
||||
print("\n=== 8. 私聊:轮数解析与额度判据 ===")
|
||||
import private_chat as P
|
||||
|
||||
check("默认 10 轮(需求定的)", P.resolve_rounds({}) == 10, str(P.resolve_rounds({})))
|
||||
check("全局设置能改", P.resolve_rounds({'privateChatRounds': 6}) == 6)
|
||||
check("本次调用能改(压过全局)",
|
||||
P.resolve_rounds({'privateChatRounds': 6}, {'maxRounds': 3}) == 3)
|
||||
check("有硬顶(不让人调成刷爆账单的规模)",
|
||||
P.resolve_rounds({}, {'maxRounds': 99999}) == P.MAX_ROUNDS_CAP,
|
||||
str(P.resolve_rounds({}, {'maxRounds': 99999})))
|
||||
check("乱填回落默认", P.resolve_rounds({'privateChatRounds': 'x'}) == 10)
|
||||
check("额度为 0 表示无限制", P.limit_hit({'id': a['id'], 'tokenLimit': 0}) is False)
|
||||
import agentdb as _db
|
||||
|
||||
_db.token_add(a['id'], 'm', 100, 50)
|
||||
check("没超限额时 false", P.limit_hit({**a, 'tokenLimit': 1000}) is False)
|
||||
check("超了才是 true", P.limit_hit({**a, 'tokenLimit': 100}) is True)
|
||||
|
||||
print("\n=== 9. ★ 私聊的视角转换(两个 AI 各自在「跟对方聊天」)===")
|
||||
turns = [
|
||||
{'agentId': a['id'], 'name': '玄辰', 'content': '你来了。'},
|
||||
{'agentId': b['id'], 'name': '仙辰', 'content': '嗯,路上耽搁了。'},
|
||||
]
|
||||
ha = P._history_for(a['id'], turns)
|
||||
hb = P._history_for(b['id'], turns)
|
||||
check("对 A 来说:自己说的是 assistant", ha[0]['role'] == 'assistant', str(ha))
|
||||
check("对 A 来说:**对方说的是 user**", ha[1]['role'] == 'user', str(ha))
|
||||
check("对 B 来说正好反过来",
|
||||
hb[0]['role'] == 'user' and hb[1]['role'] == 'assistant', str(hb))
|
||||
|
||||
print("\n=== 10. ★★ 安全网:轮数上限 + 一方触顶不撑独角戏 ===")
|
||||
#
|
||||
# 这两个是需求里私聊唯一的硬约束,也是最该测的 ——
|
||||
# 两个 AI 互相对话最大的风险就是**停不下来**(用户不在场,没人喊停)。
|
||||
#
|
||||
# mock 掉真正的大模型调用:这里要测的是**编排逻辑**,不是模型输出。
|
||||
# 真跑的话一次测试要烧 20 次 API 调用。
|
||||
import agent_loop
|
||||
|
||||
real_run_agent = agent_loop.run_agent
|
||||
|
||||
def fake(**kw):
|
||||
who = (kw.get('agent') or {}).get('name') or '?'
|
||||
fake.calls.append(who)
|
||||
return {'content': f'{who} 说了第 {len(fake.calls)} 句'}
|
||||
|
||||
fake.calls = []
|
||||
agent_loop.run_agent = fake
|
||||
try:
|
||||
r = P.run_pair(store.read_settings(), a, b, max_rounds=3)
|
||||
check("跑完了", r.get('ok') is True, str(r.get('error')))
|
||||
check("**说到 3 轮就停**(一轮 = A 一次 + B 一次)",
|
||||
r.get('rounds') == 6, str(r.get('rounds')))
|
||||
check("结束原因标成 rounds", r.get('endReason') == 'rounds', str(r.get('endReason')))
|
||||
check("两个人都说了话", set(fake.calls) == {'玄辰', '仙辰'}, str(fake.calls))
|
||||
conv2 = store.get_conversation(r['conversationId']) or {}
|
||||
check("会话类型是 private", conv2.get('type') == 'private', str(conv2.get('type')))
|
||||
check("两个参与者都在", set(conv2.get('participants') or []) == {a['id'], b['id']},
|
||||
str(conv2.get('participants')))
|
||||
check("完整记录落库(用户能翻看)", len(conv2.get('messages') or []) == 6,
|
||||
str(len(conv2.get('messages') or [])))
|
||||
check("摘要写进了**共享记忆**", bool(r.get('summary')), str(r.get('summary')))
|
||||
shared = _db.memory_list(_db.world_scope(world['id']))
|
||||
check("记忆里能查到这条摘要",
|
||||
any('私下交谈' in (m.get('title') or '') for m in shared),
|
||||
str([m.get('title') for m in shared]))
|
||||
|
||||
# ★ 一方触顶:另一方**单方面收尾**后立即结束
|
||||
#
|
||||
# 注意:光设 `tokenLimit: 1` 是不够的 —— 判据是"用掉的总量 >= 上限",
|
||||
# 而 b 这时候还没说过话(用量 0),0 >= 1 是 False,根本没触顶。
|
||||
# 得让它**真的用过**才算超。
|
||||
store.update_agent(b['id'], {'tokenLimit': 1})
|
||||
_db.token_add(b['id'], 'm', 100, 50)
|
||||
b_limited = store.get_agent(b['id'])
|
||||
check("前置条件:b 确实触顶了", P.limit_hit(b_limited) is True, str(b_limited.get('tokenLimit')))
|
||||
fake.calls = []
|
||||
r2 = P.run_pair(store.read_settings(), a, b_limited, max_rounds=5)
|
||||
check("**一方触顶时立刻结束**", r2.get('endReason') == 'limit', str(r2.get('endReason')))
|
||||
check("标了是谁触顶的", r2.get('endedBy') == '仙辰', str(r2.get('endedBy')))
|
||||
check("**另一方做了收尾**(不是一句话没有就断)",
|
||||
len(r2.get('turns') or []) >= 2, str(len(r2.get('turns') or [])))
|
||||
check("**没撑独角戏**(触顶那个没继续说话)",
|
||||
sum(1 for t in (r2.get('turns') or []) if t.get('name') == '仙辰') <= 1,
|
||||
str([t.get('name') for t in r2.get('turns') or []]))
|
||||
store.update_agent(b['id'], {'tokenLimit': 0})
|
||||
finally:
|
||||
agent_loop.run_agent = real_run_agent
|
||||
|
||||
print("\n=== 11. 私聊的边界 ===")
|
||||
check("没有共同世界观时不写记忆(不替人记他没参与的事)",
|
||||
P.summarize_to_memory(a, {'id': 'x', 'worldviewId': ''}, turns) == '')
|
||||
check("没说话时不写摘要", P.summarize_to_memory(a, b, []) == '')
|
||||
check("缺参数时返回错误而不是抛异常",
|
||||
P.run_pair(store.read_settings(), {}, {}, max_rounds=1).get('ok') is False)
|
||||
|
||||
print()
|
||||
if fails:
|
||||
print(f"❌ {len(fails)} 项不合格:")
|
||||
|
||||
Reference in New Issue
Block a user