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(bot-groups): retain OSS download diagnostics and type failure fixture

Chris00fc665e6f18
diff --git a/apps/desktop/src/main/device-link/__tests__/mediaTransfer.test.ts b/apps/desktop/src/main/device-link/__tests__/mediaTransfer.test.ts
index e5ee08c292..9effb126cb 100644
--- a/apps/desktop/src/main/device-link/__tests__/mediaTransfer.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/mediaTransfer.test.ts
@@ -705,6 +705,13 @@ describe('uploadBuffer — 内存字节(base64 附件)', () => {
});
describe('downloadToFile — 原子完整性校验', () => {
+ it.each([403, 500])('preserves a safe code and HTTP %s without publishing a file', async (status) => {
+ netFetchMock.mockResolvedValue({ ok: false, status });
+ await expect(downloadToFile(KEY, '/tmp/final.bin')).rejects.toMatchObject({ code: 'OSS_DOWNLOAD_FAILED', status });
+ expect(createWriteStreamMock).not.toHaveBeenCalled();
+ expect(renameMock).not.toHaveBeenCalled();
+ });
+
it('拒绝超出声明大小的流,不发布目标文件', async () => {
netFetchMock.mockResolvedValue({ ok: true, status: 200, body: webBody(Uint8Array.from([1, 2, 3])) });
await expect(downloadToFile(KEY, '/tmp/final.bin', { size: 1, sha256: 'a'.repeat(64) }))
diff --git a/apps/desktop/src/main/device-link/mediaTransfer.ts b/apps/desktop/src/main/device-link/mediaTransfer.ts
index 41b724a60d..5eb42a94d1 100644
--- a/apps/desktop/src/main/device-link/mediaTransfer.ts
+++ b/apps/desktop/src/main/device-link/mediaTransfer.ts
@@ -661,8 +661,8 @@ export async function downloadToFile(
): Promise<void> {
const { getUrl } = await presignGet(key);
const resp = await net.fetch(getUrl, { method: 'GET', signal });
- if (!resp.ok) throw new Error(`OSS GET 失败 (${resp.status})`);
- if (!resp.body) throw new Error('OSS GET 响应无 body');
+ if (!resp.ok) throw Object.assign(new Error(`OSS GET 失败 (${resp.status})`), { code: 'OSS_DOWNLOAD_FAILED', status: resp.status });
+ if (!resp.body) throw Object.assign(new Error('OSS GET 响应无 body'), { code: 'OSS_DOWNLOAD_EMPTY', status: resp.status });
const partPath = `${destPath}.${randomUUID()}.part`;
const hasher = createHash('sha256');
let size = 0;
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botGroupAttachments.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botGroupAttachments.test.ts
index fdada6abd7..9a19d817b6 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botGroupAttachments.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botGroupAttachments.test.ts
@@ -67,10 +67,25 @@ vi.mock('../../device-link/remoteAttachment.js', () => ({
}));
import { createBotGroupAttachmentStore, safeAttachmentFileName } from '../botGroupAttachments.js';
+import { materializeRemoteAttachment } from '../../device-link/remoteAttachment.js';
let root: string;
const hash = 'a'.repeat(64);
+it.each([403, 500])('classifies OSS HTTP %s as unavailable and logs only safe diagnostics', async (status) => {
+ const warn = vi.fn();
+ vi.mocked(materializeRemoteAttachment).mockRejectedValueOnce(Object.assign(new Error('private signed URL and path'), { code: 'OSS_DOWNLOAD_FAILED', status }));
+ const result = await createBotGroupAttachmentStore({ ownerRoot: () => path.join(root, 'owner'), log: { warn } }).prepare({
+ groupId: 'g1', controllerDeviceId: 'phone-1',
+ attachments: [{ id: 'p1', name: 'private.pdf', path: 'upload://oss-brief.pdf', category: 'pdf', mimeType: 'application/pdf' }],
+ });
+ expect(result).toEqual({ ok: false, errorCode: 'ATTACHMENT_UNAVAILABLE', message: 'OSS_DOWNLOAD_FAILED' });
+ expect(warn).toHaveBeenCalledWith('Chat attachment preparation failed', { groupId: 'g1', stage: 'prepare', code: 'OSS_DOWNLOAD_FAILED', status });
+ expect(JSON.stringify(warn.mock.calls)).not.toMatch(/private|upload:\/\//);
+ expect(h.removed).toEqual([]);
+ expect(await fs.readdir(path.join(root, 'owner', 'bot-groups', 'g1', 'attachments'))).toEqual([]);
+});
+
beforeEach(async () => {
root = await fs.mkdtemp(path.join(os.tmpdir(), 'bot-group-attachments-'));
h.blobDir = path.join(root, 'blobs');
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 e3701b4f2e..6337100c80 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
@@ -115,7 +115,7 @@ describe('Chat Server result delivery and refresh', () => {
});
afterEach(() => { service.dispose(); vi.useRealTimers(); vi.unstubAllEnvs(); fixture.config = ''; vi.clearAllMocks(); });
it.each(['ATTACHMENT_UNAVAILABLE', 'INVALID_PARAMS'] as const)('keeps a useful reason for prepare %s', async (errorCode) => {
- deps.prepareAttachments = vi.fn(async () => ({ ok: false, errorCode, message: 'private details' }));
+ deps.prepareAttachments = vi.fn(async () => ({ ok: false as const, errorCode, message: 'private details' }));
deps.log = { warn: vi.fn() };
const result = await service.sendMessage({ groupId: roomId, text: '', clientId: 'phone-send-1', mentions: { all: false, botIds: [] }, attachments: [{}] }, { controllerDeviceId: 'phone' });
expect(result).toMatchObject({ ok: false, errorCode: errorCode === 'INVALID_PARAMS' ? 'INVALID_ATTACHMENT' : errorCode });
diff --git a/apps/desktop/src/main/maker-ipc/botGroupAttachments.ts b/apps/desktop/src/main/maker-ipc/botGroupAttachments.ts
index 6bffccb98a..1af4a7936e 100644
--- a/apps/desktop/src/main/maker-ipc/botGroupAttachments.ts
+++ b/apps/desktop/src/main/maker-ipc/botGroupAttachments.ts
@@ -104,7 +104,10 @@ export interface BotGroupAttachmentStore {
}) => Promise<BotGroupPreparedAttachments | BotGroupFailure>;
}
-export function createBotGroupAttachmentStore(deps: { ownerRoot: () => string }): BotGroupAttachmentStore {
+export function createBotGroupAttachmentStore(deps: {
+ ownerRoot: () => string;
+ log?: { warn: (message: string, fields: Record<string, unknown>) => void };
+}): BotGroupAttachmentStore {
const prepare: BotGroupAttachmentStore['prepare'] = async (input) => {
if (input.attachments.length > BOT_GROUP_ATTACHMENTS_MAX) return invalid();
// One account for the whole batch: an account switch while a phone upload is fetched must
@@ -195,8 +198,10 @@ export function createBotGroupAttachmentStore(deps: { ownerRoot: () => string })
} catch (error) {
await discard();
// Transfer errors carry no host paths; anything else is reported generically.
- const { code } = chatErrorDiagnostic(error);
- return { ok: false, errorCode: code.startsWith('FILE_PEER_') || code.startsWith('DEVICE_LINK_') ? 'ATTACHMENT_UNAVAILABLE' : 'INVALID_ATTACHMENT', message: code };
+ const diagnostic = chatErrorDiagnostic(error);
+ const { code } = diagnostic;
+ deps.log?.warn('Chat attachment preparation failed', { groupId: input.groupId, stage: 'prepare', ...diagnostic });
+ return { ok: false, errorCode: code.startsWith('FILE_PEER_') || code.startsWith('DEVICE_LINK_') || code.startsWith('OSS_DOWNLOAD_') ? 'ATTACHMENT_UNAVAILABLE' : 'INVALID_ATTACHMENT', message: code };
}
return {
ok: true,
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 4481bb8205..25980074ee 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -10481,7 +10481,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
setBotRemoteMessageService(botDirectMessageServiceHolder);
botGroupChatServiceHolder?.dispose();
// 分工 steps run in the group's folder, the 项目文件夹, or one worktree per plan (bot-group-chat.md §7.5).
- const botGroupAttachments = createBotGroupAttachmentStore({ ownerRoot: () => ownerScopedUserDataPath() });
+ const botGroupAttachments = createBotGroupAttachmentStore({ ownerRoot: () => ownerScopedUserDataPath(), log });
const botGroupWorkDir = createBotGroupWorkDir({
ownerRoot: () => ownerScopedUserDataPath(),
detectRepo: async (dir) => {