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): 固定图片准备权限快照并明确 BMP 预览降级

Dash44c2ad0237c6
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/collabSendOutcome.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/collabSendOutcome.test.ts
index 0647c413c2..a44ad26bea 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/collabSendOutcome.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/collabSendOutcome.test.ts
@@ -198,7 +198,7 @@ describe('collab send outcome semantics', () => {
// Unit: exercise the real provider callback with the existing media importer
// as its boundary. Missing sent-image previews and stale-task publication were
// not covered by the channel transport tests.
- it.each(['ready', 'unavailable', 'non-image', 'remote'] as const)('prepares outbound chat images with task ownership: %s', async (state) => {
+ it.each(['ready', 'unavailable', 'non-image', 'unsupported-image', 'remote'] as const)('prepares outbound chat images with task ownership: %s', async (state) => {
mockState.publishImage.mockReset().mockResolvedValue(state === 'unavailable'
? { ok: false, errorCode: 'MEDIA_PUBLISH_FAILED' }
: { ok: true, url: `cindy-media://blobs/${'a'.repeat(64)}.png` });
@@ -206,16 +206,20 @@ describe('collab send outcome semantics', () => {
createDesktopMcpProviders({
botCapabilities, getMakerMemoryManager: vi.fn(), lspPool: {} as never,
pluginRegistry: { isEnabled: () => true, getPlugins: () => [] } as never,
- invokeRemote: vi.fn(), getLiveSessionGrantState: () => live ? {} as never : null,
+ invokeRemote: vi.fn(), getLiveSessionGrantState: () => ({
+ permissionMode: 'auto', remoteHostId: null, isCurrent: () => live,
+ }),
});
const prepare = mockState.capturedProvidersConfig!.prepareOutboundImage as NonNullable<CreateLiziMcpProvidersOptions['prepareOutboundImage']>;
- const source = path.join(path.sep, 'task', state === 'non-image' ? 'report.pdf' : 'photo.png');
+ const source = path.join(path.sep, 'task', state === 'non-image' ? 'report.pdf'
+ : state === 'unsupported-image' ? 'photo.BMP' : 'photo.png');
const prepared = await prepare(source, { agentKind: 'codex', workingDir: path.dirname(source),
sessionId: 'image-task', sessionInstanceId: 'runtime', ...(state === 'remote' ? { remoteHostId: 'ssh-host' } : {}) });
- if (state === 'non-image' || state === 'remote') {
+ if (state === 'non-image' || state === 'remote' || state === 'unsupported-image') {
expect(mockState.publishImage).not.toHaveBeenCalled();
expect(prepared.path).toBe(source);
expect(prepared.url).toBeUndefined();
+ if (state === 'unsupported-image') expect(prepared.errorCode).toBe('IMAGE_PREVIEW_UNSUPPORTED');
return;
}
expect(mockState.publishImage).toHaveBeenCalledWith(source,
@@ -230,6 +234,30 @@ describe('collab send outcome semantics', () => {
expect(prepared.assertCurrent).toThrow('SESSION_SCOPE_CHANGED');
});
+ // The live authority API returns a fresh snapshot on every call. Reacquiring
+ // one after I/O would silently authorize a new permission/Plan generation.
+ it('pins the permission generation before image preparation first yields', async () => {
+ let generation = 0;
+ mockState.publishImage.mockReset().mockResolvedValue({ ok: false, errorCode: 'PERMISSION_DENIED' });
+ createDesktopMcpProviders({
+ botCapabilities, getMakerMemoryManager: vi.fn(), lspPool: {} as never,
+ pluginRegistry: { isEnabled: () => true, getPlugins: () => [] } as never,
+ invokeRemote: vi.fn(), getLiveSessionGrantState: () => {
+ const captured = generation;
+ return { permissionMode: 'auto', remoteHostId: null, isCurrent: () => captured === generation };
+ },
+ });
+ const prepare = mockState.capturedProvidersConfig!.prepareOutboundImage as NonNullable<CreateLiziMcpProvidersOptions['prepareOutboundImage']>;
+ const preparing = prepare(path.join(path.sep, 'task', 'photo.png'), {
+ agentKind: 'codex', workingDir: path.join(path.sep, 'task'),
+ sessionId: 'image-task', sessionInstanceId: 'runtime',
+ });
+ generation++;
+ const prepared = await preparing;
+ expect(mockState.publishImage.mock.calls[0]![2].isCurrent()).toBe(false);
+ expect(prepared.assertCurrent).toThrow('SESSION_SCOPE_CHANGED');
+ });
+
afterEach(() => {
mockState.logger.warn.mockClear();
mockState.logger.info.mockClear();
diff --git a/apps/desktop/src/main/mcp-integrations/mcp-providers.ts b/apps/desktop/src/main/mcp-integrations/mcp-providers.ts
index 9faeb703c6..0332228af3 100644
--- a/apps/desktop/src/main/mcp-integrations/mcp-providers.ts
+++ b/apps/desktop/src/main/mcp-integrations/mcp-providers.ts
@@ -338,9 +338,14 @@ export function createDesktopMcpProviders(deps: DesktopMcpProvidersDeps): LiziMc
const prepareOutboundImage = async (sourcePath: string, context: LiziMcpSessionContext): Promise<PreparedOutboundImage> => {
const owner = getActiveAppSession();
const scope = readChannelToolScope(context);
- const { mimeForExt, resolveSafe } = await import('../cindy-media/blobStore.js');
- if (!mimeForExt(extname(sourcePath).toLowerCase())?.startsWith('image/')) return { path: sourcePath };
const { sessionId, sessionInstanceId, workingDir } = context;
+ // Keep the original permission/Plan generation across all preparation I/O.
+ const grant = sessionId && sessionInstanceId
+ ? deps.getLiveSessionGrantState?.(sessionId, sessionInstanceId) : null;
+ const { mimeForExt, resolveSafe } = await import('../cindy-media/blobStore.js');
+ const ext = extname(sourcePath).toLowerCase();
+ const supportedImage = mimeForExt(ext)?.startsWith('image/');
+ if (!supportedImage && ext !== '.bmp') return { path: sourcePath };
if (!sessionId || !sessionInstanceId || !workingDir || context.remoteHostId) {
return { path: sourcePath, errorCode: 'IMAGE_PREVIEW_UNAVAILABLE' };
}
@@ -348,10 +353,11 @@ export function createDesktopMcpProviders(deps: DesktopMcpProvidersDeps): LiziMc
const current = getActiveAppSession();
return !isAppSessionBoundaryPending()
&& current.dataOwnerId === owner.dataOwnerId && current.generation === owner.generation
- && !!deps.getLiveSessionGrantState?.(sessionId, sessionInstanceId)
+ && !!grant && grant.isCurrent?.() !== false
&& (!scope || readChannelToolScope(context) === scope);
};
const assertCurrent = () => { if (!isCurrent()) throw new Error('SESSION_SCOPE_CHANGED'); };
+ if (!supportedImage) return { path: sourcePath, errorCode: 'IMAGE_PREVIEW_UNSUPPORTED', assertCurrent };
try {
const { publishImage } = await import('../cindy-media/publishImage.js');
const image = await publishImage(sourcePath, { sessionId, workingDir }, {
diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.md
index 19d2e25b4d..3efb52e38c 100644
--- a/docs/dev-rules/media-storage-and-protocols.md
+++ b/docs/dev-rules/media-storage-and-protocols.md
@@ -284,6 +284,9 @@
个人 IM 与官方 hook 的工具媒体收集器跳过自动转发,该标记不影响聊天展示。
`sendFileWithImagePreview` 为具名发送工具提供共用适配;已接飞书/Lark 的 `send_file`、
`send_file_to_user`,微信 `send_file_to_user`,Telegram `send_media` 的本地 `photo`。
+ 预览沿用媒体仓支持的 PNG / JPEG / GIF / WebP;渠道可发但媒体仓尚不支持的 BMP
+ 仍按原渠道发送,并返回 `imagePreviewError: IMAGE_PREVIEW_UNSUPPORTED`,不静默当作普通文件。
+ 权限/Plan 的当前性判据在首次异步操作前捕获,入仓与发送前均校验同一代次。
普通文件、Telegram URL / file_id 与任意底层 `call_api` 保持既有行为,不推测媒体字节。
新字段为可选返回字段,旧工具和未注入宿主回调的调用保持原样,旧客户端仍识别既有图片字段。
预览准备失败不把渠道发送成功改报失败:结果保留真实回执并附 `imagePreviewError`;渠道发送