Skip to content

Commit e9ccb81

Browse files
committed
fix(clients): chat images show the new file after an agent rewrites it
Each assistant message, reasoning entry, and viewed-image work row signs its own asset URL. A later message that shows a file the agent rewrote no longer reuses the earlier URL whose bytes the browser cached.
1 parent 7ff2eab commit e9ccb81

11 files changed

Lines changed: 172 additions & 46 deletions

File tree

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ import { resolveUserMessageIntentBadge } from "./userMessageIntentBadge";
208208
import { fileChipMenu, resolveFileChipTarget, type FileChipAction } from "./fileChipMenu";
209209
import { useFileChipShare } from "./useFileChipShare";
210210
import {
211+
MarkdownImageAssetScopeContext,
211212
MarkdownImageAvailableWidthContext,
212213
ThreadMarkdownImage,
213214
ThreadMarkdownImageUnavailable,
@@ -1838,14 +1839,16 @@ function renderFeedEntry(
18381839
>
18391840
{renderedText.trim().length > 0 ? (
18401841
<MarkdownImageAvailableWidthContext value={props.markdownContentWidth}>
1841-
<AssistantMarkdownContent
1842-
markdown={renderedText}
1843-
markdownStyles={styles}
1844-
linkHandlers={props.markdownLinkHandlers}
1845-
onUseArtifactTemplate={props.onUseArtifactTemplate}
1846-
renderImage={props.renderMarkdownImage}
1847-
skills={props.skills}
1848-
/>
1842+
<MarkdownImageAssetScopeContext value={message.id}>
1843+
<AssistantMarkdownContent
1844+
markdown={renderedText}
1845+
markdownStyles={styles}
1846+
linkHandlers={props.markdownLinkHandlers}
1847+
onUseArtifactTemplate={props.onUseArtifactTemplate}
1848+
renderImage={props.renderMarkdownImage}
1849+
skills={props.skills}
1850+
/>
1851+
</MarkdownImageAssetScopeContext>
18491852
</MarkdownImageAvailableWidthContext>
18501853
) : null}
18511854
{attachments.map((attachment) => {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ import {
3131
*/
3232
export const MarkdownImageAvailableWidthContext = createContext(0);
3333

34+
/**
35+
* The message or work-log row that shows these images. Each one signs its own
36+
* URL, so an agent that rewrites a file and shows it again shows the new file,
37+
* not the cached one.
38+
*/
39+
export const MarkdownImageAssetScopeContext = createContext<string | undefined>(undefined);
40+
3441
export function ThreadMarkdownImageView(props: {
3542
readonly uri: string | null;
3643
readonly sourceKey: string;
@@ -184,7 +191,11 @@ export function ThreadMarkdownImage(props: {
184191
readonly actionsSource?: MediaActionsSource;
185192
readonly onPressPreview: (source: FilePreviewSource) => void;
186193
}) {
187-
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
194+
const assetUrl = useAssetUrlState(
195+
props.environmentId,
196+
props.resource,
197+
useContext(MarkdownImageAssetScopeContext),
198+
);
188199

189200
return (
190201
<ThreadMarkdownImageView

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import Animated, {
8181
withTiming,
8282
} from "react-native-reanimated";
8383
import { useAssetUrl } from "../../state/assets";
84+
import { MarkdownImageAssetScopeContext } from "./ThreadMarkdownImage";
8485

8586
const SHIMMER_WIDTH = 72;
8687
const SHIMMER_SWEEP_MS = 1_350;
@@ -1011,32 +1012,37 @@ const ThreadWorkLogRow = memo(function ThreadWorkLogRow(
10111012
layout={WORK_LOG_LAYOUT_TRANSITION}
10121013
className={reasoning ? "ml-7 py-1" : "ml-7 border-l border-border pb-1 pl-3 pt-0.5"}
10131014
>
1014-
{row.workEntry.questionAnswer ? (
1015-
<QuestionAnswerHistory
1016-
environmentId={props.environmentId}
1017-
answer={row.workEntry.questionAnswer}
1018-
/>
1019-
) : null}
1020-
{viewedImagePath ? (
1021-
<View className="pb-1.5">
1022-
{props.renderImage({ href: viewedImagePath, alt: null, title: null })}
1023-
</View>
1024-
) : null}
1025-
<ScrollView
1026-
nestedScrollEnabled
1027-
directionalLockEnabled
1028-
showsVerticalScrollIndicator
1029-
className="max-h-60"
1030-
contentContainerStyle={{ paddingRight: 8 }}
1031-
>
1032-
{reasoning ? (
1033-
props.renderReasoning(reasoning.text)
1034-
) : (
1035-
<Text selectable className="font-mono text-2xs leading-normal text-foreground-muted">
1036-
{fullDetail}
1037-
</Text>
1038-
)}
1039-
</ScrollView>
1015+
<MarkdownImageAssetScopeContext value={row.workEntry.id}>
1016+
{row.workEntry.questionAnswer ? (
1017+
<QuestionAnswerHistory
1018+
environmentId={props.environmentId}
1019+
answer={row.workEntry.questionAnswer}
1020+
/>
1021+
) : null}
1022+
{viewedImagePath ? (
1023+
<View className="pb-1.5">
1024+
{props.renderImage({ href: viewedImagePath, alt: null, title: null })}
1025+
</View>
1026+
) : null}
1027+
<ScrollView
1028+
nestedScrollEnabled
1029+
directionalLockEnabled
1030+
showsVerticalScrollIndicator
1031+
className="max-h-60"
1032+
contentContainerStyle={{ paddingRight: 8 }}
1033+
>
1034+
{reasoning ? (
1035+
props.renderReasoning(reasoning.text)
1036+
) : (
1037+
<Text
1038+
selectable
1039+
className="font-mono text-2xs leading-normal text-foreground-muted"
1040+
>
1041+
{fullDetail}
1042+
</Text>
1043+
)}
1044+
</ScrollView>
1045+
</MarkdownImageAssetScopeContext>
10401046
</Animated.View>
10411047
) : null}
10421048
</Animated.View>

‎apps/mobile/src/state/assets.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ function useConnectionPhase(environmentId: EnvironmentId | null): EnvironmentCon
4747
return value === null ? "available" : presentConnectionState(value).phase;
4848
}
4949

50+
/** `scope` gives the caller its own signed URL; see `AssetUrlQuery`. */
5051
export function useAssetUrlState(
5152
environmentId: EnvironmentId | null,
5253
resource: AssetResource | null,
54+
scope?: string,
5355
): AssetUrlState {
5456
const preparedConnection = usePreparedConnection(environmentId);
5557
const connectionPhase = useConnectionPhase(environmentId);
5658
const result = useAtomValue(
5759
environmentId === null || resource === null
5860
? EMPTY_ASSET_URL_ATOM
59-
: assetEnvironment.createUrl({ environmentId, input: { resource } }),
61+
: assetEnvironment.createUrl({ environmentId, input: { resource, scope } }),
6062
);
6163
const shared = assetUrlStateFromResult(
6264
result,

‎apps/web/src/assets/assetUrls.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ import { useAtomQueryRunner } from "~/state/use-atom-query-runner";
1616

1717
export { resolveAssetUrl, type AssetUrlState } from "@t3tools/client-runtime/state/assets";
1818

19+
/** `scope` gives the caller its own signed URL; see `AssetUrlQuery`. */
1920
export function useAssetUrlState(
2021
environmentId: EnvironmentId | null,
2122
resource: AssetResource | null,
23+
scope?: string,
2224
): AssetUrlState {
2325
const preparedConnection = usePreparedConnection(environmentId);
2426
const result = useAtomValue(
2527
environmentId === null || resource === null
2628
? EMPTY_ASSET_URL_ATOM
27-
: assetEnvironment.createUrl({ environmentId, input: { resource } }),
29+
: assetEnvironment.createUrl({ environmentId, input: { resource, scope } }),
2830
);
2931
return assetUrlStateFromResult(
3032
result,
@@ -35,6 +37,7 @@ export function useAssetUrlState(
3537
export function useAssetUrlRefresh(
3638
environmentId: EnvironmentId | null,
3739
resource: AssetResource | null,
40+
scope?: string,
3841
): () => Promise<string | null> {
3942
const connection = usePreparedConnection(environmentId);
4043
const httpBaseUrl = connection._tag === "Some" ? connection.value.httpBaseUrl : null;
@@ -44,10 +47,10 @@ export function useAssetUrlRefresh(
4447
});
4548
return useCallback(async () => {
4649
if (environmentId === null || resource === null || httpBaseUrl === null) return null;
47-
const result = await refresh({ environmentId, input: { resource } });
50+
const result = await refresh({ environmentId, input: { resource, scope } });
4851
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
4952
return resolveAssetUrl(httpBaseUrl, result.value.relativeUrl);
50-
}, [environmentId, resource, refresh, httpBaseUrl]);
53+
}, [environmentId, resource, scope, refresh, httpBaseUrl]);
5154
}
5255

5356
export function useAssetUrls(

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ interface ChatMarkdownProps {
233233
text nests under the heading that introduces it, such as a chat message's
234234
author. Rendered tags and their styling are unchanged. */
235235
headingLevelOffset?: number | undefined;
236+
/** Gives this text's workspace images their own signed URLs, such as per chat message. An agent
237+
that rewrites a file and shows it again then shows the new file, not the cached one. */
238+
assetScope?: string | undefined;
236239
}
237240

238241
export interface ChatMarkdownContextReference {
@@ -1688,9 +1691,11 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
16881691
readonly fallbackSrc?: string | undefined;
16891692
readonly workspaceRoot?: string | undefined;
16901693
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
1694+
/** See `ChatMarkdownProps.assetScope`. */
1695+
readonly assetScope?: string | undefined;
16911696
}) {
1692-
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
1693-
const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource);
1697+
const assetUrl = useAssetUrlState(props.environmentId, props.resource, props.assetScope);
1698+
const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource, props.assetScope);
16941699
const resource = props.resource;
16951700
const path =
16961701
resource._tag === "media-file"
@@ -1728,7 +1733,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
17281733
name: props.alt || (props.kind ?? "image"),
17291734
src,
17301735
...(fallbackSrc === undefined
1731-
? { asset: { environmentId: props.environmentId, resource } }
1736+
? { asset: { environmentId: props.environmentId, resource, scope: props.assetScope } }
17321737
: {}),
17331738
...(reference ? { reference } : {}),
17341739
...(relativePath && (resource._tag === "media-file" || resource._tag === "workspace-file")
@@ -2336,6 +2341,7 @@ function useChatMarkdownState({
23362341
renderContextReference,
23372342
headingLevelOffset = 0,
23382343
githubMedia = false,
2344+
assetScope,
23392345
}: ChatMarkdownProps) {
23402346
const { resolvedTheme } = useTheme();
23412347
const [localMediaPreview, setLocalMediaPreview] = useState<ExpandedImagePreview | null>(null);
@@ -2722,6 +2728,7 @@ function useChatMarkdownState({
27222728

27232729
const componentState = useMemo(
27242730
() => ({
2731+
assetScope,
27252732
cwd,
27262733
diffThemeName,
27272734
environmentId,
@@ -2753,6 +2760,7 @@ function useChatMarkdownState({
27532760
updateThreadPullRequestLink,
27542761
}),
27552762
[
2763+
assetScope,
27562764
cwd,
27572765
diffThemeName,
27582766
environmentId,
@@ -3166,6 +3174,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
31663174
},
31673175
img: function MarkdownImage({ node, title, src, alt, ...props }) {
31683176
const {
3177+
assetScope,
31693178
expandMedia,
31703179
cwd,
31713180
environmentId,
@@ -3280,6 +3289,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
32803289
threadId: threadRef.threadId,
32813290
path: imageSource.path,
32823291
}}
3292+
assetScope={assetScope}
32833293
alt={altText}
32843294
kind={kind}
32853295
copyMarkdown={copyMarkdown}

‎apps/web/src/components/chat/ExpandedImageDialog.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,16 @@ function ExpandedMediaFailure({ children }: { children: ReactNode }) {
4444

4545
function ExpandedVideo({ item }: { readonly item: ExpandedImageItem }) {
4646
const asset = item.actionsSource?.asset;
47-
const assetUrl = useAssetUrlState(asset?.environmentId ?? null, asset?.resource ?? null);
48-
const refreshAssetUrl = useAssetUrlRefresh(asset?.environmentId ?? null, asset?.resource ?? null);
47+
const assetUrl = useAssetUrlState(
48+
asset?.environmentId ?? null,
49+
asset?.resource ?? null,
50+
asset?.scope,
51+
);
52+
const refreshAssetUrl = useAssetUrlRefresh(
53+
asset?.environmentId ?? null,
54+
asset?.resource ?? null,
55+
asset?.scope,
56+
);
4957
const src = asset
5058
? assetUrl._tag === "Success"
5159
? assetUrl.url + (item.srcFragment ?? "")

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,6 +2486,7 @@ function AssistantTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "mess
24862486
text={messageText}
24872487
cwd={ctx.markdownCwd}
24882488
threadRef={ctx.threadRef ?? undefined}
2489+
assetScope={row.message.id}
24892490
isStreaming={Boolean(row.message.streaming)}
24902491
lineBreaks={shouldPreserveAssistantLineBreaks(messageText)}
24912492
skills={ctx.skills}
@@ -4915,6 +4916,7 @@ function ReasoningTraceContent({ entries }: { entries: ReadonlyArray<TimelineWor
49154916
text={entry.detail ?? ""}
49164917
cwd={ctx.markdownCwd}
49174918
threadRef={ctx.threadRef ?? undefined}
4919+
assetScope={entry.id}
49184920
skills={ctx.skills}
49194921
isStreaming={
49204922
isWorking && entry.runId === latestRunId && entry.toolLifecycleStatus === "inProgress"
@@ -5221,6 +5223,7 @@ const SimpleWorkEntryRow = memo(function SimpleWorkEntryRow(props: {
52215223
<ChatMarkdownAssetImage
52225224
environmentId={threadRef.environmentId}
52235225
resource={viewedImage.resource}
5226+
assetScope={workEntry.id}
52245227
alt={viewedImage.alt}
52255228
srcFragment={viewedImage.srcFragment}
52265229
workspaceRoot={workspaceRoot}

‎apps/web/src/components/media/MediaActions.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ export interface MediaActionSource {
2222
readonly name: string;
2323
readonly src: string | null;
2424
readonly reference?: MediaReference;
25-
readonly asset?: { readonly environmentId: EnvironmentId; readonly resource: AssetResource };
25+
/** `scope` is the signed URL scope of the rendering that opened the media; see `AssetUrlQuery`. */
26+
readonly asset?: {
27+
readonly environmentId: EnvironmentId;
28+
readonly resource: AssetResource;
29+
readonly scope?: string | undefined;
30+
};
2631
readonly onOpenFile?: () => void;
2732
}
2833

‎packages/client-runtime/src/state/assets.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,71 @@ describe("createAssetEnvironmentAtoms", () => {
179179
}).pipe(Effect.scoped),
180180
);
181181

182+
it.effect("signs a scoped query on its own and keeps the scope off the wire", () =>
183+
Effect.gen(function* () {
184+
const environmentId = EnvironmentId.make("remote");
185+
const payloads: unknown[] = [];
186+
const client = {
187+
[WS_METHODS.assetsCreateUrl]: (payload: unknown) => {
188+
payloads.push(payload);
189+
return Effect.succeed({
190+
relativeUrl: `/api/assets/token-${payloads.length}/shot.png`,
191+
expiresAt: 999999,
192+
});
193+
},
194+
} as unknown as WsRpcProtocolClient;
195+
const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
196+
target: new PrimaryConnectionTarget({
197+
environmentId,
198+
label: "remote",
199+
httpBaseUrl: "https://remote.test",
200+
wsBaseUrl: "wss://remote.test",
201+
}),
202+
state: yield* SubscriptionRef.make<SupervisorConnectionState>({
203+
...AVAILABLE_CONNECTION_STATE,
204+
phase: "connected" as const,
205+
}),
206+
session: yield* SubscriptionRef.make(Option.some({ client } as RpcSession)),
207+
prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
208+
connect: Effect.void,
209+
disconnect: Effect.void,
210+
retryNow: Effect.void,
211+
});
212+
const environments = EnvironmentRegistry.EnvironmentRegistry.of({
213+
run: (_id, effect) =>
214+
Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
215+
followStream: (_id, stream) =>
216+
Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
217+
} as EnvironmentRegistry.EnvironmentRegistry["Service"]);
218+
const registry = AtomRegistry.make();
219+
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
220+
const assets = createAssetEnvironmentAtoms(
221+
Atom.runtime(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environments)),
222+
);
223+
const resource = {
224+
_tag: "media-file" as const,
225+
threadId: ThreadId.make("thread-1"),
226+
path: "shot.png",
227+
};
228+
const signedUrl = (scope?: string) =>
229+
AtomRegistry.getResult(
230+
registry,
231+
assets.createUrl({ environmentId, input: { resource, scope } }),
232+
{ suspendOnWaiting: true },
233+
).pipe(Effect.map((result) => result.relativeUrl));
234+
235+
// Two messages that show one path each sign their own URL.
236+
expect(yield* signedUrl("message-1")).toBe("/api/assets/token-1/shot.png");
237+
expect(yield* signedUrl("message-2")).toBe("/api/assets/token-2/shot.png");
238+
expect(yield* signedUrl("message-1")).toBe("/api/assets/token-1/shot.png");
239+
expect(payloads).toEqual([{ resource }, { resource }]);
240+
// An absent scope shares the unscoped query.
241+
expect(assets.createUrl({ environmentId, input: { resource, scope: undefined } })).toBe(
242+
assets.createUrl({ environmentId, input: { resource } }),
243+
);
244+
}).pipe(Effect.scoped),
245+
);
246+
182247
it("keys asset URL queries by environment and resource", () => {
183248
const runtime = Atom.runtime(Layer.empty) as unknown as Atom.AtomRuntime<
184249
EnvironmentRegistry.EnvironmentRegistry,

0 commit comments

Comments
 (0)