fix(agents): 四家桥的 read_inbox 一律按会话收窄(dsh/opencode/zcode/homeagent)
用户:「你还是没修好不同 session agent 收件箱隔离的问题」。上一轮我只修了 **pi**, 另外四家还漏着 —— 它们是**每一家各自实现** read_inbox,不修就还是漏。 ## 缺陷 列表按 Agent 列(整个收件箱),而 read_inbox 按契约把**列出来的都标成已读** ⇒ A 会话的回合会把 B 会话的未读标掉 ⇒ B 之后按 `?status=unread` 补投时 再也看不到那封信(静默丢信,不是"少看一封")。用户是在别的 Agent 上看到它的。 ## 四家的修法(各自平台能力不同,但都要"并发安全") | 桥 | 会话来源 | 为什么这样做 | |---|---|---| | dsh | 工具第二参数 `exec.agent.id` → `reverseMap` | 平台就在上下文里给了会话;**不能用模块级"当前会话"变量**(同进程可能同时跑多条会话的回合,会互相覆盖) | | opencode | 工具第二参数 `context.sessionID` → `reverseMap` | 同上 | | zcode | `AGENTMAIL_SESSION_ID`(在**调用时**读) | 一轮一个进程,驱动本来就注入它给授权钩子用;调用时读,避免将来复用进程拿到旧值 | | homeagent | `p.currentSessionID`(回合开始设、结束清) | Go 插件,本来就有这个状态 | 取不到会话一律**退回整体收件箱**(历史行为),不猜 —— 猜错就是静默丢信。 ## 判据 - 服务端语义:`server/internal/repo/session_scope_test.go`(读 A 不动 B、列表收窄、 计数与列表口径一致)。 - 桥侧接线:dsh 4 条、opencode 3 条、zcode 3 条、homeagent Go 1 条 (`TestInboxURLScopedBySession`,直接断言拼出来的 URL)。 每家都带**判据自检**:拿旧写法喂进来必须判红;dsh/opencode 还专门断言 "不得用模块级当前会话变量"。 - **部署件**(不是仓库):四家的部署快照里都能 grep 到 `session_id=`。 - **线上实测**:用 opencode 自己的 Agent 身份请求收窄列表 —— 会话 A 3 封、 会话 B 0 封、两者无交集、且都是全量的子集。 套件:opencode **331**、dsh **381**、zcode **385**、homeagent ok,全绿。 四家桥已重新部署(dsh/opencode/zcode 快照切换 + homeagent 新 plugin.bin 并重启), 四个服务均 active。
This commit is contained in:
44
plugins/dsh-mail-bridge/test/inbox-session-scope.test.mjs
Normal file
44
plugins/dsh-mail-bridge/test/inbox-session-scope.test.mjs
Normal file
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* read_inbox 必须收窄到**自己那条会话**(用户:「你还是没修好不同 session agent
|
||||
* 收件箱隔离的问题」)。
|
||||
*
|
||||
* 缺陷:列表按 Agent 列,且 read_inbox 按契约把列出的都标已读 ⇒ A 会话的回合把
|
||||
* B 会话的未读标掉 ⇒ B 之后按 `?status=unread` 补投时再也看不到那封信(静默丢信)。
|
||||
*
|
||||
* 这里只验**接线**(工具把会话 id 传出去了、会话来源是平台上下文而不是模块级变量);
|
||||
* 服务端语义由 server/internal/repo/session_scope_test.go 负责。
|
||||
*/
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url));
|
||||
const src = readFileSync(join(HERE, '..', 'src', 'index.ts'), 'utf8');
|
||||
|
||||
test('read_inbox 的 URL 会拼上会话收窄', () => {
|
||||
assert.match(src, /mail\/inbox\?status=\$\{status\}&limit=\$\{args\.limit \|\| DEFAULT_INBOX_LIMIT\}\$\{scope\}/);
|
||||
assert.match(src, /session_id=\$\{encodeURIComponent\(mailSessionID\)\}/);
|
||||
});
|
||||
|
||||
test('★ 会话来源是平台上下文(并发安全),不是模块级变量', () => {
|
||||
// 工具签名必须接住第二个参数
|
||||
assert.match(src, /async execute\(args: any, exec\?: any\): Promise<string>/);
|
||||
assert.match(src, /exec\?\.agent\?\.id/, '要从 exec.agent.id 取 DSH 会话');
|
||||
assert.match(src, /reverseMap\.get\(dshSessionId\)/, '再经 reverseMap 换成邮件会话');
|
||||
// 反向对照:不能引入"当前会话"这种模块级可变状态来做这件事
|
||||
const bad = /let\s+currentMailSessionID|let\s+activeMailSession/;
|
||||
assert.ok(!bad.test(src), '不得用模块级"当前会话"变量(同进程多会话会互相覆盖)');
|
||||
});
|
||||
|
||||
test('取不到会话时退回整体收件箱(不猜)', () => {
|
||||
assert.match(src, /if \(!dshSessionId\) return ''/);
|
||||
assert.match(src, /const scope = mailSessionID \? `&session_id=[^`]*` : ''/);
|
||||
});
|
||||
|
||||
test('★ 判据自检:不带收窄的旧写法必须判红', () => {
|
||||
const old = '`/mail/inbox?status=${status}&limit=${args.limit || DEFAULT_INBOX_LIMIT}`';
|
||||
assert.ok(!/\$\{scope\}/.test(old), '旧写法没有 scope ⇒ 会判红');
|
||||
assert.ok(!/async execute\(args: any, exec\?: any\)/.test('async execute(args: any): Promise<string> {'));
|
||||
});
|
||||
Reference in New Issue
Block a user