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(plugins): type download events and stop policy retries

Dash43e44b69ade0
diff --git a/apps/desktop/src/main/downloader/__tests__/streaming.test.ts b/apps/desktop/src/main/downloader/__tests__/streaming.test.ts
index f7742b9846..e952a0503a 100644
--- a/apps/desktop/src/main/downloader/__tests__/streaming.test.ts
+++ b/apps/desktop/src/main/downloader/__tests__/streaming.test.ts
@@ -7,6 +7,7 @@ const mocks = vi.hoisted(() => ({ fetch: vi.fn() }));
vi.mock('electron', () => ({ net: { fetch: mocks.fetch } }));
import { executeStreaming } from '../streaming';
import { writeMeta } from '../resume';
+import { withRetry } from '../retry';
afterEach(() => vi.restoreAllMocks());
@@ -66,22 +67,80 @@ it('validates each redirect and never sends credentials', async () =>
expect(request).toMatchObject({ credentials: 'omit', redirect: 'manual' });
}));
-it('rejects a forbidden redirect before fetching it', async () =>
+it('rejects a forbidden redirect before fetching it without retrying the original URL', async () =>
fixture(async (_, opts) => {
mocks.fetch
.mockReset()
.mockResolvedValue(
new Response(null, { status: 302, headers: { location: 'http://127.0.0.1/private' } }),
);
+ const onRetry = vi.fn();
await expect(
- execute({
- ...opts,
- validateUrl: (u: string) => {
- if (u !== opts.url) throw Error('denied');
- },
+ withRetry(
+ () =>
+ execute({
+ ...opts,
+ validateUrl: (u: string) => {
+ if (u !== opts.url) throw Error('denied');
+ },
+ }),
+ { logger: {}, onRetry, config: { baseDelayMs: 0, maxDelayMs: 0 } },
+ ),
+ ).rejects.toMatchObject({ code: 'INVALID_ARG' });
+ expect(mocks.fetch).toHaveBeenCalledTimes(1);
+ expect(onRetry).not.toHaveBeenCalled();
+ }));
+
+it('does not retry malformed redirect targets or a rejected initial URL', async () =>
+ fixture(async (_, opts) => {
+ const onRetry = vi.fn();
+ mocks.fetch
+ .mockReset()
+ .mockResolvedValue(
+ new Response(null, { status: 302, headers: { location: 'https://[invalid' } }),
+ );
+ await expect(
+ withRetry(() => execute(opts), {
+ logger: {},
+ onRetry,
+ config: { baseDelayMs: 0, maxDelayMs: 0 },
}),
- ).rejects.toThrow();
+ ).rejects.toMatchObject({ code: 'INVALID_ARG' });
expect(mocks.fetch).toHaveBeenCalledTimes(1);
+ expect(onRetry).not.toHaveBeenCalled();
+ mocks.fetch.mockClear();
+ await expect(
+ withRetry(
+ () =>
+ execute({
+ ...opts,
+ validateUrl: () => {
+ throw Error('denied');
+ },
+ }),
+ { logger: {}, onRetry },
+ ),
+ ).rejects.toMatchObject({ code: 'INVALID_ARG' });
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ expect(onRetry).not.toHaveBeenCalled();
+ }));
+
+it('still retries a transient network failure', async () =>
+ fixture(async (_, opts) => {
+ const onRetry = vi.fn();
+ mocks.fetch
+ .mockReset()
+ .mockRejectedValueOnce(new Error('connection reset'))
+ .mockResolvedValueOnce(new Response('abcdef'));
+ await expect(
+ withRetry(() => execute(opts), {
+ logger: {},
+ onRetry,
+ config: { baseDelayMs: 0, maxDelayMs: 0 },
+ }),
+ ).resolves.toMatchObject({ size: 6 });
+ expect(mocks.fetch).toHaveBeenCalledTimes(2);
+ expect(onRetry).toHaveBeenCalledTimes(1);
}));
it('cleans both sidecars on oversized and short bodies', async () =>
diff --git a/apps/desktop/src/main/downloader/streaming.ts b/apps/desktop/src/main/downloader/streaming.ts
index 553d76355e..fa9a3ff0b0 100644
--- a/apps/desktop/src/main/downloader/streaming.ts
+++ b/apps/desktop/src/main/downloader/streaming.ts
@@ -56,7 +56,12 @@ export async function executeStreaming(ctx: TransportContext): Promise<Transport
let response: Response;
for (let hop = 0; ; hop++) {
if (controller.signal.aborted) throw new DownloadError('ABORTED', 'Download cancelled');
- opts.validateUrl?.(url);
+ try {
+ opts.validateUrl?.(url);
+ } catch {
+ // A caller policy refusal cannot be repaired by retrying the same URL.
+ throw new DownloadError('INVALID_ARG', 'Download URL is not allowed');
+ }
arm(opts.timeout?.connectMs ?? 10_000);
response = await net.fetch(url, {
redirect: 'manual',
@@ -77,7 +82,11 @@ export async function executeStreaming(ctx: TransportContext): Promise<Transport
await response.body?.cancel();
const location = response.headers.get('location');
if (!location || hop >= 9) throw new DownloadError('INVALID_ARG', 'Invalid redirect');
- url = new URL(location, url).href;
+ try {
+ url = new URL(location, url).href;
+ } catch {
+ throw new DownloadError('INVALID_ARG', 'Invalid redirect URL');
+ }
}
if (!response.ok || !response.body) {
await response.body?.cancel();
diff --git a/apps/desktop/src/shared/__tests__/ghost.downloadProgress.test.ts b/apps/desktop/src/shared/__tests__/ghost.downloadProgress.test.ts
new file mode 100644
index 0000000000..ee3b5236f1
--- /dev/null
+++ b/apps/desktop/src/shared/__tests__/ghost.downloadProgress.test.ts
@@ -0,0 +1,8 @@
+import { expectTypeOf, it } from 'vitest';
+import type { GhostPipeDownloadProgress, GhostPipeEventPush } from '../ghost';
+
+it('narrows download progress through the public host event union', () => {
+ expectTypeOf<
+ Extract<GhostPipeEventPush, { name: 'download-progress' }>
+ >().toEqualTypeOf<GhostPipeDownloadProgress>();
+});
diff --git a/apps/desktop/src/shared/ghost.ts b/apps/desktop/src/shared/ghost.ts
index d4a9f86e26..ec509f713a 100644
--- a/apps/desktop/src/shared/ghost.ts
+++ b/apps/desktop/src/shared/ghost.ts
@@ -7935,6 +7935,7 @@ export type GhostMessageHookData = { sessionId: string; text: string; model?: st
* GhostPipeEventVerdict,不回视为放行。
*/
export type GhostPipeEventPush =
+ | GhostPipeDownloadProgress
| {
type: 'event';
name: GhostDidEventName;