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(media): 修复桌面与手机之间的截图交付 (#5327)

DavidShenXDd984b60934ed
diff --git a/apps/desktop/src/main/cindy-media/__tests__/generatedMedia.test.ts b/apps/desktop/src/main/cindy-media/__tests__/generatedMedia.test.ts
index c31eff5a8a..518d70de8a 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/generatedMedia.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/generatedMedia.test.ts
@@ -147,6 +147,7 @@ describe('createBlobVideoStorage.saveVideo(生成视频入仓)', () => {
describe('materializeGeneratedImage(codex 生成图物化,thin adapter 的逻辑本体)', () => {
const deps = {
+ verifyManagedUrl: vi.fn(async (_url: string) => {}),
ingestFromPath: vi.fn(async ({ originalName }: { sourcePath: string; originalName?: string }) => ({
url: `cindy-media://blobs/${'b'.repeat(64)}.png`,
filename: originalName ?? 'x.png',
@@ -158,11 +159,12 @@ describe('materializeGeneratedImage(codex 生成图物化,thin adapter 的逻辑
};
beforeEach(() => {
+ deps.verifyManagedUrl.mockReset();
deps.ingestFromPath.mockClear();
deps.ingestBuffer.mockClear();
});
- it('托管地址(老 xdt-image / 新 cindy-media)原样透传,不重复入仓', async () => {
+ it('托管地址核验后复用,不重复入仓', async () => {
const legacy = await generatedMedia.materializeGeneratedImage(
{ url: 'xdt-image://sess-1/pic.png' },
deps,
@@ -176,6 +178,17 @@ describe('materializeGeneratedImage(codex 生成图物化,thin adapter 的逻辑
expect(blob?.filename).toBe(`${'d'.repeat(64)}.png`);
expect(deps.ingestFromPath).not.toHaveBeenCalled();
expect(deps.ingestBuffer).not.toHaveBeenCalled();
+ expect(deps.verifyManagedUrl).toHaveBeenCalledTimes(2);
+ });
+
+ it('拒绝不存在的受管地址;有实际文件时重新导入当前 Host', async () => {
+ const url = `cindy-media://blobs/${'e'.repeat(64)}.png`;
+ deps.verifyManagedUrl.mockRejectedValueOnce(new Error('missing'));
+ await expect(generatedMedia.materializeGeneratedImage({ url }, deps)).rejects.toThrow('missing');
+ expect(deps.ingestFromPath).not.toHaveBeenCalled();
+ deps.verifyManagedUrl.mockRejectedValueOnce(new Error('missing'));
+ await expect(generatedMedia.materializeGeneratedImage({ url, path: '/tmp/actual.png' }, deps)).resolves.toMatchObject({ filename: 'actual.png' });
+ expect(deps.ingestFromPath).toHaveBeenCalledWith({ sourcePath: '/tmp/actual.png', originalName: 'actual.png' });
});
it('本地路径 → ingestFromPath;data: base64 → ingestBuffer(mime 从 data url 头取)', async () => {
diff --git a/apps/desktop/src/main/cindy-media/__tests__/publishImage.test.ts b/apps/desktop/src/main/cindy-media/__tests__/publishImage.test.ts
new file mode 100644
index 0000000000..60deb9a77b
--- /dev/null
+++ b/apps/desktop/src/main/cindy-media/__tests__/publishImage.test.ts
@@ -0,0 +1,140 @@
+import fs from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+vi.mock('@cindy/mcps', async () => import('../../../../../../packages/lizi-mcps/src/session-path-auth.js'));
+vi.mock('../../cindy-brain/dirDeposit.js', () => ({ isPathInsideDir: (root: string, file: string) => {
+ const relative = path.relative(root, file);
+ return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
+} }));
+vi.mock('../blobStore.js', () => ({ mimeForExt: (ext: string) => ext === '.png' ? 'image/png' : null }));
+const ingest = vi.hoisted(() => vi.fn());
+const hasRef = vi.hoisted(() => vi.fn());
+const owner = vi.hoisted(() => ({ dataOwnerId: 'owner', generation: 1 }));
+vi.mock('../ingest.js', () => ({ ingestMedia: ingest }));
+vi.mock('../ledger.js', () => ({ hasRef }));
+vi.mock('../refCompensationJournal.js', () => ({ captureMediaRefCompensationScope: () => ({}) }));
+vi.mock('../../localDb/client/current.js', () => ({ getDbClient: () => ({ drizzle: 'captured-db' }) }));
+vi.mock('../../appSessionState.js', () => ({ getActiveAppSession: () => ({ ...owner }), isAppSessionBoundaryPending: () => false }));
+import { publishImage } from '../publishImage.js';
+
+let root: string;
+let workdir: string;
+const url = `cindy-media://blobs/${'a'.repeat(64)}.png`;
+const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+M8AAAMBAQDJ/pLvAAAAAElFTkSuQmCC', 'base64');
+beforeEach(async () => {
+ root = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-publish-image-'));
+ workdir = path.join(root, 'work');
+ await fs.mkdir(workdir);
+ ingest.mockReset().mockResolvedValue({ url, hash: 'a'.repeat(64), ext: '.png' });
+ hasRef.mockReset().mockResolvedValue(false);
+ owner.generation = 1;
+});
+afterEach(async () => { await fs.rm(root, { recursive: true, force: true }); });
+
+describe('publishImage', () => {
+ it('pins only once when the same task imports identical bytes concurrently', async () => {
+ await fs.writeFile(path.join(workdir, 'shot.png'), png);
+ let pinned = false;
+ let release!: () => void;
+ const gate = new Promise<void>((resolve) => { release = resolve; });
+ hasRef.mockImplementation(async () => pinned);
+ ingest.mockImplementation(async (params) => {
+ await gate;
+ if (params.refs.length) pinned = true;
+ return { url, hash: 'a'.repeat(64), ext: '.png' };
+ });
+ const imports = Array.from({ length: 8 }, () => publishImage('shot.png',
+ { workingDir: workdir, sessionId: 'task' }, { isCurrent: () => true, authorize: vi.fn() }));
+ try {
+ await vi.waitFor(() => expect(ingest).toHaveBeenCalledOnce());
+ expect(hasRef).toHaveBeenCalledOnce();
+ } finally { release(); }
+ expect((await Promise.all(imports)).every((result) => result.ok)).toBe(true);
+ expect(ingest.mock.calls.flatMap(([params]) => params.refs)).toHaveLength(1);
+ });
+
+ it('releases the reference lock after failure so a queued retry can pin', async () => {
+ await fs.writeFile(path.join(workdir, 'shot.png'), png);
+ ingest.mockRejectedValueOnce(new Error('failed'));
+ const results = await Promise.all(Array.from({ length: 2 }, () => publishImage('shot.png',
+ { workingDir: workdir, sessionId: 'task' }, { isCurrent: () => true, authorize: vi.fn() })));
+ expect(results.filter((result) => result.ok)).toHaveLength(1);
+ expect(results.filter((result) => result.errorCode === 'MEDIA_PUBLISH_FAILED')).toHaveLength(1);
+ expect(ingest.mock.calls[1][0].refs).toHaveLength(1);
+ });
+
+ it('does not share a pinned reference between tasks', async () => {
+ await fs.writeFile(path.join(workdir, 'shot.png'), png);
+ const results = await Promise.all(['first', 'second'].map((sessionId) => publishImage('shot.png',
+ { workingDir: workdir, sessionId }, { isCurrent: () => true, authorize: vi.fn() })));
+ expect(results.every((result) => result.ok)).toBe(true);
+ expect(ingest.mock.calls.flatMap(([params]) => params.refs.map((ref: { refId: string }) => ref.refId)).sort()).toEqual(['first', 'second']);
+ });
+
+ it('reads the actual file and pins the returned Host URL before reporting success', async () => {
+ const bytes = png;
+ await fs.writeFile(path.join(workdir, 'shot.png'), bytes);
+ const authorize = vi.fn();
+ const result = await publishImage('shot.png', { workingDir: workdir, sessionId: 'task' }, { isCurrent: () => true, authorize });
+ expect(result).toMatchObject({ ok: true, xdt_image_urls: [url] });
+ expect(ingest).toHaveBeenCalledWith(expect.objectContaining({ buffer: bytes, mimeType: 'image/png',
+ refs: [{ refKind: 'session-attachment', refId: 'task', originSessionId: 'task', originKind: 'tool' }],
+ assertStillValid: expect.any(Function), refCompensationScope: expect.any(Object),
+ }), 'captured-db');
+ expect(authorize).not.toHaveBeenCalled();
+ });
+
+ it('does not read outside-workdir images without the existing path grant', async () => {
+ const file = path.join(root, 'outside.png');
+ await fs.writeFile(file, 'outside');
+ const authorize = vi.fn(async () => ({ allowed: false as const, reason: 'denied' }));
+ expect(await publishImage(file, { workingDir: workdir, sessionId: 'task' }, { isCurrent: () => true, authorize })).toMatchObject({ errorCode: 'PERMISSION_DENIED' });
+ expect(authorize).toHaveBeenCalledWith(await fs.realpath(file));
+ expect(ingest).not.toHaveBeenCalled();
+ });
+
+ it('rejects missing files, stale task instances and SSH local-name fallback', async () => {
+ const context = { workingDir: workdir, sessionId: 'task' };
+ const deps = { isCurrent: () => true, authorize: vi.fn() };
+ expect(await publishImage('missing.png', context, deps)).toMatchObject({ errorCode: 'MEDIA_SOURCE_MISSING' });
+ expect(await publishImage('missing.png', context, { ...deps, isCurrent: () => false })).toMatchObject({ errorCode: 'PERMISSION_DENIED' });
+ expect(await publishImage('missing.png', { ...context, remoteHostId: 'ssh' }, deps)).toMatchObject({ errorCode: 'REMOTE_SOURCE_REQUIRED' });
+ expect(ingest).not.toHaveBeenCalled();
+ });
+
+ it('does not hand out a URL if reference registration fails', async () => {
+ await fs.writeFile(path.join(workdir, 'shot.png'), png);
+ ingest.mockRejectedValueOnce(new Error('ref registration failed'));
+ const result = await publishImage('shot.png', { workingDir: workdir, sessionId: 'task' }, { isCurrent: () => true, authorize: vi.fn() });
+ expect(result).toMatchObject({ ok: false, errorCode: 'MEDIA_PUBLISH_FAILED' });
+ expect(result).not.toHaveProperty('url');
+ });
+
+ it('reuses an existing task reference and guards every ingest await against account switches', async () => {
+ await fs.writeFile(path.join(workdir, 'shot.png'), png);
+ hasRef.mockResolvedValueOnce(true);
+ await publishImage('shot.png', { workingDir: workdir, sessionId: 'task' }, { isCurrent: () => true, authorize: vi.fn() });
+ expect(ingest.mock.calls[0][0].refs).toEqual([]);
+ const assertCurrent = ingest.mock.calls[0][0].assertStillValid;
+ expect(assertCurrent).not.toThrow();
+ owner.generation++;
+ expect(assertCurrent).toThrow('scope changed');
+ });
+
+ it('rejects text disguised as a PNG before ingesting', async () => {
+ await fs.writeFile(path.join(workdir, 'shot.png'), 'not an image');
+ const result = await publishImage('shot.png', { workingDir: workdir, sessionId: 'task' }, { isCurrent: () => true, authorize: vi.fn() });
+ expect(result).toMatchObject({ errorCode: 'INVALID_IMAGE' });
+ expect(ingest).not.toHaveBeenCalled();
+ });
+
+ it('rechecks grants after outside-workdir approval', async () => {
+ const file = path.join(root, 'outside.png');
+ await fs.writeFile(file, 'outside');
+ const authorize = vi.fn(async () => ({ allowed: true as const, isCurrent: () => false }));
+ expect(await publishImage(file, { workingDir: workdir, sessionId: 'task' }, { isCurrent: () => true, authorize })).toMatchObject({ errorCode: 'PERMISSION_DENIED' });
+ expect(ingest).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/desktop/src/main/cindy-media/generatedMedia.ts b/apps/desktop/src/main/cindy-media/generatedMedia.ts
index 18fbb303fa..f553d4ee83 100644
--- a/apps/desktop/src/main/cindy-media/generatedMedia.ts
+++ b/apps/desktop/src/main/cindy-media/generatedMedia.ts
@@ -184,7 +184,7 @@ function parseDataImageUrl(url: string): { mimeType: string; buffer: Buffer } |
}
/**
- * codex 生成图 → 总仓物化:托管地址(老 xdt-image / 新 cindy-media)透传;
+ * codex 生成图 → 总仓物化:托管地址先核验当前 Host 的文件,再复用;
* 本地路径 / data: base64 入仓(零引用,合成 tool_result 落库时挂账)。
* 形状不认识返回 null;入仓失败(白名单外 mime / 读盘失败)向上抛,由调用方
* 决定丢图语义。
@@ -192,11 +192,19 @@ function parseDataImageUrl(url: string): { mimeType: string; buffer: Buffer } |
export async function materializeGeneratedImage(
data: GeneratedImageSource,
deps: {
+ verifyManagedUrl: (url: string) => Promise<void>;
ingestFromPath: (params: { sourcePath: string; originalName?: string }) => Promise<{ url: string; filename: string }>;
ingestBuffer: (params: { buffer: Uint8Array; mimeType: string }) => Promise<{ url: string; filename: string }>;
},
): Promise<{ url: string; filename: string } | null> {
if (data.url?.startsWith('xdt-image://') || data.url?.startsWith('cindy-media://')) {
+ try {
+ await deps.verifyManagedUrl(data.url);
+ } catch (error) {
+ // Some harnesses supply both a stale reference and the actual generated file.
+ if (!data.path) throw error;
+ return deps.ingestFromPath({ sourcePath: data.path, originalName: safeGeneratedImageFilename(data.path) });
+ }
return { url: data.url, filename: safeGeneratedImageFilename(data.url) };
}
if (data.path) {
diff --git a/apps/desktop/src/main/cindy-media/invocationService.ts b/apps/desktop/src/main/cindy-media/invocationService.ts
index cdae4debdc..b99efba763 100644
--- a/apps/desktop/src/main/cindy-media/invocationService.ts
+++ b/apps/desktop/src/main/cindy-media/invocationService.ts
@@ -1778,6 +1778,7 @@ export async function callCindyMedia(
mime_type: resolved.mimeType,
};
}
+ if (request.action === 'import_image') return failure('PERMISSION_DENIED', '图片导入必须通过当前任务的 Host 授权入口。');
if (request.action === 'list_models') {
const capability = request.capability as MediaCapability | undefined;
const availability = await listExecutableMediaModels(
diff --git a/apps/desktop/src/main/cindy-media/publishImage.ts b/apps/desktop/src/main/cindy-media/publishImage.ts
new file mode 100644
index 0000000000..ed12613d3a
--- /dev/null
+++ b/apps/desktop/src/main/cindy-media/publishImage.ts
@@ -0,0 +1,112 @@
+import fs from 'node:fs/promises';
+import { constants } from 'node:fs';
+import { createHash } from 'node:crypto';
+import path from 'node:path';
+import { authorizedSessionPathStillBound, type SessionPathAuthorization } from '@cindy/mcps';
+import { classifyLocalAttachmentPath } from '../cindy-brain/ghostLocalPathGrant.js';
+import { mimeForExt } from './blobStore.js';
+import { ingestMedia } from './ingest.js';
+import { hasRef } from './ledger.js';
+import { sniffMediaMime } from './sniffMediaMime.js';
+import { captureMediaRefCompensationScope } from './refCompensationJournal.js';
+import { getDbClient } from '../localDb/client/current.js';
+import { getActiveAppSession, isAppSessionBoundaryPending } from '../appSessionState.js';
+
+const MAX_IMAGE_BYTES = 48 * 1024 * 1024;
+// Serialize only the check-and-pin for one owner/task/blob; do not share results
+// because each caller must retain its own permission and cancellation checks.
+const referenceLocks = new Map<string, Promise<void>>();
+
+async function withReferenceLock<T>(key: string, perform: () => Promise<T>): Promise<T> {
+ const previous = referenceLocks.get(key) ?? Promise.resolve();
+ let release!: () => void;
+ const next = new Promise<void>((resolve) => { release = resolve; });
+ referenceLocks.set(key, next);
+ try {
+ await previous;
+ return await perform();
+ } finally {
+ release();
+ if (referenceLocks.get(key) === next) referenceLocks.delete(key);
+ }
+}
+
+/** Import into this Host's store; never manufacture a reference to another profile's blob. */
+export async function publishImage(
+ sourcePath: string,
+ context: { sessionId: string; workingDir: string; remoteHostId?: string },
+ deps: {
+ isCurrent: () => boolean;
+ authorize: (absPath: string) => Promise<SessionPathAuthorization>;
+ },
+): Promise<Record<string, unknown>> {
+ if (context.remoteHostId) return { ok: false, errorCode: 'REMOTE_SOURCE_REQUIRED', message: 'SSH 图片请先通过已授权的远端文件工具取回,不能读取控制端同名路径。' };
+ if (!deps.isCurrent()) return { ok: false, errorCode: 'PERMISSION_DENIED', message: '当前任务实例已失效。' };
+ const owner = getActiveAppSession();
+ const db = getDbClient().drizzle;
+ const compensation = captureMediaRefCompensationScope();
+ const source = classifyLocalAttachmentPath(path.resolve(context.workingDir, sourcePath), context.workingDir, { mimeForExt });
+ if (source.kind === 'not-local') return { ok: false, errorCode: 'MEDIA_SOURCE_MISSING', message: '图片源文件不存在或不是普通文件。请提供实际生成的文件。' };
+ if (source.kind === 'unsupported-type' || !source.mimeType.startsWith('image/')) return { ok: false, errorCode: 'INVALID_INPUT', message: '只支持图片文件。' };
+ if (source.size > MAX_IMAGE_BYTES) return { ok: false, errorCode: 'MEDIA_TOO_LARGE', message: '图片超过 48 MB,请先缩小图片。' };
+ const grant = source.kind === 'outside-workdir' ? await deps.authorize(source.absPath) : { allowed: true as const };
+ if (!grant.allowed) return { ok: false, errorCode: 'PERMISSION_DENIED', message: grant.reason };
+ const isCurrent = () => {
+ const active = getActiveAppSession();
+ return deps.isCurrent() && grant.isCurrent?.() !== false && !isAppSessionBoundaryPending()
+ && active.dataOwnerId === owner.dataOwnerId && active.generation === owner.generation;
+ };
+ if (!isCurrent() || !await authorizedSessionPathStillBound(context.workingDir, source.absPath)) {
+ return { ok: false, errorCode: 'PERMISSION_DENIED', message: '图片路径或任务权限已变化,请重试。' };
+ }
+ let buffer: Buffer;
+ try {
+ const file = await fs.open(source.absPath, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
+ try {
+ const stat = await file.stat();
+ if (!stat.isFile() || stat.size > MAX_IMAGE_BYTES) return { ok: false, errorCode: 'MEDIA_TOO_LARGE', message: '图片文件类型或大小已变化,请重新选择。' };
+ // Read at most the observed size plus one byte: a growing file cannot cause an unbounded allocation.
+ const bytes = Buffer.alloc(stat.size + 1);
+ let length = 0;
+ while (length < bytes.length) {
+ const read = await file.read(bytes, length, bytes.length - length, length);
+ if (!read.bytesRead) break;
+ length += read.bytesRead;
+ }
+ const bound = await fs.stat(source.absPath);
+ if (length !== stat.size || bound.dev !== stat.dev || bound.ino !== stat.ino) {
+ return { ok: false, errorCode: 'MEDIA_SOURCE_CHANGED', message: '读取期间图片文件发生变化,请重试。' };
+ }
+ buffer = bytes.subarray(0, length);
+ } finally {
+ await file.close();
+ }
+ } catch (error) {
+ return { ok: false, errorCode: (error as NodeJS.ErrnoException).code === 'ENOENT' ? 'MEDIA_SOURCE_MISSING' : 'MEDIA_READ_FAILED', message: '无法读取图片源文件,请检查文件是否仍存在且可读。' };
+ }
+ if (!isCurrent() || !await authorizedSessionPathStillBound(context.workingDir, source.absPath)) {
+ return { ok: false, errorCode: 'PERMISSION_DENIED', message: '图片路径或任务权限已变化,请重试。' };
+ }
+ const mimeType = sniffMediaMime(buffer);
+ if (!mimeType?.startsWith('image/')) return { ok: false, errorCode: 'INVALID_IMAGE', message: '文件内容不是受支持的图片。' };
+ const assertCurrent = () => { if (!isCurrent()) throw new Error('Media import scope changed'); };
+ try {
+ const hash = createHash('sha256').update(buffer).digest('hex');
+ const reference = { refKind: 'session-attachment' as const, refId: context.sessionId };
+ const key = JSON.stringify([owner.dataOwnerId, owner.generation, context.sessionId, hash]);
+ const image = await withReferenceLock(key, async () => {
+ assertCurrent();
+ const exists = await hasRef({ hash, ...reference }, db);
+ assertCurrent();
+ return ingestMedia({ buffer, mimeType, isCache: false,
+ refs: exists ? [] : [{ ...reference, originSessionId: context.sessionId, originKind: 'tool' }],
+ assertStillValid: assertCurrent, refCompensationScope: compensation,
+ }, db);
+ });
+ assertCurrent();
+ return { ok: true, xdt_image_urls: [image.url], url: image.url, filename: `${image.hash}${image.ext}`,
+ message: '已导入当前 Host 并登记任务引用。请在回复中使用返回的受管地址嵌入图片。此结果不代表远端设备已完成下载。' };
+ } catch {
+ return { ok: false, errorCode: 'MEDIA_PUBLISH_FAILED', message: '图片未完成入库或任务归属登记,请检查当前任务后重试。' };
+ }
+}
diff --git a/apps/desktop/src/main/device-link/__tests__/mediaFetch.test.ts b/apps/desktop/src/main/device-link/__tests__/mediaFetch.test.ts
index 07f411e5af..f37b0ff3c1 100644
--- a/apps/desktop/src/main/device-link/__tests__/mediaFetch.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/mediaFetch.test.ts
@@ -55,7 +55,7 @@ beforeEach(() => {
vi.clearAllMocks();
assertSharedTaskMedia.mockReset();
__testing.uploadCache.clear();
- statMock.mockResolvedValue({ size: 42, mtimeMs: 1000 });
+ statMock.mockResolvedValue({ isFile: () => true, size: 42, mtimeMs: 1000 });
realpathMock.mockImplementation(async (p: string) => p);
// 默认上传回显 contentType,便于断言 result.mimeType 来源
uploadLocalFile.mockImplementation(async (_p: string, opts?: { contentType?: string }) => ({
@@ -78,6 +78,13 @@ beforeEach(() => {
});
});
+it('reports a missing Host source without uploading or exposing its filesystem path', async () => {
+ imageResolve.mockReturnValue({ absPath: '/private/missing.png', mimeType: 'image/png' });
+ statMock.mockRejectedValueOnce(Object.assign(new Error('ENOENT /private/missing.png'), { code: 'ENOENT' }));
+ await expect(fetchLocalMediaToOss({ url: 'xdt-image://s/missing.png' })).rejects.toThrow('[MEDIA_SOURCE_MISSING] Media source is missing on this Host');
+ expect(uploadLocalFile).not.toHaveBeenCalled();
+});
+
afterEach(() => {
__testing.setThumbnailRenderer(null);
vi.useRealTimers();
@@ -393,7 +400,7 @@ describe('fetchLocalMediaToOss — 图片上传去重', () => {
it('源文件 mtime 变化 → 缓存失效重传', async () => {
await fetchLocalMediaToOss({ url: IMAGE_URL });
- statMock.mockResolvedValue({ size: 42, mtimeMs: 2000 });
+ statMock.mockResolvedValue({ isFile: () => true, size: 42, mtimeMs: 2000 });
nextUploadKey('key-2');
const changed = await fetchLocalMediaToOss({ url: IMAGE_URL });
expect(changed.ossKey).toBe('key-2');
@@ -526,7 +533,7 @@ describe('thumbnail 护栏(输入体量 + 渲染超时)', () => {
it('输入超过 48MB 上限 → 不调渲染器,直接原图路径', async () => {
imageResolve.mockReturnValue({ absPath: '/cache/huge.png', mimeType: 'image/png' });
- statMock.mockResolvedValue({ size: __testing.THUMB_INPUT_MAX_BYTES + 1, mtimeMs: 1 });
+ statMock.mockResolvedValue({ isFile: () => true, size: __testing.THUMB_INPUT_MAX_BYTES + 1, mtimeMs: 1 });
const renderer = vi.fn(async () => Buffer.from([1]));
__testing.setThumbnailRenderer(renderer);
const out = await fetchLocalMediaToOss({ url: 'xdt-image://sess/huge.png', thumbnail: true });
@@ -538,7 +545,7 @@ describe('thumbnail 护栏(输入体量 + 渲染超时)', () => {
it('渲染超时 → 回退原图路径,invoke 不被挂住', async () => {
vi.useFakeTimers();
imageResolve.mockReturnValue({ absPath: '/cache/slow.png', mimeType: 'image/png' });
- statMock.mockResolvedValue({ size: 1000, mtimeMs: 1 });
+ statMock.mockResolvedValue({ isFile: () => true, size: 1000, mtimeMs: 1 });
__testing.setThumbnailRenderer(
() =>
new Promise(() => {
@@ -645,7 +652,7 @@ describe('fetchLocalMediaToOss — baseDir / maxBytes 服务端强制约束', ()
it('maxBytes:stat 超限 → 上传之前就拒绝(流量一个字节都不花)', async () => {
realpathMock.mockImplementation(async (p: string) => p);
- statMock.mockResolvedValue({ size: 5_000_000, mtimeMs: 1 });
+ statMock.mockResolvedValue({ isFile: () => true, size: 5_000_000, mtimeMs: 1 });
const msg = await codeOf(() =>
fetchLocalMediaToOss({
url: urlFor('/proj/out/big.css', '&maxBytes=2097152'),
@@ -657,7 +664,7 @@ describe('fetchLocalMediaToOss — baseDir / maxBytes 服务端强制约束', ()
it('maxBytes:上限内正常放行', async () => {
realpathMock.mockImplementation(async (p: string) => p);
- statMock.mockResolvedValue({ size: 1024, mtimeMs: 1 });
+ statMock.mockResolvedValue({ isFile: () => true, size: 1024, mtimeMs: 1 });
await fetchLocalMediaToOss({ url: urlFor('/proj/out/a.css', '&maxBytes=2097152') });
expect(uploadLocalFile).toHaveBeenCalledTimes(1);
});
@@ -677,7 +684,7 @@ describe('fetchLocalMediaToOss — baseDir / maxBytes 服务端强制约束', ()
it('两个参数都不带 → 行为不变(老控制端照旧取件)', async () => {
realpathMock.mockImplementation(async (p: string) => p);
- statMock.mockResolvedValue({ size: 5_000_000, mtimeMs: 1 });
+ statMock.mockResolvedValue({ isFile: () => true, size: 5_000_000, mtimeMs: 1 });
await fetchLocalMediaToOss({ url: urlFor('/proj/out/big.css') });
expect(uploadLocalFile).toHaveBeenCalledTimes(1);
});
diff --git a/apps/desktop/src/main/device-link/mediaFetch.ts b/apps/desktop/src/main/device-link/mediaFetch.ts
index 6f28fb6ae9..4fa12c1834 100644
--- a/apps/desktop/src/main/device-link/mediaFetch.ts
+++ b/apps/desktop/src/main/device-link/mediaFetch.ts
@@ -349,6 +349,17 @@ export async function resolveAuthorizedMedia(arg: unknown, maximumBytes?: number
mimeType = materialized.mime;
} else {
({ absPath, mimeType } = resolveLocalMedia(url));
+ if (!isPathMedia && mimeType?.startsWith('image/')) {
+ try {
+ const source = await stat(absPath);
+ if (!source.isFile()) throw new Error('[MEDIA_SOURCE_MISSING] Media source is not a file');
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
+ throw new Error('[MEDIA_SOURCE_MISSING] Media source is missing on this Host');
+ }
+ throw error;
+ }
+ }
}
// For file/audio schemes the requested URL path carries the semantic
// extension; if it resolves through a symlink whose target has a different
@@ -371,7 +382,8 @@ export async function resolveAuthorizedMedia(arg: unknown, maximumBytes?: number
let real: string;
try {
real = await realpath(absPath);
- } catch {
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') throw new Error('[MEDIA_SOURCE_MISSING] Media source is missing on this Host');
throw new Error('媒体文件不存在或不可读');
}
// realpath 再查:挡字面形式看似无害的 symlink 逃逸。
diff --git a/apps/desktop/src/main/maker-host/__tests__/pi-package-store-security.test.ts b/apps/desktop/src/main/maker-host/__tests__/pi-package-store-security.test.ts
index 8bf2a4ea60..3e6e154bc9 100644
--- a/apps/desktop/src/main/maker-host/__tests__/pi-package-store-security.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/pi-package-store-security.test.ts
@@ -2633,6 +2633,11 @@ describe('Pi package executable-code boundary', () => {
);
it('does not run a skipped descendant extension from an unverified ancestor snapshot', async () => {
+ // The aggregate snapshot warning lives in the one-second inspection cache.
+ // Control only Date so slow CI cannot expire it during real filesystem I/O.
+ const now = Date.now();
+ vi.useFakeTimers({ toFake: ['Date'] });
+ vi.setSystemTime(now);
const ancestorRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'cindy-pi-package-approved-overlap-'));
roots.push(ancestorRoot);
const descendantRoot = path.join(ancestorRoot, 'extension');
@@ -2705,6 +2710,16 @@ describe('Pi package executable-code boundary', () => {
{ source: descendantRoot, enabled: true, warning: 'inspection-limit' },
],
});
+ // After the cache expires, a fresh inspection clears this transient warning
+ // without disabling either native package or changing the returned snapshot.
+ vi.setSystemTime(now + 2_000);
+ const refreshed = await store.listPiPackages();
+ expect(refreshed.packages).toMatchObject([
+ { source: ancestorRoot, enabled: true },
+ { source: descendantRoot, enabled: true },
+ ]);
+ expect(refreshed.packages[1].warning).toBeUndefined();
+ expect(snapshot.extensions).toEqual([]);
const state = JSON.parse(await fs.readFile(
path.join(runtime.userData, 'pi-package-home', 'cindy-package-state.json'),
'utf8',
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 43984b01a9..d533ba43ef 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -20293,6 +20293,11 @@ async function materializeCodexImage(
// 本体在 cindy-media/generatedMedia.ts(规则 14 可测),这里只做 thin adapter。
try {
return await materializeGeneratedImage(data, {
+ verifyManagedUrl: async (url) => {
+ const resolved = url.startsWith('cindy-media://') ? resolveCindyMediaUrl(url) : imageCacheStore.resolveSafe(url);
+ const source = await fsp.stat(resolved.absPath);
+ if (!source.isFile()) throw new Error('[MEDIA_SOURCE_MISSING] Generated image is not a file');
+ },
ingestFromPath: cindyChatAttachments.ingestChatImageFromPath,
ingestBuffer: cindyChatAttachments.ingestChatImageBuffer,
});
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/ghostWorkdirGate.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/ghostWorkdirGate.test.ts
index d155b297a7..751edfc313 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/ghostWorkdirGate.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/ghostWorkdirGate.test.ts
@@ -248,6 +248,9 @@ vi.mock('../../cindy-media/ledger.js', () => ({
vi.mock('../../cindy-media/invocationService.js', () => ({
callCindyMedia: callCindyMediaMock,
}));
+// Image ingestion has its own publishImage tests; keep this permission-gate
+// fixture isolated from the real blob store and database dependency graph.
+vi.mock('../../cindy-media/publishImage.js', () => ({ publishImage: vi.fn() }));
vi.mock('../../cindy-media/attachmentGrantGate.js', () => ({ chatAttachmentOrigin: vi.fn() }));
vi.mock('../ghostAttachmentResolve.js', () => ({
resolveGhostAttachmentUrl: resolveGhostAttachmentUrlMock,
diff --git a/apps/desktop/src/main/mcp-integrations/ghost.ts b/apps/desktop/src/main/mcp-integrations/ghost.ts
index f082c074d9..0631671c21 100644
--- a/apps/desktop/src/main/mcp-integrations/ghost.ts
+++ b/apps/desktop/src/main/mcp-integrations/ghost.ts
@@ -29,6 +29,7 @@ import { isBotAuthorizationSession } from '../maker-ipc/botAuthorizationHost.js'
*/
import fs from 'node:fs';
+import { publishImage } from '../cindy-media/publishImage.js';
import path from 'node:path';
import { createHash, randomUUID } from 'node:crypto';
@@ -1767,6 +1768,17 @@ export function getCindyGhostsMcpDeps(
callMedia: async (request) => {
const sessionContext = resolveSessionContext();
const sessionId = sessionContext?.sessionId;
+ if (request.action === 'import_image') {
+ const live = requireLiveSessionInstance(sessionId, sessionContext?.sessionInstanceId, hostDeps.getLiveSessionGrantState);
+ if (!live.ok || !sessionContext?.workingDir) return { ok: false, errorCode: 'PERMISSION_DENIED', message: live.ok ? '当前任务缺少工作目录。' : live.message };
+ return publishImage(request.path, { ...sessionContext, sessionId: live.sessionId }, {
+ isCurrent: () => requireLiveSessionInstance(live.sessionId, live.sessionInstanceId, hostDeps.getLiveSessionGrantState).ok,
+ authorize: (absPath) => authorizeDesktopSessionPath({
+ ...sessionContext, sessionId: live.sessionId, sessionInstanceId: live.sessionInstanceId,
+ path: absPath, toolName: 'media.import_image', operation: 'read',
+ }, hostDeps.getLiveSessionGrantState),
+ });
+ }
const downloadContext = (request.action === 'request' || request.action === 'poll') && sessionId && sessionContext?.sessionInstanceId
? hostDeps.createMediaDownloadContext?.(sessionId, sessionContext.sessionInstanceId)
: undefined;
diff --git a/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx b/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx
index 2d8eaac635..70ece03b10 100644
--- a/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx
+++ b/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx
@@ -4,6 +4,8 @@ import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { buildAttachmentPayload } from '@/session/messagePayload';
import { svgAttachmentForDisplay } from '@/session/messageAttachments';
+import { collectMobileMessageGalleryImages, lightboxImagesForPayload } from '@/session/messageGallery';
+import type { MobileMessageRenderItem } from '@/session/messageRenderModel';
import {
ImageLightbox,
type ImageLightboxProps,
@@ -35,9 +37,6 @@ vi.mock("react-i18next", async (importOriginal) => ({
...await importOriginal<typeof import('react-i18next')>(),
useTranslation: () => ({ t: (key: string) => key }),
}));
-vi.mock("@/session/remoteMedia", () => ({
- isDesktopLocalMediaUrl: (uri: string) => uri.startsWith("cindy-media:"),
-}));
vi.mock("react-native-safe-area-context", () => ({
useSafeAreaInsets: () => runtime.insets,
}));
@@ -224,6 +223,52 @@ function mount(overrides: Partial<ImageLightboxProps> = {}) {
return { props, render };
}
+describe('Markdown gallery prefetch boundaries', () => {
+ it('does not fetch a titled outside-workdir image when a neighboring image is opened', async () => {
+ const items = [{
+ type: 'message', key: 'm1',
+ message: {
+ key: 'm1', kind: 'assistant', role: 'assistant', align: 'agent', label: 'assistant',
+ source: { clientId: 'm1', role: 'assistant', content: '', createdAt: '2026-10-01T00:00:00Z' },
+ body: '![ok](https://example.invalid/ok.png)\n\n![private](xdt-file://open?path=%2Fprivate%2Fphoto.png "title")',
+ },
+ }] as MobileMessageRenderItem[];
+ const gallery = collectMobileMessageGalleryImages(items, '/repo');
+ const images = lightboxImagesForPayload(gallery, gallery[0].payload);
+ const resolver = vi.fn(async () => ({
+ url: 'file:///download.png', previewable: true, ossKey: 'download.png',
+ mimeType: 'image/png', size: 1, expiresAt: '2099-01-01T00:00:00Z',
+ }));
+ await act(async () => { mount({ images, initialUrl: images[0].url, onResolveRemoteMedia: resolver }); });
+ expect(resolver).not.toHaveBeenCalled();
+ expect(images.map((image) => image.url)).toEqual(['https://example.invalid/ok.png']);
+ });
+
+ it('prefetches an inside-workdir neighbor with the Host realpath constraint', async () => {
+ const items = [{
+ type: 'message', key: 'm1',
+ message: {
+ key: 'm1', kind: 'assistant', role: 'assistant', align: 'agent', label: 'assistant',
+ source: { clientId: 'm1', role: 'assistant', content: '', createdAt: '2026-10-01T00:00:00Z' },
+ body: '![ok](https://example.invalid/ok.png)\n\n![local](xdt-file://open?path=%2Frepo%2Fphoto.png&baseDir=%2F "title")',
+ },
+ }] as MobileMessageRenderItem[];
+ const gallery = collectMobileMessageGalleryImages(items, '/repo');
+ const images = lightboxImagesForPayload(gallery, gallery[0].payload);
+ const resolver = vi.fn(async (_media: { url: string }) => ({
+ url: 'file:///download.png', previewable: true, ossKey: 'download.png',
+ mimeType: 'image/png', size: 1, expiresAt: '2099-01-01T00:00:00Z',
+ }));
+ await act(async () => { mount({ images, initialUrl: images[0].url, onResolveRemoteMedia: resolver }); });
+ expect(images).toHaveLength(2);
+ expect(resolver).toHaveBeenCalledTimes(2);
+ for (const [media] of resolver.mock.calls) {
+ expect(new URL(media.url).searchParams.get('path')).toBe('/repo/photo.png');
+ expect(new URL(media.url).searchParams.getAll('baseDir')).toEqual(['/repo']);
+ }
+ });
+});
+
describe('SVG images in the shared lightbox', () => {
it.each([
{ name: 'diagram', mimeType: 'image/svg+xml; charset=utf-8' },
diff --git a/apps/mobile/src/__tests__/messageGallery.test.ts b/apps/mobile/src/__tests__/messageGallery.test.ts
index a0a099f261..20efe25cbe 100644
--- a/apps/mobile/src/__tests__/messageGallery.test.ts
+++ b/apps/mobile/src/__tests__/messageGallery.test.ts
@@ -5,6 +5,8 @@ import {
lightboxImagesForPayload,
} from '@/session/messageGallery';
import type { MobileMessageRenderItem } from '@/session/messageRenderModel';
+import { mobileMarkdownImageUrlForWorkdir, mobileMarkdownManagedImagePreviewUrl } from '@/session/messageMarkdown';
+import { buildMediaPayload } from '@/session/messagePayload';
function messageItem(key: string, message: Partial<MobileMessageRenderItem & { message: unknown }>['message']): MobileMessageRenderItem {
return {
@@ -25,6 +27,34 @@ function messageItem(key: string, message: Partial<MobileMessageRenderItem & { m
}
describe('message gallery', () => {
+ it.each(['', ' "title"', " 'title'"])(
+ 'excludes out-of-workdir Markdown images from neighbor prefetch with title suffix %s', (title) => {
+ const outside = 'xdt-file://open?path=%2Fprivate%2Fphoto.png&baseDir=%2F';
+ const gallery = collectMobileMessageGalleryImages([messageItem('m1', {
+ body: `![ok](https://example.com/ok.png)\n\n![private](${outside}${title})`,
+ })], '/repo');
+ expect(gallery.map((image) => image.url)).toEqual(['https://example.com/ok.png']);
+ // Explicitly clicking the external text chip still opens just that image.
+ const url = mobileMarkdownImageUrlForWorkdir(outside, '/repo', 'm1')!;
+ const payload = buildMediaPayload({ kind: 'image', url, previewable: false }, 'private');
+ if (payload.kind !== 'media') throw new Error('Expected media payload');
+ expect(lightboxImagesForPayload(gallery, payload).map((image) => image.url)).toEqual([url]);
+ },
+ );
+
+ it('binds local gallery pages to the task root and retains their shared preview identity', () => {
+ const source = 'xdt-file://open?path=%2Frepo%2Flink%2Fphoto.png&baseDir=%2F';
+ const gallery = collectMobileMessageGalleryImages([messageItem('m1', {
+ body: `![first](${source} "title")\n\n![second](second.png)`,
+ })], '/repo');
+ const previewUrl = mobileMarkdownManagedImagePreviewUrl(source, '/repo', 'm1')!;
+ expect(gallery[0].url).toBe(previewUrl);
+ expect(gallery).toHaveLength(2);
+ for (const image of gallery) expect(new URL(image.url).searchParams.getAll('baseDir')).toEqual(['/repo']);
+ expect(lightboxImagesForPayload(gallery, gallery[0].payload)).toEqual(gallery);
+ expect(collectMobileMessageGalleryImages([messageItem('m1', { body: `![missing context](${source})` })])).toEqual([]);
+ });
+
it('uses the same SVG file attachment URL as the thumbnail and includes it in the image gallery', () => {
const gallery = collectMobileMessageGalleryImages([
messageItem('m-svg', { attachments: [
@@ -42,7 +72,7 @@ describe('message gallery', () => {
], 'C:\\repo');
expect(gallery).toHaveLength(1);
expect(gallery[0].url).toBe(
- 'xdt-file://open?path=C%3A%5Crepo%5Cartifacts%5Cbuild%20result.png&v=m-local',
+ 'xdt-file://open?path=C%3A%5Crepo%5Cartifacts%5Cbuild+result.png&v=m-local&baseDir=C%3A%5Crepo',
);
expect(gallery[0].payload).toMatchObject({
media: { previewable: false },
@@ -56,8 +86,8 @@ describe('message gallery', () => {
], '/repo');
expect(gallery.map((item) => item.url)).toEqual([
- 'xdt-file://open?path=%2Frepo%2Fartifacts%2Fplot.png&v=m1',
- 'xdt-file://open?path=%2Frepo%2Fartifacts%2Fplot.png&v=m2',
+ 'xdt-file://open?path=%2Frepo%2Fartifacts%2Fplot.png&v=m1&baseDir=%2Frepo',
+ 'xdt-file://open?path=%2Frepo%2Fartifacts%2Fplot.png&v=m2&baseDir=%2Frepo',
]);
});
@@ -71,9 +101,9 @@ describe('message gallery', () => {
expect(gallery.map((item) => item.url)).toEqual([
'xdt-file://open?path=%2Fhome%2Fu%2Fproj%2Fartifacts%2Fplot.png'
- + '&sessionId=session-ssh&remoteHostId=ssh-host-1&workdir=%2Fhome%2Fu%2Fproj&v=m-ssh',
+ + '&sessionId=session-ssh&remoteHostId=ssh-host-1&workdir=%2Fhome%2Fu%2Fproj&v=m-ssh&baseDir=%2Fhome%2Fu%2Fproj',
'xdt-file://open?path=%2Fhome%2Fu%2Fproj%2Fexisting.png'
- + '&sessionId=session-ssh&remoteHostId=ssh-host-1&workdir=%2Fhome%2Fu%2Fproj&v=m-xdt',
+ + '&sessionId=session-ssh&remoteHostId=ssh-host-1&workdir=%2Fhome%2Fu%2Fproj&v=m-xdt&baseDir=%2Fhome%2Fu%2Fproj',
]);
});
diff --git a/apps/mobile/src/__tests__/messageImageHandoff.test.tsx b/apps/mobile/src/__tests__/messageImageHandoff.test.tsx
index e0d604cfc9..ef4b39f55e 100644
--- a/apps/mobile/src/__tests__/messageImageHandoff.test.tsx
+++ b/apps/mobile/src/__tests__/messageImageHandoff.test.tsx
@@ -8,7 +8,7 @@ import ts from 'typescript';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { buildMessageContentLayout } from '@/session/messageContentLayout';
import { attachmentImageDisplaySize, mediaThumbnailPhase, shouldAutoResolveMediaThumbnail } from '@/session/mediaThumbnail';
-import { isDesktopLocalMediaUrl } from '@/session/remoteMedia';
+import { isDesktopLocalMediaUrl, mediaLoadFailureKey } from '@/session/remoteMedia';
import { spacing } from '@/theme/tokens';
// Run the production components with real React effects; only native image decoding is controlled.
@@ -38,8 +38,9 @@ function fixture() {
useThemedStyles: () => ({}), makeStyles: () => ({}),
useRecyclingState: React.useState, useState: React.useState, useLayoutEffect: React.useLayoutEffect,
useEffect: React.useEffect, useRef: React.useRef, useCallback: React.useCallback,
+ useContext: React.useContext, MessageHeavyContentVisibilityContext: React.createContext(true),
attachmentIntrinsicSizeCache: cache, ATTACHMENT_INTRINSIC_CACHE_MAX: 500,
- attachmentImageDisplaySize, mediaThumbnailPhase, shouldAutoResolveMediaThumbnail, isDesktopLocalMediaUrl,
+ attachmentImageDisplaySize, mediaThumbnailPhase, shouldAutoResolveMediaThumbnail, isDesktopLocalMediaUrl, mediaLoadFailureKey,
getSentAttachmentThumbUri: (ref: string) => durable.get(ref) ?? null,
MessageContentOpenButton: View,
buildMediaPayload: (media: unknown) => media,
@@ -61,6 +62,33 @@ function fixture() {
}
describe('image frame continuity', () => {
+ it('fetches a managed thumbnail automatically, opens the viewer and refreshes a bad cached image only once', async () => {
+ const f = fixture();
+ const url = `cindy-media://blobs/${'a'.repeat(64)}.png`;
+ const open = vi.fn();
+ const resolver = vi.fn(async (_media: unknown, _options: unknown) => ({ url: 'file:///thumb.png', previewable: true }));
+ await act(async () => f.render(<f.MediaPreview layout={f.layout} label="screenshot"
+ media={{ kind: 'image', url, previewable: false }} onResolveRemoteMedia={resolver} onOpen={open} />));
+ expect(resolver).toHaveBeenCalledWith({ kind: 'image', url, previewable: false, thumbnail: true }, expect.objectContaining({ forceRefresh: false }));
+ expect(f.host.querySelector('[data-image]')?.getAttribute('data-image')).toBe('file:///thumb.png');
+ act(() => (f.host.querySelector('[data-testid="message.mediaPreviewButton"]') as HTMLElement).click());
+ expect(open).toHaveBeenCalledOnce();
+ await act(async () => f.handlers.get('file:///thumb.png')!.onError());
+ expect(resolver).toHaveBeenCalledTimes(2);
+ expect(resolver.mock.calls[1][1]).toMatchObject({ forceRefresh: true });
+ await act(async () => f.handlers.get('file:///thumb.png')!.onError());
+ expect(resolver).toHaveBeenCalledTimes(2);
+ expect(f.host.querySelector('[data-testid="message.mediaThumbFallback"]')).not.toBeNull();
+ });
+
+ it('preserves the missing-source explanation from Host', async () => {
+ const f = fixture();
+ const resolver = vi.fn(async () => { throw new Error('[MEDIA_SOURCE_MISSING] missing'); });
+ await act(async () => f.render(<f.MediaPreview layout={f.layout} label="screenshot"
+ media={{ kind: 'image', url: 'cindy-media://blobs/missing.png', previewable: false }} onResolveRemoteMedia={resolver} />));
+ expect(f.host.textContent).toContain('message.lightbox.sourceMissing');
+ expect(resolver).toHaveBeenCalledOnce();
+ });
it('uses the SVG-capable decoder for thumbnails, measures from load and opens the existing viewer', () => {
const f = fixture();
const uri = 'https://example.com/diagram.svg';
diff --git a/apps/mobile/src/__tests__/messageMarkdown.test.ts b/apps/mobile/src/__tests__/messageMarkdown.test.ts
index 11894c7886..36be6ff291 100644
--- a/apps/mobile/src/__tests__/messageMarkdown.test.ts
+++ b/apps/mobile/src/__tests__/messageMarkdown.test.ts
@@ -6,6 +6,7 @@ import {
isMobileMarkdownImageDirectUrl,
mobileMarkdownImageTitle,
mobileMarkdownImageUrlForWorkdir,
+ mobileMarkdownManagedImagePreviewUrl,
mobileMarkdownImageAltChipText,
MOBILE_MARKDOWN_IMAGE_ALT_CHIP_MAX_UTF16_LENGTH,
mobileMarkdownInlineImageSize,
@@ -1007,6 +1008,17 @@ describe('messageMarkdown', () => {
expect(isMobileMarkdownImageDirectUrl('cindy-media://blobs/a.png')).toBe(false);
});
+ it('parses remote images with standard optional titles without exposing Markdown source', () => {
+ for (const url of ['cindy-media://blobs/a.png', 'xdt-image://cache/a.png', 'https://example.com/shot(1).png']) {
+ for (const destination of [`${url} "真实截图"`, `${url} '截图'`, `${url} (截图)`, `<${url}> "截图"`]) {
+ expect(parseMobileMarkdownInlines(`![验收](${destination})`)).toEqual([
+ { type: 'image', alt: '验收', url },
+ ]);
+ }
+ }
+ expect(parseMobileMarkdownInlines('![x](javascript:alert(1) "title")').some((inline) => inline.type === 'image')).toBe(false);
+ });
+
it('rejects img-prefixed non-img tags exactly (img must be followed by whitespace, / or >)', () => {
// \b 把 -/./: 当边界,<img-wrapper src=...> 会被误当 img 解析出 src(codex P2);
// 守卫与匹配器都要求标签名"恰好是 img"。
@@ -1580,7 +1592,72 @@ describe('bare file paths(正文纯文本形态)', () => {
});
});
+describe('automatic Markdown image fetch scope', () => {
+ const pathUrl = (file: string, extra = '') => `xdt-file://open?path=${encodeURIComponent(file)}${extra}`;
+
+ it.each(['/private/photo.png', '/repo-other/photo.png', '/repo/../private/photo.png'])(
+ 'does not automatically fetch outside or escaping path %s', (file) => {
+ expect(mobileMarkdownManagedImagePreviewUrl(file, '/repo')).toBeNull();
+ expect(mobileMarkdownManagedImagePreviewUrl(pathUrl(file), '/repo')).toBeNull();
+ // Explicitly opening the chip still uses the established viewer path.
+ expect(mobileMarkdownImageUrlForWorkdir(file, '/repo')).not.toBeNull();
+ },
+ );
+
+ it('requires workdir for file previews but not for already managed media', () => {
+ expect(mobileMarkdownManagedImagePreviewUrl(pathUrl('/repo/a.png'))).toBeNull();
+ expect(mobileMarkdownManagedImagePreviewUrl('shot.png')).toBeNull();
+ expect(mobileMarkdownManagedImagePreviewUrl('cindy-media://blobs/a.png')).toBe('cindy-media://blobs/a.png');
+ expect(mobileMarkdownManagedImagePreviewUrl('xdt-image://session/a.png')).toBe('xdt-image://session/a.png');
+ });
+
+ it('overwrites supplied baseDir with the task root for Host realpath checks', () => {
+ const result = mobileMarkdownManagedImagePreviewUrl(pathUrl('/repo/link/a.png', '&baseDir=%2F&baseDir=%2Fprivate'), '/repo', 'message:2');
+ const parsed = new URL(result!);
+ expect(parsed.searchParams.getAll('baseDir')).toEqual(['/repo']);
+ expect(parsed.searchParams.get('path')).toBe('/repo/link/a.png');
+ expect(parsed.searchParams.get('v')).toBe('message:2');
+ expect(new URL(mobileMarkdownManagedImagePreviewUrl('out/a.png', '/repo')!).searchParams.get('baseDir')).toBe('/repo');
+ });
+
+ it('handles Windows separators and roots without treating neighboring directories as children', () => {
+ const root = 'C:\\Repo';
+ const preview = mobileMarkdownManagedImagePreviewUrl(pathUrl('c:\\repo\\out\\a.png'), root);
+ expect(new URL(preview!).searchParams.get('baseDir')).toBe(root);
+ expect(mobileMarkdownManagedImagePreviewUrl(pathUrl('C:\\Repo-other\\a.png'), root)).toBeNull();
+ expect(mobileMarkdownManagedImagePreviewUrl(pathUrl('D:\\Repo\\a.png'), root)).toBeNull();
+ expect(mobileMarkdownManagedImagePreviewUrl(pathUrl('C:\\Repo\\..\\private\\a.png'), root)).toBeNull();
+ });
+
+ it('keeps SSH task provenance and supplies the same remote root constraint', () => {
+ const result = mobileMarkdownManagedImagePreviewUrl(pathUrl('/srv/work/out/a.png', '&remoteHostId=other&baseDir=%2F'), '/srv/work', 'm', 'host', 'task');
+ const parsed = new URL(result!);
+ expect(parsed.searchParams.get('remoteHostId')).toBe('host');
+ expect(parsed.searchParams.get('sessionId')).toBe('task');
+ expect(parsed.searchParams.get('baseDir')).toBe('/srv/work');
+ expect(mobileMarkdownManagedImagePreviewUrl('a.png', '/srv/work', 'm', 'host')).toBeNull();
+ });
+});
+
describe('groupMobileMarkdownSelectableBlocks', () => {
+ it('keeps fallback image chips and surrounding paragraphs in the selectable text run', () => {
+ const blocks = parseMobileMarkdown('前段\n\n正文 ![图](shot.png) 结尾\n\n后段');
+ // No resolver or no workdir: the renderer can only show the existing text chip.
+ for (const imageRendersPreview of [() => false, (image: { url: string }) => mobileMarkdownManagedImagePreviewUrl(image.url) !== null]) {
+ const groups = groupMobileMarkdownSelectableBlocks(blocks, { imageRendersPreview });
+ expect(groups).toHaveLength(1);
+ expect(groups[0].type).toBe('text_run');
+ }
+ });
+
+ it('separates actual managed previews while keeping outside-workdir chips selectable', () => {
+ const imageRendersPreview = (image: { url: string }) => mobileMarkdownManagedImagePreviewUrl(image.url, '/repo') !== null;
+ const groups = groupMobileMarkdownSelectableBlocks(
+ parseMobileMarkdown('前段\n\n![图](shot.png)\n\n说明 ![外部图](/private/photo.png) 结尾'),
+ { imageRendersPreview },
+ );
+ expect(groups.map((group) => group.type)).toEqual(['text_run', 'single', 'text_run']);
+ });
it('merges consecutive text blocks into one run so native selection can cross paragraphs', () => {
const blocks = parseMobileMarkdown([
'# 标题',
@@ -1726,14 +1803,16 @@ describe('groupMobileMarkdownSelectableBlocks', () => {
expect(chunkText.join('')).toBe(text);
});
- it('keeps oversized non-direct image alt text in one image inline while bounding rendered chip text', () => {
+ it('keeps managed image previews out of selectable text runs without splitting oversized alt text', () => {
const alt = 'a'.repeat(MOBILE_MARKDOWN_IMAGE_ALT_CHIP_MAX_UTF16_LENGTH + 21);
const blocks = parseMobileMarkdown(`![${alt}](docs/local-image.png)`);
- const groups = groupMobileMarkdownSelectableBlocks(blocks, { maxTextRunUtf16Length: 1800 });
- const chunks = groups.flatMap((group) => (group.type === 'text_run' ? group.blocks : []));
- const imageInlines = chunks.flatMap((block) => block.inlines.filter((inline) => inline.type === 'image'));
+ const groups = groupMobileMarkdownSelectableBlocks(blocks, {
+ maxTextRunUtf16Length: 1800,
+ imageRendersPreview: (image) => mobileMarkdownManagedImagePreviewUrl(image.url, '/repo') !== null,
+ });
+ const imageInlines = blocks.flatMap((block) => 'inlines' in block ? block.inlines.filter((inline) => inline.type === 'image') : []);
- expect(groups.map((group) => group.type)).toEqual(['text_run']);
+ expect(groups.map((group) => group.type)).toEqual(['single']);
expect(imageInlines).toHaveLength(1);
expect(imageInlines[0]).toMatchObject({ alt, url: 'docs/local-image.png' });
expect(mobileMarkdownImageAltChipText(imageInlines[0].alt)).toHaveLength(
diff --git a/apps/mobile/src/__tests__/messageSelectionDesktopFirst.test.ts b/apps/mobile/src/__tests__/messageSelectionDesktopFirst.test.ts
index de506719b2..9b9ae66098 100644
--- a/apps/mobile/src/__tests__/messageSelectionDesktopFirst.test.ts
+++ b/apps/mobile/src/__tests__/messageSelectionDesktopFirst.test.ts
@@ -61,13 +61,15 @@ describe('mobile message text selection', () => {
// 各块 Text 开原生选中,含内嵌图片 View 的块除外(Android 上 selectable+内嵌 View 有风险)。
expect(markdownBodySource).toContain('const inlinesSelectable = useCallback((inlines: readonly MobileMarkdownInline[]) => (');
- expect(markdownBodySource).toContain("inline.type === 'image' && isMobileMarkdownImageDirectUrl(inline.url)");
+ expect(markdownBodySource).toContain("inline.type === 'image' && imageRendersPreview(inline)");
+ expect(markdownBodySource).toContain('isMobileMarkdownImageDirectUrl(inline.url) || managedImagePreviewUrl(inline.url) !== null');
expect(markdownBodySource).toContain('selectable={inlinesSelectable(block.inlines)}');
// 跨段选择:连续纯文本块合并进同一个原生文本视图(text_run),原生选择手柄可横跨段落。
// Android 上长 selectable Text 分块,避免单个超高原生文本视图干扰列表测高/滚动。
expect(markdownBodySource).toContain('ANDROID_SELECTABLE_TEXT_RUN_GROUPING_OPTIONS');
- expect(markdownBodySource).toContain("const textRunGroupingOptions = Platform.OS === 'android'");
+ expect(markdownBodySource).toContain("...(Platform.OS === 'android' ? ANDROID_SELECTABLE_TEXT_RUN_GROUPING_OPTIONS : {})");
+ expect(markdownBodySource).toMatch(/const textRunGroupingOptions = useMemo[(][\s\S]*?imageRendersPreview,/);
expect(markdownBodySource).not.toContain("selectable === true && Platform.OS === 'android'");
expect(markdownBodySource).toContain('groupMobileMarkdownSelectableBlocks(blocks, textRunGroupingOptions)');
expect(markdownBodySource).toContain('testID="message.markdownTextRun"');
diff --git a/apps/mobile/src/__tests__/remoteMedia.test.ts b/apps/mobile/src/__tests__/remoteMedia.test.ts
index a2920a21ec..0b1d7adf6a 100644
--- a/apps/mobile/src/__tests__/remoteMedia.test.ts
+++ b/apps/mobile/src/__tests__/remoteMedia.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vitest";
import {
canPreviewResolvedRemoteMedia,
formatRemoteMediaSize,
+ mediaLoadFailureKey,
isDesktopLocalMediaUrl,
isDirectPreviewableMediaUrl,
isResolvedRemoteMediaFresh,
@@ -11,6 +12,25 @@ import {
} from "@/session/remoteMedia";
describe("mobile remote media", () => {
+ it.each(['DEVICE_OFFLINE', 'NOT_CONNECTED', 'DEVICE_LINK_NOT_CONNECTED', 'PEER_OFFLINE'])(
+ 'recognizes the structured %s code even when its message has no code', (code) => {
+ expect(mediaLoadFailureKey(Object.assign(new Error('remote device is unavailable'), { code }))).toBe('message.lightbox.deviceOffline');
+ expect(mediaLoadFailureKey({ code, message: 'unavailable' })).toBe('message.lightbox.deviceOffline');
+ },
+ );
+ it('does not misclassify permission, timeout or configuration errors as offline', () => {
+ for (const code of ['REMOTE_DISABLED', 'PERMISSION_DENIED', 'TIMEOUT', 'VERSION_MISMATCH']) {
+ expect(mediaLoadFailureKey(Object.assign(new Error('request failed'), { code }))).toBe('message.lightbox.loadFailed');
+ }
+ expect(mediaLoadFailureKey({ code: 'MEDIA_SOURCE_MISSING' })).toBe('message.lightbox.sourceMissing');
+ expect(mediaLoadFailureKey(new Error('[NOT_CONNECTED] relay closed'))).toBe('message.lightbox.deviceOffline');
+ });
+ it('classifies stable failure codes without exposing paths or assuming every failure is a missing file', () => {
+ expect(mediaLoadFailureKey(new Error('[MEDIA_SOURCE_MISSING] missing'))).toBe('message.lightbox.sourceMissing');
+ expect(mediaLoadFailureKey(new Error('[DEVICE_LINK_NOT_CONNECTED]'))).toBe('message.lightbox.deviceOffline');
+ expect(mediaLoadFailureKey(new Error('ENOENT /private/user.png'))).toBe('message.lightbox.loadFailed');
+ expect(mediaLoadFailureKey(new Error('timeout'))).toBe('message.lightbox.loadFailed');
+ });
it("accepts inline audio and video data URLs for the media player", () => {
expect(isDirectPreviewableMediaUrl("https://example.com/a.mp4")).toBe(true);
expect(isDirectPreviewableMediaUrl("data:image/png;base64,aaa")).toBe(true);
diff --git a/apps/mobile/src/i18n/locales/en/message.json b/apps/mobile/src/i18n/locales/en/message.json
index d878164ce1..e863755281 100644
--- a/apps/mobile/src/i18n/locales/en/message.json
+++ b/apps/mobile/src/i18n/locales/en/message.json
@@ -148,6 +148,8 @@
"turnTokens": "{{tokens}} tokens"
},
"lightbox": {
+ "sourceMissing": "The source image is missing on this computer. Import the original file again.",
+ "deviceOffline": "The computer is disconnected. Reconnect and try again.",
"cancelAnnotation": "Cancel annotation",
"cancel": "Cancel",
"undoLastStroke": "Undo last stroke",
diff --git a/apps/mobile/src/i18n/locales/ja/message.json b/apps/mobile/src/i18n/locales/ja/message.json
index b56af8b26d..89d17b5799 100644
--- a/apps/mobile/src/i18n/locales/ja/message.json
+++ b/apps/mobile/src/i18n/locales/ja/message.json
@@ -148,6 +148,8 @@
"turnTokens": "{{tokens}} tokens"
},
"lightbox": {
+ "sourceMissing": "このコンピューターに元の画像がありません。元のファイルを再度インポートしてください。",
+ "deviceOffline": "コンピューターが未接続です。再接続してからお試しください。",
"cancelAnnotation": "注釈をキャンセル",
"cancel": "キャンセル",
"undoLastStroke": "直前の描画を取り消す",
diff --git a/apps/mobile/src/i18n/locales/ko/message.json b/apps/mobile/src/i18n/locales/ko/message.json
index 215587209b..1e1be6e587 100644
--- a/apps/mobile/src/i18n/locales/ko/message.json
+++ b/apps/mobile/src/i18n/locales/ko/message.json
@@ -148,6 +148,8 @@
"turnTokens": "{{tokens}} tokens"
},
"lightbox": {
+ "sourceMissing": "이 컴퓨터에 원본 이미지가 없습니다. 원본 파일을 다시 가져오세요.",
+ "deviceOffline": "컴퓨터가 연결되지 않았습니다. 다시 연결한 후 시도하세요.",
"cancelAnnotation": "주석 취소",
"cancel": "취소",
"undoLastStroke": "마지막 획 취소",
diff --git a/apps/mobile/src/i18n/locales/zh-CN/message.json b/apps/mobile/src/i18n/locales/zh-CN/message.json
index 30ec5c340e..60f66a96c0 100644
--- a/apps/mobile/src/i18n/locales/zh-CN/message.json
+++ b/apps/mobile/src/i18n/locales/zh-CN/message.json
@@ -148,6 +148,8 @@
"turnTokens": "{{tokens}} tokens"
},
"lightbox": {
+ "sourceMissing": "源图片已不在这台电脑上,请重新导入原文件。",
+ "deviceOffline": "电脑尚未连接,请恢复连接后重试。",
"cancelAnnotation": "取消标注",
"cancel": "取消",
"undoLastStroke": "撤销上一笔",
diff --git a/apps/mobile/src/i18n/locales/zh-TW/message.json b/apps/mobile/src/i18n/locales/zh-TW/message.json
index 387d74f199..91e013a2c6 100644
--- a/apps/mobile/src/i18n/locales/zh-TW/message.json
+++ b/apps/mobile/src/i18n/locales/zh-TW/message.json
@@ -148,6 +148,8 @@
"turnTokens": "{{tokens}} tokens"
},
"lightbox": {
+ "sourceMissing": "原始圖片已不在這台電腦上,請重新匯入原始檔案。",
+ "deviceOffline": "電腦尚未連線,請恢復連線後重試。",
"cancelAnnotation": "取消標註",
"cancel": "取消",
"undoLastStroke": "撤銷上一筆",
diff --git a/apps/mobile/src/session/ImageLightbox.tsx b/apps/mobile/src/session/ImageLightbox.tsx
index 579e5c1f84..1f8cacc071 100644
--- a/apps/mobile/src/session/ImageLightbox.tsx
+++ b/apps/mobile/src/session/ImageLightbox.tsx
@@ -48,6 +48,7 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context';
import type { MobileMessageGalleryImage } from '@/session/messageGallery';
import {
isDesktopLocalMediaUrl,
+ mediaLoadFailureKey,
type MobileResolvedRemoteMedia,
type ResolveRemoteMediaFn,
} from '@/session/remoteMedia';
@@ -100,7 +101,7 @@ const AnimatedImage = Animated.createAnimatedComponent(Image);
type PageResolveState =
| { status: 'loading' }
| { status: 'ready'; media: MobileResolvedRemoteMedia }
- | { status: 'error' };
+ | { status: 'error'; error?: unknown };
/** 宿主注入的底部操作(文件浏览器:复制路径/发送到会话);回调收当前活跃页。 */
export interface ImageLightboxAction {
@@ -442,8 +443,8 @@ export const ImageLightbox = memo(function ImageLightbox({
.then((resolved) => {
setResolveMap((prev) => ({ ...prev, [key]: { status: 'ready', media: resolved } }));
})
- .catch(() => {
- setResolveMap((prev) => ({ ...prev, [key]: { status: 'error' } }));
+ .catch((error: unknown) => {
+ setResolveMap((prev) => ({ ...prev, [key]: { status: 'error', error } }));
});
// 垫底缩略图:列表里这张图已经解码好了,拿来从打开那一刻接住画面,一直垫到
// 原图 onLoad(见 lightboxImageLayers)。写入**不看取件态** —— 原图可能在同
@@ -1587,7 +1588,7 @@ const LightboxPage = memo(function LightboxPage({
(!retryable && !media.previewable) ? (
<>
<Text style={styles.stateText}>
- {t('message.lightbox.loadFailed')}
+ {t(mediaLoadFailureKey(resolveState?.status === 'error' ? resolveState.error : undefined))}
</Text>
{retryable || layers.showFailure ? (
<Pressable
diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsx
index db0ab86882..0a39de7902 100644
--- a/apps/mobile/src/session/MessageRenderer.tsx
+++ b/apps/mobile/src/session/MessageRenderer.tsx
@@ -204,6 +204,7 @@ import {
} from '@/session/filePreview';
import {
groupMobileMarkdownSelectableBlocks,
+ mobileMarkdownManagedImagePreviewUrl,
isMobileMarkdownImageDirectUrl,
mobileMarkdownImageAltChipText,
mobileMarkdownImageTitle,
@@ -310,6 +311,7 @@ import {
} from '@/session/messagePresentation';
import {
formatRemoteMediaSize,
+ mediaLoadFailureKey,
isDesktopLocalMediaUrl,
isDirectPreviewableMediaUrl,
type MobileResolvedRemoteMedia,
@@ -408,6 +410,7 @@ const MESSAGE_LIST_VIEWABILITY_CONFIG_ID = 'message-heavy-content';
// Heavy Markdown blocks inherit the visibility of their outer list cell. Nested
// work/sub-agent cards should not create a second visibility window of their own.
const MessageHeavyContentVisibilityContext = createContext(true);
+const MarkdownRemoteMediaContext = createContext<ResolveRemoteMediaFn | undefined>(undefined);
/** 分享模式吸顶 check 与行内 check 共用 44px 触达高度。 */
const SHARE_STICKY_CHECK_HEIGHT = 44;
@@ -2751,6 +2754,7 @@ export function MessageRenderer({
// chat-text-quote:Provider 恒挂载(值可为 null),避免启用态翻转时整棵消息树
// 因 Provider 增删而重挂;value 稳定(useMemo),不触发订阅方重渲。
<SelectionQuoteContext.Provider value={selectionQuoteContextValue}>
+ <MarkdownRemoteMediaContext.Provider value={onResolveRemoteMedia}>
<View
style={styles.messageFrame}
onTouchStart={handleHistoryTouchStart}
@@ -2889,6 +2893,7 @@ export function MessageRenderer({
/>
)}
</View>
+ </MarkdownRemoteMediaContext.Provider>
</SelectionQuoteContext.Provider>
);
}
@@ -5408,6 +5413,7 @@ function MarkdownBody({
const { t } = useTranslation();
const styles = useThemedStyles(makeStyles);
const chatFilePathContext = useContext(ChatFilePathContext);
+ const resolveMarkdownMedia = useContext(MarkdownRemoteMediaContext);
// iOS UITextView 在 stretch/百分比宽度下会偶发只量出部分高度,LegendList 按这次
// 偏矮的 onLayout 裁切 agent 回复;点分享会换上确定宽度的容器从而完整显示。
// 外层始终 stretch 测可用宽,内层再钉像素宽:测宽不能钉在自己身上,否则旋转/
@@ -5445,15 +5451,25 @@ function MarkdownBody({
});
}, [markdownParse]);
const blocks = markdownParse.result.blocks;
- // Android 的 selectable Text 内嵌 View(直连内联图)行为未定义,含这类 inline 的块不开选中。
+ const managedImagePreviewUrl = useCallback((url: string) => resolveMarkdownMedia
+ ? mobileMarkdownManagedImagePreviewUrl(
+ url, chatFilePathContext?.workdir, markdownImageCacheKey,
+ chatFilePathContext?.remoteHostId, chatFilePathContext?.sessionId,
+ ) : null, [chatFilePathContext, markdownImageCacheKey, resolveMarkdownMedia]);
+ const imageRendersPreview = useCallback((inline: Extract<MobileMarkdownInline, { type: 'image' }>) => (
+ isMobileMarkdownImageDirectUrl(inline.url) || managedImagePreviewUrl(inline.url) !== null
+ ), [managedImagePreviewUrl]);
+ // 图片预览内嵌 View,不能放进 Android selectable Text 或 iOS UITextView。
const inlinesSelectable = useCallback((inlines: readonly MobileMarkdownInline[]) => (
selectable === true
- && !inlines.some((inline) => inline.type === 'image' && isMobileMarkdownImageDirectUrl(inline.url))
- ), [selectable]);
+ && !inlines.some((inline) => inline.type === 'image' && imageRendersPreview(inline))
+ ), [imageRendersPreview, selectable]);
// 正文 Markdown 图片(![](url) / 安全 <img>)点击后走既有媒体 payload 查看器,与附件图片同一条链路。
const openMarkdownImage = useCallback((url: string, alt?: string) => {
if (!onOpenPayload) return;
- const resolvedUrl = mobileMarkdownImageUrlForWorkdir(
+ // Match the scoped preview/gallery URL; outside-workdir chips remain
+ // explicitly openable through the existing single-image fallback.
+ const resolvedUrl = managedImagePreviewUrl(url) ?? mobileMarkdownImageUrlForWorkdir(
url,
chatFilePathContext?.workdir,
markdownImageCacheKey,
@@ -5472,6 +5488,7 @@ function MarkdownBody({
chatFilePathContext?.sessionId,
chatFilePathContext?.workdir,
markdownImageCacheKey,
+ managedImagePreviewUrl,
onOpenPayload,
]);
const openMarkdownMedia = useMemo(() => onOpenPayload
@@ -5479,6 +5496,24 @@ function MarkdownBody({
onOpenPayload(buildMediaPayload({ kind, url, title, previewable: false }, title));
}
: undefined, [onOpenPayload]);
+ const renderManagedImage = useCallback((inline: Extract<MobileMarkdownInline, { type: 'image' }>) => {
+ const url = managedImagePreviewUrl(inline.url);
+ if (!url) return null;
+ const size = mobileMarkdownInlineImageSize(inline);
+ const label = mobileMarkdownImageTitle(url, inline.alt);
+ return (
+ <View style={size}>
+ <MediaPreview
+ key={url}
+ layout={{ ...layout, imagePreviewWidth: size.width, imagePreviewHeight: size.height }}
+ media={{ kind: 'image', url, title: label, previewable: false }}
+ label={label}
+ onOpen={onOpenPayload ? () => openMarkdownImage(inline.url, inline.alt) : undefined}
+ onResolveRemoteMedia={resolveMarkdownMedia}
+ />
+ </View>
+ );
+ }, [layout, managedImagePreviewUrl, onOpenPayload, openMarkdownImage, resolveMarkdownMedia]);
// Preserve the inline renderer while streaming or unrelated task metadata
// changes; referenced task title changes still refresh every affected chip.
const remoteSessions = useRemoteSessions();
@@ -5503,6 +5538,7 @@ function MarkdownBody({
baseStyle,
keyPrefix,
onOpenImage: openMarkdownImage,
+ renderManagedImage,
onOpenMedia: openMarkdownMedia,
onOpenSessionLink,
sessionReferenceDetails,
@@ -5513,12 +5549,13 @@ function MarkdownBody({
),
// renderInline also reads translated fallback labels. Invalidate completed
// memoized text blocks when useTranslation refreshes its bound translator.
- [onOpenSessionLink, openMarkdownImage, openMarkdownMedia, sessionLinkTitles, sessionReferenceDetails, streaming, styles, t],
+ [onOpenSessionLink, openMarkdownImage, openMarkdownMedia, renderManagedImage, sessionLinkTitles, sessionReferenceDetails, streaming, styles, t],
);
- const textRunGroupingOptions = Platform.OS === 'android'
- ? ANDROID_SELECTABLE_TEXT_RUN_GROUPING_OPTIONS
- : undefined;
- // 连续纯文本块合并为 text_run(跨段选择),代码块/表格/mermaid/含直连图块保持独立。
+ const textRunGroupingOptions = useMemo(() => ({
+ ...(Platform.OS === 'android' ? ANDROID_SELECTABLE_TEXT_RUN_GROUPING_OPTIONS : {}),
+ imageRendersPreview,
+ }), [imageRendersPreview]);
+ // 连续纯文本块合并为 text_run,图片预览保持独立。
// Android selectable Text 在超长原生文本视图里会偶发高度/滚动协商异常,长 run 分块
// 后仍保留块内跨段选择,同时避免单个 LegendList item 内出现巨型 selectable Text。
const groups = useMemo(
@@ -6031,6 +6068,7 @@ function renderInline(
/** text_run 合并树里多个块共父,key 需要块级前缀防冲突。 */
keyPrefix?: string;
onOpenImage?: (url: string, alt?: string) => void;
+ renderManagedImage?: (inline: Extract<MobileMarkdownInline, { type: 'image' }>) => ReactNode;
onOpenMedia?: (url: string, title: string, kind: 'video') => void;
onOpenPayload?: (payload: MessagePayload) => void;
onOpenSessionLink?: (url: string) => void;
@@ -6166,11 +6204,14 @@ function renderInline(
</SpanText>
);
case 'image': {
+ const managedPreview = ctx.renderManagedImage?.(inline);
+ if (managedPreview) {
+ return <Text key={spanKey(`image:${index}:${inline.url}`)} testID="message.markdownManagedImage">{managedPreview}</Text>;
+ }
// openImage 由上层可选注入 → 缺席时 chip 不可点,下划线也必须跟着不加
// (clickableInlineStyle 保证两者同源)。
const openImageChip = openImage ? () => openImage(inline.url, inline.alt) : undefined;
- // xdt 系非直连图:RN Image 无法直接加载内部 scheme,渲染可点 chip,
- // 点开后由 ImageLightbox 经 remote-media resolver 取图。
+ // 缺少路径上下文时保留文字入口;合法受管图由上面的 MediaPreview 取件。
if (!isMobileMarkdownImageDirectUrl(inline.url)) {
const imageChipText = inline.alt
? mobileMarkdownImageAltChipText(inline.alt)
@@ -6544,8 +6585,8 @@ function MediaPreview({
.then((resolved) => {
if (!cancelled && !signal?.aborted) setResolveState({ status: 'ready', media: resolved });
})
- .catch(() => {
- if (!cancelled && !signal?.aborted) setResolveState({ status: 'error' });
+ .catch((error: unknown) => {
+ if (!cancelled && !signal?.aborted) setResolveState({ status: 'error', error });
});
return () => {
cancelled = true;
@@ -6566,7 +6607,7 @@ function MediaPreview({
const phase = mediaThumbnailPhase(media, resolveState, !!onResolveRemoteMedia);
const { t } = useTranslation();
const fallbackDetail = phase.kind === 'fallback' && phase.reason === 'error'
- ? t('message.lightbox.loadFailed') : preview.detail;
+ ? t(mediaLoadFailureKey(resolveState.status === 'error' ? resolveState.error : undefined)) : preview.detail;
const thumbUri = phase.kind === 'direct' ? media.url : phase.kind === 'resolved' ? phase.uri : null;
const handleImageError = useCallback(() => {
diff --git a/apps/mobile/src/session/mediaThumbnail.ts b/apps/mobile/src/session/mediaThumbnail.ts
index bfc45b9faa..f54484a5eb 100644
--- a/apps/mobile/src/session/mediaThumbnail.ts
+++ b/apps/mobile/src/session/mediaThumbnail.ts
@@ -38,7 +38,7 @@ export type MediaThumbnailResolveState =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'ready'; media: MobileResolvedRemoteMedia }
- | { status: 'error' };
+ | { status: 'error'; error?: unknown };
/** 是否应在 mount 时自动取件:仅 image、不可直接预览、桌面端媒体 URL、且有取件回调。 */
export function shouldAutoResolveMediaThumbnail(
diff --git a/apps/mobile/src/session/messageGallery.ts b/apps/mobile/src/session/messageGallery.ts
index 07c9bf2d75..dce580414d 100644
--- a/apps/mobile/src/session/messageGallery.ts
+++ b/apps/mobile/src/session/messageGallery.ts
@@ -2,7 +2,7 @@ import {
collectMobileMarkdownImages,
isMobileMarkdownImageDirectUrl,
mobileMarkdownImageTitle,
- mobileMarkdownImageUrlForWorkdir,
+ mobileMarkdownManagedImagePreviewUrl,
} from '@/session/messageMarkdown';
import type { NormalizedRemoteMessage, NormalizedToolMedia } from '@/session/messageNormalize';
import type { MessagePayload } from '@/session/messagePayload';
@@ -73,7 +73,9 @@ export function collectMobileMessageGalleryImages(
// 正文 Markdown 图片(![](url) / 安全 <img>)也纳入图集,点开后可与附件图片一起横滑翻页。
if (includeBodyImages && message.body) {
collectMobileMarkdownImages(message.body).forEach((image, index) => {
- const url = mobileMarkdownImageUrlForWorkdir(
+ // The viewer automatically fetches neighboring pages. Apply the same
+ // workdir/realpath boundary as inline previews before adding any page.
+ const url = isMobileMarkdownImageDirectUrl(image.url) ? image.url : mobileMarkdownManagedImagePreviewUrl(
image.url,
workdir,
message.key,
diff --git a/apps/mobile/src/session/messageMarkdown.ts b/apps/mobile/src/session/messageMarkdown.ts
index b3ad2830d4..6cf5e0e57a 100644
--- a/apps/mobile/src/session/messageMarkdown.ts
+++ b/apps/mobile/src/session/messageMarkdown.ts
@@ -8,6 +8,7 @@ import {
classifyChatPathLinkTarget,
findBareFilePathMatch,
resolveChatAbsPath,
+ toWorkdirRel,
} from '@/session/chatPathCandidate';
import { DEEP_LINK_SCHEME_GROUP } from '@/session/sessionLinks';
import { i18n } from '@/i18n';
@@ -628,6 +629,24 @@ export function mobileMarkdownImageUrlForWorkdir(
return `${base}${sshContext}${version}`;
}
+/** Automatic previews must not upload arbitrary model-named files. Explicit
+ * clicks keep the existing viewer path; the Host enforces baseDir after realpath. */
+export function mobileMarkdownManagedImagePreviewUrl(
+ url: string, workdir?: string, cacheKey?: string, remoteHostId?: string, sessionId?: string,
+): string | null {
+ const resolved = mobileMarkdownImageUrlForWorkdir(url, workdir, cacheKey, remoteHostId, sessionId);
+ if (!resolved || isMobileMarkdownImageDirectUrl(resolved)) return null;
+ if (!resolved.startsWith('xdt-file://')) return resolved;
+ if (!workdir) return null;
+ const parsed = new URL(resolved);
+ const file = parsed.searchParams.get('path');
+ if (!file || toWorkdirRel(workdir, file) === null) return null;
+ // Do not trust baseDir supplied in Markdown (including duplicate parameters).
+ parsed.searchParams.delete('baseDir');
+ parsed.searchParams.set('baseDir', workdir);
+ return parsed.toString();
+}
+
// 流式(native)路径下正文图片的缩略图尺寸:默认宽 150,宽高都封顶 220。有声明宽高时按比例
// 换算,但换算结果同样封顶——width/height 只过了「1-4 位纯数字」白名单,height="9999" 这类
// 极端比例若不封顶会在流式阶段渲染出近万像素高的图,撑爆气泡(review P2)。
@@ -646,6 +665,8 @@ export type MobileMarkdownBlockGroup =
| { type: 'single'; key: string; block: MobileMarkdownBlock };
export interface MobileMarkdownTextRunGroupingOptions {
+ /** Only actual native image previews break text selection; text chips do not. */
+ imageRendersPreview?: (image: MobileMarkdownImageInline) => boolean;
/**
* Upper bound for one selectable native text view. Undefined keeps the
* historical "merge until a non-text block" behavior.
@@ -684,7 +705,7 @@ export function groupMobileMarkdownSelectableBlocks(
runInlineFragmentCount = 0;
};
for (const block of blocks) {
- if (isTextRunBlock(block)) {
+ if (isTextRunBlock(block, options?.imageRendersPreview)) {
for (const chunk of splitOversizedTextRunBlock(
block,
maxTextRunUtf16Length,
@@ -721,13 +742,16 @@ export function groupMobileMarkdownSelectableBlocks(
return groups;
}
-function isTextRunBlock(block: MobileMarkdownBlock): block is MobileMarkdownTextRunBlock {
+function isTextRunBlock(
+ block: MobileMarkdownBlock,
+ imageRendersPreview: (image: MobileMarkdownImageInline) => boolean = (image) => isMobileMarkdownImageDirectUrl(image.url),
+): block is MobileMarkdownTextRunBlock {
if (block.type !== 'paragraph' && block.type !== 'heading' && block.type !== 'list_item') {
return false;
}
- // 直连内联图渲染为 Text 内嵌 View,不能进合并文本树(Android selectable+内嵌 View 行为未定义)。
+ // Only image previews embed Views; fallback text chips remain selectable.
return !block.inlines.some(
- (inline) => inline.type === 'image' && isMobileMarkdownImageDirectUrl(inline.url),
+ (inline) => inline.type === 'image' && imageRendersPreview(inline),
);
}
@@ -1572,7 +1596,10 @@ function matchMarkdownImage(
let candidate: RegExpExecArray | null;
while ((candidate = matcher.re.exec(input)) !== null) {
const rawUrl = matcher.local ? parseLocalMarkdownDestination(candidate[2]) : candidate[2];
- if (matcher.local && !classifyChatPathLinkTarget(rawUrl)) continue;
+ // The destination parser also handles standard titles and angle brackets
+ // for remote images, just as it does for local image paths.
+ if (matcher.local && !classifyChatPathLinkTarget(rawUrl)
+ && !(SAFE_IMAGE_SRC_RE.test(rawUrl) && !/\s/.test(rawUrl))) continue;
// 当前 matcher 的第一个正则命中可能只是注释/转义里的示例;必须继续 exec,
// 否则同段后面的合法图片会被丢掉(review P2)。
if (isInsideHtmlComment(guarded, candidate.index, startsInsideHtmlComment)) continue;
diff --git a/apps/mobile/src/session/remoteMedia.ts b/apps/mobile/src/session/remoteMedia.ts
index e6e3c44d39..85414bada1 100644
--- a/apps/mobile/src/session/remoteMedia.ts
+++ b/apps/mobile/src/session/remoteMedia.ts
@@ -10,6 +10,17 @@ import { mobileDebugLog } from "@/debug/mobileDebugLog";
const EXPIRY_SAFETY_WINDOW_MS = 60 * 1000;
+/** Match stable Host/transport codes, never expose raw errors containing local paths. */
+export function mediaLoadFailureKey(error: unknown): 'message.lightbox.sourceMissing' | 'message.lightbox.deviceOffline' | 'message.lightbox.loadFailed' {
+ const code = error && typeof error === 'object' && 'code' in error ? error.code : undefined;
+ const message = error instanceof Error ? error.message : String(error ?? '');
+ if (code === 'MEDIA_SOURCE_MISSING' || message.includes('[MEDIA_SOURCE_MISSING]')) return 'message.lightbox.sourceMissing';
+ const offlineCodes = ['NOT_CONNECTED', 'DEVICE_LINK_NOT_CONNECTED', 'DEVICE_OFFLINE', 'PEER_OFFLINE'];
+ if ((typeof code === 'string' && offlineCodes.includes(code))
+ || /\b(?:NOT_CONNECTED|DEVICE_LINK_NOT_CONNECTED|DEVICE_OFFLINE|PEER_OFFLINE)\b/.test(message)) return 'message.lightbox.deviceOffline';
+ return 'message.lightbox.loadFailed';
+}
+
export interface MobileRemoteMediaFetchResult {
/** inline 缩略图回包(新被控端)时为空串。 */
ossKey: string;
diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.md
index d6ce6efcce..5ae8ae7d5a 100644
--- a/docs/dev-rules/media-storage-and-protocols.md
+++ b/docs/dev-rules/media-storage-and-protocols.md
@@ -142,6 +142,25 @@
## 多端与远程
+- 将已有截图或本地图片保存为持久受管附件时,Agent 使用 Core `media(action: "import_image", path)`:Host
+ 按当前任务文件权限读取,在当前媒体仓入库并登记任务引用,再返回 `xdt_image_urls`。
+ 工作目录外路径复用现有授权,SSH 任务不得回退读取控制端同名文件。返回成功仅表示
+ Host 已完成交付准备,不代表手机已经下载;禁止自行拼接 hash 或复用其它沙盒的地址。
+ 同一任务并发导入相同内容时,引用检查与登记必须串行,避免重复持久引用。
+- 工具选择由 Agent 根据自然语言与已有文件上下文完成,不能要求用户记住工具名或参数。
+ 验收必须另用不含工具名、action 或协议提示的新任务验证自然语言交付;指定工具的调用测试
+ 只证明工具链路可用,不能证明普通用户的使用路径已通过。
+- `import_image` 不是客户端展示图片的唯一入口。Agent 输出正常的本地 Markdown 图片时,
+ 客户端继续按当前任务的路径上下文和既有权限取件,不能因为没有调用指定工具而拒绝展示。
+ 直接路径仍依赖源文件存在,不得把这种兼容展示表述成已持久入库。
+- Mobile 自动预览本地 Markdown 图片仅限当前任务工作目录内;请求携带当前目录作为
+ `baseDir`,Host 按真实路径复验,防止符号链接越界。目录外图片保留显式点击入口。
+ 正文图集收集同样复用此边界,避免查看器预取相邻页时绕过限制;目录外文字入口点击后
+ 只打开该图片,不把它加入其它图片的预取集合。目录内缩略图、点击地址与图集地址保持一致。
+- Mobile 正文 Markdown 的受管图片与附件共用缩略图取件队列和原图查看器。实际包含预览的块
+ 不合并进原生可选文本;回退到文字入口时保留正文选择。错误按稳定代码区分源文件缺失与设备离线,旧 Host 的未分类错误
+ 保留通用加载失败。原生解码失败沿用一次强制刷新,手动重试仅重取当前图片。
+
- 媒体记录必须保留设备、会话、插件或远程主机等真实归属信息,不能相信调用方自报的
本机路径或 owner id。
- 远程来源无法证明、路径归属不明确或媒体类型无法安全确认时应 fail closed,不得回退读取
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 0ee8c28b7b..1224b99245 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -210,6 +210,13 @@ link-accept 双向声明,不改 relay)。Desktop 控制端在本机没有订
受信 renderer 开放。不改 relay、帧限制或服务器权限,服务端无需改动。实现见
`apps/desktop/src/main/usage/usageDeviceRows.ts` 与 `peerUsageSync.ts`。
+## 图片交付与缺失源文件
+
+媒体取件沿用既有 `MEDIA_FETCH_FAILED` 错误包;源图片不存在时,Host 在消息中附加
+`[MEDIA_SOURCE_MISSING]` 稳定标记,不回传本机路径。新版 Mobile 据此提示重新导入,
+旧版继续按通用加载失败处理;新版连接旧 Host 时也保留通用失败回退。不改变 relay、
+取件权限、缓存键或重试范围,不需要服务端同步上线。
+
## 图片标注区域说明
`maker:input:enqueue` / `maker:input:steer` / `maker:input:update-content` 的队列附件
diff --git a/packages/cindy-tools/src/__tests__/ghostMcp.test.ts b/packages/cindy-tools/src/__tests__/ghostMcp.test.ts
index adb202f47b..9eba73713a 100644
--- a/packages/cindy-tools/src/__tests__/ghostMcp.test.ts
+++ b/packages/cindy-tools/src/__tests__/ghostMcp.test.ts
@@ -1147,6 +1147,14 @@ describe("cindy_ghosts · ghost_call(派活透传)", () => {
});
describe("cindy · media MCP 边界", () => {
+ it("imports existing screenshots without requiring a model or plugin", async () => {
+ const callMedia = vi.fn(async () => ({ ok: true, xdt_image_urls: ['cindy-media://blobs/image.png'] }));
+ const deps = fakeDeps({ callMedia });
+ expect(parsePayload(await handleMedia(deps, { action: 'import_image' }))).toMatchObject({ errorCode: 'INVALID_INPUT' });
+ expect(callMedia).not.toHaveBeenCalled();
+ expect(parsePayload(await handleMedia(deps, { action: 'import_image', path: 'screenshots/actual.png' }))).toMatchObject({ ok: true });
+ expect(callMedia).toHaveBeenCalledWith({ action: 'import_image', path: 'screenshots/actual.png' });
+ });
it("把 snake_case 输入转换为 Host 稳定类型", async () => {
const callMedia = vi.fn(async () => ({ ok: true, status: "prepared" }));
const result = await handleMedia(fakeDeps({ callMedia }), {
diff --git a/packages/cindy-tools/src/ghost/mcpServer.ts b/packages/cindy-tools/src/ghost/mcpServer.ts
index 1d430798a8..69d890c6c8 100644
--- a/packages/cindy-tools/src/ghost/mcpServer.ts
+++ b/packages/cindy-tools/src/ghost/mcpServer.ts
@@ -98,8 +98,10 @@ const D_GHOST_CALL = [
].join("\n");
const D_MEDIA = [
- "Cindy Core 的低级媒体接口,供当前 Agent 执行插件提供的上层能力;插件负责场景语义和可选 UI,Core 负责模型调用与媒体交付。",
- "常规链路是 Agent 先调用插件工具,读取插件返回的普通 JSON,再自行调用本工具;不存在 mediaIntent 等保留结果格式,Host 也不会自动把插件结果转成本工具调用。",
+ "交付已有图片与生成新媒体的 Core 接口。用户只需说「把截图发给我」「让我在手机上看这张图」等自然语言;由 Agent 根据已有文件和上下文完成交付,不要求用户提供工具名、action、协议或 hash。",
+ "发送已有截图或本地图片:直接调用本工具的 import_image,path 使用实际文件路径。此动作无需生成模型或插件,不要走生成新图片流程。Host 按当前任务文件权限读取、统一入库并登记引用,成功后才返回 xdt_image_urls。",
+ "查看图片供自己分析不等于把图片发给用户;用户要求查看成果时,应在回复中展示可交付图片,不能仅说「已发送」或只给本机路径。用户反馈打不开时,根据工具结果检查或重新导入已知原文件,不要反复发送同一失效地址;原文件不在上下文中时才询问文件来源。不得自行拼接 hash、冒用其它沙盒地址或把导入成功说成手机已经下载。",
+ "生成新图片或视频时,插件负责场景语义和可选 UI,Core 负责模型调用与媒体交付。Agent 先调用插件工具,读取插件返回的普通 JSON,再自行调用本工具;不存在 mediaIntent 等保留结果格式,Host 也不会自动把插件结果转成本工具调用。",
"如果插件需要消费最终结果,Agent 可在本工具成功后再调用插件声明的普通工具,并通过 ghost_call.attachments 显式交接;这不是生成请求的必经步骤。",
"模型存在性来自当前账号的 Model Access model group;list_models 只投影同时满足 Gateway modalities、Guide operation 与当前客户端协议支持度的可执行模型。",
"媒体生成必须由当前 Agent 通过本工具发起;插件面板和插件沙箱代码不得直接提交生成请求。",
@@ -556,7 +558,8 @@ const MEDIA_CAPABILITIES = new Set<CindyMediaCapability>([
export async function handleMedia(
deps: CindyGhostsMcpDeps,
input: {
- action: "list_models" | "resolve_local_path" | "prepare" | "request" | "poll";
+ action: "list_models" | "resolve_local_path" | "import_image" | "prepare" | "request" | "poll";
+ path?: string;
capability?: CindyMediaCapability;
url?: string;
provider_id?: string;
@@ -588,6 +591,9 @@ export async function handleMedia(
action: "list_models",
...(input.capability ? { capability: input.capability } : {}),
});
+ } else if (input.action === "import_image") {
+ if (!input.path?.trim()) return textResult({ ok: false, errorCode: "INVALID_INPUT", message: "import_image 必须提供 path。" }, true);
+ result = await deps.callMedia({ action: "import_image", path: input.path });
} else if (input.action === "resolve_local_path") {
if (!input.url) {
return textResult(
@@ -1337,8 +1343,9 @@ export function createCindyGhostsMcpServer(
D_MEDIA,
{
action: z
- .enum(["list_models", "resolve_local_path", "prepare", "request", "poll"])
+ .enum(["list_models", "resolve_local_path", "import_image", "prepare", "request", "poll"])
.describe("要执行的媒体调用阶段"),
+ path: z.string().max(32768).optional().describe("import_image 时必填;实际存在的图片路径,相对路径按当前任务工作目录解析"),
capability: z
.enum([
"image.generate",
diff --git a/packages/cindy-tools/src/types.ts b/packages/cindy-tools/src/types.ts
index 9d52e0706d..cb04ad6317 100644
--- a/packages/cindy-tools/src/types.ts
+++ b/packages/cindy-tools/src/types.ts
@@ -337,6 +337,7 @@ export type CindyMediaCapability =
export type CindyMediaToolRequest =
| { action: 'list_models'; capability?: CindyMediaCapability }
| { action: 'resolve_local_path'; url: string }
+ | { action: 'import_image'; path: string }
| {
action: 'prepare';
/** 精确执行来源;插件配置返回 providerId 时必须原样传入。 */