From 8653dccb0fde4c4e87f113637a6066238c869075 Mon Sep 17 00:00:00 2001 From: antianqi <75944423+antianqi@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:02:15 +0800 Subject: [PATCH 1/4] fix(mmx-octopus): cross-session awareness is off by default and really stops collecting (v1.10.0) The marketplace review (issue #136) found two real problems, and both were ours: - petAware defaulted to true. A privacy-sensitive cross-session feature has to be off by default, so a fresh install now starts with it off and the user turns it on themselves. - Turning it off only stopped the summary from reaching the prompt; the collection kept going. observe({all:true}) and sessions() were still called. The switch now reaches the collection layer: while it is off the package does not subscribe to session events, does not list sessions, and does not put anything about other sessions into her prompt. Turning it off at runtime aborts the subscription, stops the board timer and clears what the board had already shown. A restart keeps it off. Also: the README said the package needs MiniMax Code 3.1.5; it needs 3.1.6. Version 1.9.0 -> 1.10.0. --- .../mmx-octopus/.minimax-plugin/plugin.json | 4 +- plugins/antianqi/mmx-octopus/README.md | 25 ++++++---- plugins/antianqi/mmx-octopus/README.zh-CN.md | 17 ++++--- .../mmx-octopus/miniapp/client/index.html | 15 +++--- .../mmx-octopus/miniapp/node/observe.mjs | 48 ++++++++++++++++++- .../mmx-octopus/miniapp/node/server.mjs | 7 ++- .../mmx-octopus/miniapp/node/state.mjs | 17 +++++-- plugins/antianqi/mmx-octopus/package.json | 2 +- 8 files changed, 103 insertions(+), 32 deletions(-) diff --git a/plugins/antianqi/mmx-octopus/.minimax-plugin/plugin.json b/plugins/antianqi/mmx-octopus/.minimax-plugin/plugin.json index 0f8bd4f7..4f252be6 100644 --- a/plugins/antianqi/mmx-octopus/.minimax-plugin/plugin.json +++ b/plugins/antianqi/mmx-octopus/.minimax-plugin/plugin.json @@ -2,8 +2,8 @@ "schemaVersion": 1, "name": "mmx-octopus", "displayName": "MMX 章鱼娘", - "version": "1.9.0", - "description": "桌面上的 MMX 章鱼娘 —— MMX Agent 的可视化身体,也是你自己的专属聊天对象。她会反映 Agent 正在思考还是正在干活,还能被 Agent 和技能通过托管 MCP 直接指挥说话、换表情、挪位置。权限说明:本插件会读取你每个对话每轮的最终回复、标题与运行状态,用来判断 Agent 正在思考还是已经完成,并在你主动问她时用她自己的固定会话跑一轮 Agent,把回答直接说出来;她的对话历史只存在本机。", + "version": "1.10.0", + "description": "桌面上的 MMX 章鱼娘 —— MMX Agent 的可视化身体,也是你自己的专属聊天对象。打开面板里的「她知道我在忙什么」之后,她会反映 Agent 正在思考还是正在干活;她还能被 Agent 和技能通过托管 MCP 直接指挥说话、换表情、挪位置。权限说明:本插件声明了 session.read,会读你每个对话每轮的最终回复、标题与运行状态,用来判断 Agent 正在思考还是已经完成 —— **这项跨会话感知默认关闭**,需要你在面板里手动打开;打开后它才订阅你的会话事件、把你其它会话的标题与运行状态送进她的提示词,关着的时候一概不采集。另外它会在你主动问她时用她自己的固定会话跑一轮 Agent,把回答直接说出来;她的对话历史只存在本机。", "author": "antianqi", "icon": "icon.png", "category": "Productivity", diff --git a/plugins/antianqi/mmx-octopus/README.md b/plugins/antianqi/mmx-octopus/README.md index 05bc5f2c..422d98c1 100644 --- a/plugins/antianqi/mmx-octopus/README.md +++ b/plugins/antianqi/mmx-octopus/README.md @@ -108,9 +108,9 @@ it like the rest of the port. ## Tested environment - MiniMax Code 3.1.7 on Windows 11 (x64). -- The package needs **MiniMax Code 3.1.5 or later**: it declares +- The package needs **MiniMax Code 3.1.6 or later**: it declares `agentAccess: ["session.read", "session.run"]`, and a client older than - 3.1.5 rejects the whole package rather than installing half of it. + 3.1.6 rejects the whole package rather than installing half of it. - A desktop client that supports MiniApps and Agent turns. - The bundled self-check script needs Node 22 or later. @@ -167,10 +167,15 @@ it like the rest of the port. sent again every turn. Since 1.9.0 this is the only channel: the sheet is no longer written into any agent file, so the per-turn copy is the one and only copy. -- **Sessions read** — the package declares `session.read`. It reads the title, - the running state and the final reply of a turn, to tell whether the Agent is - thinking or has finished, and to summarise the turn. The board in the panel - lists the running sessions and refreshes every 30 seconds. +- **Sessions read** — the package declares `session.read`. **Since 1.10.0 this is + off by default**: on a fresh install the package does **not** subscribe to your + session events, does **not** list your sessions, and does **not** put anything + about them into her prompt. Turn it on in the panel (Settings → + 「她知道我在忙什么」) and it reads the title, the running state and the final + reply of a turn, to tell whether the Agent is thinking or has finished, and to + summarise the turn; the board in the panel then lists the running sessions and + refreshes every 30 seconds. Turning it off again stops the subscription and + the reads — nothing about your other sessions is collected while it is off. - **Agent turns it runs** — it declares `session.run`, and it runs **one Agent turn per message you send her in the chat area**, in her own session. The turn runs through the Host Agent, so it carries the capabilities the Host @@ -194,9 +199,11 @@ it like the rest of the port. Her static lines (a greeting, "you have been quiet", "you ignored me") are **not** model turns and cost nothing — turning idle talk off only stops the model ones. - **Other sessions' titles** — with 「她知道我在忙什么」 ("She knows what I am - busy with") **on (the default)**, the titles and running state of your **other** - sessions are put into her prompt, so she can tell what you are busy with. **Turn - it off in the panel**: Settings → 「她知道我在忙什么」. + busy with") **on**, the titles and running state of your **other** sessions are + put into her prompt, so she can tell what you are busy with. It is **off by + default** (since 1.10.0): a fresh install neither subscribes to your session + events nor lists your sessions, and nothing about them reaches her prompt. + **Turn it on in the panel**: Settings → 「她知道我在忙什么」. - **Tools it calls** — only its own four MCP tools, on its own port. It calls no connector, no provider and no third-party service. - **Processes spawned** — none. This package starts no child process and runs no diff --git a/plugins/antianqi/mmx-octopus/README.zh-CN.md b/plugins/antianqi/mmx-octopus/README.zh-CN.md index d02179ee..25faed31 100644 --- a/plugins/antianqi/mmx-octopus/README.zh-CN.md +++ b/plugins/antianqi/mmx-octopus/README.zh-CN.md @@ -82,8 +82,8 @@ English | [简体中文](README.zh-CN.md) ## 已验证环境 - MiniMax Code 3.1.7 on Windows 11 (x64)。 -- 这个包**要求 MiniMax Code 3.1.5 或更高**:它声明了 - `agentAccess: ["session.read", "session.run"]`,比 3.1.5 老的客户端会直接拒装整个包, +- 这个包**要求 MiniMax Code 3.1.6 或更高**:它声明了 + `agentAccess: ["session.read", "session.run"]`,比 3.1.6 老的客户端会直接拒装整个包, 而不是只装一半。 - 需要支持 MiniApp 与 Agent 轮次的桌面客户端。 - 附带的自检脚本需要 Node 22 或更高。 @@ -124,9 +124,11 @@ English | [简体中文](README.zh-CN.md) ⚠️ 代价:这一份走的是会话历史 ⇒ 宿主做上下文压缩时可能把它压掉(所以每轮都重发一份)。 1.9.0 起这就是**唯一**一条路:不再往任何 agent 文件里写她的底稿了,每轮随消息带的 那份就是唯一的一份。 -- **读哪些会话** —— 这个包声明了 `session.read`。它读一轮对话的标题、运行状态和最终回复, - 用来分辨 Agent 是在想还是做完了,也用来给这一轮做摘要。面板里的看板列出正在跑的会话, - 每 30 秒刷新一次。 +- **读哪些会话** —— 这个包声明了 `session.read`。**1.10.0 起这一项默认关闭**: + 新装时它**不订阅**你的会话事件、**不列**你的会话,提示词里也**不会出现**任何你别的会话的 + 信息。在面板里打开(设置 → 「她知道我在忙什么」)之后,它才会读一轮对话的标题、运行状态和 + 最终回复,用来分辨 Agent 是在想还是做完了,也用来给这一轮做摘要;面板里的看板这时才列出 + 正在跑的会话,每 30 秒刷新一次。关掉之后订阅与读取**一起停** —— 关着的时候她一概不采集。 - **会跑哪些 Agent 轮次** —— 它声明了 `session.run`,并且**你在聊天区给她发的每一句话, 都在她自己的会话里跑一轮 Agent**。这一轮走宿主的 Agent,因此带着宿主给那个 agent 的权限; 它会读宿主的 agent 列表;如果你自己有一个叫 `mmx` 的 agent,就用它来回答 @@ -142,8 +144,9 @@ English | [简体中文](README.zh-CN.md) 她的静态台词(打招呼、「好久没人理我了」、「你刚才不理我」)**不走模型、不花额度** —— 关掉闲话只停模型那几轮。 -- **别的会话标题** —— 「她知道我在忙什么」**默认开着**,这时你**其它会话**的标题与运行状态 - 会进她的提示词,她才知道你在忙什么。**面板里能关**:设置 → 「她知道我在忙什么」。 +- **别的会话标题** —— 「她知道我在忙什么」**开着**的时候,你**其它会话**的标题与运行状态 + 会进她的提示词,她才知道你在忙什么。**1.10.0 起默认关闭**:新装时她既不订阅你的会话事件、 + 也不列你的会话,你的其它会话信息不会进她的提示词。**面板里能开**:设置 → 「她知道我在忙什么」。 - **会调用哪些工具** —— 只有它自己那四个 MCP 工具,走它自己的端口。 不调用 connector、不调用任何厂商服务、不调用任何第三方服务。 - **进程** —— 不起。这个包不 spawn 任何子进程,也不跑任何外部二进制。 diff --git a/plugins/antianqi/mmx-octopus/miniapp/client/index.html b/plugins/antianqi/mmx-octopus/miniapp/client/index.html index 6d61cf4a..caf0cf0c 100644 --- a/plugins/antianqi/mmx-octopus/miniapp/client/index.html +++ b/plugins/antianqi/mmx-octopus/miniapp/client/index.html @@ -1956,11 +1956,13 @@
她知道我在忙什么
- - + +

- 开着的话,你其它会话的标题和运行状态会进她的提示词;关掉她就不看这些了。 + 默认关。开着的话,她会订阅你的会话事件、列出你的会话, + 并把其它会话的标题、运行状态和最终回复写进她的提示词; + 关掉这些一概不采、也不进提示词。

@@ -3434,8 +3436,9 @@

先认识一下

scopeButtons[j].getAttribute('data-scope') === state.scope ? 'true' : 'false' ); } - // ⚠️ **undefined 算「开」** —— 老运行时的快照没有这两个字段, - // 按「默认开」显示,别让面板显示成「关」。 + // ⚠️ **undefined 按各自的默认值走** —— 老运行时的快照可能没有这两个字段: + // 闲话按「开」显示(v1.7.0 之前的默认);状态感知按「关」显示 + // (2026-11-16 起默认关,取更安全的那一边)。 var idleButtons = document.querySelectorAll('#idletalk-group button'); for (var k = 0; k < idleButtons.length; k++) { idleButtons[k].setAttribute( @@ -3449,7 +3452,7 @@

先认识一下

for (var m = 0; m < awareButtons.length; m++) { awareButtons[m].setAttribute( 'aria-pressed', - awareButtons[m].getAttribute('data-petaware') === (snapshot.petAware === false ? 'off' : 'on') + awareButtons[m].getAttribute('data-petaware') === (snapshot.petAware === true ? 'on' : 'off') ? 'true' : 'false' ); diff --git a/plugins/antianqi/mmx-octopus/miniapp/node/observe.mjs b/plugins/antianqi/mmx-octopus/miniapp/node/observe.mjs index 99ab0d59..cb73a973 100644 --- a/plugins/antianqi/mmx-octopus/miniapp/node/observe.mjs +++ b/plugins/antianqi/mmx-octopus/miniapp/node/observe.mjs @@ -53,7 +53,7 @@ const TURN_TEXT_SESSIONS = 50; * dispose: () => void, * }} */ -export function createObserve({ context, state, onTaskEvent = null }) { +export function createObserve({ context, state, onTaskEvent = null, boardIntervalMs = BOARD_INTERVAL }) { // 这几个在 createServer 之前算好:请求处理器会读它们,不能等到后面才初始化。 const agentAvailable = !!context.agent; const capabilitiesKnown = !!context.capabilities; @@ -481,6 +481,10 @@ export function createObserve({ context, state, onTaskEvent = null }) { */ function scheduleObserve() { if (observeStarted || !agentAvailable || context.signal.aborted) return; + // ⚠️ **她知道 MCode 在干什么**这个开关关着 ⇒ **一个跨会话事件都不订阅**。 + // (官方插件市场 2026-11-16 评审:关掉就必须真的不采集, + // 不能只是「收了不往提示词里写」。) + if (!state.petAware) return; observeStarted = true; // G2:宿主明确不支持 observe —— 直说,不做无意义的退避重试。 if (!observeSupported) { @@ -498,6 +502,8 @@ export function createObserve({ context, state, onTaskEvent = null }) { */ function scheduleBoard() { if (boardStarted || !agentAvailable || context.signal.aborted) return; + // ⚠️ **同一个开关**:关着就不列会话 —— 看板本身也是跨会话信息。 + if (!state.petAware) return; boardStarted = true; // 激活延迟是毫秒级的,1 秒足够越过;太早调用只会拿到一次假的失败。 boardTimer = setTimeout(() => { @@ -505,7 +511,7 @@ export function createObserve({ context, state, onTaskEvent = null }) { void refreshBoard(); boardTimer = setInterval(() => { void refreshBoard(); - }, BOARD_INTERVAL); + }, boardIntervalMs); }, 1000); } @@ -516,11 +522,48 @@ export function createObserve({ context, state, onTaskEvent = null }) { function restartObserve() { if (!agentAvailable || context.signal.aborted) return; stopObserve(); + // ⚠️ **关着就不订阅**(与 scheduleObserve 同一个判断在同一个地方)。 + if (!state.petAware) { + observeStarted = false; + return; + } observeAttempts = 0; observeStarted = true; observeTimer = setTimeout(attemptObserve, 900); } + /** + * 「她知道 MCode 在干什么」开关落地:**关 ⇒ 真的不采集**。 + * + * ⚠️ **关掉时要做三件事**(官方插件市场 2026-11-16 评审): + * ① 掐断订阅(`observe`)—— 不再收任何跨会话事件; + * ② 停掉看板计时器 —— 不再调 `sessions()` 列会话; + * ③ 清掉已经显示出来的跨会话信息(看板、订阅状态、会话计数)。 + * ⚠️ 只在提示词里不拼摘要**不算数** —— 那是「收了不用」,不是「没收」。 + * + * @param {boolean} enabled + */ + function setAware(enabled) { + if (enabled) { + // 重新走一遍「排订阅 / 排看板」—— 关过一次之后这两步被跳过过了。 + observeStarted = false; + boardStarted = false; + scheduleObserve(); + scheduleBoard(); + return; + } + stopObserve(); + observeStarted = false; + if (boardTimer) { + clearTimeout(boardTimer); + clearInterval(boardTimer); + boardTimer = null; + } + boardStarted = false; + state.setAgentTracking({ subscribed: false, sessions: 0, sessionTitle: null, error: null }); + void state.setBoard({ running: [], recent: [] }).catch(() => undefined); + } + /** * 关停:掐断订阅 + 停掉看板计时器。 * @@ -544,6 +587,7 @@ export function createObserve({ context, state, onTaskEvent = null }) { scheduleBoard, restartObserve, stopObserve, + setAware, listSessions, runningSince, dispose, diff --git a/plugins/antianqi/mmx-octopus/miniapp/node/server.mjs b/plugins/antianqi/mmx-octopus/miniapp/node/server.mjs index 610d4ff4..12aa1685 100644 --- a/plugins/antianqi/mmx-octopus/miniapp/node/server.mjs +++ b/plugins/antianqi/mmx-octopus/miniapp/node/server.mjs @@ -384,7 +384,7 @@ function formatDuration(ms) { /** * @param {any} context - * @param {{bubbleMs?: number}} [options] 测试用的时长覆盖(生产不传) + * @param {{bubbleMs?: number, boardIntervalMs?: number}} [options] 测试用的时长覆盖(生产不传) */ export async function start(context, options = {}) { // 聊天历史独立于 state.json:chat.json。 @@ -502,7 +502,7 @@ export async function start(context, options = {}) { // 从本文件抽成独立模块 `observe.mjs`: // 它只认注入进来的 `context` 和 `state`,自己的状态(订阅取消器 / 退避计时器 / // 看板计时器 / 轮次正文缓存)全在自己的闭包里,不再挤在这个大闭包的外层。 - const observe = createObserve({ context, state, onTaskEvent }); + const observe = createObserve({ context, state, onTaskEvent, boardIntervalMs: options.boardIntervalMs }); state.setAgentTracking({ available: observe.agentAvailable, capability: observe.observeCapability }); // ============ 闲话生产者(idle):她不说话的时候也在活着 ============ @@ -1155,6 +1155,9 @@ export async function start(context, options = {}) { if (path === '/api/petaware') { const enabled = body.enabled === true || body.enabled === 'true'; const petAware = await state.setPetAware(enabled); + // ⚠️ **开关要真的落到采集层**(官方插件市场 2026-11-16 评审): + // 关 ⇒ 掐断订阅 + 停掉 sessions() 轮询;开 ⇒ 恢复订阅与看板。 + observe.setAware(petAware); sendJson(response, 200, { ok: true, petAware, snapshot: state.snapshot() }); return; } diff --git a/plugins/antianqi/mmx-octopus/miniapp/node/state.mjs b/plugins/antianqi/mmx-octopus/miniapp/node/state.mjs index 3e4bb93f..0b811f5e 100644 --- a/plugins/antianqi/mmx-octopus/miniapp/node/state.mjs +++ b/plugins/antianqi/mmx-octopus/miniapp/node/state.mjs @@ -353,8 +353,16 @@ export class PetState { this.asking = null; /** @type {boolean} 她正在回话(运行时态,不持久化) */ this.chatBusy = false; - /** @type {boolean} 是否把 MCode 状态摘要拼进她的输入(持久化,默认开) */ - this.petAware = true; + /** + * 是否读取 MCode 的跨会话信息(持久化,**默认关**)。 + * + * ⚠️ **默认关**(官方插件市场 2026-11-16 评审:隐私敏感的跨会话能力必须默认关闭, + * 由用户自己打开):关着的时候她**不订阅**宿主的事件、**不列**会话、 + * 提示词里也**不拼**状态摘要 ⇒ 别的会话的标题、运行状态、最终回复她一概看不到。 + * 已经装过的用户保留他自己上次的开关值(`load()` 里读回来)。 + * @type {boolean} + */ + this.petAware = false; // ★ **闲话开关**:她没人的时候要不要自己开口(模型闲话)—— **默认关闭**。 // ⚠️ **默认关**(Marketplace 评审「强烈建议」):自动对话会消耗用户自己的额度, // 所以新装的用户一律**由用户主动打开**;已经装过的用户保留他自己上次的 @@ -1164,7 +1172,10 @@ export class PetState { * @param {boolean} aware */ async setPetAware(aware) { - this.petAware = aware !== false; + // ⚠️ **只有明确传 true 才开** —— 默认是关的(官方插件市场 2026-11-16 评审: + // 隐私敏感的跨会话能力默认关闭、由用户主动打开)。 + // 传 undefined / 其它值一律按「关」处理(与 setIdleTalk 同一口径)。 + this.petAware = aware === true; await this.commit(); return this.petAware; } diff --git a/plugins/antianqi/mmx-octopus/package.json b/plugins/antianqi/mmx-octopus/package.json index 8085abda..a48a0b8d 100644 --- a/plugins/antianqi/mmx-octopus/package.json +++ b/plugins/antianqi/mmx-octopus/package.json @@ -1,6 +1,6 @@ { "name": "mmx-octopus", - "version": "1.9.0", + "version": "1.10.0", "private": true, "description": "MMX Octopus: a desktop companion for MiniMax Code that mirrors what the Agent is doing and can be driven by it through a hosted MCP server.", "mcode": { From 32957a8dad22b0be14b3e36c69214d764986bcbe Mon Sep 17 00:00:00 2001 From: antianqi Date: Mon, 16 Nov 2026 18:56:24 +0800 Subject: [PATCH 2/4] test(mmx-octopus): ship the petAware scenarios so the default-off behaviour is verifiable --- .../tests/petaware-default-off.test.mjs | 74 ++++ .../mmx-octopus/tests/petaware-runner.mjs | 19 + .../mmx-octopus/tests/petaware-scenarios.mjs | 336 ++++++++++++++++++ 3 files changed, 429 insertions(+) create mode 100644 plugins/antianqi/mmx-octopus/tests/petaware-default-off.test.mjs create mode 100644 plugins/antianqi/mmx-octopus/tests/petaware-runner.mjs create mode 100644 plugins/antianqi/mmx-octopus/tests/petaware-scenarios.mjs diff --git a/plugins/antianqi/mmx-octopus/tests/petaware-default-off.test.mjs b/plugins/antianqi/mmx-octopus/tests/petaware-default-off.test.mjs new file mode 100644 index 00000000..d798651b --- /dev/null +++ b/plugins/antianqi/mmx-octopus/tests/petaware-default-off.test.mjs @@ -0,0 +1,74 @@ +// @ts-check +/** + * 随包发布的测试:跨会话感知默认关,且关的时候真的不采集。 + * + * 跑法(包根目录下): + * node --test tests/petaware-default-off.test.mjs + * + * 覆盖上游 issue #136 点名的三组:**首次启动**、**切换开关**、**重启**。 + * 判断的实现见同目录 `petaware-scenarios.mjs` —— 与包内自检共用同一份, + * 所以「同一个判断不许写在两个地方」这条规矩守得住。 + * + * ⚠️ **为什么用子进程跑**:这些场景会**真起一套 server**。如果生产代码被改坏 + * (比如把开关守卫删了),它自己起的定时器就没人收了 —— 同一个进程里 + * `node --test` 会一直等进程退出,**挂死而不是失败**。 + * 放子进程里跑并设超时 ⇒ 真代码坏了的时候看到的是一条**干净的失败**, + * 而不是 CI 卡住。判据要能被抓:挂死也算被抓,但没法告诉人哪儿坏了。 + */ + +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { spawn } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import { dirname, join } from 'node:path'; + +const HERE = dirname(fileURLToPath(import.meta.url)); +const RUNNER = join(HERE, 'petaware-runner.mjs'); +/** 场景本身要起 4 套 server,正常 7 秒左右;给 3 分钟余量。 */ +const TIMEOUT_MS = 180000; + +/** + * 跑一遍场景(子进程),把结果 JSON 取回来。 + * + * @returns {Promise<{code: number|null, stdout: string, stderr: string, timedOut: boolean}>} + */ +function runScenarios() { + return new Promise((resolve) => { + const child = spawn(process.execPath, [RUNNER], { stdio: ['ignore', 'pipe', 'pipe'] }); + let stdout = ''; + let stderr = ''; + let timedOut = false; + const timer = setTimeout(() => { + timedOut = true; + child.kill('SIGKILL'); + }, TIMEOUT_MS); + child.stdout.on('data', (d) => { + stdout += d.toString(); + }); + child.stderr.on('data', (d) => { + stderr += d.toString(); + }); + child.on('close', (code) => { + clearTimeout(timer); + resolve({ code, stdout, stderr, timedOut }); + }); + }); +} + +test('petAware:默认关,关的时候真的不采集(首启 / 切换 / 重启)', async () => { + const run = await runScenarios(); + assert.equal( + run.timedOut, + false, + '跑场景超时(' + TIMEOUT_MS + 'ms)⇒ 生产代码可能改坏了,进程退不出去' + ); + assert.equal(run.code, 0, '跑场景的子进程退出了 ' + run.code + ':\n' + run.stderr.slice(-2000)); + const results = JSON.parse(run.stdout); + const failed = results.filter((r) => !r.ok); + assert.deepEqual( + failed, + [], + failed.map((f) => f.name + ' -> ' + f.detail).join('; ') + ); + assert.ok(results.length >= 12, '场景数不对:' + results.length); +}); diff --git a/plugins/antianqi/mmx-octopus/tests/petaware-runner.mjs b/plugins/antianqi/mmx-octopus/tests/petaware-runner.mjs new file mode 100644 index 00000000..53e2d002 --- /dev/null +++ b/plugins/antianqi/mmx-octopus/tests/petaware-runner.mjs @@ -0,0 +1,19 @@ +// @ts-check +/** + * 场景跑批的**子进程入口** —— 判断的实现见 `petaware-scenarios.mjs`。 + * + * 把结果以 JSON 打到 stdout(**同步写**),父进程好解析。 + * + * ⚠️ **写完就主动退出**(`writeSync` + `process.exit`): + * 生产代码一旦被改坏,它自己起的定时器就没人收了 —— 这个子进程会永远 + * 退不出去,父进程只能等到超时才判失败(慢,而且看不出哪儿坏了)。 + * 同步写完直接退出 ⇒ 父进程**立刻**拿到结果,红在哪一条一目了然。 + * (`writeSync` 是同步的,退出不会把输出截断。) + */ + +import { writeSync } from 'node:fs'; +import { runPetAwareScenarios } from './petaware-scenarios.mjs'; + +const results = await runPetAwareScenarios(); +writeSync(1, JSON.stringify(results) + '\n'); +process.exit(0); diff --git a/plugins/antianqi/mmx-octopus/tests/petaware-scenarios.mjs b/plugins/antianqi/mmx-octopus/tests/petaware-scenarios.mjs new file mode 100644 index 00000000..2d42062a --- /dev/null +++ b/plugins/antianqi/mmx-octopus/tests/petaware-scenarios.mjs @@ -0,0 +1,336 @@ +// @ts-check +/** + * 「她知道 MCode 在干什么」(`petAware`)的行为场景 —— **判断的唯一实现**。 + * + * ⚠️ **为什么单独放一个模块**:同一个判断不许写在两个地方(改了这边忘了那边, + * 两边的结论就会打架)。于是: + * - 随包发布的 `tests/petaware-default-off.test.mjs` 用它跑 `node --test`; + * - 我们自己的自检(`miniapp/node/selftest.mjs` 第五十一轮)也用它。 + * 两边跑的是**同一份实现**,不会各说各话。 + * + * 判的是**装配后的行为**:真起一套 server + 模拟宿主,数 API 的**调用次数** —— + * 不是读源码字面量。 + * + * 覆盖评审(上游 issue #136)点名的三组:**首次启动**、**切换开关**、**重启**。 + */ + +import { mkdtemp, readFile, writeFile, rm } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { dirname, join } from 'node:path'; +import { fileURLToPath, pathToFileURL } from 'node:url'; + +const HERE = dirname(fileURLToPath(import.meta.url)); +/** 包根目录(本文件在 `<包根>/tests/` 下)*/ +export const PACKAGE_ROOT = join(HERE, '..'); +const NODE_DIR = join(PACKAGE_ROOT, 'miniapp', 'node'); + +const { start } = await import(pathToFileURL(join(NODE_DIR, 'server.mjs')).href); +const { PetState } = await import(pathToFileURL(join(NODE_DIR, 'state.mjs')).href); + +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); + +/** 本次跑出来的临时目录与 server —— 跑完必须清掉,不在 %TEMP% 留垃圾。 */ +const tempDirs = []; +const liveServers = []; + +async function makeTempDir(prefix) { + const dir = await mkdtemp(join(tmpdir(), prefix)); + tempDirs.push(dir); + return dir; +} + +/** 释放 server + 清临时目录(失败也不该拦住结论)。 */ +async function cleanup() { + while (liveServers.length) { + const life = liveServers.pop(); + try { + await life.dispose(); + } catch { + // 释放失败不影响结论。 + } + } + while (tempDirs.length) { + const dir = tempDirs.pop(); + try { + await rm(dir, { recursive: true, force: true }); + } catch { + // 清理失败不影响结论。 + } + } +} + +/** + * 假的 Agent:只记调用次数与订阅的 signal。 + * + * ⚠️ `observe` 返回的 promise 永不 resolve —— 订阅本来就是长驻的。 + */ +function makeMockAgent({ observeImpl, sessionList = [] } = {}) { + const calls = { observe: 0, sessions: 0, create: 0, run: 0 }; + const signals = []; + let emit = null; + return { + calls, + signals, + emit(event) { + if (emit) emit(event); + }, + observe(target, opts) { + calls.observe += 1; + emit = opts.onEvent; + if (opts && opts.signal) signals.push(opts.signal); + if (observeImpl) return observeImpl(target, opts); + return new Promise(() => {}); + }, + async sessions() { + calls.sessions += 1; + return { sessions: sessionList }; + }, + async create() { + calls.create += 1; + return { sessionId: 'sess-mock-1' }; + }, + async run() { + calls.run += 1; + return { status: 'finished', text: '好' }; + }, + async reply() { + return {}; + }, + async answer() { + return {}; + }, + }; +} + +/** 起一套独立的 server(独立端口 + 独立 dataDir)。 */ +async function startHarness({ agent, dataDir, boardIntervalMs = 200 }) { + const dir = dataDir || (await makeTempDir('mmx-petaware-')); + const ctrl = new AbortController(); + const ctx = { + pluginId: 'mmx-octopus', + pluginRoot: PACKAGE_ROOT, + dataDir: dir, + listen: { host: '127.0.0.1', port: 0 }, + signal: ctrl.signal, + logger: { info() {}, warn() {}, error() {} }, + capabilities: { has: () => true }, + agent, + }; + const life = await start(ctx, { boardIntervalMs }); + liveServers.push(life); + const base = `http://127.0.0.1:${life.port}`; + return { + life, + base, + dir, + async state() { + const r = await fetch(`${base}/api/state`); + return r.json(); + }, + }; +} + +/** + * 把某个生产模块改坏后**独立导入**一份(真源一个字节不动)。 + * ⚠️ **依赖清单自动补齐**:把被测模块 import 的同目录模块都复制过去 —— + * 手写清单会漏(加一个 import 就漏一个 ⇒ 直接崩,不是判据红)。 + */ +async function importBadNode(fileName, mutate) { + const dir = await makeTempDir('mmx-petaware-bad-'); + const src = await readFile(join(NODE_DIR, fileName), 'utf8'); + const bad = mutate(src); + if (bad === src) return null; + const deps = new Set(); + for (const m of src.matchAll(/from\s+'\.\/([\w.-]+\.mjs)'/g)) deps.add(m[1]); + for (const dep of deps) { + await writeFile(join(dir, dep), await readFile(join(NODE_DIR, dep), 'utf8')); + } + await writeFile(join(dir, fileName), bad); + return import(pathToFileURL(join(dir, fileName)).href); +} + +const SESSIONS = [{ sessionId: 's-other', title: '别人的活', status: 'running' }]; + +/** + * 跑一遍全部场景,返回逐条结果。 + * + * @returns {Promise>} + */ +async function runScenariosInner() { + const out = []; + const push = (name, ok, detail) => out.push({ name, ok: !!ok, detail: detail || '' }); + + // ---------- ① 首启:开关是关的 ⇒ 一个 observe / sessions 都不该发生 ---------- + // + // ⚠️ 每个 harness 的 server 都会在收尾时 dispose(见 cleanup)—— + // server 不关,端口和计时器就一直占着,进程退不出去。 + const agentOff = makeMockAgent({ sessionList: SESSIONS }); + const hOff = await startHarness({ agent: agentOff }); + await fetch(`${hOff.base}/api/state`); + await sleep(1200); + push( + '关着时一次 observe 都不订阅(不是「收了不用」)', + agentOff.calls.observe === 0, + `observe 被调 ${agentOff.calls.observe} 次` + ); + push( + '关着时一次 sessions() 都不调(看板不采集)', + agentOff.calls.sessions === 0, + `sessions 被调 ${agentOff.calls.sessions} 次` + ); + await hOff.life.dispose(); + + // ---------- ② 打开之后才采 ---------- + const agentOn = makeMockAgent({ sessionList: SESSIONS }); + const hOn = await startHarness({ agent: agentOn }); + await fetch(`${hOn.base}/api/state`); + await sleep(300); + const onRes = await fetch(`${hOn.base}/api/petaware`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ enabled: true }), + }); + const onJson = await onRes.json(); + push( + '打开后接口回 petAware=true', + onJson.petAware === true, + `返回=${JSON.stringify(onJson.petAware)}` + ); + await sleep(1200); + push( + '打开之后才订阅 observe', + agentOn.calls.observe >= 1, + `observe 被调 ${agentOn.calls.observe} 次` + ); + push( + '打开之后才列会话 sessions()', + agentOn.calls.sessions >= 1, + `sessions 被调 ${agentOn.calls.sessions} 次` + ); + + // ---------- ③ 关掉之后立刻停 ---------- + const offRes = await fetch(`${hOn.base}/api/petaware`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ enabled: false }), + }); + const offJson = await offRes.json(); + push( + '关掉后接口回 petAware=false', + offJson.petAware === false, + `返回=${JSON.stringify(offJson.petAware)}` + ); + const lastSignal = agentOn.signals[agentOn.signals.length - 1]; + push( + '关掉之后订阅被掐断(observe 的 signal aborted)', + !!lastSignal && lastSignal.aborted === true, + `signal=${lastSignal ? String(lastSignal.aborted) : '没有订阅过'}` + ); + const sessionsAtOff = agentOn.calls.sessions; + await sleep(900); + push( + '关掉之后不再轮询 sessions()(看板计时器停了)', + agentOn.calls.sessions === sessionsAtOff, + `关掉后又调了 ${agentOn.calls.sessions - sessionsAtOff} 次` + ); + const snapOff = await hOn.state(); + push( + '关掉之后看板里的跨会话信息被清空', + Array.isArray(snapOff.board?.running) && snapOff.board.running.length === 0, + `board=${JSON.stringify(snapOff.board)}` + ); + await hOn.life.dispose(); + + // ---------- ④ 重启后仍然是关的(同一个 dataDir) ---------- + const dirRestart = await makeTempDir('mmx-petaware-restart-'); + const agentR1 = makeMockAgent({ sessionList: SESSIONS }); + const hR1 = await startHarness({ agent: agentR1, dataDir: dirRestart }); + await fetch(`${hR1.base}/api/state`); + await fetch(`${hR1.base}/api/petaware`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ enabled: false }), + }); + await hR1.life.dispose(); + const agentR2 = makeMockAgent({ sessionList: SESSIONS }); + const hR2 = await startHarness({ agent: agentR2, dataDir: dirRestart }); + await fetch(`${hR2.base}/api/state`); + await sleep(1200); + push( + '重启后仍然是关的(设置被记住,采集不再发生)', + agentR2.calls.observe === 0 && agentR2.calls.sessions === 0, + `observe=${agentR2.calls.observe} sessions=${agentR2.calls.sessions}` + ); + await hR2.life.dispose(); + + // ---------- ⑤ 负向注入①:拆掉 observe.mjs 的开关守卫 ⇒ 判据必须红 ---------- + { + const mod = await importBadNode('observe.mjs', (s) => + s.split(' if (!state.petAware) return;\n').join('') + ); + if (!mod) { + push('负向注入|把 observe.mjs 的开关守卫拆掉时判据必须红', false, '注入没生效'); + } else { + const badState = new PetState(await makeTempDir('mmx-petaware-badstate-')); + await badState.setPetAware(false); + const badAgent = makeMockAgent({ sessionList: SESSIONS }); + const badObserve = mod.createObserve({ + context: { + agent: badAgent, + signal: new AbortController().signal, + capabilities: { has: () => true }, + logger: { warn() {}, info() {}, error() {} }, + }, + state: badState, + boardIntervalMs: 200, + }); + badObserve.scheduleObserve(); + badObserve.scheduleBoard(); + await sleep(1200); + push( + '负向注入|把 observe.mjs 的开关守卫拆掉时判据必须红', + badAgent.calls.observe > 0 || badAgent.calls.sessions > 0, + `坏副本在关着的时候仍然采集了(observe=${badAgent.calls.observe}` + + ` sessions=${badAgent.calls.sessions})⇒ 真判据会红` + ); + // ⚠️ **坏副本也得收尾**:它的看板轮询是个 `setInterval` —— 不关掉的话 + // 进程退不出去(`node --test` 会一直挂着,内部自检末尾有 process.exit 才没暴露)。 + badObserve.dispose(); + badState.dispose(); + } + } + + // ---------- ⑥ 负向注入②:把默认关改回默认开 ⇒ 判据必须红 ---------- + { + const mod = await importBadNode('state.mjs', (s) => + s.replace('this.petAware = false;', 'this.petAware = true;') + ); + if (!mod) { + push('负向注入|把默认关改回默认开时判据必须红', false, '注入没生效'); + } else { + const badFresh = new mod.PetState(await makeTempDir('mmx-petaware-badstate2-')); + badFresh.dispose(); + push( + '负向注入|把默认关改回默认开时判据必须红', + badFresh.petAware === true, + `坏副本默认是开的(petAware=${String(badFresh.petAware)})⇒ 真判据会红` + ); + } + } + + return out; +} + +/** + * 跑一遍全部场景,返回逐条结果。 + * + * ⚠️ **收尾放在 finally**:中途抛异常也必须把 server 与临时目录收干净 —— + * 漏一个定时器,进程就退不出去(`node --test` 会一直挂着)。 + */ +export async function runPetAwareScenarios() { + try { + return await runScenariosInner(); + } finally { + await cleanup(); + } +} From f41a0342056a27141e099daea14677dd67ba8fb8 Mon Sep 17 00:00:00 2001 From: antianqi Date: Mon, 16 Nov 2026 18:57:53 +0800 Subject: [PATCH 3/4] docs(mmx-octopus): point the self-check note at the bundled test command --- plugins/antianqi/mmx-octopus/README.md | 6 +++++- plugins/antianqi/mmx-octopus/README.zh-CN.md | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/antianqi/mmx-octopus/README.md b/plugins/antianqi/mmx-octopus/README.md index 422d98c1..d365f638 100644 --- a/plugins/antianqi/mmx-octopus/README.md +++ b/plugins/antianqi/mmx-octopus/README.md @@ -112,7 +112,11 @@ it like the rest of the port. `agentAccess: ["session.read", "session.run"]`, and a client older than 3.1.6 rejects the whole package rather than installing half of it. - A desktop client that supports MiniApps and Agent turns. -- The bundled self-check script needs Node 22 or later. +- The bundled tests need Node 22 or later. Run them from the package directory: + `node --test tests/petaware-default-off.test.mjs` + It starts a real server against a mock host and asserts that cross-session + awareness is **off by default**, and that turning it off really stops the + collection (no `observe`, no `sessions()`). ## Data & access diff --git a/plugins/antianqi/mmx-octopus/README.zh-CN.md b/plugins/antianqi/mmx-octopus/README.zh-CN.md index 25faed31..e04db4c3 100644 --- a/plugins/antianqi/mmx-octopus/README.zh-CN.md +++ b/plugins/antianqi/mmx-octopus/README.zh-CN.md @@ -86,7 +86,10 @@ English | [简体中文](README.zh-CN.md) `agentAccess: ["session.read", "session.run"]`,比 3.1.6 老的客户端会直接拒装整个包, 而不是只装一半。 - 需要支持 MiniApp 与 Agent 轮次的桌面客户端。 -- 附带的自检脚本需要 Node 22 或更高。 +- 附带的测试需要 Node 22 或更高。在包目录里跑: + `node --test tests/petaware-default-off.test.mjs` + 它会真起一套 server 配一个假宿主,断言跨会话感知**默认是关的**,而且关掉之后 + 真的不再采集(不调 `observe`、不调 `sessions()`)。 ## 数据与访问 From 2cf48430011a5d5f3e19383ad1e03f7d35359214 Mon Sep 17 00:00:00 2001 From: antianqi <75944423+antianqi@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:10:09 +0800 Subject: [PATCH 4/4] fix(mmx-octopus): reset cross-session awareness on upgrade unless the user opted in The marketplace review (2026-11-17) pointed out that an existing state.json with petAware: true kept that value after upgrading, so cross-session collection continued without a new opt-in. 1.9.0 persisted true because that was its default, and a saved true does not show that the user ever enabled collection. - state.mjs: new pure function migratePetAware() is the single place holding the rule. Only a saved petAwareOptIn marker counts as an explicit opt-in; older states are reset to off and the panel keeps a "please turn it on again" note. setPetAware(true) is the only place that records the opt-in; setPetAware(false) withdraws it. The reset is written back to disk so the old true cannot be read back as a choice. - client: the panel shows the upgrade note while the switch is reset. - tests: the regression the review asked for, in the shared scenario module that the bundled test and the internal self-check both run: start from a legacy state.json with petAware: true, assert the switch is off and that no observe or sessions() call happens, then assert that turning it on starts collection and that the opt-in survives a restart. Plus a negative injection that puts the old read-back rule back and must turn the check red. - README (both languages): the upgrade section says the switch is reset to off. --- plugins/antianqi/mmx-octopus/README.md | 13 ++ plugins/antianqi/mmx-octopus/README.zh-CN.md | 9 + .../mmx-octopus/miniapp/client/index.html | 8 + .../mmx-octopus/miniapp/node/state.mjs | 83 ++++++++- .../tests/petaware-default-off.test.mjs | 10 +- .../mmx-octopus/tests/petaware-scenarios.mjs | 164 +++++++++++++++++- 6 files changed, 280 insertions(+), 7 deletions(-) diff --git a/plugins/antianqi/mmx-octopus/README.md b/plugins/antianqi/mmx-octopus/README.md index d365f638..7058a226 100644 --- a/plugins/antianqi/mmx-octopus/README.md +++ b/plugins/antianqi/mmx-octopus/README.md @@ -180,6 +180,9 @@ it like the rest of the port. summarise the turn; the board in the panel then lists the running sessions and refreshes every 30 seconds. Turning it off again stops the subscription and the reads — nothing about your other sessions is collected while it is off. + **Upgrading from 1.9.0**: the switch starts **off** — 1.9.0 wrote `true` into + the saved state because that was its default value, and a saved `true` does not + show that you ever turned it on. Turn it on again in the panel if you want it. - **Agent turns it runs** — it declares `session.run`, and it runs **one Agent turn per message you send her in the chat area**, in her own session. The turn runs through the Host Agent, so it carries the capabilities the Host @@ -221,6 +224,16 @@ it like the rest of the port. - **State location** — `context.dataDir`. Treat it as opaque: it may move between versions, so do not hard-code it. +### Upgrading from 1.9.0 + +1.10.0 changes one thing about your saved state: the cross-session awareness switch +(「她知道我在忙什么」 / 「She knows what I am busy with」) is **reset to off**. +1.9.0 wrote `true` into the state file because that was its default value, and a +saved `true` does not show that you ever turned it on — so the package now starts +with the switch off and stays off until you turn it on again in the panel +(Settings → 「她知道我在忙什么」). Until then nothing about your other sessions is +read or collected. + ### Upgrading from 1.8.0 1.9.0 changes what the plugin does with the Host's agents, not just the diff --git a/plugins/antianqi/mmx-octopus/README.zh-CN.md b/plugins/antianqi/mmx-octopus/README.zh-CN.md index e04db4c3..948bea39 100644 --- a/plugins/antianqi/mmx-octopus/README.zh-CN.md +++ b/plugins/antianqi/mmx-octopus/README.zh-CN.md @@ -132,6 +132,8 @@ English | [简体中文](README.zh-CN.md) 信息。在面板里打开(设置 → 「她知道我在忙什么」)之后,它才会读一轮对话的标题、运行状态和 最终回复,用来分辨 Agent 是在想还是做完了,也用来给这一轮做摘要;面板里的看板这时才列出 正在跑的会话,每 30 秒刷新一次。关掉之后订阅与读取**一起停** —— 关着的时候她一概不采集。 + **从 1.9.0 升级**:这个开关升级后是**关**的 —— 1.9.0 把 `true` 存了下来,可那只是它当时的默认值, + 存下来并不说明你开过 ⇒ 现在一律从关开始,你要的话在面板里再打开一次。 - **会跑哪些 Agent 轮次** —— 它声明了 `session.run`,并且**你在聊天区给她发的每一句话, 都在她自己的会话里跑一轮 Agent**。这一轮走宿主的 Agent,因此带着宿主给那个 agent 的权限; 它会读宿主的 agent 列表;如果你自己有一个叫 `mmx` 的 agent,就用它来回答 @@ -159,6 +161,13 @@ English | [简体中文](README.zh-CN.md) - **麦克风** —— 从不。这个包没有声明 `deviceAccess`,也不申请任何浏览器权限。 - **状态放在哪** —— `context.dataDir`。把它当成不透明路径:版本之间可能变,别写死。 +### 从 1.9.0 升级 + +1.10.0 对你的存档只改一件事:跨会话感知开关(「她知道我在忙什么」)**被重置为关**。 +1.9.0 把 `true` 存进存档,可那只是它当时的默认值,存下来的 `true` 说明不了你开过 ⇒ +现在一律从关开始,你要的话在面板里(设置 → 「她知道我在忙什么」)再打开一次。 +在那之前,她的其它会话一概不读、不采集。 + ### 从 1.8.0 升级 1.9.0 改的是插件和宿主 agent 的关系,不只是一个版本号:**它不再创建任何 agent 文件**, diff --git a/plugins/antianqi/mmx-octopus/miniapp/client/index.html b/plugins/antianqi/mmx-octopus/miniapp/client/index.html index caf0cf0c..7689f552 100644 --- a/plugins/antianqi/mmx-octopus/miniapp/client/index.html +++ b/plugins/antianqi/mmx-octopus/miniapp/client/index.html @@ -1963,6 +1963,8 @@ 默认关。开着的话,她会订阅你的会话事件、列出你的会话, 并把其它会话的标题、运行状态和最终回复写进她的提示词; 关掉这些一概不采、也不进提示词。 +

@@ -3457,6 +3459,12 @@

先认识一下

: 'false' ); } + // ⚠️ **升级提示**(官方评审 2026-11-17):旧存档里那个「开」是 1.9.0 的 + // 默认值、不是用户选的 ⇒ 升级后已重置为关,并在这里请用户重新打开。 + var petAwareResetNote = document.getElementById('petaware-reset-note'); + if (petAwareResetNote) { + petAwareResetNote.hidden = snapshot.petAwareReset !== true; + } // 诊断与看板 renderTracking(snapshot); renderBoard(state.board); diff --git a/plugins/antianqi/mmx-octopus/miniapp/node/state.mjs b/plugins/antianqi/mmx-octopus/miniapp/node/state.mjs index 0b811f5e..f69dbdec 100644 --- a/plugins/antianqi/mmx-octopus/miniapp/node/state.mjs +++ b/plugins/antianqi/mmx-octopus/miniapp/node/state.mjs @@ -234,6 +234,40 @@ function clock(ms) { return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`; } +/** + * 跨会话采集开关的**升级迁移**(**唯一实现** —— load() 与判据都走这里)。 + * + * ⚠️ **为什么必须有这一步**(官方插件市场评审 2026-11-17,PR #138): + * 1.9.0 的默认值是 true ⇒ 1.9.0 会把 true 落进 state.json。 + * 升级上来的用户**从没点过那个开关** —— 存下来的那个 true 只是旧默认值, + * **说明不了他同意过跨会话采集** ⇒ 拿它当「用户的选择」= 没经同意就采集。 + * + * 规则(就这一处,别处不再写一遍): + * - 带 petAwareOptIn 标记的存档 ⇒ 用户**明确 opt-in 过** ⇒ 按他存下来的开关值恢复; + * - 其余(旧存档,1.9.0 及更早,没有这个标记)⇒ **一律重置为关**, + * 并留一条 petAwareReset 提示,请用户需要时手动打开。 + * + * @param {any} saved 读回来的整个存档(没有存档时是 null) + * @returns {{petAware: boolean, petAwareOptIn: boolean, petAwareReset: boolean}} + */ +export function migratePetAware(saved) { + // 用户明确 opt-in 过 ⇒ 按他存下来的开关值恢复(之后他自己关过就是关)。 + if (saved && saved.petAwareOptIn === true) { + return { + petAware: saved.petAware === true, + petAwareOptIn: true, + petAwareReset: false, + }; + } + // 没有 opt-in 标记 ⇒ 旧值可能只是旧默认值 ⇒ 一律当作「关」。 + return { + petAware: false, + petAwareOptIn: false, + // 之前是开着的话 ⇒ 留一条「请重新打开」的提示(用户真打开了才消失)。 + petAwareReset: (saved && saved.petAware === true) || (saved && saved.petAwareReset === true), + }; +} + export class PetState { /** * @param {string} dataDir Host 注入的运行期数据目录 @@ -363,6 +397,23 @@ export class PetState { * @type {boolean} */ this.petAware = false; + /** + * 用户**是否明确 opt-in 过**跨会话采集(持久化)。 + * + * ⚠️ **这个字段是升级迁移的凭据**(官方评审 2026-11-17):1.9.0 的默认值是 + * true 并把它落盘 ⇒ 旧存档里那个 true 说明不了用户同意过。 + * 只有这个标记为 true 的存档才算「用户自己开的」。 + * @type {boolean} + */ + this.petAwareOptIn = false; + /** + * 升级后要提示用户「请重新打开」(持久化,直到用户真打开为止)。 + * + * ⚠️ 评审原话:旧状态分不清「显式选择」与「旧默认值」时, + * **重置为关并请用户重新打开** —— 这个字段就是那句「请」。 + * @type {boolean} + */ + this.petAwareReset = false; // ★ **闲话开关**:她没人的时候要不要自己开口(模型闲话)—— **默认关闭**。 // ⚠️ **默认关**(Marketplace 评审「强烈建议」):自动对话会消耗用户自己的额度, // 所以新装的用户一律**由用户主动打开**;已经装过的用户保留他自己上次的 @@ -461,7 +512,13 @@ export class PetState { // 丢掉之后回到「用户自己建的 mmx 或默认 agent」(见 agentpick.mjs)。 if (typeof saved.petAgent === 'string' && saved.petAgent && saved.petAgent !== LEGACY_SELF_AGENT_NAME) this.petAgent = saved.petAgent; - if (typeof saved.petAware === 'boolean') this.petAware = saved.petAware; + // ⚠️ **跨会话采集开关的升级迁移**(官方插件市场评审 2026-11-17,PR #138): + // 1.9.0 的默认值是 true 并把它落盘 ⇒ 旧存档里那个 true + // **说明不了用户同意过采集** ⇒ 没有 opt-in 标记的一律重置为关。 + const petAwareMigrated = migratePetAware(saved); + this.petAware = petAwareMigrated.petAware; + this.petAwareOptIn = petAwareMigrated.petAwareOptIn; + this.petAwareReset = petAwareMigrated.petAwareReset; if (typeof saved.idleTalk === 'boolean') this.idleTalk = saved.idleTalk; if (saved.naming && typeof saved.naming === 'object') { this.naming = { @@ -487,6 +544,16 @@ export class PetState { // 结算「重启期间她被冷落了多久」—— 锚点恢复完就结算,不能等第一次 commit // (否则第一次 commit 会把锚点当成从未存在过,那段时间白丢)。 this.settleHeart(); + // ⚠️ **迁移结果与文件不一致就把新形状落回去**(官方评审 2026-11-17): + // 旧存档里那个 petAware:true 必须从磁盘上消失 —— + // 留着它,下次读回来又会被当成「用户开过」。 + // ⚠️ **必须放在最后**:前面那些字段(heart 等)刚从存档恢复回来, + // 这时候落盘会把它们写成默认值。 + if (saved.petAware !== this.petAware + || saved.petAwareOptIn !== this.petAwareOptIn + || saved.petAwareReset !== this.petAwareReset) { + await this.persist(); + } } } catch (error) { // 首次运行没有存档(ENOENT),用默认值 —— 这是正常的。 @@ -523,6 +590,8 @@ export class PetState { petHeadInjected: this.petHeadInjected, petAgent: this.petAgent, petAware: this.petAware, + petAwareOptIn: this.petAwareOptIn, + petAwareReset: this.petAwareReset, idleTalk: this.idleTalk, naming: { ...this.naming }, windowState: { ...this.windowState }, @@ -593,6 +662,7 @@ export class PetState { asking: this.asking, petAgent: this.petAgent, petAware: this.petAware, + petAwareReset: this.petAwareReset, idleTalk: this.idleTalk, naming: { ...this.naming }, chatBusy: this.chatBusy, @@ -1175,7 +1245,16 @@ export class PetState { // ⚠️ **只有明确传 true 才开** —— 默认是关的(官方插件市场 2026-11-16 评审: // 隐私敏感的跨会话能力默认关闭、由用户主动打开)。 // 传 undefined / 其它值一律按「关」处理(与 setIdleTalk 同一口径)。 - this.petAware = aware === true; + const next = aware === true; + this.petAware = next; + // ⚠️ **只有用户明确打开才记 opt-in**(官方评审 2026-11-17): + // 这个标记是「用户同意过跨会话采集」的**唯一凭据** —— + // 升级迁移靠它区分「用户自己开的」与「1.9.0 落下来的旧默认值」。 + // 关掉 ⇒ 同意被收回,标记一起清掉。 + this.petAwareOptIn = next; + // ⚠️ **用户真打开了就不用再提示**;只是又点了一次「关」不算 —— + // 提示要留到他愿意打开为止。 + if (next) this.petAwareReset = false; await this.commit(); return this.petAware; } diff --git a/plugins/antianqi/mmx-octopus/tests/petaware-default-off.test.mjs b/plugins/antianqi/mmx-octopus/tests/petaware-default-off.test.mjs index d798651b..8feaf998 100644 --- a/plugins/antianqi/mmx-octopus/tests/petaware-default-off.test.mjs +++ b/plugins/antianqi/mmx-octopus/tests/petaware-default-off.test.mjs @@ -55,7 +55,7 @@ function runScenarios() { }); } -test('petAware:默认关,关的时候真的不采集(首启 / 切换 / 重启)', async () => { +test('petAware:默认关 + 旧存档升级后重置为关(首启 / 切换 / 重启 / 升级)', async () => { const run = await runScenarios(); assert.equal( run.timedOut, @@ -70,5 +70,11 @@ test('petAware:默认关,关的时候真的不采集(首启 / 切换 / 重 [], failed.map((f) => f.name + ' -> ' + f.detail).join('; ') ); - assert.ok(results.length >= 12, '场景数不对:' + results.length); + assert.ok(results.length >= 20, '场景数不对:' + results.length); + // ⚠️ **升级迁移那几条必须真的跑到**(官方评审 2026-11-17 点名的回归测试): + // 旧存档(petAware:true)→ 升级后是关的 → 显式打开后采集恢复。 + assert.ok( + results.some((r) => r.name.includes('旧存档')), + '升级迁移的场景没跑到(results 里没有「旧存档」那几条)' + ); }); diff --git a/plugins/antianqi/mmx-octopus/tests/petaware-scenarios.mjs b/plugins/antianqi/mmx-octopus/tests/petaware-scenarios.mjs index 2d42062a..1f75391a 100644 --- a/plugins/antianqi/mmx-octopus/tests/petaware-scenarios.mjs +++ b/plugins/antianqi/mmx-octopus/tests/petaware-scenarios.mjs @@ -14,7 +14,7 @@ * 覆盖评审(上游 issue #136)点名的三组:**首次启动**、**切换开关**、**重启**。 */ -import { mkdtemp, readFile, writeFile, rm } from 'node:fs/promises'; +import { mkdtemp, readFile, writeFile, rm, readdir } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; @@ -103,7 +103,7 @@ function makeMockAgent({ observeImpl, sessionList = [] } = {}) { } /** 起一套独立的 server(独立端口 + 独立 dataDir)。 */ -async function startHarness({ agent, dataDir, boardIntervalMs = 200 }) { +async function startHarness({ agent, dataDir, boardIntervalMs = 200, startFn }) { const dir = dataDir || (await makeTempDir('mmx-petaware-')); const ctrl = new AbortController(); const ctx = { @@ -116,7 +116,7 @@ async function startHarness({ agent, dataDir, boardIntervalMs = 200 }) { capabilities: { has: () => true }, agent, }; - const life = await start(ctx, { boardIntervalMs }); + const life = await (startFn || start)(ctx, { boardIntervalMs }); liveServers.push(life); const base = `http://127.0.0.1:${life.port}`; return { @@ -149,6 +149,37 @@ async function importBadNode(fileName, mutate) { return import(pathToFileURL(join(dir, fileName)).href); } +/** + * 把整个 node 目录复制一份并改坏其中一个模块,然后**独立导入**那一份 server。 + * + * ⚠️ **为什么复制整个目录**:负向注入要落在**装配后的行为**上 —— + * 只复制一个模块的话,server 还在用真模块跑,注入根本碰不到真实路径。 + * 目录里的模块都按 pluginRoot 找资源(不按自身目录找),所以复制一份 + * 照样能跑起来(context.pluginRoot 仍然指真包)。 + */ +async function importBadServer(mutate) { + const dir = await makeTempDir('mmx-petaware-badserver-'); + for (const name of await readdir(NODE_DIR)) { + if (!name.endsWith('.mjs')) continue; + await writeFile(join(dir, name), await readFile(join(NODE_DIR, name), 'utf8')); + } + const src = await readFile(join(NODE_DIR, 'state.mjs'), 'utf8'); + const bad = mutate(src); + if (bad === src) return null; + await writeFile(join(dir, 'state.mjs'), bad); + return import(pathToFileURL(join(dir, 'server.mjs')).href); +} + +/** + * **旧规则**(1.9.0 之前的写法):读回存档里存着的值就算数。 + * 负向注入用它替换真规则 ⇒ 判据必须红。 + */ +const LEGACY_MIGRATION = [ + 'export function migratePetAware(saved) {', + ' return { petAware: !!(saved && saved.petAware === true), petAwareOptIn: false, petAwareReset: false };', + '}', +].join('\n'); + const SESSIONS = [{ sessionId: 's-other', title: '别人的活', status: 'running' }]; /** @@ -318,6 +349,133 @@ async function runScenariosInner() { } } + // ---------- ⑦ 升级迁移:旧存档里那个「开」不算用户同意过 ---------- + // + // ⚠️ **评审原话**(官方插件市场 2026-11-17,PR #138): + // "an existing state.json with petAware: true retains that value after + // upgrading, so cross-session collection continues without a new opt-in" + // → 旧存档没有 opt-in 标记 ⇒ **一律重置为关**,并请用户重新打开。 + const legacyDir = await makeTempDir('mmx-petaware-legacy-'); + await writeFile( + join(legacyDir, 'state.json'), + JSON.stringify({ mood: 'idle', petAware: true, heart: 50, intimacy: 0 }), + 'utf8' + ); + const agentLegacy = makeMockAgent({ sessionList: SESSIONS }); + const hLegacy = await startHarness({ agent: agentLegacy, dataDir: legacyDir }); + const legacySnap = await hLegacy.state(); + push( + '旧存档里的 petAware:true 升级后被重置为关(1.9.0 的默认值不算用户同意)', + legacySnap.petAware === false, + 'petAware=' + String(legacySnap.petAware) + ); + await sleep(1200); + push( + '旧存档升级后一个 observe 都不订阅', + agentLegacy.calls.observe === 0, + 'observe 被调 ' + agentLegacy.calls.observe + ' 次' + ); + push( + '旧存档升级后一次 sessions() 都不调', + agentLegacy.calls.sessions === 0, + 'sessions 被调 ' + agentLegacy.calls.sessions + ' 次' + ); + push( + '旧存档升级后面板留着「请重新打开」的提示', + legacySnap.petAwareReset === true, + 'petAwareReset=' + String(legacySnap.petAwareReset) + ); + // 用户明确打开 ⇒ 采集恢复 + await fetch(hLegacy.base + '/api/petaware', { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ enabled: true }), + }); + await sleep(1200); + push( + '用户明确打开之后订阅恢复了', + agentLegacy.calls.observe >= 1, + 'observe 被调 ' + agentLegacy.calls.observe + ' 次' + ); + push( + '用户明确打开之后看板恢复了', + agentLegacy.calls.sessions >= 1, + 'sessions 被调 ' + agentLegacy.calls.sessions + ' 次' + ); + await hLegacy.life.dispose(); + + // 重启:明确 opt-in 过 ⇒ 仍然是开的 + const agentLegacy2 = makeMockAgent({ sessionList: SESSIONS }); + const hLegacy2 = await startHarness({ agent: agentLegacy2, dataDir: legacyDir }); + const legacySnap2 = await hLegacy2.state(); + push( + '明确打开过之后重启仍然是开的(opt-in 被记住)', + legacySnap2.petAware === true, + 'petAware=' + String(legacySnap2.petAware) + ); + await sleep(1200); + push( + '明确打开过之后重启继续采集', + agentLegacy2.calls.observe >= 1, + 'observe 被调 ' + agentLegacy2.calls.observe + ' 次' + ); + await hLegacy2.life.dispose(); + + // ---------- ⑧ 旧存档压根没这个字段 ⇒ 仍然是关的 ---------- + { + const oldDir = await makeTempDir('mmx-petaware-old-'); + await writeFile(join(oldDir, 'state.json'), JSON.stringify({ mood: 'idle' }), 'utf8'); + const agentOld = makeMockAgent({ sessionList: SESSIONS }); + const hOld = await startHarness({ agent: agentOld, dataDir: oldDir }); + const oldSnap = await hOld.state(); + push( + '旧存档里压根没有这个字段时仍然是关的', + oldSnap.petAware === false, + 'petAware=' + String(oldSnap.petAware) + ); + await sleep(1200); + push( + '旧存档里没有这个字段时也不采集', + agentOld.calls.observe === 0 && agentOld.calls.sessions === 0, + 'observe=' + agentOld.calls.observe + ' sessions=' + agentOld.calls.sessions + ); + await hOld.life.dispose(); + } + + // ---------- ⑨ 负向注入③:把迁移规则改回「读回旧值」⇒ 判据必须红 ---------- + { + const bad = await importBadServer((s) => { + const m = s.match(/export function migratePetAware\(saved\) \{[\s\S]*?\n\}/); + if (!m) return s; + return s.replace(m[0], LEGACY_MIGRATION); + }); + if (!bad) { + push('负向注入|把迁移规则改回「读回旧值」时判据必须红', false, '注入没生效'); + } else { + const badDir = await makeTempDir('mmx-petaware-badlegacy-'); + await writeFile( + join(badDir, 'state.json'), + JSON.stringify({ mood: 'idle', petAware: true }), + 'utf8' + ); + const badAgent = makeMockAgent({ sessionList: SESSIONS }); + const badH = await startHarness({ + agent: badAgent, + dataDir: badDir, + startFn: bad.start, + }); + const badSnap = await badH.state(); + await sleep(1200); + push( + '负向注入|把迁移规则改回「读回旧值」时判据必须红', + badSnap.petAware === true && badAgent.calls.observe > 0, + '坏副本把旧值读回来了(petAware=' + String(badSnap.petAware) + + ' observe=' + badAgent.calls.observe + ')⇒ 真判据会红' + ); + await badH.life.dispose(); + } + } + return out; }