Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(media): 保留手机和桌面已发送图片的离线缓存
Dash42dde81a272a
diff --git a/apps/desktop/src/main/device-link/__tests__/outboundMediaCompress.test.ts b/apps/desktop/src/main/device-link/__tests__/outboundMediaCompress.test.tsindex c4a95f5e03..4b273ee43b 100644--- a/apps/desktop/src/main/device-link/__tests__/outboundMediaCompress.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/outboundMediaCompress.test.ts@@ -32,7 +32,7 @@ vi.mock('../../logger', () => ({createLogger: () => ({ info: vi.fn(), debug: vi.fn(), warn: vi.fn(), error: vi.fn() }),}));-import { rewriteOutboundMedia } from '../outboundMedia';+import { rewriteOutboundMedia, withPeerAttachmentUpload } from '../outboundMedia';import { isAttachmentOssRef } from '../../../shared/attachmentOssRef';const SHA256 = 'a'.repeat(64);@@ -60,6 +60,19 @@ beforeEach(() => {});describe('base64 来源', () => {+ // Unit regression: old coverage checked upload bytes, not the retained offline copy.+ // A compressed send must retain the same bytes/ref before handing off its message.+ it('retains the actual compressed upload for offline history before returning', async () => {+ const bytes = Buffer.from([9, 9]);+ compressOutboundImage.mockResolvedValue({ bytes, contentType: 'image/jpeg', ext: 'jpg' });+ let retained: { source: string | Buffer; ref: string } | undefined;+ const result = await withPeerAttachmentUpload(async () => null, () => rewriteOutboundMedia('maker:send', [+ 'session', { type: 'user', content: [{ type: 'image', base64: 'AQID', mimeType: 'image/png' }] },+ ]), async (source, ref) => { retained = { source, ref }; });+ const ref = (result[1] as { content: Array<{ path: string }> }).content[0].path;+ expect(retained).toEqual({ source: bytes, ref });+ });+it('压缩命中 → uploadBuffer 收到压缩字节与压缩后的 contentType/ext', async () => {const compressedBytes = Buffer.from([9, 9]);compressOutboundImage.mockResolvedValue({diff --git a/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.ts b/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.tsindex 9e99460c24..990c1e73dc 100644--- a/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/remoteMediaPersistentCache.test.ts@@ -5,6 +5,7 @@ import { createHash } from 'node:crypto';import fs from 'node:fs/promises';import os from 'node:os';import path from 'node:path';+import { buildAttachmentOssRef, buildPeerAttachmentRef } from '@cindy/device-link';import type { LedgerDb } from '../../cindy-media/ledger';const state = vi.hoisted(() => ({@@ -31,6 +32,7 @@ import { listAllBlobRecords, listCacheBlobsByLru } from '../../cindy-media/ledgeimport { resolveHashRef } from '../../cindy-media/blobStore';import {invalidateRemoteMediaPersistentWrites,+ createSentImageRetainer,remoteMediaPersistentCache,} from '../remoteMediaPersistentCache';import { remoteFileVersionCache } from '../remoteFileVersionCache';@@ -95,6 +97,35 @@ it('reuses image bytes after closing and reopening the database, across login geexpect((await listCacheBlobsByLru(state.db, 'remote-media')).length).toBe(1);});+// Existing DB-tier coverage only persisted downloaded images. Sent bytes must populate+// that same index before any read from the host, and survive process/database restart.+it.each(['oss', 'peer'] as const)('retains a sent %s image by the final content address across restart', async (transport) => {+ const metadata = { sha256: createHash('sha256').update(png).digest('hex'), size: png.length, mimeType: 'image/png' };+ const ref = transport === 'oss' ? buildAttachmentOssRef({ ...metadata, ossKey: 'uploads/image' })+ : buildPeerAttachmentRef({ ...metadata, ticket: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' });+ const source = path.join(state.root, 'sent.png');+ await fs.writeFile(source, png);+ await createSentImageRetainer('computer')(transport === 'oss' ? png : source, ref);+ await fs.rm(source);+ raw.close();+ state.generation++;+ reopen();+ expect((await remoteMediaPersistentCache('computer', url)!.get(1024))?.bytes).toEqual(png);+ expect(await remoteMediaPersistentCache('another-computer', url)!.get(1024)).toBeNull();+});++it.each(['switch-account', 'clear-cache', 'changed-source'] as const)('does not retain stale sent pixels after %s', async (change) => {+ const retain = createSentImageRetainer('computer');+ const ref = buildAttachmentOssRef({ ossKey: 'uploads/image', size: png.length,+ sha256: createHash('sha256').update(png).digest('hex'), mimeType: 'image/png' });+ if (change === 'switch-account') { state.owner = 'bob'; state.generation++; }+ if (change === 'clear-cache') invalidateRemoteMediaPersistentWrites();+ const pending = retain(change === 'changed-source' ? Buffer.alloc(png.length) : png, ref);+ if (change === 'changed-source') await pending;+ else await expect(pending).rejects.toThrow();+ expect(await listCacheBlobsByLru(state.db, 'remote-media')).toEqual([]);+});+it('direct transfers and preview downloads share one persistent index and cleanup category', async () => {const direct = remoteFileVersionCache('computer', url, {version: 'b'.repeat(64),diff --git a/apps/desktop/src/main/device-link/ipc.ts b/apps/desktop/src/main/device-link/ipc.tsindex 2a18d25efe..14fc91eb63 100644--- a/apps/desktop/src/main/device-link/ipc.ts+++ b/apps/desktop/src/main/device-link/ipc.ts@@ -65,6 +65,7 @@ import {} from './index';import { getActiveControllers } from './dispatch';import { rewriteOutboundMedia, withPeerAttachmentUpload } from './outboundMedia';+import { createSentImageRetainer } from './remoteMediaPersistentCache';import { withOutboundReviewConfirmation } from '../maker-ipc/reviewOutboundInput.js';import { confirmReviewArtifacts } from '../reviewer/confirmReviewArtifacts.js';import { tryUploadPeerAttachment } from './filePeer';@@ -707,7 +708,8 @@ export async function handleInvoke(}callArgs = await withPeerAttachmentUpload((source, mime) => peer ? Promise.resolve(null) : tryUploadPeerAttachment(normalizedDeviceId, source, mime, deps.invoke), () =>withSharedTaskMedia(peer?.role === 'host' ? peer.sharedTaskId : undefined,- () => existing ? deps.rewriteOutboundMedia!(channel, callArgs, existing) : deps.rewriteOutboundMedia!(channel, callArgs)));+ () => existing ? deps.rewriteOutboundMedia!(channel, callArgs, existing) : deps.rewriteOutboundMedia!(channel, callArgs)),+ createSentImageRetainer(normalizedDeviceId));} catch (err) {if (isIpcError(err) && err.code === 'DEVICE_LINK_CHANNEL_NOT_ALLOWED') {throw err;diff --git a/apps/desktop/src/main/device-link/outboundMedia.ts b/apps/desktop/src/main/device-link/outboundMedia.tsindex c1aa16d0a1..dee4fea79c 100644--- a/apps/desktop/src/main/device-link/outboundMedia.ts+++ b/apps/desktop/src/main/device-link/outboundMedia.ts@@ -15,9 +15,10 @@ import path from 'node:path';import { AsyncLocalStorage } from 'node:async_hooks';import { buildPeerAttachmentRef, parsePeerAttachmentRef } from '@cindy/device-link';type PeerUpload = (source: string | Buffer, mimeType?: string) => Promise<string | null>;-const peerUploadContext = new AsyncLocalStorage<PeerUpload>();-export function withPeerAttachmentUpload<T>(upload: PeerUpload, operation: () => Promise<T>) {- return peerUploadContext.run(upload, operation);+type RetainSentImage = (source: string | Buffer, ref: string) => Promise<void>;+const peerUploadContext = new AsyncLocalStorage<{ upload: PeerUpload; retain?: RetainSentImage }>();+export function withPeerAttachmentUpload<T>(upload: PeerUpload, operation: () => Promise<T>, retain?: RetainSentImage) {+ return peerUploadContext.run({ upload, retain }, operation);}import { createLogger } from '../logger';@@ -100,14 +101,20 @@ async function uploadAttachment(src: AttachmentSource): Promise<string> {const context = peerUploadContext.getStore();if (!context || sharedTaskMediaId()) return null;const effectiveMime = contentType ?? mimeOf(typeof source === 'string' ? path.extname(source).slice(1).toLowerCase() : 'bin');- const result = await context(source, effectiveMime);+ const result = await context.upload(source, effectiveMime);const parsed = result && parsePeerAttachmentRef(result);return parsed ? buildPeerAttachmentRef({ ...parsed, originalName }) : null;};- const bufferRef = async (bytes: Buffer, options: Parameters<typeof uploadBuffer>[1]) =>- await direct(bytes, options?.contentType) ?? buildUploadedAttachmentRef(await uploadBuffer(bytes, options), originalName);- const fileRef = async (file: string, options: Parameters<typeof uploadLocalFile>[1]) =>- await direct(file, options?.contentType) ?? buildUploadedAttachmentRef(await uploadLocalFile(file, options), originalName);+ const retain = async (source: string | Buffer, ref: string) => {+ // The compressed bytes may have a different content address from the local draft.+ // Save before returning so history can immediately resolve its final address offline.+ await peerUploadContext.getStore()?.retain?.(source, ref).catch(() => undefined);+ return ref;+ };+ const bufferRef = async (bytes: Buffer, options: Parameters<typeof uploadBuffer>[1]) => retain(bytes,+ await direct(bytes, options?.contentType) ?? buildUploadedAttachmentRef(await uploadBuffer(bytes, options), originalName));+ const fileRef = async (file: string, options: Parameters<typeof uploadLocalFile>[1]) => retain(file,+ await direct(file, options?.contentType) ?? buildUploadedAttachmentRef(await uploadLocalFile(file, options), originalName));// 1) 内存 base64(剪贴板/截图,视觉上下文语义)→ 压缩 → uploadBuffer(不把字节内联进 relay)if (typeof src.base64 === 'string' && src.base64) {diff --git a/apps/desktop/src/main/device-link/remoteMediaPersistentCache.ts b/apps/desktop/src/main/device-link/remoteMediaPersistentCache.tsindex 655bde5d5e..c86e14ad2b 100644--- a/apps/desktop/src/main/device-link/remoteMediaPersistentCache.ts+++ b/apps/desktop/src/main/device-link/remoteMediaPersistentCache.ts@@ -1,6 +1,8 @@import { createHash } from 'node:crypto';import fs from 'node:fs/promises';import { constants } from 'node:fs';+import { parseAttachmentOssRef, parsePeerAttachmentRef } from '@cindy/device-link';+import { blobUrl, extForMime } from '../cindy-media/blobStore';import { getActiveAppSession } from '../appSessionState';import { getDbClient } from '../localDb/client/current';import { integrationCacheGet, integrationCachePut } from '../cindy-media/integrationCache';@@ -37,6 +39,41 @@ export function isImmutableRemoteMedia(url: string): boolean {);}+/** Seed the existing device/account cache with the actual upload, including compressed+ * images. Capture validity before uploading; account switches and cache clearing win. */+export function createSentImageRetainer(device: string) {+ const assertCurrent = captureRemoteMediaCacheValidity();+ return async (source: string | Buffer, ref: string): Promise<void> => {+ assertCurrent();+ const metadata = parseAttachmentOssRef(ref) ?? parsePeerAttachmentRef(ref);+ // Same bound as the remote protocol's buffered image path. Never read a large+ // non-image attachment into memory just to keep a preview.+ if (!metadata?.mimeType?.startsWith('image/') || !metadata.sha256 || !metadata.size+ || metadata.size > 64 * 1024 * 1024) return;+ let bytes: Buffer;+ if (typeof source === 'string') {+ const file = await fs.open(source, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));+ try {+ const stat = await file.stat();+ if (!stat.isFile() || stat.size !== metadata.size) return;+ bytes = Buffer.alloc(stat.size);+ let offset = 0;+ while (offset < bytes.length) {+ const { bytesRead } = await file.read(bytes, offset, bytes.length - offset, offset);+ if (!bytesRead) return;+ offset += bytesRead;+ }+ } finally { await file.close(); }+ } else bytes = source;+ assertCurrent();+ if (bytes.length !== metadata.size || createHash('sha256').update(bytes).digest('hex') !== metadata.sha256) return;+ const mimeType = sniffMediaMime(bytes, metadata.mimeType);+ const ext = mimeType && extForMime(mimeType);+ if (!mimeType?.startsWith('image/') || !ext) return;+ await remoteMediaPersistentCache(device, blobUrl(metadata.sha256, ext))?.put(bytes, mimeType);+ };+}+/** Only an index adapter: bytes and references belong to the existing media warehouse.* No age/size eviction here. The owner explicitly clears these reusable copies in Storage.* Capture the database and owner before downloading so late work cannot enter a new account. */diff --git a/apps/mobile/src/__tests__/mobileAttachmentUpload.test.ts b/apps/mobile/src/__tests__/mobileAttachmentUpload.test.tsindex 7e8631d345..ce227c3241 100644--- a/apps/mobile/src/__tests__/mobileAttachmentUpload.test.ts+++ b/apps/mobile/src/__tests__/mobileAttachmentUpload.test.ts@@ -24,6 +24,28 @@ const readFileChunk = vi.fn(async (_uri: string, _position: number, length: numb);describe('mobileAttachmentUpload', () => {+ // Unit regression: background outbox calls this entry without the composer's onUploaded.+ // The immutable snapshot must be retained before it is removed on either transport.+ it.each(['oss', 'peer'] as const)('retains the sent %s image snapshot before cleanup', async (transport) => {+ const order: string[] = [];+ let retained: { ref: string; uri: string } | undefined;+ const apiFetch = vi.fn(async () => ({ putUrl: 'https://oss.example/upload', key: 'uploads/photo.png' }));+ const off = installPeerUpload(async (_device, _uri, metadata) => transport === 'peer'+ ? buildPeerAttachmentRef({ ...metadata, ticket: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' }) : null);+ try {+ const attachment = await uploadMobileAttachmentFromFile({ name: 'photo.png', size: 4, mimeType: 'image/png' }, 'file:///draft', {+ deviceId: 'computer', token: 'token', deps: {+ apiFetch: apiFetch as unknown as typeof apiFetchRaw,+ readFileChunk, uploadFile: async () => ({ status: 200 }),+ snapshotFile: async () => ({ uri: 'file:///snapshot', size: 4, cleanup: async () => { order.push('cleanup'); } }),+ retainImage: async (ref, uri) => { retained = { ref, uri }; order.push('retain'); },+ },+ });+ expect(retained).toEqual({ ref: attachment.url ?? attachment.path, uri: 'file:///snapshot' });+ expect(order).toEqual(['retain', 'cleanup']);+ } finally { off(); }+ });+it('uploads prepared bytes to the captured peer without presigning OSS', async () => {const apiFetch = vi.fn();const peer = vi.fn(async (_device, _uri, metadata) => buildPeerAttachmentRef({ ...metadata, ticket: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' }));diff --git a/apps/mobile/src/__tests__/remoteMedia.test.ts b/apps/mobile/src/__tests__/remoteMedia.test.tsindex 0b1d7adf6a..da5a927a78 100644--- a/apps/mobile/src/__tests__/remoteMedia.test.ts+++ b/apps/mobile/src/__tests__/remoteMedia.test.ts@@ -12,6 +12,25 @@ import {} from "@/session/remoteMedia";describe("mobile remote media", () => {+ // Unit regression: retained sent images must serve original and thumbnail requests+ // offline. Existing tests covered only remote downloads and already-resolved copies.+ it.each([false, true])('reads a retained sent image offline (thumbnail=%s), with forced repair bypass', async (thumbnail) => {+ const findLocalImage = vi.fn(async () => ({ uri: 'file:///doc/sent.png', size: 100, mimeType: 'image/png' }));+ const fetchRemoteMedia = vi.fn(async () => { throw new Error('NOT_CONNECTED'); });+ const presignGet = vi.fn();+ const media = { kind: 'image' as const, url: `cindy-media://blobs/${'a'.repeat(64)}.png` };+ const deps = { findLocalImage, fetchRemoteMedia, presignGet };+ await expect(resolveMobileRemoteMedia(media, deps, { thumbnail })).resolves.toMatchObject({+ url: 'file:///doc/sent.png', size: 100, ossKey: '', expiresAt: REMOTE_MEDIA_NEVER_EXPIRES,+ });+ expect(fetchRemoteMedia).not.toHaveBeenCalled();+ expect(presignGet).not.toHaveBeenCalled();+ await expect(resolveMobileRemoteMedia(media, deps, { skipCache: true })).rejects.toThrow('NOT_CONNECTED');+ expect(findLocalImage).toHaveBeenCalledOnce();+ findLocalImage.mockRejectedValueOnce(new Error('unreadable local file'));+ await expect(resolveMobileRemoteMedia(media, deps)).rejects.toThrow('NOT_CONNECTED');+ });+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');diff --git a/apps/mobile/src/__tests__/sentAttachmentThumbStore.test.ts b/apps/mobile/src/__tests__/sentAttachmentThumbStore.test.tsindex 768dcd5cff..af812ea751 100644--- a/apps/mobile/src/__tests__/sentAttachmentThumbStore.test.ts+++ b/apps/mobile/src/__tests__/sentAttachmentThumbStore.test.ts@@ -1,7 +1,12 @@import { beforeEach, describe, expect, it } from 'vitest';import { vi } from 'vitest';+import { buildAttachmentOssRef, buildPeerAttachmentRef } from '@cindy/device-link';+import { resolveMobileRemoteMedia } from '@/session/remoteMedia';const storage = vi.hoisted(() => new Map<string, string>());+vi.mock('expo-file-system/legacy', () => ({+ getInfoAsync: async () => ({ exists: true, size: 1000 }),+}));vi.mock('@react-native-async-storage/async-storage', () => ({default: {@@ -22,6 +27,7 @@ import {getSentAttachmentThumbUri,getSentAttachmentThumbsVersion,registerSentAttachmentThumb,+ resolveSentAttachmentThumb,subscribeSentAttachmentThumbs,type SentAttachmentThumbFsDeps,} from '@/session/sentAttachmentThumbStore';@@ -65,6 +71,34 @@ beforeEach(async () => {});describe('sentAttachmentThumbStore', () => {+ // Regression: sent photos became unavailable offline after history replaced the upload URL.+ // Existing tests only queried the original ref. Unit tier exercises persistence + real lookup.+ it.each(['oss', 'peer'] as const)('keeps a %s upload readable by its final content address after restart', async (transport) => {+ const sha256 = 'a'.repeat(64);+ const metadata = { sha256, size: 1000, mimeType: 'image/png', originalName: 'photo.png' };+ const ref = transport === 'oss'+ ? buildAttachmentOssRef({ ...metadata, ossKey: 'uploads/photo' })+ : buildPeerAttachmentRef({ ...metadata, ticket: '11111111-1111-1111-1111-111111111111' });+ const { deps, copies } = makeFsDeps();+ await registerSentAttachmentThumb(ref, 'file:///cache/upload-snapshot', deps);+ await __testing.flushPersist();+ expect(copies).toHaveLength(1);+ expect(copies[0].to.endsWith('.png')).toBe(true);+ __testing.reset();+ await expect(resolveSentAttachmentThumb(`cindy-media://blobs/${sha256}.png`, deps)).resolves.toEqual({+ uri: copies[0].to, size: 1000, mimeType: 'image/png',+ });+ expect(getSentAttachmentThumbUri(`cindy-media://blobs/${sha256}.png`)).toBe(copies[0].to);+ expect(getSentAttachmentThumbUri(`cindy-media://blobs/${'b'.repeat(64)}.png`)).toBeNull();+ expect(applySentAttachmentThumbOverlay({ kind: 'image', uri: ref, previewable: false }).uri).toBe(copies[0].to);+ const fetchRemoteMedia = vi.fn(async () => { throw new Error('NOT_CONNECTED'); });+ await expect(resolveMobileRemoteMedia({ kind: 'image', url: `cindy-media://blobs/${sha256}.png` }, {+ fetchRemoteMedia, presignGet: vi.fn(),+ })).resolves.toMatchObject({ url: copies[0].to, mimeType: 'image/png', ossKey: '' });+ expect(fetchRemoteMedia).not.toHaveBeenCalled();+ await expect(resolveSentAttachmentThumb(ref, { ...deps, statSize: async () => 0 })).resolves.toBeNull();+ });+it('register 拷贝文件进自有目录并可同步查询;版本号通知订阅者', async () => {const { deps, copies } = makeFsDeps();const versions: number[] = [];diff --git a/apps/mobile/src/session/mobileAttachmentUpload.ts b/apps/mobile/src/session/mobileAttachmentUpload.tsindex 720476a78a..85e58d2a4a 100644--- a/apps/mobile/src/session/mobileAttachmentUpload.ts+++ b/apps/mobile/src/session/mobileAttachmentUpload.ts@@ -46,6 +46,7 @@ interface UploadDeps {uploadFile?: MobileAttachmentFileUploader;readFileChunk?: MobileAttachmentChunkReader;snapshotFile?: (uri: string) => Promise<{ uri: string; size: number; cleanup?: () => Promise<void> }>;+ retainImage?: (ref: string, uri: string) => Promise<void>;}/** presign 是小 POST,不该吃满默认 20s 超时;弱网抖动重试一次(重复 presign 无副作用,PUT 前不产生对象)。 */@@ -349,6 +350,18 @@ export async function uploadMobileAttachmentFromFile(: options.deps?.readFileChunk? { uri: fileUri, size: candidate.size }: await snapshotMobileAttachmentFile(fileUri);+ // Both the composer and the background outbox upload here. Keep the exact hashed+ // snapshot before cleanup; retaining only in the composer loses background sends.+ const retain = async (attachment: RemoteSerializedAttachment) => {+ if (attachment.category === 'image') {+ try {+ const save = options.deps?.retainImage+ ?? (await import('@/session/sentAttachmentThumbStore')).registerSentAttachmentThumb;+ await save(attachment.url ?? attachment.path, snapshot.uri);+ } catch { /* Optional local copy must not turn a completed upload into a failed send. */ }+ }+ return attachment;+ };try {if (snapshot.size !== candidate.size) {throw new Error(`Attachment size changed: expected ${candidate.size}, actual ${snapshot.size}`);@@ -364,7 +377,7 @@ export async function uploadMobileAttachmentFromFile(if (peerRef) {const attachment = buildMobileUploadedAttachment({ ...candidate, sha256, peerRef, id: options.id });if (!attachment) throw new Error(i18n.t('composer.upload.noFileRead'));- return attachment;+ return await retain(attachment);}// 直连没走通:OSS 保底只收上限以内的附件,presign 前拦下,避免孤儿对象。assertMobileOssAttachmentSize(candidate.size);@@ -396,7 +409,7 @@ export async function uploadMobileAttachmentFromFile(if (!attachment) {throw new Error(i18n.t('composer.upload.noFileRead'));}- return attachment;+ return await retain(attachment);} finally {await snapshot.cleanup?.()?.catch(() => undefined);}diff --git a/apps/mobile/src/session/remoteMedia.ts b/apps/mobile/src/session/remoteMedia.tsindex 85414bada1..29a3ab3cff 100644--- a/apps/mobile/src/session/remoteMedia.ts+++ b/apps/mobile/src/session/remoteMedia.ts@@ -58,6 +58,7 @@ export interface MobileRemoteMediaFetchOptions {}export interface MobileRemoteMediaResolverDeps {+ findLocalImage?(url: string): Promise<{ uri: string; mimeType: string; size: number } | null>;fetchRemoteMedia(url: string,opts?: MobileRemoteMediaFetchOptions,@@ -176,6 +177,18 @@ export async function resolveMobileRemoteMedia(deps: MobileRemoteMediaResolverDeps,opts?: MobileRemoteMediaResolveOptions,): Promise<MobileResolvedRemoteMedia> {+ if (media.kind === 'image' && !opts?.skipCache) {+ // Shared by task history, group chat and file viewers. Keeping this here+ // avoids fixing thumbnails in one screen while original-image reads still go online.+ const local = await (deps.findLocalImage+ ? deps.findLocalImage(media.url)+ : import('./sentAttachmentThumbStore').then((store) => store.resolveSentAttachmentThumb(media.url)))+ .catch(() => null);+ if (local) return {+ url: local.uri, mimeType: local.mimeType, size: local.size,+ ossKey: '', expiresAt: REMOTE_MEDIA_NEVER_EXPIRES, previewable: true,+ };+ }if (!isDesktopLocalMediaUrl(media.url)) {throw new Error(i18n.t("composer.attachments.notFetchableMedia"));}diff --git a/apps/mobile/src/session/sentAttachmentThumbStore.ts b/apps/mobile/src/session/sentAttachmentThumbStore.tsindex 50a22dd30a..d649e8c529 100644--- a/apps/mobile/src/session/sentAttachmentThumbStore.ts+++ b/apps/mobile/src/session/sentAttachmentThumbStore.ts@@ -1,15 +1,9 @@/*** sentAttachmentThumbStore — 已发送本机图片附件的本地缩略图兜底映射。* ---------------------------------------------------------------------------- * 手机端上传的图片在消息里持久化为 `cindy-oss-attach://` 中转引用;该引用要等被控端- * (桌面)取走消息、物化进 image cache 后才被改写成可远程取件的 `xdt-image://`。- * 改写结果不会回推,所以「发出后停留在当前界面」期间(以及桌面端离线、消息压在- * 队列里的整个窗口)气泡只能显示「暂不能直接预览」占位卡——图明明就在手机本地。- *- * 本 store 补上这个空档:上传成功时把实际 PUT 的文件(降采样产物,通常几百 KB)- * 拷贝进 app 自有目录,记录「ossRef → 本地相对文件名」映射并持久化;渲染用户消息- * 图片时 url 仍是 `cindy-oss-attach://` 且映射命中 → 用本地图当缩略图。桌面端物化后- * url 变成 `xdt-image://`,兜底自然退场,本地副本保留供再次查看。+ * 上传成功后保留实际发送的图片字节,同时接受 OSS 与直传引用。+ * 原引用继续持久化;从其中的完整性指纹重建查询索引,让消息换成 cindy-media+ * 内容地址后仍能使用同一副本。缩略图与原图查看共用这份已上传的完整图片。** 设计要点:* - 映射存**相对文件名**:iOS app 容器绝对路径会随更新变化,不能持久化绝对路径;@@ -21,8 +15,9 @@*/import AsyncStorage from '@react-native-async-storage/async-storage';import { useSyncExternalStore } from 'react';-import { isAttachmentOssRef } from '@/session/attachmentOssRef';+import { isAttachmentOssRef, parseAttachmentOssRef, isPeerAttachmentRef, parsePeerAttachmentRef } from '@cindy/device-link';import { isPayloadDesktopLocalMediaUrl } from '@cindy/maker-shared/payload-summary';+import { extOfMime, imageMimeFromUrl, type RemoteMediaDiskCacheHit } from '@/session/remoteMediaDiskCache';const STORAGE_KEY = 'xdt.sentAttachmentThumbs.v1';/** documentDirectory 下的自有子目录(不放 cache:系统清缓存会把兜底图清裂)。 */@@ -76,6 +71,8 @@ const defaultFsDeps: SentAttachmentThumbFsDeps = {};const entries = new Map<string, ThumbEntry>();+/** Derived from the existing persisted refs; no second copy or persistence format. */+const entriesByHash = new Map<string, ThumbEntry>();const listeners = new Set<() => void>();let version = 0;/** hydrate 后缓存的 documentDirectory(以 / 结尾),同步查询拼路径用。 */@@ -93,6 +90,22 @@ function thumbDir(): string | null {return cachedDocDir ? `${cachedDocDir}${THUMB_DIR_NAME}` : null;}+function contentHash(ref: string): string | undefined {+ return /^cindy-media:\/\/blobs\/([a-f0-9]{64})\.[a-z0-9]+$/.exec(ref)?.[1]+ ?? parseAttachmentOssRef(ref)?.sha256 ?? parsePeerAttachmentRef(ref)?.sha256;+}++function imageMimeForRef(ref: string): string | undefined {+ return parseAttachmentOssRef(ref)?.mimeType ?? parsePeerAttachmentRef(ref)?.mimeType+ ?? imageMimeFromUrl(ref) ?? undefined;+}++function rememberEntry(ref: string, entry: ThumbEntry): void {+ entries.set(ref, entry);+ const hash = contentHash(ref);+ if (hash) entriesByHash.set(hash, entry);+}+/*** 冷启动回填:读持久化映射进内存,顺带清目录孤儿文件。幂等,* 首次查询 / 注册时惰性触发。@@ -113,7 +126,7 @@ async function hydrateInternal(deps: SentAttachmentThumbFsDeps): Promise<void> {// 内存以磁盘为底、保留 hydrate 期间已注册的新条目(register 先 await hydrate,// 实际不会先写;防御性合并保证后写胜)。for (const [ossRef, entry] of keep) {- if (!entries.has(ossRef)) entries.set(ossRef, entry);+ if (!entries.has(ossRef)) rememberEntry(ossRef, entry);}// 目录孤儿清理:磁盘上存在但映射不再引用的文件。const dir = thumbDir();@@ -137,7 +150,7 @@ async function hydrateInternal(deps: SentAttachmentThumbFsDeps): Promise<void> {* 引用,应当当场记下对应关系 —— 留底之后气泡第一帧就有图,远端取件退化为后备。*/function isThumbBackedRef(value: unknown): value is string {- return isAttachmentOssRef(value) || isPayloadDesktopLocalMediaUrl(value);+ return isAttachmentOssRef(value) || isPeerAttachmentRef(value) || isPayloadDesktopLocalMediaUrl(value);}/**@@ -157,10 +170,13 @@ export async function registerSentAttachmentThumb(if (!dir) return;const at = Date.now();fileSeq += 1;- const file = `thumb-${at}-${fileSeq}.${extForUri(sourceUri)}`;+ // Upload snapshots intentionally have no extension. Keep the image's actual+ // format so sharing a retained PNG/GIF does not label its bytes as JPEG.+ const mime = imageMimeForRef(ossRef);+ const file = `thumb-${at}-${fileSeq}.${mime?.startsWith('image/') ? extOfMime(mime) : extForUri(sourceUri)}`;await deps.makeDirectory(dir);await deps.copy(sourceUri, `${dir}/${file}`);- entries.set(ossRef, { file, at });+ rememberEntry(ossRef, { file, at });schedulePersist();bump();} catch {@@ -176,10 +192,25 @@ export function getSentAttachmentThumbUri(ossRef: string): string | null {if (!hydratePromise) void ensureSentAttachmentThumbsHydrated();const dir = thumbDir();if (!dir) return null;- const entry = entries.get(ossRef);+ const hash = contentHash(ossRef);+ const entry = entries.get(ossRef) ?? (hash ? entriesByHash.get(hash) : undefined);return entry ? `${dir}/${entry.file}` : null;}+/** Original-image reads must wait for hydration and verify the retained file still exists. */+export async function resolveSentAttachmentThumb(+ ref: string,+ deps: SentAttachmentThumbFsDeps = defaultFsDeps,+): Promise<RemoteMediaDiskCacheHit | null> {+ await ensureSentAttachmentThumbsHydrated(deps);+ const uri = getSentAttachmentThumbUri(ref);+ if (!uri) return null;+ const size = await deps.statSize(uri).catch(() => 0);+ if (size <= 0) return null;+ const mimeType = imageMimeForRef(ref) ?? imageMimeFromUrl(uri);+ return mimeType?.startsWith('image/') ? { uri, size, mimeType } : null;+}+/*** 渲染层 overlay:用户消息图片附件 url 仍是 `cindy-oss-attach://`(previewable=false)* 且本地兜底命中时,替换成本地 file:// 并标记可直接预览;其余原样返回。@@ -188,7 +219,8 @@ export function getSentAttachmentThumbUri(ossRef: string): string | null {export function applySentAttachmentThumbOverlay<T extends { kind: string; uri?: string; previewable: boolean },>(item: T): T {- if (item.kind !== 'image' || item.previewable || !item.uri || !isAttachmentOssRef(item.uri)) {+ if (item.kind !== 'image' || item.previewable || !item.uri+ || !(isAttachmentOssRef(item.uri) || isPeerAttachmentRef(item.uri))) {return item;}const local = getSentAttachmentThumbUri(item.uri);@@ -272,6 +304,7 @@ export const __testing = {},reset(): void {entries.clear();+ entriesByHash.clear();listeners.clear();version = 0;cachedDocDir = null;diff --git a/docs/dev-rules/media-storage-and-protocols.md b/docs/dev-rules/media-storage-and-protocols.mdindex 8fcece13ad..54c07a2c96 100644--- a/docs/dev-rules/media-storage-and-protocols.md+++ b/docs/dev-rules/media-storage-and-protocols.md@@ -87,6 +87,11 @@重启和断连不清理,不增加自动到期或容量淘汰。设置中的「远程图片缓存」复用账本扫描和条件删除,只清该命名空间独占的缓存,保护草稿、已保存附件及其他集成共享字节。普通路径不按 URL 永久缓存:继续由主机复核版本,完整中转下载也可写入已有版本缓存;+ 控制端发送图片时,同一缓存还保留实际上传的字节(含压缩结果),按其最终内容地址登记,+ 覆盖直传与 OSS、文件选择与内存图片。不能用压缩前的本机原图代替历史消息中的压缩版本。+ 留存遵守远程协议既有的 64 MiB 整图读取上限,并核对上传大小、指纹和真实图片类型;+ 文件在上传后发生变化时不登记错误副本。上传开始时捕获账号与清理代次,迟到结果不能+ 写入新账号或重建用户刚清理的缓存。复用现有 `remote-media` 清理入口,不增设目录或 TTL。音视频仍走流式播放。旧主机不返回版本时不把可修改文件登记为长期有效。回归见 `remoteMediaPersistentCache.test.ts`、`remoteMediaProtocol.test.ts` 与 `storageIpc.test.ts`。- 零引用入仓只适用于已有草稿或生成结果提交链路。新增零引用窗口时,必须证明内容在消息diff --git a/docs/dev-rules/mobile-development.md b/docs/dev-rules/mobile-development.mdindex 93acfb078d..af1672e06b 100644--- a/docs/dev-rules/mobile-development.md+++ b/docs/dev-rules/mobile-development.md@@ -96,6 +96,18 @@ iOS 的系统选图器在确认后立即把选中项交给现有附件队列。独立交付;旧原生包缺模块时继续使用 Expo 选图入口。首次发布应随下一次 iOS 原生包交付,指纹与存量装机影响仍须按下文冷更门确认。Android 保持系统选图器和既有附件队列。+### 已发送图片的离线读取++相册、粘贴及后台发件箱共用 `uploadMobileAttachmentFromFile`:上传成功后、删除上传快照前,+把实际发送的图片保留到已有 `sentAttachmentThumbStore`。OSS 与直传引用都留底;从既有引用+的完整性指纹重建查询索引,正式消息换成 `cindy-media://blobs/…` 后仍使用同一副本。+重启恢复无需重新下载,旧版已保存且带指纹的 OSS 记录也能直接关联。没有指纹的旧引用仅按原地址匹配。+缓存文件扩展名按图片格式保存,不能因上传快照没有后缀而把 PNG/GIF 当成 JPEG 分享。+缩略图、原图查看和分享均先读取本地副本;原图读取须等索引恢复并检查文件存在,+副本缺失或强制修复时沿用远端取件。缓存失败不阻断已经成功的上传。++回归:`sentAttachmentThumbStore.test.ts`、`mobileAttachmentUpload.test.ts`、`remoteMedia.test.ts`。+### 任务入口与创建失败恢复- 来源未确认时不挂载远端任务内容,但必须显示读取状态和返回入口;读取失败、空记录或