Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(media): 修复视频下载预算和连接变化恢复
nana60c981c4eefe
diff --git a/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts b/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.tsindex f566cc7ae4..c197402a1d 100644--- a/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts+++ b/apps/desktop/src/main/cindy-media/__tests__/invocationService.test.ts@@ -462,16 +462,76 @@ describe('Cindy Core media invocation state and security boundary', () => {expect(mocks.outboundFetch).toHaveBeenCalledTimes(1);});- it('changing the custom source after paid dispatch makes outcome unknown and prevents another POST', async () => {+ it.each(['dispatch', 'response-body'] as const)('preserves a paid task when the source changes during %s and recovers only through the original connection', async boundary => {+ const connection = setupCustomVideo();+ const p = await callCindyMedia({ action: 'prepare', providerId: 'relay', modelId: 'private-video', capability: 'video.generate' });+ const invocationId = p.invocation_id as string;+ const changeSource = () => {+ connection.assertCurrent.mockImplementation(() => { throw new Error('source changed'); });+ mocks.videoConnection.mockReturnValue({ ...connection, revision: '1:changed' });+ };+ mocks.outboundFetch.mockImplementationOnce(async () => {+ if (boundary === 'dispatch') {+ changeSource();+ return Response.json({ request_id: 'paid-task' });+ }+ return new Response(new ReadableStream({+ start(controller) {+ controller.enqueue(new TextEncoder().encode('{"request_id":"paid-task"}'));+ controller.close();+ },+ }).pipeThrough(new TransformStream({ transform(chunk, controller) {+ changeSource();+ controller.enqueue(chunk);+ } })));+ });+ expect(await callCindyMedia({ action: 'request', invocationId, body: { prompt: 'cat' } })).toMatchObject({ errorCode: 'CONNECTION_CHANGED', retryable: false });+ expect(mocks.rows.get(invocationId)).toMatchObject({ state: 'pending', taskId: 'paid-task', guideRevision: '1:original' });+ expect(await callCindyMedia({ action: 'poll', invocationId })).toMatchObject({ errorCode: 'CONNECTION_CHANGED' });+ expect(mocks.outboundFetch).toHaveBeenCalledTimes(1);+ connection.assertCurrent.mockReset();+ mocks.videoConnection.mockReturnValue(connection);+ expect(await callCindyMedia({ action: 'request', invocationId, body: { prompt: 'cat' } })).toMatchObject({ errorCode: 'INVOCATION_ALREADY_USED' });+ mocks.outboundFetch.mockResolvedValueOnce(Response.json({ status: 'done', video: { url: '/videos/paid-task/content' } }))+ .mockResolvedValueOnce(new Response(MP4, { headers: { 'content-type': 'video/mp4' } }));+ mocks.ingestMedia.mockResolvedValueOnce({ url: `cindy-media://blobs/${'a'.repeat(64)}.mp4` });+ expect(await callCindyMedia({ action: 'poll', invocationId })).toMatchObject({ status: 'complete' });+ expect(mocks.outboundFetch.mock.calls.filter(([, init]) => init.method === 'POST')).toHaveLength(1);+ expect(mocks.outboundFetch.mock.calls.slice(1).map(([url]) => url)).toEqual([+ 'https://relay.example/v1/videos/paid-task', 'https://relay.example/v1/videos/paid-task/content',+ ]);+ });++ it('restores prepared when the connection changes after claim but before the paid POST', async () => {+ const connection = setupCustomVideo();+ const p = await callCindyMedia({ action: 'prepare', providerId: 'relay', modelId: 'private-video', capability: 'video.generate' });+ const invocationId = p.invocation_id as string;+ mocks.onClaim.mockImplementationOnce(() => {+ connection.assertCurrent.mockImplementation(() => { throw new Error('source changed'); });+ });+ expect(await callCindyMedia({ action: 'request', invocationId, body: { prompt: 'cat' } })).toMatchObject({+ errorCode: 'CONNECTION_CHANGED', retryable: true, outcomeKnown: true,+ });+ expect(mocks.rows.get(invocationId)?.state).toBe('prepared');+ expect(mocks.outboundFetch).not.toHaveBeenCalled();+ connection.assertCurrent.mockReset();+ mocks.outboundFetch.mockResolvedValueOnce(Response.json({ request_id: 'paid-task' }));+ expect(await callCindyMedia({ action: 'request', invocationId, body: { prompt: 'cat' } })).toMatchObject({ status: 'pending' });+ expect(mocks.rows.get(invocationId)).toMatchObject({ state: 'pending', taskId: 'paid-task' });+ expect(mocks.outboundFetch).toHaveBeenCalledTimes(1);+ });++ it('retains unknown for a dispatched request without a valid task response even when the source changes', async () => {const connection = setupCustomVideo();const p = await callCindyMedia({ action: 'prepare', providerId: 'relay', modelId: 'private-video', capability: 'video.generate' });const invocationId = p.invocation_id as string;mocks.outboundFetch.mockImplementationOnce(async () => {connection.assertCurrent.mockImplementation(() => { throw new Error('source changed'); });- return Response.json({ request_id: 'paid-task' });+ return new Response('invalid JSON');});expect(await callCindyMedia({ action: 'request', invocationId, body: { prompt: 'cat' } })).toMatchObject({ errorCode: 'SUBMISSION_OUTCOME_UNKNOWN' });expect(mocks.rows.get(invocationId)?.state).toBe('unknown');+ connection.assertCurrent.mockReset();expect(await callCindyMedia({ action: 'request', invocationId, body: { prompt: 'cat' } })).toMatchObject({ errorCode: 'INVOCATION_ALREADY_USED' });expect(mocks.outboundFetch).toHaveBeenCalledTimes(1);});diff --git a/apps/desktop/src/main/cindy-media/__tests__/videoTaskContent.test.ts b/apps/desktop/src/main/cindy-media/__tests__/videoTaskContent.test.tsnew file mode 100644index 0000000000..9bd08f54ea--- /dev/null+++ b/apps/desktop/src/main/cindy-media/__tests__/videoTaskContent.test.ts@@ -0,0 +1,68 @@+import fs from 'node:fs/promises';+import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from 'vitest';+import { fetchVideoTaskContent } from '../videoTaskContent.js';++const fetch = vi.hoisted(() => vi.fn());+vi.mock('../../maker-host/outbound-fetch.js', () => ({ outboundFetch: fetch }));++const reserve = 256n * 1024n * 1024n;+const connection = { baseUrl: 'https://relay.example/v1', headers: { authorization: 'Bearer test-key' }, revision: 'original', assertCurrent: vi.fn() };++describe('private video task download disk budget', () => {+ let directories: MockInstance<typeof fs.mkdtemp>;+ beforeEach(() => {+ fetch.mockReset();+ directories = vi.spyOn(fs, 'mkdtemp');+ vi.spyOn(fs, 'statfs').mockResolvedValue({ bavail: reserve + 16n, bsize: 1n } as never);+ });+ afterEach(async () => {+ for (const result of directories.mock.results) {+ if (result.type === 'return') await fs.rm(await result.value, { recursive: true, force: true });+ }+ vi.restoreAllMocks();+ });++ it.each([undefined, '1'])('counts actual streamed bytes with content-length=%s and cancels, cleans up, and does not retry overflow', async contentLength => {+ const cancel = vi.fn();+ const chunks = [new Uint8Array(4), new Uint8Array(5)];+ fetch.mockResolvedValue(new Response(new ReadableStream({+ pull(controller) {+ if (chunks.length) controller.enqueue(chunks.shift()!);+ else controller.close();+ },+ cancel,+ }, { highWaterMark: 0 }), { headers: contentLength ? { 'content-length': contentLength } : {} }));+ await expect(fetchVideoTaskContent(connection, 'paid-task')).rejects.toMatchObject({ code: 'MEDIA_DOWNLOAD_UNAVAILABLE' });+ expect(fetch).toHaveBeenCalledTimes(1);+ expect(cancel).toHaveBeenCalledTimes(1);+ expect(chunks).toHaveLength(0);+ await expect(fs.stat(await directories.mock.results[0]!.value)).rejects.toMatchObject({ code: 'ENOENT' });+ });++ it('rechecks available disk space before writing when another writer consumes the reserve', async () => {+ vi.mocked(fs.statfs).mockResolvedValueOnce({ bavail: reserve + 16n, bsize: 1n } as never)+ .mockResolvedValue({ bavail: reserve + 1n, bsize: 1n } as never);+ fetch.mockResolvedValue(new Response(new Uint8Array(2)));+ await expect(fetchVideoTaskContent(connection, 'paid-task')).rejects.toMatchObject({ code: 'MEDIA_DOWNLOAD_UNAVAILABLE' });+ expect(fetch).toHaveBeenCalledTimes(1);+ await expect(fs.stat(await directories.mock.results[0]!.value)).rejects.toMatchObject({ code: 'ENOENT' });+ });++ it('fails without retry when disk capacity cannot be determined', async () => {+ vi.mocked(fs.statfs).mockRejectedValue(new Error('statfs unavailable'));+ fetch.mockResolvedValue(new Response(new Uint8Array(2)));+ await expect(fetchVideoTaskContent(connection, 'paid-task')).rejects.toMatchObject({ code: 'MEDIA_DOWNLOAD_UNAVAILABLE' });+ expect(fetch).toHaveBeenCalledTimes(1);+ });++ it('allows a complete stream within the budget and disposes the private file', async () => {+ const content = new Uint8Array(8).fill(7);+ fetch.mockResolvedValue(new Response(content, { headers: { 'content-type': 'video/mp4' } }));+ const result = await fetchVideoTaskContent(connection, 'paid-task');+ expect(await fs.readFile(result.filePath)).toEqual(Buffer.from(content));+ expect(result.headerMime).toBe('video/mp4');+ await result.dispose();+ await expect(fs.stat(result.filePath)).rejects.toMatchObject({ code: 'ENOENT' });+ expect(fetch).toHaveBeenCalledTimes(1);+ });+});diff --git a/apps/desktop/src/main/cindy-media/invocationService.ts b/apps/desktop/src/main/cindy-media/invocationService.tsindex 27eb43565f..26529b9897 100644--- a/apps/desktop/src/main/cindy-media/invocationService.ts+++ b/apps/desktop/src/main/cindy-media/invocationService.ts@@ -525,7 +525,9 @@ async function dispatchRequest(input: {durationMs: Date.now() - startedAt,});const buffer = await readBoundedResponse(response, input.maxResponseBytes);- input.connection.assertCurrent?.();+ // A successful paid response belongs to the original invocation even if+ // its connection changed in flight. Save it before guarding later reads.+ if (input.operation === 'poll') input.connection.assertCurrent?.();if (!response.ok) {const message = providerErrorMessage(buffer);if (response.status >= 500) {@@ -1537,6 +1539,7 @@ async function submitInvocation('上游结果已生成,但本地未能保存结果;不要自动重提',);}+ connection.assertCurrent?.();assertAuthScope(scope, invocation.owner);return materializeSyncInvocation({ ...invocation, state: 'pending', responseJson },@@ -1596,6 +1599,7 @@ async function submitInvocation('上游任务已创建,但本地未能保存任务 id;不要自动重提',);}+ connection.assertCurrent?.();assertAuthScope(scope, invocation.owner);return {ok: true,@@ -1605,21 +1609,22 @@ async function submitInvocation(};} catch (error) {const expected = error instanceof MediaInvocationError ? error : null;- // 本地组装失败或提交前模型已停用:请求从未出站、无费用,把 invocation 放回 prepared,+ // 本地组装失败、提交前模型停用或连接变化:请求从未出站、无费用,把 invocation 放回 prepared,// 让同一 invocation_id 可以直接再次 request;其余失败仍进入 failed / unknown。- const buildFailed = expected?.code === 'REQUEST_BUILD_FAILED' || expected?.code === 'MODEL_NOT_AVAILABLE';+ // 已保存结果为 pending 时,submitting 条件更新不匹配,不回退已付费任务。+ const notSubmitted = expected?.code === 'REQUEST_BUILD_FAILED' || expected?.code === 'MODEL_NOT_AVAILABLE' || expected?.code === 'CONNECTION_CHANGED';const restored = await transitionMediaInvocation({id: invocation.id,owner: invocation.owner,from: 'submitting',- to: buildFailed ? 'prepared' : expected?.outcomeUnknown ? 'unknown' : 'failed',+ to: notSubmitted ? 'prepared' : expected?.outcomeUnknown ? 'unknown' : 'failed',},db,).catch(() => false);if (expected) {if (expected.outcomeUnknown) return submissionOutcomeUnknown(expected.message);- if (buildFailed && restored) {+ if (notSubmitted && restored) {return failure(expected.code, `${expected.message};同一 invocation_id 可直接再次 request`, true, {outcomeKnown: true,});diff --git a/apps/desktop/src/main/cindy-media/videoTaskContent.ts b/apps/desktop/src/main/cindy-media/videoTaskContent.tsindex 63682d0bba..f5def7064d 100644--- a/apps/desktop/src/main/cindy-media/videoTaskContent.ts+++ b/apps/desktop/src/main/cindy-media/videoTaskContent.ts@@ -5,6 +5,17 @@ import { outboundFetch } from '../maker-host/outbound-fetch.js';import { MediaDownloadError } from './mediaDownload.js';import type { ProviderVideoConnection } from './providerMediaRuntime.js';+const DISK_RESERVE_BYTES = 256n * 1024n * 1024n;++async function availableDownloadBytes(directory: string): Promise<bigint> {+ try {+ const space = await fs.statfs(directory, { bigint: true });+ return space.bavail * space.bsize - DISK_RESERVE_BYTES;+ } catch {+ throw new MediaDownloadError('MEDIA_DOWNLOAD_UNAVAILABLE', '无法确认视频临时文件的可用磁盘空间,生成结果已保留');+ }+}+/** A private task resource, never an arbitrary URL authorized to receive an API key. */export function isVideoTaskContentRef(raw: string, taskId: string, baseUrl: string): boolean {if (!/^[a-zA-Z0-9_-]{1,256}$/.test(taskId)) return false;@@ -48,6 +59,10 @@ export async function fetchVideoTaskContent(if (!response.ok) throw new MediaDownloadError(response.status >= 500 || [408, 425, 429].includes(response.status) ? 'MEDIA_DOWNLOAD_FAILED' : 'MEDIA_DOWNLOAD_UNAVAILABLE', `原视频任务的内容接口返回 HTTP ${response.status},生成结果已保留`);if (!response.body) throw new MediaDownloadError('MEDIA_RESULT_INVALID', '视频内容为空');tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-media-video-task-'));+ // Bound actual streamed bytes by disk capacity, preserving room for the+ // managed-store copy. Do not restore the old in-memory media size limit.+ const maxBytes = (await availableDownloadBytes(tempDir)) / 2n;+ let writtenBytes = 0n;const filePath = path.join(tempDir, 'result');const file = await fs.open(filePath, 'wx', 0o600);const reader = response.body.getReader();@@ -58,9 +73,15 @@ export async function fetchVideoTaskContent(connection.assertCurrent();signal?.throwIfAborted();resetIdle();+ const chunkBytes = BigInt(value.byteLength);+ const nextBytes = writtenBytes + chunkBytes;+ if (nextBytes > maxBytes || nextBytes + chunkBytes > await availableDownloadBytes(tempDir)) {+ throw new MediaDownloadError('MEDIA_DOWNLOAD_UNAVAILABLE', '视频下载所需空间超过可用磁盘预算,生成结果已保留;请清理空间后读取原任务,不要重新生成');+ }await file.writeFile(value);+ writtenBytes = nextBytes;}- } finally { reader.releaseLock(); await file.close(); }+ } finally { await reader.cancel().catch(() => undefined); reader.releaseLock(); await file.close(); }connection.assertCurrent();signal?.throwIfAborted();const completedDir = tempDir;diff --git a/docs/custom-video-api.md b/docs/custom-video-api.mdindex 4b5f32f9da..b2b059a15d 100644--- a/docs/custom-video-api.md+++ b/docs/custom-video-api.md@@ -48,12 +48,17 @@ OpenAI 官方 Sora Videos API 已于 2026-09-24 停止服务,本次不新增若结果是 `/videos/<原任务ID>/content`,只读取原连接下由已保存任务 ID 构造的固定内容接口,使用原连接凭证,不解释任意 URL 为可带 Key 的地址,不跟随重定向。此 API 资源读取同样分块落私有临时文件、校验归属、MIME 后入仓,并最多重试同一个 GET 三次。+ 写入按实际累计字节受可用磁盘预算限制,预留 256 MiB 和入库副本空间;写入前复查剩余空间。+ 超预算或无法探测空间时取消读取、清理临时文件,不自动重试下载,原任务结果保留。- 调用快照保存连接的不可逆绑定摘要,不保存 Key/请求头;跨客户端重启保持绑定。地址、Key、来源引擎或业务请求头变化时停止旧任务请求,恢复原连接后可查询原任务。- 一次付费提交保存任务 ID,之后只查询它。完成响应先持久化,下载或入库恢复不重新生成;签名地址过期时只刷新原任务。提交结果未知时禁止自动重提。+ 即使等待响应期间连接变化,已返回的有效任务 ID 仍先保存到原调用与原连接绑定下;+ 查询与下载继续要求原连接,恢复原连接即可继续读取,不向替换连接发送请求。- 新付费请求在参考图读取和调用占用完成后,实际发出 POST 前再次检查模型、来源及能力。- 若已停用则不提交,保留原 prepared 调用;已付费任务的查询与结果恢复不受展示/停用开关影响。+ 若已停用或连接已改变则不提交,保留原 prepared 调用;恢复原配置后可继续使用同一调用。+ 已付费任务的查询与结果恢复不受展示/停用开关影响。视频参数严格检查类型;受管参考图展开后再次校验图片 MIME,视频或音频不能作为图片提交。- 新来源使用 Core 媒体能力;冻结的旧 `cindy-request` 视频入口不扩展。- 出现在目录或通过接口探测不等于真实出片。验收需分别从 Art 执行两种操作,