fix(v1.2): 独立核对揪出 6 处「内核写完、界面没接」,修掉 4 处(v2.0.67)

功能全部做完之后做了一次**独立核对**(逐条比对需求与代码)——
结果**不是"全部实现"**:6 处「内核实现了、界面/调用方没接上」。

★★ 共同点:**三层各自看都对,只有真的跑一遍才看得见** ——
正是我这几轮一直防的那一类,结果自己犯了 6 次。

## ★★ 最严重:`pass_turn` 一轮一次**根本不生效**

标记写在 `tools_runtime` 的**工具上下文**里,但工具跑在 **worker 线程**,
而 `_ctx` 是 thread-local —— 标记随线程消失,主线程每次又 `push_context` 重置。
于是 `handoffUsed` **永远是 False**,可以一轮移交无限次。

**更糟:我的探针假通过了。** 探针直接调 `agent_tools.pass_turn`(主线程),
标记留在主线程没被清 → 看着完全正常,真机才露馅。

修法:提到 `tools_runtime` 的**模块级轮次状态**(`begin_round`/`note_handoff`/
`handoff_state`),和线程无关;`agent_loop` 每轮重置。

★ 并加了一节**走真实路径**(`execute_tool`)的探针 ——
**探针的调用路径必须和真实调用路径一致**。直接调函数测的是"逻辑对不对",
不是"它在这个系统里生不生效"。

## 第二处:移交链路是死的

`app.py` 读的是 **HTTP body** 的 `handoffTo`,而 Kotlin **从不发这个字段**
(全仓库只有 2 处:一写一读,读的永远拿空串)→「② AI 移交」是死代码。

修法:改读 `tools_runtime.handoff_state()['to']`。

## 三处「收了但没用」

· **状态过期事件被丢**:`parseEvent` 没有 `"status"` 分支 → `else -> null`
· **状态脚本报错静默**:只 `print` 到 stderr,从不 emit(附录 E 明写"UI 提示")
· **仲裁理由算了不显示**:`speakerNote` 赋值了但**没有任何一处读它**

## 一处界面缺入口

**「拉人要先问我」**(§6.1 静默/需确认):内核早就按 `inviteMode` 分流,
界面没入口 → `confirm` 永远打不开。现在加在「能力」组。

## 还剩 2 处(下一版)

· **私聊没有界面入口**(内核完整)
· **私聊轮数「可配」界面上改不了**

同一个原因:私聊整个形态还没有界面。

## 验证

新探针走真实路径确认"第 2 次移交被拒";八个探针全绿;构建通过。
This commit is contained in:
l
2026-10-16 20:13:29 +08:00
parent f84c68762b
commit 8ac14bdaa2
11 changed files with 267 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
"""群聊仲裁的探针(v1.2 §3.9)。
"""群聊仲裁的探针(v1.2 §3.9)。
点名制的分支有五种(用户 @ / AI 移交 / 兜底 / 被点的人死亡 / 一轮内重复移交),
每一种都有明确的期望结果,而且**都不能报错**(报错的话用户看到的是
@@ -144,13 +144,18 @@ def main():
b['id'] in G.member_ids(store.get_conversation(c2['id']) or {}))
print("\n=== 6. pass_turn 工具 ===")
# ★ 每次都 push 完整上下文 —— `push_context` 是**替换**语义,
# 只补一个键会把 agentId / conversationId 冲掉(这个坑踩过)
# ★ 每次 `begin_round()` —— agent_loop 每轮开头会调它。
# 不调的话上一节留下的 `used=True` 会让这里全变成"已经移交过"。
tools_runtime.begin_round()
tools_runtime.push_context(agentId=a['id'], conversationId=c2['id'])
r = agent_tools.pass_turn({'target': '仙辰'})
check("移交成功", r.get('handed_to') == '仙辰', str(r))
check("上下文里记了移交标记", tools_runtime.current_context().get('handoffUsed') is True)
check("也记了交给谁", tools_runtime.current_context().get('handoffTo') == b['id'])
# ★ 读**轮次状态**,不是工具上下文 —— 上下文是 thread-local,
# 工具跑在 worker 线程里写的东西回不来(这就是当初失效的原因)
check("轮次状态里记了移交标记", tools_runtime.handoff_state().get('used') is True,
str(tools_runtime.handoff_state()))
check("也记了交给谁", tools_runtime.handoff_state().get('to') == b['id'],
str(tools_runtime.handoff_state()))
# 一轮内第二次:拒
try:
agent_tools.pass_turn({'target': '仙辰'})
@@ -162,6 +167,7 @@ def main():
dead = store.create_agent({'name': '死者'})
store.update_agent(dead['id'], {'worldviewId': world['id']})
agentdb.emotion_set(dead['id'], preset='死亡')
tools_runtime.begin_round() # 开新一轮,否则先撞上"已经移交过"
tools_runtime.push_context(agentId=a['id'], conversationId=c2['id'])
try:
agent_tools.pass_turn({'target': '死者'})
@@ -169,11 +175,14 @@ def main():
except ValueError as exc:
check("**死亡目标移交无效**", '死亡' in str(exc), str(exc))
check("理由说的是死亡,不是别的", '已经移交过' not in str(exc), str(exc))
tools_runtime.begin_round()
tools_runtime.push_context(agentId=a['id'], conversationId=c2['id'])
try:
agent_tools.pass_turn({'target': '玄辰'})
check("交给自己被拒", False, "居然允许了")
except ValueError as exc:
check("交给自己被拒", '自己' in str(exc), str(exc))
tools_runtime.begin_round()
print("\n=== 7. 附录 B 的工具清单齐了吗 ===")
for want in ('memory_add', 'memory_update', 'memory_forget', 'memory_search',
@@ -184,7 +193,54 @@ def main():
check("一共 11 个(附录 B 的 10 个 + 建议机制)", len(agent_tools.AGENT_TOOLS) == 11,
str(sorted(agent_tools.AGENT_TOOLS)))
print("\n=== 8. 私聊:轮数解析与额度判据 ===")
print("\n=== 8. ★★ 走真实路径测 pass_turn(旧探针是假通过)===")
#
# ★★ 这一节**必须**走 `tools_runtime.execute_tool`。
#
# 它会把上下文复制到 **worker 线程** 去执行(`run_with_timeout` 起线程),
# 而 `_ctx` 是 thread-local —— 工具在 worker 里写的东西**回不到主线程**。
#
# 旧探针直接调 `agent_tools.pass_turn`(主线程),标记留在主线程里
# 没被清,于是"一轮最多一次"看着是好的 —— **假通过**,
# 而真机上每一轮都能无限次移交(`handoffUsed` 永远是 False)。
#
# 教训:**探针的调用路径必须和真实调用路径一致**。
# 直接调函数测的是"函数逻辑对不对",不是"它在这个系统里生不生效"。
settings = store.read_settings()
c2 = store.create_conversation('群2', agent_id=a['id'], kind='group',
participants=[a['id'], b['id']])
def real_call(name: str, args: dict):
"""走真实的工具调用路径(含 worker 线程 + 上下文重置)"""
tools_runtime.push_context(agentId=a['id'], conversationId=c2['id'], toolName=name)
try:
return tools_runtime.execute_tool(name, args, settings)
finally:
tools_runtime.clear_context()
tools_runtime.begin_round() # agent_loop 每轮开头会调这个
r1 = real_call('pass_turn', {'target': '仙辰'})
check("真实路径:第 1 次移交成功", r1.get('ok') is True, str(r1))
st = tools_runtime.handoff_state()
check("**移交目标跨线程保住了**(这是旧实现丢掉的那个)",
st.get('to') == b['id'], str(st))
check("状态标成 used", st.get('used') is True, str(st))
r2 = real_call('pass_turn', {'target': '仙辰'})
check("真实路径:**第 2 次被拒**(一轮最多一次真的生效了)",
r2.get('ok') is False and '已经移交过' in str(r2.get('error')), str(r2))
# app.py 从轮次状态取(不是从 HTTP body —— 界面根本不发那个字段)
check("移交目标能被 app.py 读到(仲裁的 ② 分支不再是死代码)",
tools_runtime.handoff_state().get('to') == b['id'], str(tools_runtime.handoff_state()))
# 新一轮开始要重置
tools_runtime.begin_round()
check("新一轮开始后重置(可以再移交)",
tools_runtime.handoff_state() == {'to': '', 'used': False},
str(tools_runtime.handoff_state()))
r3 = real_call('pass_turn', {'target': '仙辰'})
check("新一轮里又能移交了", r3.get('ok') is True, str(r3))
tools_runtime.begin_round()
print("\n=== 9. 私聊:轮数解析与额度判据 ===")
import private_chat as P
check("默认 10 轮(需求定的)", P.resolve_rounds({}) == 10, str(P.resolve_rounds({})))
@@ -202,7 +258,7 @@ def main():
check("没超限额时 false", P.limit_hit({**a, 'tokenLimit': 1000}) is False)
check("超了才是 true", P.limit_hit({**a, 'tokenLimit': 100}) is True)
print("\n=== 9. ★ 私聊的视角转换(两个 AI 各自在「跟对方聊天」)===")
print("\n=== 10. ★ 私聊的视角转换(两个 AI 各自在「跟对方聊天」)===")
turns = [
{'agentId': a['id'], 'name': '玄辰', 'content': '你来了。'},
{'agentId': b['id'], 'name': '仙辰', 'content': '嗯,路上耽搁了。'},
@@ -214,7 +270,7 @@ def main():
check("对 B 来说正好反过来",
hb[0]['role'] == 'user' and hb[1]['role'] == 'assistant', str(hb))
print("\n=== 10. ★★ 安全网:轮数上限 + 一方触顶不撑独角戏 ===")
print("\n=== 11. ★★ 安全网:轮数上限 + 一方触顶不撑独角戏 ===")
#
# 这两个是需求里私聊唯一的硬约束,也是最该测的 ——
# 两个 AI 互相对话最大的风险就是**停不下来**(用户不在场,没人喊停)。
@@ -273,7 +329,7 @@ def main():
finally:
agent_loop.run_agent = real_run_agent
print("\n=== 11. 私聊的边界 ===")
print("\n=== 12. 私聊的边界 ===")
check("没有共同世界观时不写记忆(不替人记他没参与的事)",
P.summarize_to_memory(a, {'id': 'x', 'worldviewId': ''}, turns) == '')
check("没说话时不写摘要", P.summarize_to_memory(a, b, []) == '')