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(group-chat): 非群房间被拒时不订阅,且所有入口都按房间不存在回答

Dash545a8f5bb79f
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
index 6e4d5650a2..27590bdb11 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
@@ -1966,11 +1966,34 @@ describe('Chat Server result delivery and refresh', () => {
expect(await service.setMembers({ groupId: directId, botIds: [] })).toMatchObject({ ok: false, errorCode: 'NOT_FOUND' });
expect(await service.sendMessage({ groupId: directId, text: 'hi', clientId: 'client-direct-1', mentions: { all: false, botIds: [] } }))
.toMatchObject({ ok: false, errorCode: 'NOT_FOUND' });
+ // Raw-code entry points answer exactly like a missing room; the internal NOT_A_GROUP never leaves main.
expect(await service.chatServer!.manage({ groupId: directId, action: { type: 'member', actorId: selfId, action: 'leave' } }))
- .toMatchObject({ ok: false, errorCode: 'NOT_A_GROUP' });
- expect(await service.chatServer!.createInvite({ groupId: directId, clientId: 'invite-direct-1' })).toMatchObject({ ok: false, errorCode: 'NOT_A_GROUP' });
+ .toMatchObject({ ok: false, errorCode: 'CONVERSATION_NOT_FOUND' });
+ expect(await service.chatServer!.createInvite({ groupId: directId, clientId: 'invite-direct-1' })).toMatchObject({ ok: false, errorCode: 'CONVERSATION_NOT_FOUND' });
+ expect(await service.chatServer!.revokeInvite({ groupId: directId, link: `cindy://chat-invite/${'a'.repeat(43)}`, clientId: '90000000-0000-4000-8000-000000000001' }))
+ .toMatchObject({ ok: false, errorCode: 'CONVERSATION_NOT_FOUND' });
expect(writes()).toEqual([]);
});
+
+ it('never subscribes to a rejected room, so its traffic cannot invalidate the group list', async () => {
+ await service.getGroup(roomId);
+ await vi.advanceTimersByTimeAsync(2000);
+ const socket = fixture.sockets[0];
+ Object.defineProperty(socket, 'readyState', { value: 1 });
+ socket.emit('message', JSON.stringify({ type: 'ready' }));
+ await vi.advanceTimersByTimeAsync(0);
+ expect(socket.send).toHaveBeenCalledWith(JSON.stringify({ type: 'subscribe', scope: `conversation:${roomId}`, after: '1' }));
+ vi.mocked(socket.send).mockClear();
+ expect(await service.getGroup(directId)).toMatchObject({ ok: false, errorCode: 'NOT_FOUND' });
+ expect(await service.chatServer!.createInvite({ groupId: channelId, clientId: 'invite-channel-1' })).toMatchObject({ ok: false, errorCode: 'CONVERSATION_NOT_FOUND' });
+ expect(vi.mocked(socket.send).mock.calls.filter(([raw]) => String(raw).includes('"subscribe"'))).toEqual([]);
+ // A later reconnect resubscribes only the group.
+ socket.emit('message', JSON.stringify({ type: 'ready' }));
+ await vi.advanceTimersByTimeAsync(0);
+ const scopes = vi.mocked(socket.send).mock.calls.map(([raw]) => JSON.parse(String(raw)))
+ .filter(m => m.type === 'subscribe' && String(m.scope).startsWith('conversation:')).map(m => m.scope);
+ expect(scopes).toEqual([`conversation:${roomId}`]);
+ });
});
});
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServerErrors.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServerErrors.test.ts
index cd8a5196cc..b185c57ea2 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServerErrors.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServerErrors.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { chatErrorDiagnostic, chatGroupFailure } from '../chatServerErrors.js';
+import { chatErrorDiagnostic, chatGroupFailure, missingGroupCode } from '../chatServerErrors.js';
describe('chat group errors', () => {
it.each([
@@ -15,6 +15,11 @@ describe('chat group errors', () => {
])('classifies %s without exposing exception text', (code, expected) => {
expect(chatGroupFailure(new Error(code))).toEqual({ ok: false, errorCode: expected, message: expected });
});
+ it('answers a non-group room exactly like a missing room on every entry point', () => {
+ expect(missingGroupCode('NOT_A_GROUP')).toBe('CONVERSATION_NOT_FOUND');
+ expect(missingGroupCode('ROLE_REQUIRED')).toBe('ROLE_REQUIRED');
+ expect(chatGroupFailure(new Error('NOT_A_GROUP'))).toEqual(chatGroupFailure(new Error('CONVERSATION_NOT_FOUND')));
+ });
it('keeps only diagnostic code and HTTP status', () => {
expect(chatErrorDiagnostic(Object.assign(new Error('private URL https://x/?token=secret'), { status: 500 })))
.toEqual({ code: 'UNKNOWN', status: 500 });
diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.ts
index d9f111ffcd..38c351cf89 100644
--- a/apps/desktop/src/main/maker-ipc/chatServer.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts
@@ -13,7 +13,7 @@ import { existsSync, readFileSync } from 'node:fs';
import { request as httpRequest } from 'node:http';
import { request as httpsRequest } from 'node:https';
import { migrateLocalGroups } from './chatServerMigration.js';
-import { chatErrorDiagnostic, chatGroupFailure } from './chatServerErrors.js';
+import { chatErrorDiagnostic, chatGroupFailure, missingGroupCode } from './chatServerErrors.js';
import { createChatMedia } from './chatServerMedia.js';
import { chatServerWorkspaces } from './chatServerWorkspaces.js';
import { buildPlanStepBrief } from './botGroupDivision.js';
@@ -679,17 +679,22 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
reconnectDelay = Math.min(reconnectDelay * 2, 15000);
});
}
- async function snapshot(roomId: string) {
+ async function readSnapshot(roomId: string) {
id.parse(roomId); await register();
const value = await api<Snapshot>(`/conversations/${roomId}/snapshot`);
if (value.room.dissolved_at) retirePlanning(roomId);
+ return value;
+ }
+ async function snapshot(roomId: string) {
+ const value = await readSnapshot(roomId);
subscribe(roomId, value.cursor);
return value;
}
- /** A room read through a bot-group entry point; any other kind answers as a missing group. */
+ /** A room read through a bot-group entry point; any other kind answers as a missing group and is never subscribed. */
async function groupSnapshot(roomId: string) {
- const value = await snapshot(roomId);
+ const value = await readSnapshot(roomId);
if (!isGroupRoom(value.room)) throw new Error('NOT_A_GROUP');
+ subscribe(roomId, value.cursor);
return value;
}
function managementActor(s: Snapshot) {
@@ -1443,7 +1448,7 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
}
const result = async <T>(fn: () => Promise<T>) => {
try { return { ok: true as const, ...await fn() }; }
- catch (error) { return { ok: false as const, errorCode: error instanceof z.ZodError ? 'INVALID_INPUT' : error instanceof Error ? error.message : 'REQUEST_FAILED' }; }
+ catch (error) { return { ok: false as const, errorCode: error instanceof z.ZodError ? 'INVALID_INPUT' : error instanceof Error ? missingGroupCode(error.message) : 'REQUEST_FAILED' }; }
};
// Bare token, cindy://chat-invite/<token> or the public https page link; only the token
// reaches this account's own Chat Server. An unreadable link is INVALID_INPUT.
diff --git a/apps/desktop/src/main/maker-ipc/chatServerErrors.ts b/apps/desktop/src/main/maker-ipc/chatServerErrors.ts
index 60499d9bf6..6a806239a0 100644
--- a/apps/desktop/src/main/maker-ipc/chatServerErrors.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerErrors.ts
@@ -9,14 +9,23 @@ export function chatErrorDiagnostic(error: unknown): { code: string; status?: nu
return { code, ...(typeof value?.status === 'number' && Number.isInteger(value.status) ? { status: value.status } : {}) };
}
+/**
+ * A room reached through a bot-group entry point that is not a group (a 1:1 companion chat,
+ * a channel) answers exactly like a missing room. The internal code stays in diagnostics only.
+ */
+export function missingGroupCode(code: string): string {
+ return code === 'NOT_A_GROUP' ? 'CONVERSATION_NOT_FOUND' : code;
+}
+
/** Stable localized categories for desktop and remote controllers; raw errors stay private. */
export function chatGroupFailure(error: unknown): BotGroupFailure {
- const { code, status } = chatErrorDiagnostic(error);
+ const { code: raw, status } = chatErrorDiagnostic(error);
+ const code = missingGroupCode(raw);
let errorCode: BotGroupErrorCode;
if (['PLAN_OPEN', 'PLAN_CLOSED', 'MENTION_UNAVAILABLE', 'MEMBER_LIMIT', 'MEMBER_UNAVAILABLE', 'HOST_NOT_READY',
'INVALID_ATTACHMENT', 'ATTACHMENT_UNAVAILABLE', 'ATTACHMENT_TOO_LARGE', 'MEDIA_UPLOAD_FAILED',
'LIFECYCLE_UNAVAILABLE', 'LAST_OWNER', 'AUTH_REQUIRED', 'IMPORT_PENDING', 'REQUEST_TIMEOUT', 'REVISION_CONFLICT', 'QUOTE_UNAVAILABLE'].includes(code)) errorCode = code as BotGroupErrorCode;
- else if (code === 'CONVERSATION_NOT_FOUND' || code === 'NOT_A_GROUP' || code === 'OWNER_CHANGED') errorCode = 'NOT_FOUND';
+ else if (code === 'CONVERSATION_NOT_FOUND' || code === 'OWNER_CHANGED') errorCode = 'NOT_FOUND';
else if (['ROLE_REQUIRED', 'ACTOR_NOT_OWNED', 'OWNER_REQUIRED'].includes(code) || status === 403) errorCode = 'PERMISSION_DENIED';
else if (status === 401) errorCode = 'AUTH_REQUIRED';
else if (code === 'CHAT_ENDPOINT_UNAVAILABLE' || code === 'INVALID_CHAT_ENDPOINT' || ['ECONNREFUSED', 'ENOTFOUND', 'EAI_AGAIN'].includes(code)) errorCode = 'CHAT_UNAVAILABLE';