The best way to control Claude Code, Codex and more from any computer or phone
fix(web): SnapShots taken during a question attach to that question (#15538)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
tiliakoos committed
Oct 20, 2026 at 02:09 UTC
b874ff6c22a7d05ab92cff3daaf77410198c3af7
4 files changed
+250
-20
apps/web/src/components/chat/ChatComposer.tsx
+29
-1
@@ -18,6 +18,7 @@ import { RefreshIcon } from "~/components/ui/refresh-icon";
18
import {
19
questionAttachmentDraftId,
20
countQuestionAttachments,
21
+ trackOpenQuestionAttachmentDraft,
22
useQuestionAttachmentPreparation,
23
changeQuestionAttachmentPreparation,
24
} from "../../questionAttachments";
@@ -2041,6 +2042,33 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
2042
: null;
2043
const attachmentDraftTarget = questionAttachmentTarget ?? composerDraftTarget;
2044
const attachmentDraft = useComposerThreadDraft(attachmentDraftTarget);
2045
+ const questionTakesSnapShots =
2046
+ supportsQuestionAttachments &&
2047
+ activePendingProgress?.activeQuestion?.allowCustomAnswer !== false &&
2048
+ !activePendingIsResponding;
2049
+ const questionRequest = pendingUserInputs[0];
2050
+ // Publish eligibility before async captures can observe a question the UI has already closed.
2051
+ useLayoutEffect(() => {
2052
+ if (!questionAttachmentTarget || !questionRequest || !questionTakesSnapShots) return;
2053
+ return trackOpenQuestionAttachmentDraft(routeThreadRef, {
2054
+ draftId: questionAttachmentTarget,
2055
+ requestKeys: questionRequest.questions.map((question) =>
2056
+ questionAttachmentDraftId(
2057
+ environmentId,
2058
+ activeThreadId!,
2059
+ questionRequest.requestId,
2060
+ question.id,
2061
+ ),
2062
+ ),
2063
+ });
2064
+ }, [
2065
+ activeThreadId,
2066
+ environmentId,
2067
+ questionAttachmentTarget,
2068
+ questionRequest,
2069
+ questionTakesSnapShots,
2070
+ routeThreadRef,
2071
+ ]);
2072
const attachmentTargetKey = composerTargetKey(attachmentDraftTarget);
2073
// An import that finishes after a draft change must compare against the draft open *now*, not
2074
// the one captured in the closure that started it.
@@ -2079,7 +2107,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
2107
pendingSnapShotIdSet,
2108
uncommittedSnapShotIds,
2109
standaloneComposerImages,
2082
- } = useComposerImageGroups(composerDraftTarget, composerImages, composerPreviewAnnotations);
2110
+ } = useComposerImageGroups(attachmentDraftTarget, composerImages, composerPreviewAnnotations);
2111
const nonPersistedComposerImageIds = attachmentDraft.nonPersistedImageIds;
2112
const uploadsByImageId = useAttachmentUploadStore((state) => state.uploadsByImageId);
2113
const openPrLink = useOpenPrLink(routeThreadRef);
apps/web/src/components/desktop/SnapShotCoordinator.browser.test.ts
+133
-3
@@ -5,6 +5,7 @@ import {
5
type DesktopSnapShotEvent,
6
DEFAULT_CLIENT_SETTINGS,
7
EnvironmentId,
8
+ PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
9
ProjectId,
10
ThreadId,
11
} from "@t3tools/contracts";
@@ -18,11 +19,17 @@ import * as settings from "../../hooks/useSettings";
19
import * as newThread from "../../hooks/useHandleNewThread";
20
import * as snapShotSound from "../../lib/snapShotSound";
21
import { toastManager } from "../ui/toast";
22
+import * as imageCompression from "../../lib/imageCompression";
23
+import {
24
+ questionAttachmentDraftId,
25
+ trackOpenQuestionAttachmentDraft,
26
+} from "../../questionAttachments";
27
import {
28
beginSnapShotAnimationWhenReady,
29
deliverSnapShot,
30
dismissFailedSnapShot,
31
resolveExistingSnapShotTarget,
32
+ resolveSnapShotAttachmentTarget,
33
resolveSnapShotTargetOnce,
34
resolveSnapShotDeliveryTarget,
35
SnapShotCoordinator,
@@ -38,6 +45,7 @@ import {
45
vi.mock(import("../../hooks/useSettings"), { spy: true });
46
vi.mock(import("../../hooks/useHandleNewThread"), { spy: true });
47
vi.mock(import("../../lib/snapShotSound"), { spy: true });
48
+vi.mock(import("../../lib/imageCompression"), { spy: true });
49
50
const storage = vi.hoisted(() => {
51
const values = new Map<string, string>();
@@ -537,7 +545,7 @@ describe("window capture delivery", () => {
545
}
546
});
547
});
540
- const delivery = deliverSnapShot(bridge, item, target);
548
+ const delivery = deliverSnapShot(bridge, item, () => target);
549
await persisted;
550
await vi.advanceTimersByTimeAsync(0);
551
@@ -605,6 +613,128 @@ describe("window capture target resolution", () => {
613
});
614
});
615
616
+describe("open question delivery", () => {
617
+ const threadRef = scopeThreadRef(environmentId, ThreadId.make("question-thread"));
618
+ const first = questionAttachmentDraftId(environmentId, threadRef.threadId, "request-1", "q1");
619
+ const second = questionAttachmentDraftId(environmentId, threadRef.threadId, "request-1", "q2");
620
+ const request = { draftId: first, requestKeys: [first, second] };
621
+ const capture = {
622
+ id: "12345678-1234-1234-1234-123456789abc",
623
+ name: "window.png",
624
+ mimeType: "image/png" as const,
625
+ sizeBytes: 3,
626
+ dataUrl: "data:image/png;base64,AQID",
627
+ source: {
628
+ kind: "snap-shot" as const,
629
+ capturedAt: "2026-09-01T00:00:00.000Z",
630
+ appName: "Editor",
631
+ windowTitle: "main.ts",
632
+ },
633
+ };
634
+ const makeBridge = (readSnapShot: () => Promise<typeof capture> = async () => capture) => {
635
+ const acknowledgeSnapShot = vi.fn(async () => undefined);
636
+ const bridge = { readSnapShot, acknowledgeSnapShot } as unknown as DesktopSnapShotBridge;
637
+ return { bridge, acknowledgeSnapShot };
638
+ };
639
+
640
+ it("attaches a capture to the open question draft and leaves the thread draft alone", async () => {
641
+ const pins = new Map();
642
+ const untrack = trackOpenQuestionAttachmentDraft(threadRef, request);
643
+ try {
644
+ const { bridge, acknowledgeSnapShot } = makeBridge();
645
+ await deliverSnapShot(bridge, capture, () =>
646
+ resolveSnapShotAttachmentTarget(pins, capture.id, threadRef),
647
+ );
648
+ const store = useComposerDraftStore.getState();
649
+ expect(store.getComposerDraft(first)?.images.map(({ id }) => id)).toEqual([capture.id]);
650
+ expect(store.getComposerDraft(threadRef)?.images ?? []).toHaveLength(0);
651
+ expect(acknowledgeSnapShot).toHaveBeenCalledExactlyOnceWith(capture.id);
652
+ } finally {
653
+ untrack();
654
+ }
655
+ });
656
+
657
+ it("falls back to the thread when a newer question replaces the pinned one during compression", async () => {
658
+ const pins = new Map();
659
+ const untrackFirst = trackOpenQuestionAttachmentDraft(threadRef, request);
660
+ let untrackSecond = () => {};
661
+ const compression = vi
662
+ .spyOn(imageCompression, "compressImageToByteLimit")
663
+ .mockImplementationOnce(async (file) => {
664
+ untrackSecond = trackOpenQuestionAttachmentDraft(threadRef, {
665
+ ...request,
666
+ draftId: second,
667
+ });
668
+ untrackFirst();
669
+ return { ok: true, file, recompressed: false };
670
+ });
671
+ try {
672
+ const { bridge, acknowledgeSnapShot } = makeBridge();
673
+ const resolveTarget = () => resolveSnapShotAttachmentTarget(pins, capture.id, threadRef);
674
+ expect(resolveTarget()).toBe(first);
675
+ await deliverSnapShot(bridge, capture, resolveTarget);
676
+ const store = useComposerDraftStore.getState();
677
+ expect(store.getComposerDraft(first)?.images ?? []).toHaveLength(0);
678
+ expect(store.getComposerDraft(second)?.images ?? []).toHaveLength(0);
679
+ expect(store.getComposerDraft(threadRef)?.images.map(({ id }) => id)).toEqual([capture.id]);
680
+ expect(acknowledgeSnapShot).toHaveBeenCalledExactlyOnceWith(capture.id);
681
+ } finally {
682
+ compression.mockRestore();
683
+ untrackFirst();
684
+ untrackSecond();
685
+ }
686
+ });
687
+
688
+ it("keeps a capture pinned before any question off a question that opens during the read", async () => {
689
+ const pins = new Map();
690
+ const resolveTarget = () => resolveSnapShotAttachmentTarget(pins, capture.id, threadRef);
691
+ // The drain pins a capture it never saw requested before reading it.
692
+ expect(resolveTarget()).toEqual(threadRef);
693
+ let untrack = () => {};
694
+ const { bridge, acknowledgeSnapShot } = makeBridge(async () => {
695
+ untrack = trackOpenQuestionAttachmentDraft(threadRef, request);
696
+ return capture;
697
+ });
698
+ try {
699
+ await deliverSnapShot(bridge, capture, resolveTarget);
700
+ const store = useComposerDraftStore.getState();
701
+ expect(store.getComposerDraft(first)?.images ?? []).toHaveLength(0);
702
+ expect(store.getComposerDraft(threadRef)?.images.map(({ id }) => id)).toEqual([capture.id]);
703
+ expect(acknowledgeSnapShot).toHaveBeenCalledExactlyOnceWith(capture.id);
704
+ } finally {
705
+ untrack();
706
+ }
707
+ });
708
+
709
+ it("refuses a capture once the request's other questions hold the shared limit", async () => {
710
+ useComposerDraftStore.getState().addImages(
711
+ first,
712
+ Array.from({ length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS }, (_, index) => ({
713
+ type: "image" as const,
714
+ id: `staged-${index}`,
715
+ name: `staged-${index}.png`,
716
+ mimeType: "image/png",
717
+ sizeBytes: 3,
718
+ previewUrl: `data:image/png;base64,AQID${index}`,
719
+ file: new File([new Uint8Array([1, 2, 3])], `staged-${index}.png`, { type: "image/png" }),
720
+ })),
721
+ );
722
+ const untrack = trackOpenQuestionAttachmentDraft(threadRef, { ...request, draftId: second });
723
+ try {
724
+ const { bridge, acknowledgeSnapShot } = makeBridge();
725
+ await expect(deliverSnapShot(bridge, capture, () => second)).rejects.toThrow(
726
+ "Remove an attachment",
727
+ );
728
+ expect(useComposerDraftStore.getState().getComposerDraft(second)?.images ?? []).toHaveLength(
729
+ 0,
730
+ );
731
+ expect(acknowledgeSnapShot).not.toHaveBeenCalled();
732
+ } finally {
733
+ untrack();
734
+ }
735
+ });
736
+});
737
+
738
describe("durable snapshot delivery", () => {
739
it.each([false, true])(
740
"retains a capture on quota failure and retries without duplicates (staged: %s)",
@@ -643,7 +773,7 @@ describe("durable snapshot delivery", () => {
773
});
774
void store.syncPersistedAttachments(target, [capture]);
775
}
646
- await expect(deliverSnapShot(bridge, capture, target)).rejects.toThrow(
776
+ await expect(deliverSnapShot(bridge, capture, () => target)).rejects.toThrow(
777
"could not be saved",
778
);
779
expect(acknowledgeSnapShot).not.toHaveBeenCalled();
@@ -651,7 +781,7 @@ describe("durable snapshot delivery", () => {
781
useComposerDraftStore.getState().getComposerDraft(target)?.nonPersistedImageIds,
782
).toContain(capture.id);
783
storage.setItem.mockImplementation(write);
654
- await deliverSnapShot(bridge, capture, target);
784
+ await deliverSnapShot(bridge, capture, () => target);
785
expect(acknowledgeSnapShot).toHaveBeenCalledExactlyOnceWith(capture.id);
786
expect(useComposerDraftStore.getState().getComposerDraft(target)?.images).toHaveLength(1);
787
expect(
apps/web/src/components/desktop/SnapShotCoordinator.tsx
+55
-15
@@ -1,6 +1,7 @@
1
import {
2
type DesktopPendingSnapShot,
3
type DesktopSnapShotEvent,
4
+ PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
5
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
6
type ScopedThreadRef,
7
} from "@t3tools/contracts";
@@ -33,6 +34,10 @@ import {
34
getDesktopSnapShotBridge,
35
type DesktopSnapShotBridge,
36
} from "../../lib/desktopSnapShot";
37
+import {
38
+ countQuestionRequestAttachments,
39
+ openQuestionAttachmentDraft,
40
+} from "../../questionAttachments";
41
import { readFileAsDataUrl } from "../ChatView.logic";
42
import { stackedThreadToast, toastManager } from "../ui/toast";
43
@@ -65,6 +70,18 @@ export function resolveExistingSnapShotTarget(
70
: null;
71
}
72
73
+// Pin absence too: a later question must never claim an earlier capture.
74
+export function resolveSnapShotAttachmentTarget(
75
+ pins: Map<string, DraftId | null>,
76
+ id: string,
77
+ target: CaptureTarget,
78
+): CaptureTarget {
79
+ const open = typeof target === "string" ? null : openQuestionAttachmentDraft(target);
80
+ if (!pins.has(id)) pins.set(id, open?.draftId ?? null);
81
+ const pinned = pins.get(id);
82
+ return pinned && pinned === open?.draftId ? pinned : target;
83
+}
84
+
85
const NEXT_PAINT_FALLBACK_MS = 100;
86
87
export async function beginSnapShotAnimationWhenReady(
@@ -141,7 +158,7 @@ async function afterNextPaint(): Promise<void> {
158
export async function deliverSnapShot(
159
bridge: DesktopSnapShotBridge,
160
item: DesktopPendingSnapShot,
144
- target: CaptureTarget,
161
+ resolveTarget: () => CaptureTarget,
162
): Promise<void> {
163
const store = useComposerDraftStore.getState();
164
updateSnapShotAnimationSource(item.id, item.source);
@@ -155,12 +172,14 @@ export async function deliverSnapShot(
172
const file = compressed.file;
173
const source = resizeSnapShotSource(capture.source, compressed.imageSize);
174
const dataUrl = compressed.recompressed ? await readFileAsDataUrl(file) : capture.dataUrl;
175
+ const target = resolveTarget();
176
const alreadyAttached =
177
store.getComposerDraft(target)?.images.some(({ id }) => id === capture.id) ?? false;
160
- if (
161
- !alreadyAttached &&
162
- !store.addImage(target, {
163
- type: "image",
178
+ if (!alreadyAttached) {
179
+ const questionAttachmentCount =
180
+ typeof target === "string" ? countQuestionRequestAttachments(target) : null;
181
+ const image = {
182
+ type: "image" as const,
183
id: capture.id,
184
name: file.name,
185
mimeType: file.type,
@@ -168,9 +187,14 @@ export async function deliverSnapShot(
187
previewUrl: dataUrl,
188
file,
189
source,
171
- })
172
- ) {
173
- throw new Error("Remove an attachment, then try this capture again.");
190
+ };
191
+ // Only question drafts may bypass addImage's thread-session gate.
192
+ const added =
193
+ questionAttachmentCount === null
194
+ ? store.addImage(target, image)
195
+ : questionAttachmentCount < PROVIDER_SEND_TURN_MAX_ATTACHMENTS &&
196
+ store.addImages(target, [image]).includes(capture.id);
197
+ if (!added) throw new Error("Remove an attachment, then try this capture again.");
198
}
199
const persisted: PersistedComposerImageAttachment = {
200
id: capture.id,
@@ -216,6 +240,7 @@ export function SnapShotCoordinator() {
240
const animateCaptures = useClientSettings((settings) => settings.snapShotAnimations);
241
const enabled = useClientSettings((settings) => settings.snapShotEnabled);
242
const captureTargetsRef = useRef(new Map<string, Promise<CaptureTarget | null>>());
243
+ const questionPinsRef = useRef(new Map<string, DraftId | null>());
244
const targetResolutionRef = useRef<Promise<CaptureTarget | null> | null>(null);
245
const drainingRef = useRef<Promise<void> | null>(null);
246
const rerunRequestedRef = useRef(false);
@@ -336,8 +361,13 @@ export function SnapShotCoordinator() {
361
}
362
363
try {
339
- await deliverSnapShot(bridge, item, target);
364
+ // Pending captures may have arrived without a requested event; pin before reading.
365
+ resolveSnapShotAttachmentTarget(questionPinsRef.current, item.id, target);
366
+ await deliverSnapShot(bridge, item, () =>
367
+ resolveSnapShotAttachmentTarget(questionPinsRef.current, item.id, target),
368
+ );
369
captureTargetsRef.current.delete(item.id);
370
+ questionPinsRef.current.delete(item.id);
371
soundedCaptureIdsRef.current.delete(item.id);
372
reportedCaptureFailuresRef.current.delete(item.id);
373
} catch (error) {
@@ -387,22 +417,29 @@ export function SnapShotCoordinator() {
417
const target = current ? resolveExistingSnapShotTarget(current, routeThreadRef) : null;
418
// Creating a new draft would navigate the renderer before a self-capture finishes.
419
if (target) {
420
+ resolveSnapShotAttachmentTarget(questionPinsRef.current, event.id, target);
421
void resolveSnapShotDeliveryTarget(captureTargetsRef.current, event.id, () =>
422
Promise.resolve(target),
423
);
424
+ } else if (!questionPinsRef.current.has(event.id)) {
425
+ questionPinsRef.current.set(event.id, null);
426
}
427
return;
428
}
429
case "started": {
430
playCaptureSound(event.id);
431
+ const target = resolveSnapShotDeliveryTarget(
432
+ captureTargetsRef.current,
433
+ event.id,
434
+ resolveCaptureTarget,
435
+ ).then(
436
+ (target) =>
437
+ target && resolveSnapShotAttachmentTarget(questionPinsRef.current, event.id, target),
438
+ );
439
if (animateCaptures && !window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
440
void beginSnapShotAnimationWhenReady(
441
event.id,
401
- resolveSnapShotDeliveryTarget(
402
- captureTargetsRef.current,
403
- event.id,
404
- resolveCaptureTarget,
405
- ),
442
+ target,
443
pendingAnimationStartsRef.current,
444
);
445
}
@@ -412,7 +449,10 @@ export function SnapShotCoordinator() {
449
void drain();
450
return;
451
case "failed": {
415
- if (event.id) captureTargetsRef.current.delete(event.id);
452
+ if (event.id) {
453
+ captureTargetsRef.current.delete(event.id);
454
+ questionPinsRef.current.delete(event.id);
455
+ }
456
dismissFailedSnapShot(
457
event.id,
458
soundedCaptureIdsRef.current,
apps/web/src/questionAttachments.ts
+33
-1
@@ -1,4 +1,5 @@
1
-import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
1
+import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2
+import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
3
import { create } from "zustand";
4
import { DraftId, useComposerDraftStore } from "./composerDraftStore";
5
import { releaseDraftAttachments } from "./lib/attachmentUploadQueue";
@@ -21,6 +22,37 @@ export function questionAttachmentDraftId(
22
);
23
}
24
25
+type OpenQuestionAttachmentDraft = {
26
+ readonly draftId: DraftId;
27
+ readonly requestKeys: ReadonlyArray<DraftId>;
28
+};
29
+
30
+const openQuestionDrafts = new Map<string, OpenQuestionAttachmentDraft>();
31
+
32
+export function openQuestionAttachmentDraft(
33
+ threadRef: ScopedThreadRef,
34
+): OpenQuestionAttachmentDraft | null {
35
+ return openQuestionDrafts.get(scopedThreadKey(threadRef)) ?? null;
36
+}
37
+
38
+export function countQuestionRequestAttachments(draftId: DraftId): number | null {
39
+ for (const draft of openQuestionDrafts.values()) {
40
+ if (draft.draftId === draftId) return countQuestionAttachments(draft.requestKeys);
41
+ }
42
+ return null;
43
+}
44
+
45
+export function trackOpenQuestionAttachmentDraft(
46
+ threadRef: ScopedThreadRef,
47
+ draft: OpenQuestionAttachmentDraft,
48
+): () => void {
49
+ const key = scopedThreadKey(threadRef);
50
+ openQuestionDrafts.set(key, draft);
51
+ return () => {
52
+ if (openQuestionDrafts.get(key) === draft) openQuestionDrafts.delete(key);
53
+ };
54
+}
55
+
56
export const useQuestionAttachmentPreparation = create<{ counts: Record<string, number> }>(() => ({
57
counts: {},
58
}));