Files
MailUI4Agents/plugins/zcode-mail-bridge/test/relay-policy.test.mjs
JianFeeeee c5e1d562eb feat(zcode): 邮件驱动 —— 收到来信就自动开工,并把结论回信
第三步(补齐一等 Agent 的另一半):驱动进程订阅 SSE,按邮件起一轮 headless
ZCode,取最终文本回信。

## --mode 是必传的(不传等于关掉授权系统)

ZCode 的权限判定里 `mode === "yolo"` 一律 allow
("Yolo mode bypasses permission prompts"),而 `--prompt` 的默认 mode **就是 yolo**。
所以驱动不传 --mode 时:授权钩子根本不会触发,整个授权系统**静默消失** ——
不报错,只是没有任何询问,看起来一切正常。

档位映射(依据是 CLI 产物里的规则表,不是猜):
  plan → --mode plan    (mode.plan.nonReadOnly:非只读一律拒)
  workspace → --mode build(mode.build.highRisk / sideEffect:Bash/Write/Edit → ask)
  full → --mode yolo    (刻意绕过)
buildRunArgs 收不到 mode 直接抛错;测试里有一条反向对照钉住「只有 full 能得到 yolo」,
含大写 FULL(共用库 normalizeMode 严格匹配,落回 default 而不是 yolo —— 好性质,也钉住)。

## 一轮怎么跑

  node <zcode.cjs> --prompt <提示词> --output-format stream-json \
       --cwd <工作目录> --mode <m> [--resume sess_xxx] --max-turns N

用 stream-json 而不是 --json:`--json` 全程无输出,一个卡住的回合与一个正在
干活的回合在外部完全一样,而邮件驱动的会话没有界面,日志是唯一能看见它的地方。
输出契约(逐条事件 + 末尾 {type:"result",sessionId,response})同样逆自 CLI 产物。
会话延续靠 --resume + 存回的 sess_…:丢了它模型每封信都从零开始。

## 回信策略(与另三桥同源)

- 人来信 → 自动把本轮最终文本回过去(relay:'summary' + relay_key 走免配额通道)
- Agent 来信 → **不**自动回(Agent 间必须自己 send_mail,否则两边把对方的
  「已收到」当待办,无限客套)
- 一轮跑不起来 → **必回**失败信,且给出 ZCode 自己的成因(没登录/缺模型配置/
  CLI 路径不对)。没有本地界面时,什么都不发等于「信发出去了,然后再无音讯」。
  刻意不复用共用库那份 renderFailureReport:它的建议是「调整可用模型范围」,
  对 ZCode 什么也解决不了。
- 模型这一轮自己发过信 → 让位。工具跑在 ZCode 派生的 MCP 服务器**进程**里,
  与驱动内存不通,所以经 lib/explicit-sends.mjs 落盘对齐(不记的后果线上实测过:
  收件箱里两封说同一件事的邮件,311 与 342 字节)。

## 两处健壮性(都是实现时自己发现的真问题)

- 超时必须**必然** settle:既不退也不报错的孩子会让 Promise 永不 settle,
  而队列是串行的 → 那封信永远挂住、后面的信全都不再被处理。
  现在 SIGTERM → SIGKILL → 无论如何收尾;定时器刻意不 unref
  (unref 过的定时器让「没有其它句柄」的进程直接退出,收尾根本没机会跑)。
- 关停时终止在途回合:否则 systemd 杀掉驱动后那个 ZCode 还在跑工具,
  而既没有驱动看着它、也没有本地界面看着它。

## 自报强制力只声明得出来的事

驱动启动时读自己的 hooks/hooks.json,确认 PermissionRequest 已注册才报 native,
否则报 advisory 并在日志里写明原因 —— 不替一个不存在的能力背书。

## 验证

- 单元 320/320(新增 90 项:turn-mode 8、zcode-run 17、driver 19、prompt 14 +
  继承的共用测试;含反向对照)
- 邮件驱动端到端 7/7 × 3 次连跑稳定:桩 CLI 替掉 ZCode,真网关真邮件 ——
  SSE 订阅、去重、工作目录、档位映射、参数拼装(--mode 必须对)、
  stream-json 解析、回信、Agent 来信不回、CLI 失败必回失败信
- 授权桥端到端 5/5 × 3 次连跑稳定
- 共用模块四方同源(新纳入 catchup/relay-dedup/relay-policy/workspace,
  反向验证:让 workspace.js 分叉会被抓住)

## 我自己写错并被测试抓出来的三处(值得记)

1. 验证脚本把人类发信写成了 /api/v1/mail/send(**Agent** 路由)→ 401。
   报错「Missing Authorization: Bearer …」其实已经指明走错了路由表。
2. findReply 按「驱动验证(人)」这种片段找,第二次跑时命中了**上一轮遗留的回信**
   → 正文比对失败、后续参数核对变成「无法判定」。收件箱是跨轮次共享的持久状态,
   必须按唯一 marker 定位(与之前「待决权限列表」那次是同一类错误)。
3. 停旧驱动只发 SIGTERM 不等退出 → 新旧两个驱动同时订阅 SSE,
   同一封信被回两次,判据取到哪封取决于时序 → 时灵时不灵。改成等 exit 事件。
   另:桩脚本用 process.exit 截断管道写入,导致 stderr 时有时无 —— 改用 exitCode。
2026-09-12 14:58:36 +08:00

132 lines
5.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 自动转发适用范围的判定lib/relay-policy.js
*
* 三个函数是同一件事的三个出口,必须一起看:
* - autoRelayDecision 插件该不该替模型把结论发出去
* - replyInstruction 提示词里怎么跟模型说这件事
* - inboundHeadline 进来的这封是新活、是回复、还是补投
*
* 分开写必然分叉,而分叉的代价是模型被骗:以为插件会替它回信,于是把话说完
* 就停手,那封信却永远不会发出去。所以这里逐条钉住它们的一致性。
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
addrName,
autoRelayDecision,
replyInstruction,
inboundHeadline,
} from '../lib/relay-policy.js';
// ─── addrName ───
test('addrName 取三维地址的名字段', () => {
assert.equal(addrName('pi@/home/program/agentmail.某别名'), 'pi');
assert.equal(addrName('jianf'), 'jianf');
assert.equal(addrName(' dsh@/x '), 'dsh');
assert.equal(addrName(''), '');
assert.equal(addrName(undefined), '');
});
// ─── autoRelayDecision ───
test('人类来信 → 自动转发', () => {
const d = autoRelayDecision({ fromHuman: true, replyTo: 'jianf' });
assert.equal(d.relay, true);
});
test('Agent 来信 → 不自动转发', () => {
const d = autoRelayDecision({ fromHuman: false, replyTo: 'dsh' });
assert.equal(d.relay, false,
'Agent 间通信必须由模型主动 send_mail —— 两边都自动回会无休止互相唤醒');
assert.match(d.reason, /dsh/, '日志要说清是谁');
assert.match(d.reason, /Agent/);
});
test('不知道回给谁 → 不转发,且理由与「对方是 Agent」区分得开', () => {
const d = autoRelayDecision({ fromHuman: true, replyTo: '' });
assert.equal(d.relay, false);
assert.match(d.reason, /不知道回给谁/,
'「本轮没有回信」有三种原因,日志里必须能分辨');
});
test('replyTo 带三维地址时也能认出 Agent 名', () => {
const d = autoRelayDecision({ fromHuman: false, replyTo: 'opencode@/home/x.别名' });
assert.equal(d.relay, false);
assert.match(d.reason, /opencode/);
});
test('缺省参数不抛错(畸形事件不该弄死投递)', () => {
assert.equal(autoRelayDecision().relay, false);
assert.equal(autoRelayDecision({}).relay, false);
});
// ─── replyInstruction 与 autoRelayDecision 的一致性 ───
test('人类来信的提示词承诺「插件会替你发」,且这与决策一致', () => {
const lines = replyInstruction({ fromHuman: true });
const text = lines.join('\n');
assert.match(text, /回信不用你自己发/);
assert.equal(autoRelayDecision({ fromHuman: true, replyTo: 'jianf' }).relay, true,
'承诺了就必须真的做');
});
test('Agent 来信的提示词必须明说「插件不会替你回信」', () => {
const text = replyInstruction({ fromHuman: false }).join('\n');
assert.match(text, /不会替你回信/);
assert.match(text, /send_mail/, '必须给出唯一可行的做法');
assert.doesNotMatch(text, /回信不用你自己发/,
'这句话在 Agent → Agent 时是假的 —— 说了它模型就会把话说完然后停手');
});
test('Agent 来信的提示词要劝阻纯客套', () => {
const text = replyInstruction({ fromHuman: false }).join('\n');
assert.match(text, /收到|确认/, '要点名那种没有信息量的回复');
assert.match(text, /互相客套|无休止/, '要说清后果,否则模型不知道为什么被劝阻');
});
test('Agent 来信时把回信地址带进提示词(有就带)', () => {
const withAddr = replyInstruction({ fromHuman: false, replyAddress: 'dsh@/x.别名' }).join('\n');
assert.match(withAddr, /dsh@\/x\.别名/,
'要它自己发信却不给地址,它会拼一个 .new 出来 —— 那会静默开新会话');
const without = replyInstruction({ fromHuman: false }).join('\n');
assert.doesNotMatch(without, /(回信地址:)/, '没有地址时不该留一个空括号');
});
// ─── inboundHeadline ───
test('回复到了 → 明说「这不是新任务」', () => {
const h = inboundHeadline({ inReplyTo: 'm-1', fromHuman: false });
assert.match(h, /回复/);
assert.match(h, /不是新任务/,
'把回复当新任务处理正是互相客套的起点');
});
test('回复的标题优先于续谈/补投标记', () => {
const h = inboundHeadline({ inReplyTo: 'm-1', fromHuman: true, reused: true, catchup: true });
assert.match(h, /回复/, 'in_reply_to 是最强信号');
});
test('Agent 来信在标题里就标出来', () => {
assert.match(inboundHeadline({ fromHuman: false }), /Agent/);
assert.doesNotMatch(inboundHeadline({ fromHuman: true }), /Agent/,
'人类来信不该带这个括号 —— 那是噪音');
});
test('补投要说明,否则模型按「刚到的」语气回', () => {
const h = inboundHeadline({ fromHuman: true, catchup: true });
assert.match(h, /积压|补投/);
});
test('续谈与新会话的措辞不同', () => {
assert.match(inboundHeadline({ fromHuman: true, reused: true }), /本会话/);
assert.match(inboundHeadline({ fromHuman: true, reused: false }), /你收到/);
});
test('缺省参数不抛错', () => {
assert.equal(typeof inboundHeadline(), 'string');
assert.equal(typeof inboundHeadline({}), 'string');
});