@devries-cameron20 / t3code Mirror · read-only / commits / 90f064685e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 09:30 UTC

fix(mobile): images stop re-downloading when their URL is re-signed (#21527)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

oliver committed Oct 31, 2026 at 19:03 UTC 90f064685e517c95bf85a35b8573ce49514c78c4
8 files changed +154 -25
apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx
+1
@@ -77,6 +77,7 @@ function MarkdownImage(props: {
77 <ThreadMarkdownImageView
78 uri={media.uri}
79 sourceKey={props.href}
80 + cacheKey={media.cacheKey}
81 unavailable={media.unavailable || !/^https?:\/\//i.test(props.href)}
82 alt={props.alt}
83 onPressPreview={(source) =>
apps/mobile/src/features/pull-requests/PullRequestMedia.tsx
+4 -1
@@ -12,6 +12,7 @@ import { Pressable, View } from "react-native";
12 import { Image } from "expo-image";
13 import { AppText } from "../../components/ui/AppText";
14 import { useAssetUrlState, useRefreshAssetUrl } from "../../state/assets";
15 +import { assetImageCacheKey } from "../../lib/assetImageSource";
16 import { MediaVideoPlayer } from "../../components/MediaVideoPlayer";
17 import { VideoAttachmentTile } from "../../components/VideoAttachmentTile";
18 import { VideoPreviewModal, type VideoPreviewSource } from "../../components/VideoPreviewModal";
@@ -40,6 +41,7 @@ export function usePullRequestMediaUrl(url: string) {
41 const refresh = useRefreshAssetUrl(context?.environmentId ?? null, resource);
42 return {
43 uri: asset._tag === "Success" ? asset.url : resource ? null : url,
44 + cacheKey: context && resource ? assetImageCacheKey(context.environmentId, resource) : undefined,
45 unavailable: resource !== null && asset._tag === "Failure",
46 source: pullRequestMediaSource(url, context),
47 resolvePlaybackUri: resource ? async () => (await refresh()) ?? null : undefined,
@@ -65,6 +67,7 @@ export function PullRequestMedia(props: {
67 <ThreadMarkdownImageView
68 uri={media.uri}
69 sourceKey={props.url}
70 + cacheKey={media.cacheKey}
71 unavailable={media.unavailable}
72 alt={name}
73 onPressPreview={(source) =>
@@ -134,7 +137,7 @@ export function PullRequestImageAttachment(props: {
137 >
138 <View className="overflow-hidden rounded-2xl border border-border-subtle bg-grouped-card">
139 <Image
137 - source={media.uri ? { uri: media.uri } : null}
140 + source={media.uri ? { uri: media.uri, cacheKey: media.cacheKey } : null}
141 style={{ width: 174, height: 112 }}
142 contentFit="contain"
143 cachePolicy="memory-disk"
apps/mobile/src/features/threads/ThreadMarkdownImage.tsx
+10 -1
@@ -19,6 +19,7 @@ import { PresentationSource } from "../../components/ui/NativePresentation";
19 import { useMediaActions } from "../../state/mediaActions";
20 import type { MediaActionsSource } from "../../lib/mediaActionsSource";
21 import { useAssetUrlState } from "../../state/assets";
22 +import { assetImageCacheKey } from "../../lib/assetImageSource";
23 import {
24 MARKDOWN_IMAGE_MAX_WIDTH,
25 type MarkdownImageDisplaySize,
@@ -84,6 +85,7 @@ const noSvgDocumentAtom = Atom.make(AsyncResult.initial<SvgDocument, SvgLoadErro
85 export function ThreadMarkdownImageView(props: {
86 readonly uri: string | null;
87 readonly sourceKey: string;
88 + readonly cacheKey?: string | undefined;
89 readonly unavailable: boolean;
90 readonly unavailableDetail?: string | undefined;
91 readonly alt: string | null;
@@ -189,6 +191,7 @@ export function ThreadMarkdownImageView(props: {
191 <ThreadMarkdownImageRequest
192 key={props.sourceKey}
193 uri={props.uri}
194 + cacheKey={props.cacheKey}
195 onLoad={(size) => setDecoded({ sourceKey: props.sourceKey, size })}
196 onError={() => setFailure({ uri: props.uri, failed: true })}
197 />
@@ -209,6 +212,7 @@ export function ThreadMarkdownImageView(props: {
212
213 function ThreadMarkdownImageRequest(props: {
214 readonly uri: string;
215 + readonly cacheKey: string | undefined;
216 readonly onLoad: (sourceSize: ImageSize) => void;
217 readonly onError: () => void;
218 }) {
@@ -217,7 +221,7 @@ function ThreadMarkdownImageRequest(props: {
221 return (
222 <>
223 <Image
220 - source={{ uri: props.uri }}
224 + source={{ uri: props.uri, cacheKey: props.cacheKey }}
225 contentFit="contain"
226 cachePolicy="memory-disk"
227 accessible={false}
@@ -269,6 +273,11 @@ export function ThreadMarkdownImage(props: {
273 return (
274 <ThreadMarkdownImageView
275 uri={assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null}
276 + cacheKey={
277 + assetUrl._tag === "Success"
278 + ? assetImageCacheKey(props.environmentId, props.resource, assetUrl.fileRevision)
279 + : undefined
280 + }
281 sourceKey={
282 props.resource._tag === "attachment"
283 ? `attachment:${props.resource.attachmentId}`
apps/mobile/src/lib/assetImageSource.test.ts
+55 -7
@@ -1,7 +1,7 @@
1 -import { EnvironmentId } from "@t3tools/contracts";
1 +import { EnvironmentId, SOURCE_CONTROL_MEDIA_ASSET, ThreadId } from "@t3tools/contracts";
2 import { describe, expect, it } from "vite-plus/test";
3
4 -import { assetImageSource } from "./assetImageSource";
4 +import { assetImageCacheKey, assetImageSource } from "./assetImageSource";
5
6 const environmentId = EnvironmentId.make("environment-1");
7
@@ -46,13 +46,61 @@ describe("assetImageSource", () => {
46 expect(left.cacheKey).not.toBe(right.cacheKey);
47 });
48
49 - it("keeps workspace files keyed by URL because they can change on disk", () => {
50 - const source = assetImageSource(
49 + it("keys host files by the revision the server read, and by URL without one", () => {
50 + const resource = {
51 + _tag: "media-file",
52 + threadId: ThreadId.make("thread-1"),
53 + path: "docs/shot.png",
54 + } as const;
55 + const first = assetImageSource(
56 environmentId,
52 - { _tag: "draft-workspace-file", cwd: "/repo", path: "logo.png" },
53 - "https://host/api/assets/token-a/logo.png",
57 + resource,
58 + "https://host/api/assets/token-a/shot.png",
59 + "1:2:5:1000:1000",
60 );
61
56 - expect(source).toStrictEqual({ uri: "https://host/api/assets/token-a/logo.png" });
62 + expect(first.cacheKey).toBeDefined();
63 + expect(
64 + assetImageSource(
65 + environmentId,
66 + resource,
67 + "https://host/api/assets/token-b/shot.png",
68 + "1:2:5:1000:1000",
69 + ),
70 + ).toStrictEqual({ uri: "https://host/api/assets/token-b/shot.png", cacheKey: first.cacheKey });
71 + expect(assetImageCacheKey(environmentId, resource, "1:2:5:2000:2000")).not.toBe(first.cacheKey);
72 + expect(
73 + assetImageCacheKey(environmentId, { ...resource, path: "docs/other.png" }, "1:2:5:1000:1000"),
74 + ).not.toBe(first.cacheKey);
75 + expect(
76 + assetImageCacheKey(EnvironmentId.make("environment-2"), resource, "1:2:5:1000:1000"),
77 + ).not.toBe(first.cacheKey);
78 + expect(
79 + assetImageSource(
80 + environmentId,
81 + { _tag: "draft-workspace-file", cwd: "/repo", path: "logo.png" },
82 + "https://host/api/assets/token-a/logo.png",
83 + ),
84 + ).toStrictEqual({ uri: "https://host/api/assets/token-a/logo.png" });
85 + });
86 +
87 + it("keys source control media by URL only when its bytes cannot change", () => {
88 + const sha = "0123456789abcdef0123456789abcdef01234567";
89 + const key = (url: string, environment = environmentId) =>
90 + assetImageCacheKey(environment, { _tag: SOURCE_CONTROL_MEDIA_ASSET, cwd: "/repo", url });
91 + const pinned = key(`https://raw.githubusercontent.com/owner/repo/${sha}/docs/shot.png`);
92 +
93 + expect(pinned).toBeDefined();
94 + expect(key(`https://github.com/owner/repo/blob/${sha}/docs/shot.png`)).toBe(pinned);
95 + expect(key(`https://raw.githubusercontent.com/owner/repo/${sha}/docs/other.png`)).not.toBe(
96 + pinned,
97 + );
98 + expect(
99 + key(
100 + `https://raw.githubusercontent.com/owner/repo/${sha}/docs/shot.png`,
101 + EnvironmentId.make("environment-2"),
102 + ),
103 + ).not.toBe(pinned);
104 + expect(key("https://raw.githubusercontent.com/owner/repo/main/docs/shot.png")).toBeUndefined();
105 });
106 });
apps/mobile/src/lib/assetImageSource.ts
+45 -8
@@ -1,16 +1,53 @@
1 -import type { AssetResource, EnvironmentId } from "@t3tools/contracts";
1 +import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2 +import {
3 + SOURCE_CONTROL_MEDIA_ASSET,
4 + type AssetResource,
5 + type EnvironmentId,
6 +} from "@t3tools/contracts";
7 import type { ImageSource } from "expo-image";
8
4 -/** Cache immutable attachments by id; signed URLs change on each mint and reconnect. */
9 +/**
10 + * Signed URLs change on each mint and reconnect, so a URL-keyed cache misses every time.
11 + * Content that cannot change caches under its identity, and host files under the revision the
12 + * server read. Anything else stays keyed by URL.
13 + */
14 +export function assetImageCacheKey(
15 + environmentId: EnvironmentId,
16 + resource: AssetResource,
17 + fileRevision?: string,
18 +): string | undefined {
19 + switch (resource._tag) {
20 + case "attachment":
21 + return `t3-attachment:${JSON.stringify([environmentId, resource.attachmentId])}`;
22 + case SOURCE_CONTROL_MEDIA_ASSET: {
23 + const media = sourceControlClients.mediaUrl(resource.url);
24 + return media?.immutable
25 + ? `t3-media:${JSON.stringify([environmentId, media.fetchUrl])}`
26 + : undefined;
27 + }
28 + case "media-file":
29 + case "workspace-file":
30 + case "draft-workspace-file":
31 + return fileRevision === undefined
32 + ? undefined
33 + : `t3-file:${JSON.stringify([
34 + environmentId,
35 + resource._tag === "draft-workspace-file" ? resource.cwd : resource.threadId,
36 + resource.path,
37 + fileRevision,
38 + ])}`;
39 + // A tool can replace the image at its index, and nothing cheap identifies its bytes.
40 + default:
41 + return undefined;
42 + }
43 +}
44 +
45 export function assetImageSource(
46 environmentId: EnvironmentId,
47 resource: AssetResource,
48 uri: string,
49 + fileRevision?: string,
50 ): ImageSource {
10 - return resource._tag === "attachment"
11 - ? {
12 - uri,
13 - cacheKey: `t3-attachment:${JSON.stringify([environmentId, resource.attachmentId])}`,
14 - }
15 - : { uri };
51 + const cacheKey = assetImageCacheKey(environmentId, resource, fileRevision);
52 + return cacheKey === undefined ? { uri } : { uri, cacheKey };
53 }
packages/source-control-core/src/shared/definition.ts
+2
@@ -101,6 +101,8 @@ export interface SourceControlMediaUrl {
101 readonly fileName: string;
102 /** Set when the URL alone says what the media is, as an extensionless upload can. */
103 readonly kind?: "image" | "video";
104 + /** Set when the bytes behind `fetchUrl` can never change, so a client may cache them by it. */
105 + readonly immutable?: true;
106 }
107
108 export interface ReferenceAutolink {
packages/source-control-github/src/shared/definition.test.ts
+19
@@ -132,6 +132,7 @@ describe("GitHub media URLs", () => {
132 fetchUrl: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
133 fileName: "1a1842fb-6383-492f-873c-57aa0033fa6c",
134 kind: "video",
135 + immutable: true,
136 });
137 expect(media("https://github.com/owner/repo/blob/main/docs/shot.png")).toEqual({
138 fetchUrl: "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png",
@@ -147,6 +148,24 @@ describe("GitHub media URLs", () => {
148 expect(media("https://raw.githubusercontent.com/o/r/main/100%.png")?.fileName).toBe("100%.png");
149 });
150
151 + it("marks only uploads and files at a full commit SHA as immutable", () => {
152 + const sha = "0123456789abcdef0123456789abcdef01234567";
153 + for (const url of [
154 + "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
155 + "https://github.com/owner/repo/assets/45952064/1a1842fb",
156 + `https://github.com/owner/repo/blob/${sha}/docs/shot.png`,
157 + `https://raw.githubusercontent.com/owner/repo/${sha}/docs/shot.png`,
158 + ]) {
159 + expect(media(url)?.immutable, url).toBe(true);
160 + }
161 + for (const url of [
162 + "https://raw.githubusercontent.com/owner/repo/0123456789ab/docs/shot.png",
163 + `https://media.githubusercontent.com/media/owner/repo/${sha}/a.mp4`,
164 + ]) {
165 + expect(media(url)?.immutable, url).toBeUndefined();
166 + }
167 + });
168 +
169 it("claims nothing a request without a credential loads just as well", () => {
170 for (const url of [
171 "https://example.com/shot.png",
packages/source-control-github/src/shared/gitHubMedia.ts
+18 -8
@@ -18,6 +18,8 @@ const ATTACHMENT_PATH_PATTERN = /^\/user-attachments\/assets\/[\w-]+$/u;
18 const LEGACY_ATTACHMENT_PATH_PATTERN = /^\/[^/]+\/[^/]+\/assets\/\d+\/[\w-]+$/u;
19 /** `blob` and `raw` both address file bytes; `raw` is the one the token is honoured on. */
20 const REPOSITORY_FILE_PATTERN = /^\/([^/]+)\/([^/]+)\/(?:raw|blob)\/(.*[^/])$/u;
21 +/** GitHub rejects 40-hex branch and tag names, and serves no SHA-256 repositories. */
22 +const COMMIT_SHA_PATTERN = /^[0-9a-f]{40}$/u;
23
24 function fileName(fetchUrl: string): string {
25 const segment = new URL(fetchUrl).pathname.split("/").pop() ?? "";
@@ -31,26 +33,34 @@ function fileName(fetchUrl: string): string {
33 return name.length > 0 ? name : "github-media";
34 }
35
34 -function media(fetchUrl: string, kind?: "video"): SourceControlMediaUrl {
35 - return { fetchUrl, fileName: fileName(fetchUrl), ...(kind ? { kind } : {}) };
36 +function media(
37 + fetchUrl: string,
38 + extra: Pick<SourceControlMediaUrl, "kind" | "immutable"> = {},
39 +): SourceControlMediaUrl {
40 + return { fetchUrl, fileName: fileName(fetchUrl), ...extra };
41 +}
42 +
43 +/** A branch or tag can move; a raw file is fixed only when read at a full commit SHA. */
44 +function rawFile(fetchUrl: string): SourceControlMediaUrl {
45 + const ref = new URL(fetchUrl).pathname.split("/")[3] ?? "";
46 + return media(fetchUrl, COMMIT_SHA_PATTERN.test(ref) ? { immutable: true } : {});
47 }
48
49 export function gitHubMediaUrl(url: URL): SourceControlMediaUrl | null {
50 if (url.protocol !== "https:") return null;
51 const host = url.hostname.toLowerCase();
41 - if (host === RAW_HOST || host === LFS_HOST) {
42 - return media(`https://${host}${url.pathname}${url.search}`);
43 - }
52 + if (host === RAW_HOST) return rawFile(`https://${host}${url.pathname}${url.search}`);
53 + if (host === LFS_HOST) return media(`https://${host}${url.pathname}${url.search}`);
54 if (!WEB_HOSTS.has(host)) return null;
55 // GitHub links an uploaded video bare, with no extension, and an image as markdown.
56 if (ATTACHMENT_PATH_PATTERN.test(url.pathname)) {
47 - return media(`https://github.com${url.pathname}`, "video");
57 + return media(`https://github.com${url.pathname}`, { kind: "video", immutable: true });
58 }
59 if (LEGACY_ATTACHMENT_PATH_PATTERN.test(url.pathname)) {
50 - return media(`https://github.com${url.pathname}`);
60 + return media(`https://github.com${url.pathname}`, { immutable: true });
61 }
62 const repositoryFile = REPOSITORY_FILE_PATTERN.exec(url.pathname);
63 return repositoryFile
54 - ? media(`https://${RAW_HOST}/${repositoryFile[1]}/${repositoryFile[2]}/${repositoryFile[3]}`)
64 + ? rawFile(`https://${RAW_HOST}/${repositoryFile[1]}/${repositoryFile[2]}/${repositoryFile[3]}`)
65 : null;
66 }