Skip to content

Commit 9a5a1de

Browse files
Xanacasclaude
andcommitted
refactor: treat GitHub uploads like any other signed asset
Drops the signed-then-direct fallback chain in web and mobile. A server without the github-attachment kind fails the asset request, and the client shows the same unavailable chip it shows for any other asset. Trims the proxy tests to the one behavior worth guarding: the token is read once and sent on every redirect resolution. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 48d5133 commit 9a5a1de

3 files changed

Lines changed: 57 additions & 155 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ function ThreadMarkdownImageView(props: {
452452
readonly sourceKey: string;
453453
readonly unavailable: boolean;
454454
readonly alt: string | null;
455-
readonly onLoadError?: (uri: string) => void;
456455
readonly onPressPreview: (source: FilePreviewSource) => void;
457456
}) {
458457
const sourceIdentifier = useId();
@@ -524,10 +523,7 @@ function ThreadMarkdownImageView(props: {
524523
key={props.uri}
525524
uri={props.uri}
526525
onLoad={setSourceSize}
527-
onError={() => {
528-
setFailedUri(props.uri);
529-
props.onLoadError?.(props.uri!);
530-
}}
526+
onError={() => setFailedUri(props.uri)}
531527
/>
532528
</View>
533529
</Pressable>
@@ -574,11 +570,7 @@ function ThreadMarkdownImageRequest(props: {
574570
);
575571
}
576572

577-
/**
578-
* Environment-hosted image that loads through a signed asset URL.
579-
* `fallbackUri` is tried directly when the signed URL cannot be issued or fails to load — an
580-
* older server rejects the `github-attachment` resource, and public uploads still load direct.
581-
*/
573+
/** Environment-hosted image that loads through a signed asset URL. */
582574
function ThreadMarkdownImage(props: {
583575
readonly environmentId: EnvironmentId;
584576
readonly resource: Extract<
@@ -587,37 +579,22 @@ function ThreadMarkdownImage(props: {
587579
>;
588580
readonly alt: string | null;
589581
readonly srcFragment?: string;
590-
readonly fallbackUri?: string;
591582
readonly onPressPreview: (source: FilePreviewSource) => void;
592583
}) {
593584
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
594-
const [failedUris, setFailedUris] = useState<ReadonlyArray<string>>([]);
595-
596-
const signedUri = assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null;
597-
const signedFailed = signedUri !== null && failedUris.includes(signedUri);
598-
const fallbackUri =
599-
(assetUrl._tag === "Failure" || signedFailed) &&
600-
props.fallbackUri !== undefined &&
601-
!failedUris.includes(props.fallbackUri)
602-
? props.fallbackUri
603-
: null;
604-
const uri = fallbackUri ?? (signedFailed ? null : signedUri);
605585

606586
return (
607587
<ThreadMarkdownImageView
608-
uri={uri}
588+
uri={assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null}
609589
sourceKey={
610590
props.resource._tag === "attachment"
611591
? `attachment:${props.resource.attachmentId}`
612592
: props.resource._tag === "github-attachment"
613593
? `github:${props.resource.url}`
614594
: `workspace:${props.resource.path}`
615595
}
616-
unavailable={uri === null && assetUrl._tag !== "Loading"}
596+
unavailable={assetUrl._tag === "Failure"}
617597
alt={props.alt}
618-
onLoadError={(failedUri) =>
619-
setFailedUris((failed) => (failed.includes(failedUri) ? failed : [...failed, failedUri]))
620-
}
621598
onPressPreview={props.onPressPreview}
622599
/>
623600
);
@@ -2023,7 +2000,6 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
20232000
? { _tag: "github-attachment", url: imageSource.uri }
20242001
: { _tag: "workspace-file", threadId: props.threadId, path: imageSource.path }
20252002
}
2026-
{...(imageSource._tag === "GitHubAttachment" ? { fallbackUri: imageSource.uri } : {})}
20272003
alt={image.alt}
20282004
srcFragment={markdownImageSourceFragment(image.href)}
20292005
onPressPreview={(source) => setExpandedFile((current) => current ?? source)}

‎apps/server/src/assets/GitHubAttachmentProxy.test.ts‎

Lines changed: 45 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14,119 +14,57 @@ const STORAGE_URL = "https://objects.example.test/signed/4dcab2ba";
1414

1515
const notImplemented = () => Effect.die("not implemented in this test");
1616

17-
type RecordedRequest = { readonly url: string; readonly authorization: string | undefined };
18-
19-
const resolveAttachment = (options: {
20-
readonly calls?: Array<ReadonlyArray<string>>;
21-
readonly requests?: Array<RecordedRequest>;
22-
readonly cliFails?: boolean;
23-
readonly status?: number;
24-
readonly location?: string;
25-
readonly times?: number;
26-
}) =>
27-
Effect.gen(function* () {
28-
const proxy = yield* GitHubAttachmentProxy.GitHubAttachmentProxy;
29-
const results: Array<string | null> = [];
30-
for (let i = 0; i < (options.times ?? 1); i++) {
31-
results.push(yield* proxy.resolveAttachmentLocation(ATTACHMENT_URL));
32-
}
33-
return results;
34-
}).pipe(
35-
Effect.provide(
36-
GitHubAttachmentProxy.layer.pipe(
37-
Layer.provide(
38-
Layer.succeed(
39-
GitHubCli.GitHubCli,
40-
GitHubCli.GitHubCli.of({
41-
execute: ({ args }) => {
42-
options.calls?.push(args);
43-
return options.cliFails
44-
? Effect.fail(
45-
new GitHubCli.GitHubCliUnavailableError({
46-
command: "gh",
47-
cwd: "/tmp",
48-
cause: "gh is not installed",
49-
}),
50-
)
51-
: Effect.succeed({
52-
exitCode: ExitCode(0),
53-
stdout: "gh-token-1\n",
54-
stderr: "",
55-
stdoutTruncated: false,
56-
stderrTruncated: false,
57-
});
58-
},
59-
listOpenPullRequests: notImplemented,
60-
getPullRequest: notImplemented,
61-
getRepositoryCloneUrls: notImplemented,
62-
createRepository: notImplemented,
63-
createPullRequest: notImplemented,
64-
getDefaultBranch: notImplemented,
65-
checkoutPullRequest: notImplemented,
66-
}),
67-
),
68-
),
69-
Layer.provide(
70-
Layer.succeed(
71-
HttpClient.HttpClient,
72-
HttpClient.make((request) =>
73-
Effect.sync(() => {
74-
options.requests?.push({
75-
url: request.url,
76-
authorization: request.headers["authorization"],
77-
});
78-
return HttpClientResponse.fromWeb(
79-
request,
80-
new Response(null, {
81-
status: options.status ?? 302,
82-
headers: options.location === undefined ? {} : { location: options.location },
83-
}),
84-
);
85-
}),
86-
),
87-
),
88-
),
89-
),
90-
),
91-
);
92-
9317
describe("GitHubAttachmentProxy", () => {
9418
it.effect("resolves the redirect with the gh token and caches the token read", () =>
9519
Effect.gen(function* () {
96-
const calls: Array<ReadonlyArray<string>> = [];
97-
const requests: Array<RecordedRequest> = [];
98-
const results = yield* resolveAttachment({
99-
calls,
100-
requests,
101-
location: STORAGE_URL,
102-
times: 2,
103-
});
104-
105-
expect(results).toEqual([STORAGE_URL, STORAGE_URL]);
106-
expect(requests.map((request) => request.authorization)).toEqual([
107-
"token gh-token-1",
108-
"token gh-token-1",
109-
]);
110-
expect(calls).toEqual([["auth", "token", "--hostname", "github.com"]]);
111-
}),
112-
);
20+
const cliCalls: Array<ReadonlyArray<string>> = [];
21+
const authorizations: Array<string | undefined> = [];
22+
const gitHubCli = Layer.succeed(
23+
GitHubCli.GitHubCli,
24+
GitHubCli.GitHubCli.of({
25+
execute: ({ args }) => {
26+
cliCalls.push(args);
27+
return Effect.succeed({
28+
exitCode: ExitCode(0),
29+
stdout: "gh-token-1\n",
30+
stderr: "",
31+
stdoutTruncated: false,
32+
stderrTruncated: false,
33+
});
34+
},
35+
listOpenPullRequests: notImplemented,
36+
getPullRequest: notImplemented,
37+
getRepositoryCloneUrls: notImplemented,
38+
createRepository: notImplemented,
39+
createPullRequest: notImplemented,
40+
getDefaultBranch: notImplemented,
41+
checkoutPullRequest: notImplemented,
42+
}),
43+
);
44+
const httpClient = Layer.succeed(
45+
HttpClient.HttpClient,
46+
HttpClient.make((request) =>
47+
Effect.sync(() => {
48+
authorizations.push(request.headers["authorization"]);
49+
return HttpClientResponse.fromWeb(
50+
request,
51+
new Response(null, { status: 302, headers: { location: STORAGE_URL } }),
52+
);
53+
}),
54+
),
55+
);
11356

114-
it.effect("still resolves anonymously when no gh token is available", () =>
115-
Effect.gen(function* () {
116-
const requests: Array<RecordedRequest> = [];
117-
expect(yield* resolveAttachment({ requests, cliFails: true, location: STORAGE_URL })).toEqual(
118-
[STORAGE_URL],
57+
const proxy = yield* GitHubAttachmentProxy.GitHubAttachmentProxy.pipe(
58+
Effect.provide(
59+
GitHubAttachmentProxy.layer.pipe(Layer.provide(gitHubCli), Layer.provide(httpClient)),
60+
),
11961
);
120-
expect(requests.map((request) => request.authorization)).toEqual([undefined]);
121-
}),
122-
);
62+
const first = yield* proxy.resolveAttachmentLocation(ATTACHMENT_URL);
63+
const second = yield* proxy.resolveAttachmentLocation(ATTACHMENT_URL);
12364

124-
it.effect("answers null for non-redirect responses and non-https locations", () =>
125-
Effect.gen(function* () {
126-
expect(yield* resolveAttachment({ status: 200, location: STORAGE_URL })).toEqual([null]);
127-
expect(yield* resolveAttachment({ location: "http://plain.example.test/asset" })).toEqual([
128-
null,
129-
]);
65+
expect([first, second]).toEqual([STORAGE_URL, STORAGE_URL]);
66+
expect(authorizations).toEqual(["token gh-token-1", "token gh-token-1"]);
67+
expect(cliCalls).toEqual([["auth", "token", "--hostname", "github.com"]]);
13068
}),
13169
);
13270
});

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,11 +1219,7 @@ function ChatMarkdownImageFallback(props: {
12191219
);
12201220
}
12211221

1222-
/**
1223-
* Environment-hosted images load through a signed asset URL.
1224-
* `fallbackUrl` is tried directly when the signed URL cannot be issued or fails to load — an
1225-
* older server rejects the `github-attachment` resource, and public uploads still load direct.
1226-
*/
1222+
/** Environment-hosted images load through a signed asset URL. */
12271223
export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props: {
12281224
readonly environmentId: EnvironmentId;
12291225
readonly resource: Extract<
@@ -1233,14 +1229,16 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
12331229
readonly alt: string;
12341230
readonly copyMarkdown?: string;
12351231
readonly srcFragment?: string;
1236-
readonly fallbackUrl?: string;
12371232
readonly style?: CSSProperties | undefined;
12381233
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
12391234
}) {
12401235
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
1241-
const [failedUrls, setFailedUrls] = useState<ReadonlyArray<string>>([]);
1236+
const [failedUrl, setFailedUrl] = useState<string | null>(null);
12421237

1243-
if (assetUrl._tag === "Loading") {
1238+
if (assetUrl._tag === "Failure" || (assetUrl._tag === "Success" && failedUrl === assetUrl.url)) {
1239+
return <ChatMarkdownImageFallback alt={props.alt} copyMarkdown={props.copyMarkdown} />;
1240+
}
1241+
if (assetUrl._tag !== "Success") {
12441242
return (
12451243
<span
12461244
data-markdown-copy={props.copyMarkdown}
@@ -1255,16 +1253,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
12551253
/>
12561254
);
12571255
}
1258-
const signedUrl = assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null;
1259-
const src =
1260-
signedUrl !== null && !failedUrls.includes(signedUrl)
1261-
? signedUrl
1262-
: props.fallbackUrl !== undefined && !failedUrls.includes(props.fallbackUrl)
1263-
? props.fallbackUrl
1264-
: null;
1265-
if (src === null) {
1266-
return <ChatMarkdownImageFallback alt={props.alt} copyMarkdown={props.copyMarkdown} />;
1267-
}
1256+
const src = assetUrl.url + (props.srcFragment ?? "");
12681257
return (
12691258
<img
12701259
src={src}
@@ -1278,7 +1267,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
12781267
)}
12791268
style={props.style}
12801269
{...expandableMarkdownImageProps(props.onImageExpand, src, props.alt)}
1281-
onError={() => setFailedUrls((failed) => (failed.includes(src) ? failed : [...failed, src]))}
1270+
onError={() => setFailedUrl(assetUrl.url)}
12821271
/>
12831272
);
12841273
});
@@ -2375,7 +2364,6 @@ function ChatMarkdown({
23752364
environmentId={environmentId}
23762365
resource={{ _tag: "github-attachment", url: imageSource.uri }}
23772366
alt={altText}
2378-
fallbackUrl={imageSource.uri}
23792367
copyMarkdown={copyMarkdown}
23802368
style={authoredSizeStyle}
23812369
onImageExpand={imageExpand}

0 commit comments

Comments
 (0)