Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(im): 控制命令收归主人专属,群成员的 !stop / slash 不再生效 (#1658)

Chris Zhang3491918697a7
diff --git a/apps/desktop/src/main/im/shared/__tests__/stopCommandRouting.test.ts b/apps/desktop/src/main/im/shared/__tests__/stopCommandRouting.test.ts
index 4bd11106d2..9b3d342da9 100644
--- a/apps/desktop/src/main/im/shared/__tests__/stopCommandRouting.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/stopCommandRouting.test.ts
@@ -261,6 +261,118 @@ describe('messageHandler !stop routing', () => {
});
});
+ // ── 控制命令的主人门 ──────────────────────────────────────────────────────
+ // 群消息的 senderId 是群 lane, 所以任何群成员的 !stop 都会解析到同一个群会话,
+ // 等于掐掉主人正在跑的那一轮; slash 则会去动主人的目录/会话。speaker 存在即
+ // 代表这是群里的一次发言, 必须 isOwner 才放行(fail-closed)。
+ const groupSpeaker = (isOwner: boolean): IMMessageEvent['speaker'] => ({
+ id: 'member-9001',
+ name: '群友',
+ isOwner,
+ });
+
+ it('群成员的 !stop 静默丢弃: 不掐主人的 turn、不回提示、不落 agent', async () => {
+ deliver(makeEvent({ text: '!stop', speaker: groupSpeaker(false) }));
+ await flushMicrotasks();
+
+ expect(stopActiveTurn).not.toHaveBeenCalled();
+ expect(sendMarkdownText).not.toHaveBeenCalled();
+ expect(sendText).not.toHaveBeenCalled();
+ expect(runAgentTurn).not.toHaveBeenCalled();
+ });
+
+ it('群成员的 slash 命令同样静默丢弃(不动主人的目录/会话)', async () => {
+ deliver(makeEvent({ text: '/project', speaker: groupSpeaker(false) }));
+ await flushMicrotasks();
+
+ expect(handleSlashCommand).not.toHaveBeenCalled();
+ expect(runAgentTurn).not.toHaveBeenCalled();
+ expect(sendMarkdownText).not.toHaveBeenCalled();
+ });
+
+ it('群主人的 !stop 照常执行', async () => {
+ deliver(makeEvent({ text: '!stop', speaker: groupSpeaker(true) }));
+ await flushMicrotasks();
+
+ expect(stopActiveTurn).toHaveBeenCalledTimes(1);
+ expect(sendMarkdownText).toHaveBeenCalledWith('U123456789', slackUi.agent.stopDone(0), {
+ threadTs: '1234.5678',
+ });
+ });
+
+ it('群主人的 slash 命令照常执行', async () => {
+ deliver(makeEvent({ text: '/project', speaker: groupSpeaker(true) }));
+ await flushMicrotasks();
+
+ expect(handleSlashCommand).toHaveBeenCalledTimes(1);
+ });
+
+ it('无 speaker(私聊/单人对话)放行: 各渠道入站已做过 owner 门', async () => {
+ deliver(makeEvent({ text: '!stop' }));
+ await flushMicrotasks();
+
+ expect(stopActiveTurn).toHaveBeenCalledTimes(1);
+ });
+
+ it('群成员的普通消息不受影响: 仍照常进 agent', async () => {
+ deliver(makeEvent({ text: '帮我看看这个', speaker: groupSpeaker(false) }));
+ await flushMicrotasks();
+
+ expect(runAgentTurn).toHaveBeenCalledTimes(1);
+ expect(stopActiveTurn).not.toHaveBeenCalled();
+ });
+
+ // 文本 + unsupported(音视频/超限/未知类型)不是"裸命令": 必须留在原有的
+ // unsupportedNotice + agent 路径上, 否则连"你那个音频我处理不了"的反馈一起被吞。
+ const unsupportedEntry = [{ type: 'audio', label: '语音(暂不支持)' }] as IMMessageEvent['unsupported'];
+
+ it('群成员发 !stop 但带 unsupported 内容: 不当命令, 照常走 unsupported + agent', async () => {
+ deliver(
+ makeEvent({ text: '!stop', speaker: groupSpeaker(false), unsupported: unsupportedEntry }),
+ );
+ await flushMicrotasks();
+
+ expect(stopActiveTurn).not.toHaveBeenCalled();
+ // 关键: 没有被主人门静默丢掉 —— 反馈与 agent 路径都还在。
+ expect(sendText).toHaveBeenCalledTimes(1);
+ expect(runAgentTurn).toHaveBeenCalledTimes(1);
+ });
+
+ it('群成员发 slash 但带 unsupported 内容: 不当命令, 照常走 unsupported + agent', async () => {
+ deliver(
+ makeEvent({ text: '/project', speaker: groupSpeaker(false), unsupported: unsupportedEntry }),
+ );
+ await flushMicrotasks();
+
+ expect(handleSlashCommand).not.toHaveBeenCalled();
+ expect(sendText).toHaveBeenCalledTimes(1);
+ expect(runAgentTurn).toHaveBeenCalledTimes(1);
+ });
+
+ it('主人发 !stop 但带 unsupported 内容: 同样不当命令(判据与门逐字一致)', async () => {
+ deliver(
+ makeEvent({ text: '!stop', speaker: groupSpeaker(true), unsupported: unsupportedEntry }),
+ );
+ await flushMicrotasks();
+
+ expect(stopActiveTurn).not.toHaveBeenCalled();
+ expect(runAgentTurn).toHaveBeenCalledTimes(1);
+ });
+
+ it('群成员带附件发 !stop: 不是命令, 照常进 agent(不被门误伤)', async () => {
+ deliver(
+ makeEvent({
+ text: '!stop',
+ speaker: groupSpeaker(false),
+ attachments: [{ kind: 'image', absPath: '/tmp/a.png' } as unknown as IMAttachment],
+ }),
+ );
+ await flushMicrotasks();
+
+ expect(stopActiveTurn).not.toHaveBeenCalled();
+ expect(runAgentTurn).toHaveBeenCalledTimes(1);
+ });
+
it('treats !stop with attachments as a normal agent message', async () => {
deliver(
makeEvent({
diff --git a/apps/desktop/src/main/im/shared/controlCommands.ts b/apps/desktop/src/main/im/shared/controlCommands.ts
index 84090c7a1c..80768ef414 100644
--- a/apps/desktop/src/main/im/shared/controlCommands.ts
+++ b/apps/desktop/src/main/im/shared/controlCommands.ts
@@ -7,3 +7,23 @@ export function isStopCommand(text: string): boolean {
const normalized = text.trim().toLowerCase();
return normalized === '!stop' || normalized === '!stop';
}
+
+/**
+ * 控制命令(`!stop` / slash)是否被授权执行 —— **主人专属**。
+ *
+ * 判据只看 `speaker`:
+ * - 有 speaker = 群/多人对话的一次发言, 必须 `isOwner === true`(fail-closed:
+ * 字段缺失或为假一律不放行);
+ * - 无 speaker = 私聊/单人对话, 各渠道在入站处已做过 owner 门(telegram 非
+ * owner 私聊回礼貌提示即 return, wecom 一切消息都过 acceptOwner), 这里放行。
+ *
+ * 为什么必须在这一层拦: 群消息的 `senderId` 被折叠成**群 lane**(telegram 的
+ * `g/<chatId>`、钉钉的 `encodeLaneUserId(conversationId)`), 于是任何群成员发的
+ * `!stop` 都会解析到**同一个群会话**, 效果等同于掐掉主人正在跑的那一轮; slash
+ * 命令同理会操作主人的目录/会话。telegram 在入站层已经拦了(命令 owner 专属),
+ * 钉钉没有 —— 门放在共用路由上, 六个渠道一次盖住。
+ */
+export function isCommandAuthorized(event: { speaker?: { isOwner: boolean } }): boolean {
+ if (event.speaker === undefined) return true;
+ return event.speaker.isOwner === true;
+}
diff --git a/apps/desktop/src/main/im/shared/messageHandler.ts b/apps/desktop/src/main/im/shared/messageHandler.ts
index 422a0cafc4..cb0deb79c6 100644
--- a/apps/desktop/src/main/im/shared/messageHandler.ts
+++ b/apps/desktop/src/main/im/shared/messageHandler.ts
@@ -27,7 +27,7 @@ import {
} from '../accountBoundary';
import { getControlScope, isInControl } from './controlState';
-import { isStopCommand } from './controlCommands';
+import { isCommandAuthorized, isStopCommand } from './controlCommands';
import type { ImSlashHandlers } from './slashCommands';
import { looksLikeSlashCommand } from './slashCommands';
import type { ImTurnRunner } from './turnRunner';
@@ -61,6 +61,34 @@ export function createMessageHandler(
`textLen=${event.text.length} att=${event.attachments.length} unsupported=${event.unsupported.length}`,
);
+ // ── 控制命令的主人门: 群成员的 !stop / slash 静默丢弃 ────────────────────
+ // 群消息的 senderId 是**群 lane**(telegram g/<chatId>、钉钉
+ // encodeLaneUserId(conversationId)), 所以群成员发的 !stop 会解析到同一个群
+ // 会话 —— 等于掐掉主人正在跑的那一轮; slash 则会去动主人的目录/会话。
+ // 静默(不回提示)与 telegram 入站层同口径: 群里不可被探测。也不落到 agent,
+ // 否则命令会变成一句普通 prompt。
+ //
+ // 放在 /ctr 拦截**之前**: 否则主人正走 /ctr 时, 群成员发命令会收到一句
+ // "控制流程中" —— 等于把主人的状态回给了没有权限的人。
+ //
+ // 只有**纯文本**才算控制命令: 附件与 unsupported(音视频/超限/未知类型等)都要
+ // 让消息走 unsupportedNotice / unsupportedOnly / agent 的原有路径, 不能被当成
+ // 一句裸命令吞掉 —— 那会连"你那个音频我处理不了"的反馈一起吃掉。
+ // 这个判据必须与下面两条命令分支**逐字一致**: 门比分支窄一点, 非主人的
+ // `!stop` + unsupported 就会穿过门再被分支执行, 洞等于没堵。
+ const pureTextCommandInput =
+ event.text.length > 0 && event.attachments.length === 0 && event.unsupported.length === 0;
+ const commandLike =
+ pureTextCommandInput &&
+ (isStopCommand(event.text) || looksLikeSlashCommand(event.text));
+ if (commandLike && !isCommandAuthorized(event)) {
+ log.info(
+ `dropped non-owner command sender=...${event.senderId.slice(-8)} ` +
+ `speaker=...${(event.speaker?.id ?? '').slice(-8)}`,
+ );
+ return;
+ }
+
// ── /ctr 原子化拦截 ────────────────────────────────────────────────
// 该 (bot, owner) 处于 /ctr 流程中 → 任何消息都不路由到 slash/agent,
// 直接回提示让用户走卡片按钮 (back/exit/session-pick) 退出。包括重复
@@ -89,10 +117,10 @@ export function createMessageHandler(
}
// ── !stop 控制指令: 中止当前 turn, 绝不作为普通消息入队 ─────────────────
- // 放在 slash 之前; 与 slash 同口径只认无附件的纯文本。turn 运行期间
- // userLocks 并不持锁(runAgentTurn 在 dispatch 后即返回), 所以这里能在
+ // 放在 slash 之前; 与 slash 同口径只认纯文本(见 pureTextCommandInput)。turn
+ // 运行期间 userLocks 并不持锁(runAgentTurn 在 dispatch 后即返回), 所以这里能在
// 上一轮仍在跑时立刻执行, 而不是排到它后面。
- if (event.text && event.attachments.length === 0 && isStopCommand(event.text)) {
+ if (pureTextCommandInput && isStopCommand(event.text)) {
let reply: string;
try {
const result = await turnRunner.stopActiveTurn({
@@ -118,8 +146,8 @@ export function createMessageHandler(
return;
}
- // ── slash command (only on plain text, no attachments) ──────────────────
- if (event.text && event.attachments.length === 0 && looksLikeSlashCommand(event.text)) {
+ // ── slash command (only on plain text: no attachments, no unsupported) ──
+ if (pureTextCommandInput && looksLikeSlashCommand(event.text)) {
try {
await slash.handleSlashCommand(event.text, {
botContextId: event.contextId,