From e9ccb81e479c18bd6008f93ae13843450e0fffa3 Mon Sep 17 00:00:00 2001 From: ishaanko Date: Fri, 2 Oct 2026 14:18:22 -0700 Subject: [PATCH 1/6] 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. --- .../src/features/threads/ThreadFeed.tsx | 19 +++--- .../features/threads/ThreadMarkdownImage.tsx | 13 +++- .../src/features/threads/thread-work-log.tsx | 58 +++++++++-------- apps/mobile/src/state/assets.ts | 4 +- apps/web/src/assets/assetUrls.ts | 9 ++- apps/web/src/components/ChatMarkdown.tsx | 16 ++++- .../components/chat/ExpandedImageDialog.tsx | 12 +++- .../src/components/chat/MessagesTimeline.tsx | 3 + .../web/src/components/media/MediaActions.tsx | 7 +- .../client-runtime/src/state/assets.test.ts | 65 +++++++++++++++++++ packages/client-runtime/src/state/assets.ts | 12 +++- 11 files changed, 172 insertions(+), 46 deletions(-) diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 7d591d59e8c59..6b263c2faa98a 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -208,6 +208,7 @@ import { resolveUserMessageIntentBadge } from "./userMessageIntentBadge"; import { fileChipMenu, resolveFileChipTarget, type FileChipAction } from "./fileChipMenu"; import { useFileChipShare } from "./useFileChipShare"; import { + MarkdownImageAssetScopeContext, MarkdownImageAvailableWidthContext, ThreadMarkdownImage, ThreadMarkdownImageUnavailable, @@ -1838,14 +1839,16 @@ function renderFeedEntry( > {renderedText.trim().length > 0 ? ( - + + + ) : null} {attachments.map((attachment) => { diff --git a/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx b/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx index a67225f1a3364..db42830b78118 100644 --- a/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx +++ b/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx @@ -31,6 +31,13 @@ import { */ export const MarkdownImageAvailableWidthContext = createContext(0); +/** + * The message or work-log row that shows these images. Each one signs its own + * URL, so an agent that rewrites a file and shows it again shows the new file, + * not the cached one. + */ +export const MarkdownImageAssetScopeContext = createContext(undefined); + export function ThreadMarkdownImageView(props: { readonly uri: string | null; readonly sourceKey: string; @@ -184,7 +191,11 @@ export function ThreadMarkdownImage(props: { readonly actionsSource?: MediaActionsSource; readonly onPressPreview: (source: FilePreviewSource) => void; }) { - const assetUrl = useAssetUrlState(props.environmentId, props.resource); + const assetUrl = useAssetUrlState( + props.environmentId, + props.resource, + useContext(MarkdownImageAssetScopeContext), + ); return ( - {row.workEntry.questionAnswer ? ( - - ) : null} - {viewedImagePath ? ( - - {props.renderImage({ href: viewedImagePath, alt: null, title: null })} - - ) : null} - - {reasoning ? ( - props.renderReasoning(reasoning.text) - ) : ( - - {fullDetail} - - )} - + + {row.workEntry.questionAnswer ? ( + + ) : null} + {viewedImagePath ? ( + + {props.renderImage({ href: viewedImagePath, alt: null, title: null })} + + ) : null} + + {reasoning ? ( + props.renderReasoning(reasoning.text) + ) : ( + + {fullDetail} + + )} + + ) : null} diff --git a/apps/mobile/src/state/assets.ts b/apps/mobile/src/state/assets.ts index d6fefe2b8f86a..d509e28073a61 100644 --- a/apps/mobile/src/state/assets.ts +++ b/apps/mobile/src/state/assets.ts @@ -47,16 +47,18 @@ function useConnectionPhase(environmentId: EnvironmentId | null): EnvironmentCon return value === null ? "available" : presentConnectionState(value).phase; } +/** `scope` gives the caller its own signed URL; see `AssetUrlQuery`. */ export function useAssetUrlState( environmentId: EnvironmentId | null, resource: AssetResource | null, + scope?: string, ): AssetUrlState { const preparedConnection = usePreparedConnection(environmentId); const connectionPhase = useConnectionPhase(environmentId); const result = useAtomValue( environmentId === null || resource === null ? EMPTY_ASSET_URL_ATOM - : assetEnvironment.createUrl({ environmentId, input: { resource } }), + : assetEnvironment.createUrl({ environmentId, input: { resource, scope } }), ); const shared = assetUrlStateFromResult( result, diff --git a/apps/web/src/assets/assetUrls.ts b/apps/web/src/assets/assetUrls.ts index 89a864f76d419..da99b2336e72c 100644 --- a/apps/web/src/assets/assetUrls.ts +++ b/apps/web/src/assets/assetUrls.ts @@ -16,15 +16,17 @@ import { useAtomQueryRunner } from "~/state/use-atom-query-runner"; export { resolveAssetUrl, type AssetUrlState } from "@t3tools/client-runtime/state/assets"; +/** `scope` gives the caller its own signed URL; see `AssetUrlQuery`. */ export function useAssetUrlState( environmentId: EnvironmentId | null, resource: AssetResource | null, + scope?: string, ): AssetUrlState { const preparedConnection = usePreparedConnection(environmentId); const result = useAtomValue( environmentId === null || resource === null ? EMPTY_ASSET_URL_ATOM - : assetEnvironment.createUrl({ environmentId, input: { resource } }), + : assetEnvironment.createUrl({ environmentId, input: { resource, scope } }), ); return assetUrlStateFromResult( result, @@ -35,6 +37,7 @@ export function useAssetUrlState( export function useAssetUrlRefresh( environmentId: EnvironmentId | null, resource: AssetResource | null, + scope?: string, ): () => Promise { const connection = usePreparedConnection(environmentId); const httpBaseUrl = connection._tag === "Some" ? connection.value.httpBaseUrl : null; @@ -44,10 +47,10 @@ export function useAssetUrlRefresh( }); return useCallback(async () => { if (environmentId === null || resource === null || httpBaseUrl === null) return null; - const result = await refresh({ environmentId, input: { resource } }); + const result = await refresh({ environmentId, input: { resource, scope } }); if (result._tag === "Failure") throw squashAtomCommandFailure(result); return resolveAssetUrl(httpBaseUrl, result.value.relativeUrl); - }, [environmentId, resource, refresh, httpBaseUrl]); + }, [environmentId, resource, scope, refresh, httpBaseUrl]); } export function useAssetUrls( diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index ea02607cc1152..efce39ef82113 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -233,6 +233,9 @@ interface ChatMarkdownProps { text nests under the heading that introduces it, such as a chat message's author. Rendered tags and their styling are unchanged. */ headingLevelOffset?: number | undefined; + /** Gives this text's workspace images their own signed URLs, such as per chat message. An agent + that rewrites a file and shows it again then shows the new file, not the cached one. */ + assetScope?: string | undefined; } export interface ChatMarkdownContextReference { @@ -1688,9 +1691,11 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props readonly fallbackSrc?: string | undefined; readonly workspaceRoot?: string | undefined; readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined; + /** See `ChatMarkdownProps.assetScope`. */ + readonly assetScope?: string | undefined; }) { - const assetUrl = useAssetUrlState(props.environmentId, props.resource); - const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource); + const assetUrl = useAssetUrlState(props.environmentId, props.resource, props.assetScope); + const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, props.resource, props.assetScope); const resource = props.resource; const path = resource._tag === "media-file" @@ -1728,7 +1733,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props name: props.alt || (props.kind ?? "image"), src, ...(fallbackSrc === undefined - ? { asset: { environmentId: props.environmentId, resource } } + ? { asset: { environmentId: props.environmentId, resource, scope: props.assetScope } } : {}), ...(reference ? { reference } : {}), ...(relativePath && (resource._tag === "media-file" || resource._tag === "workspace-file") @@ -2336,6 +2341,7 @@ function useChatMarkdownState({ renderContextReference, headingLevelOffset = 0, githubMedia = false, + assetScope, }: ChatMarkdownProps) { const { resolvedTheme } = useTheme(); const [localMediaPreview, setLocalMediaPreview] = useState(null); @@ -2722,6 +2728,7 @@ function useChatMarkdownState({ const componentState = useMemo( () => ({ + assetScope, cwd, diffThemeName, environmentId, @@ -2753,6 +2760,7 @@ function useChatMarkdownState({ updateThreadPullRequestLink, }), [ + assetScope, cwd, diffThemeName, environmentId, @@ -3166,6 +3174,7 @@ const CHAT_MARKDOWN_COMPONENTS = { }, img: function MarkdownImage({ node, title, src, alt, ...props }) { const { + assetScope, expandMedia, cwd, environmentId, @@ -3280,6 +3289,7 @@ const CHAT_MARKDOWN_COMPONENTS = { threadId: threadRef.threadId, path: imageSource.path, }} + assetScope={assetScope} alt={altText} kind={kind} copyMarkdown={copyMarkdown} diff --git a/apps/web/src/components/chat/ExpandedImageDialog.tsx b/apps/web/src/components/chat/ExpandedImageDialog.tsx index f750693b6494c..de95e5770a4fd 100644 --- a/apps/web/src/components/chat/ExpandedImageDialog.tsx +++ b/apps/web/src/components/chat/ExpandedImageDialog.tsx @@ -44,8 +44,16 @@ function ExpandedMediaFailure({ children }: { children: ReactNode }) { function ExpandedVideo({ item }: { readonly item: ExpandedImageItem }) { const asset = item.actionsSource?.asset; - const assetUrl = useAssetUrlState(asset?.environmentId ?? null, asset?.resource ?? null); - const refreshAssetUrl = useAssetUrlRefresh(asset?.environmentId ?? null, asset?.resource ?? null); + const assetUrl = useAssetUrlState( + asset?.environmentId ?? null, + asset?.resource ?? null, + asset?.scope, + ); + const refreshAssetUrl = useAssetUrlRefresh( + asset?.environmentId ?? null, + asset?.resource ?? null, + asset?.scope, + ); const src = asset ? assetUrl._tag === "Success" ? assetUrl.url + (item.srcFragment ?? "") diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index fd71d616bae9c..47ef7991c3f1c 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -2486,6 +2486,7 @@ function AssistantTimelineRow({ row }: { row: Extract void; } diff --git a/packages/client-runtime/src/state/assets.test.ts b/packages/client-runtime/src/state/assets.test.ts index d1407fe795981..6e49d2e5d0cb0 100644 --- a/packages/client-runtime/src/state/assets.test.ts +++ b/packages/client-runtime/src/state/assets.test.ts @@ -179,6 +179,71 @@ describe("createAssetEnvironmentAtoms", () => { }).pipe(Effect.scoped), ); + it.effect("signs a scoped query on its own and keeps the scope off the wire", () => + Effect.gen(function* () { + const environmentId = EnvironmentId.make("remote"); + const payloads: unknown[] = []; + const client = { + [WS_METHODS.assetsCreateUrl]: (payload: unknown) => { + payloads.push(payload); + return Effect.succeed({ + relativeUrl: `/api/assets/token-${payloads.length}/shot.png`, + expiresAt: 999999, + }); + }, + } as unknown as WsRpcProtocolClient; + const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ + target: new PrimaryConnectionTarget({ + environmentId, + label: "remote", + httpBaseUrl: "https://remote.test", + wsBaseUrl: "wss://remote.test", + }), + state: yield* SubscriptionRef.make({ + ...AVAILABLE_CONNECTION_STATE, + phase: "connected" as const, + }), + session: yield* SubscriptionRef.make(Option.some({ client } as RpcSession)), + prepared: yield* SubscriptionRef.make(Option.none()), + connect: Effect.void, + disconnect: Effect.void, + retryNow: Effect.void, + }); + const environments = EnvironmentRegistry.EnvironmentRegistry.of({ + run: (_id, effect) => + Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), + followStream: (_id, stream) => + Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), + } as EnvironmentRegistry.EnvironmentRegistry["Service"]); + const registry = AtomRegistry.make(); + yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose())); + const assets = createAssetEnvironmentAtoms( + Atom.runtime(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environments)), + ); + const resource = { + _tag: "media-file" as const, + threadId: ThreadId.make("thread-1"), + path: "shot.png", + }; + const signedUrl = (scope?: string) => + AtomRegistry.getResult( + registry, + assets.createUrl({ environmentId, input: { resource, scope } }), + { suspendOnWaiting: true }, + ).pipe(Effect.map((result) => result.relativeUrl)); + + // Two messages that show one path each sign their own URL. + expect(yield* signedUrl("message-1")).toBe("/api/assets/token-1/shot.png"); + expect(yield* signedUrl("message-2")).toBe("/api/assets/token-2/shot.png"); + expect(yield* signedUrl("message-1")).toBe("/api/assets/token-1/shot.png"); + expect(payloads).toEqual([{ resource }, { resource }]); + // An absent scope shares the unscoped query. + expect(assets.createUrl({ environmentId, input: { resource, scope: undefined } })).toBe( + assets.createUrl({ environmentId, input: { resource } }), + ); + }).pipe(Effect.scoped), + ); + it("keys asset URL queries by environment and resource", () => { const runtime = Atom.runtime(Layer.empty) as unknown as Atom.AtomRuntime< EnvironmentRegistry.EnvironmentRegistry, diff --git a/packages/client-runtime/src/state/assets.ts b/packages/client-runtime/src/state/assets.ts index 670246610248a..2b77434878433 100644 --- a/packages/client-runtime/src/state/assets.ts +++ b/packages/client-runtime/src/state/assets.ts @@ -97,6 +97,13 @@ export function assetUrlStateFromResult( }; } +/** + * A signed URL request. `scope` stays on the client: it gives one rendering its own cached URL, + * such as one chat message or work-log row. A later message that shows a file the agent rewrote + * then signs a fresh URL, instead of reusing the earlier one that the browser already cached. + */ +export type AssetUrlQuery = AssetCreateUrlInput & { readonly scope?: string | undefined }; + export function createAssetEnvironmentAtoms( runtime: Atom.AtomRuntime, localMediaEnvironment?: Atom.Atom<{ @@ -104,7 +111,10 @@ export function createAssetEnvironmentAtoms( readonly httpBaseUrl: string; } | null>, ) { - const execute = Effect.fn("assets.createUrl")(function* (input: AssetCreateUrlInput) { + const execute = Effect.fn("assets.createUrl")(function* ({ + scope: _scope, + ...input + }: AssetUrlQuery) { const result = yield* request(WS_METHODS.assetsCreateUrl, input).pipe(Effect.result); if (Result.isSuccess(result)) return result.success; const error = result.failure; From b3bc6efb2b677e0815562052ddd54c3c36aab456 Mon Sep 17 00:00:00 2001 From: ishaanko Date: Fri, 2 Oct 2026 17:26:59 -0700 Subject: [PATCH 2/6] fix(mobile): give grouped reasoning images their own signed URLs --- apps/mobile/src/features/threads/thread-work-log.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/mobile/src/features/threads/thread-work-log.tsx b/apps/mobile/src/features/threads/thread-work-log.tsx index 3eb393165729f..10f93dcf30fd6 100644 --- a/apps/mobile/src/features/threads/thread-work-log.tsx +++ b/apps/mobile/src/features/threads/thread-work-log.tsx @@ -498,7 +498,11 @@ export function ThreadWorkLog(props: ThreadWorkLogProps) { return ( {props.activities.map((row) => ( - {props.renderReasoning(row.workEntry.detail ?? "")} + + + {props.renderReasoning(row.workEntry.detail ?? "")} + + ))} ); From d9065bf87503cccba59a3067446406420b00bb6f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 04:33:44 +0000 Subject: [PATCH 3/6] fix(clients): scope chat media URLs to each rendering --- .../src/components/FilePreviewModal.tsx | 1 + .../src/components/FilePreviewModal.types.ts | 6 +- .../src/components/VideoPreviewModal.tsx | 5 +- .../src/features/threads/ThreadFeed.tsx | 7 +- .../features/threads/ThreadMarkdownImage.tsx | 21 +- .../features/threads/ThreadMarkdownVideo.tsx | 15 +- .../features/threads/messageImageGallery.ts | 1 + .../src/features/threads/thread-work-log.tsx | 197 +++++++++--------- apps/mobile/src/lib/filePreviewGallery.ts | 1 + apps/mobile/src/lib/markdownMedia.ts | 3 + apps/mobile/src/lib/mediaActionsSource.ts | 1 + apps/mobile/src/lib/videoPreviewSource.ts | 7 +- apps/mobile/src/state/assets.ts | 5 +- apps/mobile/src/state/mediaActions.ts | 1 + apps/web/src/assets/assetUrls.ts | 1 - apps/web/src/components/ChatMarkdown.tsx | 6 +- .../components/chat/ExpandedImagePreview.tsx | 22 +- .../web/src/components/media/MediaActions.tsx | 5 +- .../client-runtime/src/state/assets.test.ts | 65 ------ packages/client-runtime/src/state/assets.ts | 13 +- .../client-runtime/src/state/runtime.test.ts | 45 +++- 21 files changed, 214 insertions(+), 214 deletions(-) diff --git a/apps/mobile/src/components/FilePreviewModal.tsx b/apps/mobile/src/components/FilePreviewModal.tsx index 3b0f0a47f6d4b..d1c32a79473b5 100644 --- a/apps/mobile/src/components/FilePreviewModal.tsx +++ b/apps/mobile/src/components/FilePreviewModal.tsx @@ -39,6 +39,7 @@ function PreviewItemResolver(props: { const refreshAssetUrl = useRefreshAssetUrl( environmentId, "resource" in source ? source.resource : null, + "resource" in source ? source.scope : undefined, ); // Resolve once per presentation; background URL refreshes must not reopen the native viewer. const [uri, setUri] = useState("uri" in source ? source.uri : null); diff --git a/apps/mobile/src/components/FilePreviewModal.types.ts b/apps/mobile/src/components/FilePreviewModal.types.ts index cc0643828aa3c..1a26448821a75 100644 --- a/apps/mobile/src/components/FilePreviewModal.types.ts +++ b/apps/mobile/src/components/FilePreviewModal.types.ts @@ -16,7 +16,11 @@ export type FilePreviewItem = FilePreviewMetadata & ( | { readonly uri: string } | { readonly attachment: FileBackedComposerAttachment } - | { readonly environmentId: EnvironmentId; readonly resource: AssetResource } + | { + readonly environmentId: EnvironmentId; + readonly resource: AssetResource; + readonly scope?: string | undefined; + } ); export type FilePreviewSource = FilePreviewItem & { diff --git a/apps/mobile/src/components/VideoPreviewModal.tsx b/apps/mobile/src/components/VideoPreviewModal.tsx index 053bf104517ee..d4cba59cb0384 100644 --- a/apps/mobile/src/components/VideoPreviewModal.tsx +++ b/apps/mobile/src/components/VideoPreviewModal.tsx @@ -37,8 +37,9 @@ function useMediaPlayback(source: MediaVideoPreviewSource): PlaybackState { const environmentId = "environmentId" in source ? source.environmentId : null; const resource = "resource" in source ? source.resource : null; const connection = usePreparedConnection(environmentId); - const asset = useAssetUrlState(environmentId, resource); - const refreshAssetUrl = useRefreshAssetUrl(environmentId, resource); + const scope = "resource" in source ? source.scope : undefined; + const asset = useAssetUrlState(environmentId, resource, scope); + const refreshAssetUrl = useRefreshAssetUrl(environmentId, resource, scope); const uri = mediaVideoPreviewUri(source, asset._tag === "Success" ? asset.url : null); return { uri, diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index b79981e4acdb5..99015364fdf19 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -2635,7 +2635,12 @@ function MessageImageGalleryScope(props: { const images = entry.type === "message" ? [ - ...messageImageGallery(entry.message, { environmentId, threadId, workspaceRoot }), + ...messageImageGallery(entry.message, { + environmentId, + threadId, + workspaceRoot, + scope: entry.message.role === "assistant" ? entry.message.id : undefined, + }), ...composerImageGallery(entry.pendingMessage?.attachments ?? []), ] : []; diff --git a/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx b/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx index e0b5d1d4588c2..d6c65cb628055 100644 --- a/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx +++ b/apps/mobile/src/features/threads/ThreadMarkdownImage.tsx @@ -30,11 +30,7 @@ import { */ export const MarkdownImageAvailableWidthContext = createContext(0); -/** - * The message or work-log row that shows these images. Each one signs its own - * URL, so an agent that rewrites a file and shows it again shows the new file, - * not the cached one. - */ +/** Later messages must re-sign workspace files that may have changed. */ export const MarkdownImageAssetScopeContext = createContext(undefined); export function ThreadMarkdownImageView(props: { @@ -255,11 +251,12 @@ export function ThreadMarkdownImage(props: { readonly onPressPreview: (source: FilePreviewSource) => void; }) { const withGallery = useFilePreviewGallery(); - const assetUrl = useAssetUrlState( - props.environmentId, - props.resource, - useContext(MarkdownImageAssetScopeContext), - ); + const scope = useContext(MarkdownImageAssetScopeContext); + const assetUrl = useAssetUrlState(props.environmentId, props.resource, scope); + const actionsSource = + props.actionsSource && "resource" in props.actionsSource + ? { ...props.actionsSource, scope } + : props.actionsSource; return ( { const { uri: _uri, ...rest } = source as FilePreviewSource & { uri?: string }; props.onPressPreview( @@ -294,6 +291,8 @@ export function ThreadMarkdownImage(props: { gallery: undefined, environmentId: props.environmentId, resource: props.resource, + scope, + actionsSource, }), ); }} diff --git a/apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx b/apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx index e01f6ae3b2aa8..92949c354f33e 100644 --- a/apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx +++ b/apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx @@ -1,4 +1,5 @@ import { createContext, useContext } from "react"; +import { MarkdownImageAssetScopeContext } from "./ThreadMarkdownImage"; import { MediaVideoPlayer } from "../../components/MediaVideoPlayer"; import { useAssetUrlState, useRefreshAssetUrl } from "../../state/assets"; import { @@ -13,17 +14,23 @@ export function ThreadMarkdownVideo(props: { readonly source: MediaVideoPreviewSource; readonly thumbnailVisible?: boolean; }) { - const { source } = props; + const scope = useContext(MarkdownImageAssetScopeContext); + const source = + "resource" in props.source + ? { ...props.source, scope: props.source.scope ?? scope } + : props.source; const feedVisible = useContext(ThreadMediaVisibleContext); const visible = props.thumbnailVisible ?? feedVisible; const thumbnailKey = mediaVideoThumbnailKey(source); const asset = useAssetUrlState( "environmentId" in source ? source.environmentId : null, "resource" in source ? source.resource : null, + "resource" in source ? source.scope : undefined, ); const refreshAssetUrl = useRefreshAssetUrl( "environmentId" in source ? source.environmentId : null, "resource" in source ? source.resource : null, + "resource" in source ? source.scope : undefined, ); const uri = mediaVideoPreviewUri(source, asset._tag === "Success" ? asset.url : null); return ( @@ -39,7 +46,11 @@ export function ThreadMarkdownVideo(props: { thumbnailKey={thumbnailKey} thumbnailVisible={visible} unavailable={"resource" in source && asset._tag === "Failure"} - actionsSource={source.actionsSource} + actionsSource={ + source.actionsSource && "resource" in source.actionsSource + ? { ...source.actionsSource, scope: "resource" in source ? source.scope : undefined } + : source.actionsSource + } /> ); } diff --git a/apps/mobile/src/features/threads/messageImageGallery.ts b/apps/mobile/src/features/threads/messageImageGallery.ts index c02269976a272..3aa378af9ec81 100644 --- a/apps/mobile/src/features/threads/messageImageGallery.ts +++ b/apps/mobile/src/features/threads/messageImageGallery.ts @@ -10,6 +10,7 @@ export function messageImageGallery( environmentId: EnvironmentId; threadId: ThreadId; workspaceRoot: string | null | undefined; + scope?: string | undefined; }, ): FilePreviewItem[] { const images: FilePreviewItem[] = []; diff --git a/apps/mobile/src/features/threads/thread-work-log.tsx b/apps/mobile/src/features/threads/thread-work-log.tsx index faedd9b6606a8..2ff6c6b1d86bf 100644 --- a/apps/mobile/src/features/threads/thread-work-log.tsx +++ b/apps/mobile/src/features/threads/thread-work-log.tsx @@ -87,7 +87,7 @@ import { notificationChildThreadId } from "@t3tools/client-runtime/state/thread- import type { MarkdownImageRenderer } from "../../native/SelectableMarkdownText"; import type { FilePreviewSource } from "../../components/FilePreviewModal"; import { FilePreviewGallery } from "../../components/FilePreviewGallery"; -import { ThreadMarkdownImage } from "./ThreadMarkdownImage"; +import { MarkdownImageAssetScopeContext, ThreadMarkdownImage } from "./ThreadMarkdownImage"; import Animated, { cancelAnimation, Easing, @@ -103,7 +103,6 @@ import Animated, { withTiming, } from "react-native-reanimated"; import { useAssetUrl } from "../../state/assets"; -import { MarkdownImageAssetScopeContext } from "./ThreadMarkdownImage"; import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs"; const SHIMMER_WIDTH = 72; @@ -1167,111 +1166,113 @@ const ThreadWorkLogRow = function ThreadWorkLogRow( layout={WORK_LOG_LAYOUT_TRANSITION} className={reasoning ? "ml-7 py-1" : "pb-1 pt-0.5"} > - - {row.workEntry.questionAnswer ? ( - - ) : null} - {viewedImagePath ? ( - + {row.workEntry.questionAnswer ? ( + + ) : null} + {viewedImagePath ? ( + + {props.renderImage({ href: viewedImagePath, alt: null, title: null })} - - ) : null} - ({ - kind: "image", - environmentId: props.environmentId, - resource, - }))} - > - {outputImages.map((resource) => ( - + + + ) : null} + ({ + kind: "image", + environmentId: props.environmentId, + resource, + scope: row.workEntry.id, + }))} + > + {outputImages.map((resource) => ( + + - - ))} - - - {reasoning ? ( - props.renderReasoning(reasoning.text) - ) : call ? ( - [ - call.command, - ...(call.args ?? []).map(([key, value]) => - key === "code" && - shownItem.type === "dynamic_tool" && - isJavaScriptReplTool(shownItem.toolName) ? ( - - - {key} - - - - ) : ( - `${key} ${value}` - ), - ), - call.argsText, - ] - .filter((line) => Boolean(line)) - .map((line, index) => - typeof line === "string" ? ( - - {line} + + + ))} + + + {reasoning ? ( + + {props.renderReasoning(reasoning.text)} + + ) : call ? ( + [ + call.command, + ...(call.args ?? []).map(([key, value]) => + key === "code" && + shownItem.type === "dynamic_tool" && + isJavaScriptReplTool(shownItem.toolName) ? ( + + + {key} - ) : ( - line - ), - ) - ) : fullDetail ? ( - - {fullDetail} - - ) : null} - {fetchedOutput ? ( - - {fetchedOutput} - - ) : null} - {streamsOutput ? null : exitLine} - - {streamsOutput ? ( - <> - - {exitLine} - + + + ) : ( + `${key} ${value}` + ), + ), + call.argsText, + ] + .filter((line) => Boolean(line)) + .map((line, index) => + typeof line === "string" ? ( + + {line} + + ) : ( + line + ), + ) + ) : fullDetail ? ( + + {fullDetail} + + ) : null} + {fetchedOutput ? ( + + {fetchedOutput} + ) : null} - + {streamsOutput ? null : exitLine} + + {streamsOutput ? ( + <> + + {exitLine} + + ) : null} ) : null} diff --git a/apps/mobile/src/lib/filePreviewGallery.ts b/apps/mobile/src/lib/filePreviewGallery.ts index 4150054562442..8e6f5d3c11b77 100644 --- a/apps/mobile/src/lib/filePreviewGallery.ts +++ b/apps/mobile/src/lib/filePreviewGallery.ts @@ -7,6 +7,7 @@ export function filePreviewKey(source: FilePreviewItem): string { Object.entries(source.resource) .filter(([key]) => key !== "mimeType" && key !== "fileName") .sort(([a], [b]) => a.localeCompare(b)), + source.scope ?? null, ]); if ("attachment" in source) return `attachment:${source.attachment.id}`; return source.uri; diff --git a/apps/mobile/src/lib/markdownMedia.ts b/apps/mobile/src/lib/markdownMedia.ts index cb67a30dd2f0e..0b9ad827b0e49 100644 --- a/apps/mobile/src/lib/markdownMedia.ts +++ b/apps/mobile/src/lib/markdownMedia.ts @@ -24,6 +24,7 @@ export function resolveMarkdownMediaPreview( readonly workspaceRoot: string | null | undefined; readonly captured?: boolean; readonly imageEmbed?: boolean; + readonly scope?: string | undefined; }, ): | { readonly kind: "image"; readonly source: FilePreviewSource } @@ -94,6 +95,7 @@ export function resolveMarkdownMediaPreview( : { environmentId: input.environmentId, resource: media.resource, + scope: input.scope, ...(srcFragment ? { srcFragment } : {}), }; const actionsSource: MediaActionsSource = @@ -104,6 +106,7 @@ export function resolveMarkdownMediaPreview( environmentId: input.environmentId, threadId: input.threadId, resource: media.resource, + scope: input.scope, name, mimeType, }; diff --git a/apps/mobile/src/lib/mediaActionsSource.ts b/apps/mobile/src/lib/mediaActionsSource.ts index d3e499a7c3df3..8337eec853975 100644 --- a/apps/mobile/src/lib/mediaActionsSource.ts +++ b/apps/mobile/src/lib/mediaActionsSource.ts @@ -14,5 +14,6 @@ export type MediaActionsSource = { readonly environmentId: EnvironmentId; readonly threadId?: ThreadId; readonly resource: AssetResource; + readonly scope?: string | undefined; } ); diff --git a/apps/mobile/src/lib/videoPreviewSource.ts b/apps/mobile/src/lib/videoPreviewSource.ts index dabc896e2ded9..d939c66aa96ad 100644 --- a/apps/mobile/src/lib/videoPreviewSource.ts +++ b/apps/mobile/src/lib/videoPreviewSource.ts @@ -15,6 +15,7 @@ export type MediaVideoPreviewSource = { | { readonly uri: string } | { readonly environmentId: EnvironmentId; + readonly scope?: string | undefined; readonly resource: Extract< AssetResource, { readonly _tag: "attachment" | "media-file" | "draft-workspace-file" | "github-media" } @@ -31,7 +32,7 @@ export function mediaVideoPreviewUri( } export function mediaVideoThumbnailKey(source: MediaVideoPreviewSource): string { - return JSON.stringify( + const key = "uri" in source ? ["media-video", source.uri] : source.resource._tag === "attachment" @@ -60,8 +61,8 @@ export function mediaVideoThumbnailKey(source: MediaVideoPreviewSource): string source.resource.cwd, source.resource.path, source.srcFragment ?? "", - ], - ); + ]; + return JSON.stringify("uri" in source ? key : [...key, source.scope ?? null]); } export interface LocalVideoPreviewSource { diff --git a/apps/mobile/src/state/assets.ts b/apps/mobile/src/state/assets.ts index be13f052ad2de..386f4d0456540 100644 --- a/apps/mobile/src/state/assets.ts +++ b/apps/mobile/src/state/assets.ts @@ -77,6 +77,7 @@ export function useAssetUrl( export function useRefreshAssetUrl( environmentId: EnvironmentId | null, resource: AssetResource | null, + scope?: string, ): () => Promise { const connection = usePreparedConnection(environmentId); const httpBaseUrl = connection._tag === "Some" ? connection.value.httpBaseUrl : null; @@ -87,9 +88,9 @@ export function useRefreshAssetUrl( return useCallback(async () => { if (environmentId === null || resource === null || httpBaseUrl === null) return null; const state = assetUrlStateFromResult( - await createUrl({ environmentId, input: { resource } }), + await createUrl({ environmentId, input: { resource, scope } }), httpBaseUrl, ); return state._tag === "Success" ? state.url : null; - }, [createUrl, environmentId, httpBaseUrl, resource]); + }, [createUrl, environmentId, httpBaseUrl, resource, scope]); } diff --git a/apps/mobile/src/state/mediaActions.ts b/apps/mobile/src/state/mediaActions.ts index a0a9a678205f3..71a05a7048a8d 100644 --- a/apps/mobile/src/state/mediaActions.ts +++ b/apps/mobile/src/state/mediaActions.ts @@ -49,6 +49,7 @@ export function useMediaActions(source: MediaActionsSource | undefined, onOpenFi const refresh = useRefreshAssetUrl( source && "environmentId" in source ? source.environmentId : null, source && "resource" in source ? source.resource : null, + source && "resource" in source ? source.scope : undefined, ); const controller = useRef(null); const [sharing, setSharing] = useState(false); diff --git a/apps/web/src/assets/assetUrls.ts b/apps/web/src/assets/assetUrls.ts index 5e2a38a47964a..d03e44deb1e21 100644 --- a/apps/web/src/assets/assetUrls.ts +++ b/apps/web/src/assets/assetUrls.ts @@ -17,7 +17,6 @@ import { useAtomQueryRunner } from "~/state/use-atom-query-runner"; export { resolveAssetUrl, type AssetUrlState } from "@t3tools/client-runtime/state/assets"; -/** `scope` gives the caller its own signed URL; see `AssetUrlQuery`. */ export function useAssetUrlState( environmentId: EnvironmentId | null, resource: AssetResource | null, diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index f28cf7dcd7b6d..6f12169ac221e 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -242,8 +242,7 @@ interface ChatMarkdownProps { uploads need; without it those images and videos load unauthenticated and 404. */ githubMedia?: boolean | undefined; headingLevelOffset?: number | undefined; - /** Gives this text's workspace images their own signed URLs, such as per chat message. An agent - that rewrites a file and shows it again then shows the new file, not the cached one. */ + /** Keeps rewritten workspace files from reusing an earlier message's cached URL. */ assetScope?: string | undefined; } @@ -1578,7 +1577,6 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props readonly fallbackSrc?: string | undefined; readonly workspaceRoot?: string | undefined; readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined; - /** See `ChatMarkdownProps.assetScope`. */ readonly assetScope?: string | undefined; }) { const assetUrl = useAssetUrlState(props.environmentId, props.resource, props.assetScope); @@ -2320,6 +2318,7 @@ function useChatMarkdownState({ explicitEnvironmentId, cwd, imageBaseDir, + assetScope, ].join("\u0000"); // Tagged with the scope it was opened in: a request from the previous scope can // resolve before the effect cleanup below invalidates it, and must stay hidden. @@ -2380,6 +2379,7 @@ function useChatMarkdownState({ httpBaseUrl: environmentId === null ? undefined : readPreparedConnection(environmentId)?.httpBaseUrl, createAssetUrl, + assetScope, onOpenFile: threadRef ? (path) => useRightPanelStore.getState().openFile(threadRef, path) : undefined, diff --git a/apps/web/src/components/chat/ExpandedImagePreview.tsx b/apps/web/src/components/chat/ExpandedImagePreview.tsx index 33c833e949a03..ab8719d7b9c04 100644 --- a/apps/web/src/components/chat/ExpandedImagePreview.tsx +++ b/apps/web/src/components/chat/ExpandedImagePreview.tsx @@ -2,15 +2,10 @@ import type { SnapShotSource } from "@t3tools/contracts"; import type { ComposerFileAttachment } from "../../composerDraftStore"; import { type ChatFileAttachment, type ChatImageAttachment, isVideoAttachment } from "../../types"; -import type { - AssetCreateUrlResult, - AssetResource, - EnvironmentId, - ScopedThreadRef, -} from "@t3tools/contracts"; +import type { AssetCreateUrlResult, EnvironmentId, ScopedThreadRef } from "@t3tools/contracts"; import { videoMimeType } from "@t3tools/shared/video"; import { resolveMediaSource } from "@t3tools/client-runtime/media-source"; -import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets"; +import { resolveAssetUrl, type AssetUrlQuery } from "@t3tools/client-runtime/state/assets"; import { squashAtomCommandFailure, type AtomCommandResult, @@ -48,10 +43,11 @@ export async function resolveMarkdownMediaPreview(input: { cwd?: string | undefined; threadRef?: ScopedThreadRef | undefined; httpBaseUrl?: string | undefined; + assetScope?: string | undefined; onOpenFile?: ((relativePath: string) => void) | undefined; createAssetUrl: (input: { environmentId: EnvironmentId; - input: { resource: AssetResource }; + input: AssetUrlQuery; }) => Promise>; }): Promise { const media = resolveMediaSource(input.source, { @@ -71,10 +67,14 @@ export async function resolveMarkdownMediaPreview(input: { if (media.access === "unavailable" || !input.threadRef || !input.httpBaseUrl) { throw new Error("Reconnect to this environment and open the media again."); } - asset = { environmentId: input.threadRef.environmentId, resource: media.resource }; + asset = { + environmentId: input.threadRef.environmentId, + resource: media.resource, + scope: input.assetScope, + }; const result = await input.createAssetUrl({ environmentId: asset.environmentId, - input: { resource: asset.resource }, + input: { resource: asset.resource, scope: asset.scope }, }); if (result._tag === "Failure") throw squashAtomCommandFailure(result); const assetUrl = resolveAssetUrl(input.httpBaseUrl, result.value.relativeUrl); @@ -154,7 +154,7 @@ export function buildAttachmentVideoPreview( export function expandedImageKey(preview: ExpandedImagePreview): string { const item = preview.images[preview.index]; const asset = item?.actionsSource?.asset; - return `${item?.src ?? (asset ? JSON.stringify([asset.environmentId, asset.resource]) : "image")}:${preview.index}`; + return `${item?.src ?? (asset ? JSON.stringify([asset.environmentId, asset.resource, asset.scope]) : "image")}:${preview.index}`; } export function attachVideoThumbnail(video: HTMLVideoElement, file: File): () => void { diff --git a/apps/web/src/components/media/MediaActions.tsx b/apps/web/src/components/media/MediaActions.tsx index b17f993825a24..175734c5862f4 100644 --- a/apps/web/src/components/media/MediaActions.tsx +++ b/apps/web/src/components/media/MediaActions.tsx @@ -33,7 +33,6 @@ export interface MediaActionSource { readonly name: string; readonly src: string | null; readonly reference?: MediaReference; - /** `scope` is the signed URL scope of the rendering that opened the media; see `AssetUrlQuery`. */ readonly asset?: { readonly environmentId: EnvironmentId; readonly resource: AssetResource; @@ -85,10 +84,10 @@ export function useMediaActions(source: MediaActionSource) { if (!source.src) throw new Error("This media is unavailable. Try reopening the preview."); return source.src; } - const { environmentId, resource } = source.asset; + const { environmentId, resource, scope } = source.asset; const connection = readPreparedConnection(environmentId); if (!connection) throw new Error("Reconnect to this environment and try again."); - const result = await createAssetUrl({ environmentId, input: { resource } }); + const result = await createAssetUrl({ environmentId, input: { resource, scope } }); if (result._tag === "Failure") throw squashAtomCommandFailure(result); assertCanReadMedia(); const url = resolveAssetUrl(connection.httpBaseUrl, result.value.relativeUrl); diff --git a/packages/client-runtime/src/state/assets.test.ts b/packages/client-runtime/src/state/assets.test.ts index 6e71abda645f7..fd665263254f9 100644 --- a/packages/client-runtime/src/state/assets.test.ts +++ b/packages/client-runtime/src/state/assets.test.ts @@ -179,71 +179,6 @@ describe("createAssetEnvironmentAtoms", () => { }).pipe(Effect.scoped), ); - it.effect("signs a scoped query on its own and keeps the scope off the wire", () => - Effect.gen(function* () { - const environmentId = EnvironmentId.make("remote"); - const payloads: unknown[] = []; - const client = { - [WS_METHODS.assetsCreateUrl]: (payload: unknown) => { - payloads.push(payload); - return Effect.succeed({ - relativeUrl: `/api/assets/token-${payloads.length}/shot.png`, - expiresAt: 999999, - }); - }, - } as unknown as WsRpcProtocolClient; - const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ - target: new PrimaryConnectionTarget({ - environmentId, - label: "remote", - httpBaseUrl: "https://remote.test", - wsBaseUrl: "wss://remote.test", - }), - state: yield* SubscriptionRef.make({ - ...AVAILABLE_CONNECTION_STATE, - phase: "connected" as const, - }), - session: yield* SubscriptionRef.make(Option.some({ client } as RpcSession)), - prepared: yield* SubscriptionRef.make(Option.none()), - connect: Effect.void, - disconnect: Effect.void, - retryNow: Effect.void, - }); - const environments = EnvironmentRegistry.EnvironmentRegistry.of({ - run: (_id, effect) => - Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), - followStream: (_id, stream) => - Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor), - } as EnvironmentRegistry.EnvironmentRegistry["Service"]); - const registry = AtomRegistry.make(); - yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose())); - const assets = createAssetEnvironmentAtoms( - Atom.runtime(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environments)), - ); - const resource = { - _tag: "media-file" as const, - threadId: ThreadId.make("thread-1"), - path: "shot.png", - }; - const signedUrl = (scope?: string) => - AtomRegistry.getResult( - registry, - assets.createUrl({ environmentId, input: { resource, scope } }), - { suspendOnWaiting: true }, - ).pipe(Effect.map((result) => result.relativeUrl)); - - // Two messages that show one path each sign their own URL. - expect(yield* signedUrl("message-1")).toBe("/api/assets/token-1/shot.png"); - expect(yield* signedUrl("message-2")).toBe("/api/assets/token-2/shot.png"); - expect(yield* signedUrl("message-1")).toBe("/api/assets/token-1/shot.png"); - expect(payloads).toEqual([{ resource }, { resource }]); - // An absent scope shares the unscoped query. - expect(assets.createUrl({ environmentId, input: { resource, scope: undefined } })).toBe( - assets.createUrl({ environmentId, input: { resource } }), - ); - }).pipe(Effect.scoped), - ); - it("keys asset URL queries by environment and resource", () => { const runtime = Atom.runtime(Layer.empty) as unknown as Atom.AtomRuntime< EnvironmentRegistry.EnvironmentRegistry, diff --git a/packages/client-runtime/src/state/assets.ts b/packages/client-runtime/src/state/assets.ts index 35483ba5872cb..76e2ed9463343 100644 --- a/packages/client-runtime/src/state/assets.ts +++ b/packages/client-runtime/src/state/assets.ts @@ -126,11 +126,7 @@ export function assetUrlStateFromResult( }; } -/** - * A signed URL request. `scope` stays on the client: it gives one rendering its own cached URL, - * such as one chat message or work-log row. A later message that shows a file the agent rewrote - * then signs a fresh URL, instead of reusing the earlier one that the browser already cached. - */ +/** Client-only cache identity: later messages must re-sign files that may have changed. */ export type AssetUrlQuery = AssetCreateUrlInput & { readonly scope?: string | undefined }; export function createAssetEnvironmentAtoms( @@ -140,14 +136,11 @@ export function createAssetEnvironmentAtoms( readonly httpBaseUrl: string; } | null>, ) { - const execute = Effect.fn("assets.createUrl")(function* ({ - scope: _scope, - ...input - }: AssetUrlQuery) { + const execute = Effect.fn("assets.createUrl")(function* ({ resource }: AssetUrlQuery) { + const input = { resource }; const result = yield* request(WS_METHODS.assetsCreateUrl, input).pipe(Effect.result); if (Result.isSuccess(result)) return result.success; const error = result.failure; - const resource = input.resource; const local = localMediaEnvironment ? (yield* AtomRegistry.AtomRegistry).get(localMediaEnvironment) : null; diff --git a/packages/client-runtime/src/state/runtime.test.ts b/packages/client-runtime/src/state/runtime.test.ts index c36bcaf3bd373..cd18f7d2d9196 100644 --- a/packages/client-runtime/src/state/runtime.test.ts +++ b/packages/client-runtime/src/state/runtime.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it, vi } from "@effect/vitest"; -import { EnvironmentId } from "@t3tools/contracts"; +import { EnvironmentId, ThreadId, WS_METHODS } from "@t3tools/contracts"; import * as Cause from "effect/Cause"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; @@ -26,6 +26,7 @@ import * as EnvironmentRegistry from "../connection/registry.ts"; import * as EnvironmentSupervisor from "../connection/supervisor.ts"; import { EnvironmentRpcUnavailableError } from "../rpc/client.ts"; import type * as RpcSession from "../rpc/session.ts"; +import { createAssetEnvironmentAtoms } from "./assets.ts"; import type { QueryPersistence } from "./queryPersistence.ts"; import { environmentRpcKey, @@ -134,6 +135,7 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness" }); return { + runtime, atom: family({ environmentId: QUERY_ENVIRONMENT.environmentId, input: undefined }), supervisorSession, supervisorState, @@ -374,6 +376,47 @@ describe("finite environment subscription lifecycle", () => { }); describe("environment query lifecycle", () => { + it.effect("caches an asset URL per rendering without sending its scope to the server", () => + Effect.gen(function* () { + const { runtime, supervisorSession } = yield* makeEnvironmentQueryHarness(Effect.void); + const sign = vi + .fn() + .mockReturnValueOnce( + Effect.succeed({ relativeUrl: "/api/assets/first/image.png", expiresAt: 999999 }), + ) + .mockReturnValueOnce( + Effect.succeed({ relativeUrl: "/api/assets/updated/image.png", expiresAt: 999999 }), + ); + yield* SubscriptionRef.set( + supervisorSession, + Option.some({ + ...QUERY_RPC_SESSION, + client: { ...QUERY_RPC_SESSION.client, [WS_METHODS.assetsCreateUrl]: sign }, + }), + ); + const assets = createAssetEnvironmentAtoms(runtime); + const resource = { + _tag: "media-file" as const, + threadId: ThreadId.make("thread"), + path: "image.png", + }; + const query = (scope: string) => + assets.createUrl({ + environmentId: QUERY_ENVIRONMENT.environmentId, + input: { resource, scope }, + }); + const registry = yield* mountEnvironmentQuery(query("message-1")); + const url = (scope: string) => + readEnvironmentQuery(registry, query(scope)).pipe(Effect.map((asset) => asset.relativeUrl)); + + expect(yield* url("message-1")).toBe("/api/assets/first/image.png"); + expect(yield* url("message-1")).toBe("/api/assets/first/image.png"); + expect(yield* url("message-2")).toBe("/api/assets/updated/image.png"); + expect(yield* url("message-1")).toBe("/api/assets/first/image.png"); + expect(sign.mock.calls).toEqual([[{ resource }], [{ resource }]]); + }).pipe(Effect.scoped), + ); + it.effect("keeps the previous value when a refetch returns equal content", () => Effect.scoped( Effect.gen(function* () { From 2f7f6e1158927b94de266915b614ec88588bba57 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 04:41:36 +0000 Subject: [PATCH 4/6] fix(mobile): preserve media preview rendering scope --- .../src/components/VideoPreviewModal.ios.tsx | 5 ++-- .../src/features/threads/ThreadFeed.tsx | 27 ++++++++++++++----- .../src/features/threads/thread-work-log.tsx | 15 +++++------ .../native/SelectableMarkdownText.types.ts | 1 + .../src/native/useMarkdownImageSource.ts | 4 +-- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/apps/mobile/src/components/VideoPreviewModal.ios.tsx b/apps/mobile/src/components/VideoPreviewModal.ios.tsx index 2c95bdca396d2..893d69f980f98 100644 --- a/apps/mobile/src/components/VideoPreviewModal.ios.tsx +++ b/apps/mobile/src/components/VideoPreviewModal.ios.tsx @@ -35,9 +35,10 @@ function NativeVideoPreview(props: { const environmentId = source.type === "media" && "environmentId" in source ? source.environmentId : null; const resource = source.type === "media" && "resource" in source ? source.resource : null; + const scope = "resource" in source ? source.scope : undefined; const preparedConnection = usePreparedConnection(environmentId); - const assetUrl = useAssetUrlState(environmentId, resource); - const refreshAssetUrl = useEffectEvent(useRefreshAssetUrl(environmentId, resource)); + const assetUrl = useAssetUrlState(environmentId, resource, scope); + const refreshAssetUrl = useEffectEvent(useRefreshAssetUrl(environmentId, resource, scope)); const name = source.type === "media" ? source.name : source.attachment.name; // The first minted URL is kept so a background refresh does not restart playback. const resolvedUrl = diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 99015364fdf19..8237a9a2b7f1b 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -721,10 +721,10 @@ function ThreadMediaVisibility(props: { readonly children: ReactNode }) { } interface MarkdownLinkHandlers { - readonly onLinkPress: (href: string) => void; + readonly onLinkPress: (href: string, scope?: string) => void; readonly fileContextMenu: (href: string) => MarkdownFileContextMenu | undefined; - readonly onFileContextMenuAction: (href: string, actionId: string) => void; - readonly onImagePress?: (href: string) => void; + readonly onFileContextMenuAction: (href: string, actionId: string, scope?: string) => void; + readonly onImagePress?: (href: string, scope?: string) => void; readonly resolveImageSource?: MarkdownImageSourceResolver; readonly linkCustomization?: (href: string) => MarkdownLinkCustomization | undefined; } @@ -807,6 +807,12 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: { readonly renderImage: MarkdownImageRenderer; readonly skills?: ReadonlyArray | undefined; }) { + const scope = useContext(MarkdownImageAssetScopeContext); + const resolveImageSource = props.linkHandlers.resolveImageSource; + const scopedImageSource = useCallback( + (image) => resolveImageSource?.(image, scope) ?? null, + [resolveImageSource, scope], + ); const liveMarkdown = useLiveThreadLinkLabels(props.markdown, props.environmentId); const segments = useMemo(() => splitCodexArtifactTemplateMarkdown(liveMarkdown), [liveMarkdown]); @@ -830,6 +836,12 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: { skills={props.skills} textStyle={props.markdownStyles.nativeTextStyle} {...props.linkHandlers} + onLinkPress={(href) => props.linkHandlers.onLinkPress(href, scope)} + onImagePress={(href) => props.linkHandlers.onImagePress?.(href, scope)} + onFileContextMenuAction={(href, actionId) => + props.linkHandlers.onFileContextMenuAction(href, actionId, scope) + } + resolveImageSource={scopedImageSource} renderImage={props.renderImage} /> ); @@ -1792,7 +1804,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { const iconSubtleColor = theme["--color-icon-subtle"]; const screenColor = theme["--color-screen"]; const userBubbleColor = theme["--color-user-bubble"]; - const onMarkdownLinkPress = (href: string, imageEmbed = false) => { + const onMarkdownLinkPress = (href: string, scope?: string, imageEmbed = false) => { const linkedThreadId = parseThreadLinkHref(href); if (linkedThreadId) { navigation.navigate("Thread", { @@ -1806,6 +1818,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { threadId: props.threadId, workspaceRoot: props.workspaceRoot, imageEmbed, + scope, }); if (media) { void Haptics.selectionAsync(); @@ -1860,13 +1873,13 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { }); const markdownLinkHandlers: MarkdownLinkHandlers = { onLinkPress: onMarkdownLinkPress, - onImagePress: (href) => onMarkdownLinkPress(href, true), + onImagePress: (href, scope) => onMarkdownLinkPress(href, scope, true), resolveImageSource, fileContextMenu: (href) => { const target = resolveFileChipTarget(href, props.workspaceRoot); return target ? fileChipMenu(target) : undefined; }, - onFileContextMenuAction: (href, actionId) => { + onFileContextMenuAction: (href, actionId, scope) => { const target = resolveFileChipTarget(href, props.workspaceRoot); if (!target) return; switch (actionId as FileChipAction) { @@ -1877,7 +1890,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { if (target.relativePath) copyTextWithHaptic(target.relativePath); return; case "open-file": - onMarkdownLinkPress(href); + onMarkdownLinkPress(href, scope); return; case "save": shareFileChip(target); diff --git a/apps/mobile/src/features/threads/thread-work-log.tsx b/apps/mobile/src/features/threads/thread-work-log.tsx index 2ff6c6b1d86bf..8d72145f5c686 100644 --- a/apps/mobile/src/features/threads/thread-work-log.tsx +++ b/apps/mobile/src/features/threads/thread-work-log.tsx @@ -1184,19 +1184,16 @@ const ThreadWorkLogRow = function ThreadWorkLogRow( kind: "image", environmentId: props.environmentId, resource, - scope: row.workEntry.id, }))} > {outputImages.map((resource) => ( - - - + ))} diff --git a/apps/mobile/src/native/SelectableMarkdownText.types.ts b/apps/mobile/src/native/SelectableMarkdownText.types.ts index 34cf66c12ee73..ff560b9bf298d 100644 --- a/apps/mobile/src/native/SelectableMarkdownText.types.ts +++ b/apps/mobile/src/native/SelectableMarkdownText.types.ts @@ -42,6 +42,7 @@ export type MarkdownImageRenderer = (image: MarkdownImageRequest) => import("rea export type MarkdownImageSourceResolver = ( image: MarkdownImageRequest, + scope?: string, ) => | { uri: string; headers?: Record } | null diff --git a/apps/mobile/src/native/useMarkdownImageSource.ts b/apps/mobile/src/native/useMarkdownImageSource.ts index 4c00705cf7121..b378e57a69e3b 100644 --- a/apps/mobile/src/native/useMarkdownImageSource.ts +++ b/apps/mobile/src/native/useMarkdownImageSource.ts @@ -33,7 +33,7 @@ export function useMarkdownImageSource(props: { const httpBaseUrl = Option.isSome(connection) ? connection.value.httpBaseUrl : null; const createUrl = useAtomQueryRunner(assetEnvironment.createUrl, { reportFailure: false }); return useCallback( - async (image) => { + async (image, scope) => { const media = resolveMobileMarkdownMediaSource(image.href, { threadId: threadId ?? undefined, workspaceRoot, @@ -52,7 +52,7 @@ export function useMarkdownImageSource(props: { connectionPhase === "reconnecting" ) throw new Error("Markdown image is unavailable"); - const result = await createUrl({ environmentId, input: { resource: media.resource } }); + const result = await createUrl({ environmentId, input: { resource: media.resource, scope } }); if (result._tag !== "Success") throw new Error("Could not authorize Markdown image"); const uri = resolveAssetUrl(httpBaseUrl, result.value.relativeUrl); if (uri === null) throw new Error("Markdown image URL is invalid"); From 234bd50f035691653504b9c5714f491aa413e993 Mon Sep 17 00:00:00 2001 From: ishaanko Date: Mon, 26 Oct 2026 12:27:06 -0700 Subject: [PATCH 5/6] fix(mobile): keep main's embed opens behind the new scope argument Main's audio and file embeds call onMarkdownLinkPress(href, true); this branch put scope before the imageEmbed flag. The merge commit missed this fix. --- apps/mobile/src/features/threads/ThreadFeed.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 11908d2903e80..446b64a799f52 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -2535,7 +2535,7 @@ export const ThreadFeed = memo(function ThreadFeed({ onMarkdownLinkPress(image.href, true)} + onOpen={() => onMarkdownLinkPress(image.href, undefined, true)} onPlaybackStart={onInlinePlaybackStart} /> ); @@ -2550,7 +2550,7 @@ export const ThreadFeed = memo(function ThreadFeed({ onMarkdownLinkPress(image.href, true)} + onPress={() => onMarkdownLinkPress(image.href, undefined, true)} style={{ color: markdownStyles.assistant.nativeTextStyle.linkColor, fontSize: markdownStyles.assistant.nativeTextStyle.fontSize, From 9d9ba2180218f42712789df17e1fcd851c800ff5 Mon Sep 17 00:00:00 2001 From: ishaanko Date: Mon, 26 Oct 2026 12:35:00 -0700 Subject: [PATCH 6/6] test(mobile): give main's media test mocks the asset scope context filePreview.test.ts and useMediaVideoPlayback.test.tsx came from main and mock ThreadMarkdownImage without MarkdownImageAssetScopeContext, which this branch renders around assistant messages and reads in ThreadMarkdownVideo. --- apps/mobile/src/lib/filePreview.test.ts | 5 ++++- apps/mobile/src/lib/useMediaVideoPlayback.test.tsx | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/apps/mobile/src/lib/filePreview.test.ts b/apps/mobile/src/lib/filePreview.test.ts index 7525868e5b049..3cab4302b18ba 100644 --- a/apps/mobile/src/lib/filePreview.test.ts +++ b/apps/mobile/src/lib/filePreview.test.ts @@ -283,7 +283,10 @@ vi.mock("../features/threads/thread-workflow-opening", () => ({ useWorkflowOpeni vi.mock("../features/threads/useFileChipShare", () => ({ useFileChipShare: () => vi.fn() })); vi.mock("../features/threads/ThreadMarkdownImage", async () => { const { createContext } = await import("react"); - return { MarkdownImageAvailableWidthContext: createContext(390) }; + return { + MarkdownImageAvailableWidthContext: createContext(390), + MarkdownImageAssetScopeContext: createContext(undefined), + }; }); vi.mock("../features/review/ReviewCommentCard", () => ({ useReviewCommentColors: () => ({}) })); vi.mock("../features/prompt-links/prompt-link-actions", () => ({})); diff --git a/apps/mobile/src/lib/useMediaVideoPlayback.test.tsx b/apps/mobile/src/lib/useMediaVideoPlayback.test.tsx index c52563b4b4e23..6b8cc22a09f98 100644 --- a/apps/mobile/src/lib/useMediaVideoPlayback.test.tsx +++ b/apps/mobile/src/lib/useMediaVideoPlayback.test.tsx @@ -108,7 +108,13 @@ vi.mock("../components/FilePreviewGallery", () => ({ })); vi.mock("../components/FilePreviewModal", () => ({ FilePreviewModal: () => null })); vi.mock("../components/VideoPreviewModal", () => ({ VideoPreviewModal: () => null })); -vi.mock("../features/threads/ThreadMarkdownImage", () => ({ ThreadMarkdownImageView: () => null })); +vi.mock("../features/threads/ThreadMarkdownImage", async () => { + const { createContext } = await import("react"); + return { + ThreadMarkdownImageView: () => null, + MarkdownImageAssetScopeContext: createContext(undefined), + }; +}); import { ThreadMarkdownVideo,