第二步:让 ZCode 上的 Bash/Write/Edit 授权走 AgentMail 的人工审批,
而不是只靠本地界面。
钩子契约从 CLI 产物里逆出来(不猜协议):
- 输入走 stdin:{hook_event_name, tool_name, tool_input, session_id, permission_mode…}
- 输出走 stdout,schema **严格**:{"decision":"approve"} / {"decision":"block","reason"}
多一个键就会报 "Hook stdout failed HookJSONOutput schema validation"
- 空输出 / 不以 { 开头 = 不表态;exit 2 = 拒绝;其它非零 = 钩子失败
- 注入的环境变量含 ZCODE_PLUGIN_ROOT / ZCODE_PLUGIN_DATA / ZCODE_SESSION_ID
(MCP 配置里用 ZCODE_SESSION_ID 反而会抛「需要运行时会话上下文」)
档位判定与 pi 桥逐条对齐(plan 直接拒 / workspace 问人 / full 批准),
判定逻辑抽成纯函数 lib/hook-policy.mjs 以便穷举:
其中 full 档必须**返回批准而不是不表态** —— 钩子一旦触发说明 ZCode 本会去问人,
不表态等于让那个询问照常发生,full 档就退化成了 workspace 档。
钩子自己开 SSE 等决定,不依赖桥进程:网关的 SSE 是扇出的
(clients 按唯一 id 存,SendToAgent 推给该 Agent 的所有客户端),
一次性进程也能订阅到自己那条 permission_decision。这样交互模式下同样可用
(人自己开着 ZCode 干活时并没有桥在跑)。先建连再发请求是有意的:
反过来会有一个窗口,人在窗口内点的同意推送给当时还不存在的客户端。
fail closed 但区分模式:永久失败(409/4xx)一律拒绝;暂时失败在
AGENTMAIL_SESSION_ID 非空(邮件驱动、没有本地界面兜底)时拒绝,
交互模式则不表态让人就地决定。
「一直同意」落盘(lib/grants-file.mjs):钩子是一个事件一个进程,
不落盘那个选项就是骗人的。判定仍交给共用的 permission-grants.js。
共用模块同源范围扩到 9 个(新增 permission-mode / relay-key /
permission-grants / sse-client)—— 档位语义与决策判定分叉会让「同意」
在 ZCode 上悄悄变成另一种意思。
验证:
- 单元 229/229(新增 hook-policy 14 项、grants-file 8 项,含反向对照)
- 共用模块四方同源检查通过
- 授权桥端到端 5/5,全部带反向对照:
同意→approve;拒绝→block 且原因必须来自人的拒绝(不能是超时兜底);
plan 档拒绝且**不产生**任何权限邮件;无人可问(409)→fail closed;
非守卫工具→不表态
- `zcode plugins list` → agentmail@inline [enabled],hooks: 1,
mcp: plugin:agentmail:agentmail
我自己写错的两处判据(都已修,值得记下):
1. 待决权限列表里有历史积压(实测 6 条,含其它 Agent 的条目),
只按「第一条新的」取会拿到无关请求 —— 于是人点了同意而钩子在等自己那条,
最后超时。第一版还把这个超时误报成「拒绝路径通过」。
现在按「启动前快照差集 + session_id + agent_name」三重过滤。
2. 「无人可问」控制组最初传了个非 UUID 的 session id,走的是 400(参数错),
验不到 409 那条真实路径。改为真的造一条只有 Agent 没有人类的会话。
99 lines
4.1 KiB
JavaScript
99 lines
4.1 KiB
JavaScript
/**
|
||
* 「一直同意」的跨进程持久化测试。
|
||
*
|
||
* 这个功能的判据只有一条最要紧:**下一个进程还认不认**。
|
||
* 钩子一封(一次工具调用)一个进程,所以「记在内存里」等于没记。
|
||
*/
|
||
|
||
import { test } from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
|
||
import { tmpdir } from 'node:os';
|
||
import { join } from 'node:path';
|
||
import { createFileGrantStore, grantsFilePath } from '../lib/grants-file.mjs';
|
||
|
||
const tmpFile = async () => join(await mkdtemp(join(tmpdir(), 'zc-grants-')), 'g.json');
|
||
|
||
test('★ 授权跨进程存活(新 store 读同一个文件仍认账)', async () => {
|
||
const f = await tmpFile();
|
||
const s1 = createFileGrantStore(f);
|
||
assert.equal(s1.isGranted('sess-1', 'Bash'), false);
|
||
assert.equal(s1.grant('sess-1', 'Bash', '一直同意'), true);
|
||
|
||
// 模拟下一个钩子进程
|
||
const s2 = createFileGrantStore(f);
|
||
assert.equal(s2.isGranted('sess-1', 'Bash'), true);
|
||
await rm(join(f, '..'), { recursive: true, force: true });
|
||
});
|
||
|
||
test('「同意」是单次,不落盘', async () => {
|
||
const f = await tmpFile();
|
||
const s = createFileGrantStore(f);
|
||
assert.equal(s.grant('sess-1', 'Bash', '同意'), false);
|
||
assert.equal(createFileGrantStore(f).isGranted('sess-1', 'Bash'), false);
|
||
await rm(join(f, '..'), { recursive: true, force: true });
|
||
});
|
||
|
||
test('★ 反向对照:同一个文件里,一直同意与单次同意必须分道扬镳', async () => {
|
||
// 只翻转决策文本,落盘结果必须不同 —— 否则「什么都记下来」也会让上一条通过。
|
||
const f = await tmpFile();
|
||
const s = createFileGrantStore(f);
|
||
assert.equal(s.grant('sess-a', 'Bash', '一直同意'), true);
|
||
assert.equal(s.grant('sess-b', 'Bash', '同意'), false);
|
||
const back = createFileGrantStore(f);
|
||
assert.equal(back.isGranted('sess-a', 'Bash'), true);
|
||
assert.equal(back.isGranted('sess-b', 'Bash'), false);
|
||
await rm(join(f, '..'), { recursive: true, force: true });
|
||
});
|
||
|
||
test('授权按会话隔离,不跨会话泄漏', async () => {
|
||
const f = await tmpFile();
|
||
const s = createFileGrantStore(f);
|
||
s.grant('sess-1', 'Bash', '一直同意');
|
||
const back = createFileGrantStore(f);
|
||
assert.equal(back.isGranted('sess-1', 'Bash'), true);
|
||
assert.equal(back.isGranted('sess-2', 'Bash'), false);
|
||
await rm(join(f, '..'), { recursive: true, force: true });
|
||
});
|
||
|
||
test('授权按工具隔离(bash 的免批不放行 write)', async () => {
|
||
const f = await tmpFile();
|
||
const s = createFileGrantStore(f);
|
||
s.grant('s', 'Bash', '一直同意');
|
||
const back = createFileGrantStore(f);
|
||
assert.equal(back.isGranted('s', 'Bash'), true);
|
||
assert.equal(back.isGranted('s', 'Write'), false);
|
||
await rm(join(f, '..'), { recursive: true, force: true });
|
||
});
|
||
|
||
test('撤销会话后不再免批', async () => {
|
||
const f = await tmpFile();
|
||
const s = createFileGrantStore(f);
|
||
s.grant('s', 'Bash', '一直同意');
|
||
assert.equal(s.revokeSession('s'), true);
|
||
assert.equal(createFileGrantStore(f).isGranted('s', 'Bash'), false);
|
||
await rm(join(f, '..'), { recursive: true, force: true });
|
||
});
|
||
|
||
test('文件不存在或内容损坏都当空表,不抛错', async () => {
|
||
const f = await tmpFile();
|
||
assert.equal(createFileGrantStore(f).isGranted('s', 'Bash'), false);
|
||
await writeFile(f, '{ 这不是 JSON', 'utf8');
|
||
assert.equal(createFileGrantStore(f).isGranted('s', 'Bash'), false);
|
||
await writeFile(f, '{"sessions":{"s":"not-an-array"}}', 'utf8');
|
||
assert.equal(createFileGrantStore(f).isGranted('s', 'Bash'), false);
|
||
await rm(join(f, '..'), { recursive: true, force: true });
|
||
});
|
||
|
||
test('文件位置按 显式 > AGENTMAIL_CONFIG_DIR > ZCODE_PLUGIN_DATA > 家目录 解析', () => {
|
||
const p = grantsFilePath({
|
||
AGENTMAIL_ZCODE_GRANTS_FILE: '/x/g.json',
|
||
AGENTMAIL_CONFIG_DIR: '/c',
|
||
ZCODE_PLUGIN_DATA: '/d'
|
||
});
|
||
assert.equal(p, '/x/g.json');
|
||
assert.equal(grantsFilePath({ AGENTMAIL_CONFIG_DIR: '/c', ZCODE_PLUGIN_DATA: '/d' }), '/c/permission-grants.json');
|
||
assert.equal(grantsFilePath({ ZCODE_PLUGIN_DATA: '/d' }), '/d/permission-grants.json');
|
||
assert.match(grantsFilePath({}), /permission-grants\.json$/);
|
||
});
|