The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 08:30 UTC
fix(web): stopped or expired questions return their attached files to the composer (#38005)
Julius Marminge committed
Nov 17, 2026 at 00:33 UTC
f79ab79721e3dc49cf738b7dc840e79282c3eca9
4 files changed
+152
-15
apps/mobile/src/state/thread-task-permissions.test.ts
+45
-3
@@ -1,12 +1,14 @@
1
import {
2
AuthOrchestrationOperateScope,
3
DEFAULT_SERVER_SETTINGS,
4
+ EnvironmentId,
5
MessageId,
6
ProjectId,
7
ProviderDriverKind,
8
ProviderInstanceId,
9
RunId,
10
RuntimeRequestId,
11
+ ThreadId,
12
TurnItemId,
13
type ModelSelection,
14
type OrchestrationV2ThreadProjection,
@@ -19,7 +21,10 @@ import {
21
} from "../../../../packages/client-runtime/src/state/orchestrationV2TestFixtures";
22
import type { DraftComposerImageAttachment } from "../lib/composerImages";
23
import type { PendingThreadRequests } from "@t3tools/client-runtime/state/thread-requests";
22
-import { sendApprovalFeedback } from "@t3tools/client-runtime/state/thread-requests";
24
+import {
25
+ makeThreadRequestsResolvedAnnouncer,
26
+ sendApprovalFeedback,
27
+} from "@t3tools/client-runtime/state/thread-requests";
28
import { DateTime } from "effect";
29
import { AsyncResult, type Atom } from "effect/reactivity";
30
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
@@ -42,6 +47,11 @@ const state = vi.hoisted(() => ({
47
compactDueAt: "",
48
setFreshPending: vi.fn(),
49
clearDraft: vi.fn(),
50
+ appendAttachments: vi.fn(),
51
+ questionDrafts: {} as Record<
52
+ string,
53
+ { text: string; attachments: DraftComposerImageAttachment[] }
54
+ >,
55
approve: vi.fn(),
56
answer: vi.fn(),
57
feedback: vi.fn(),
@@ -168,13 +178,13 @@ vi.mock("./use-composer-drafts", async () => {
178
updateComposerDraftSettings: state.updateSettings,
179
ensureComposerDraftsLoaded: () => {},
180
scheduleUnusedComposerAttachmentCleanup: () => {},
171
- appendComposerDraftAttachments: vi.fn(),
181
+ appendComposerDraftAttachments: state.appendAttachments,
182
appendComposerDraftText: vi.fn(),
183
mergeComposerDraftContent: vi.fn(),
184
removeComposerDraftAttachment: vi.fn(),
185
useComposerDraft: () => state.draft,
186
useComposerDraftSettings: () => state.draft,
177
- questionAttachmentDraftsAtom: () => Atom.make({}),
187
+ questionAttachmentDraftsAtom: () => Atom.make(state.questionDrafts),
188
};
189
});
190
vi.mock("../lib/composerImages", () => ({
@@ -195,6 +205,7 @@ vi.mock("./use-remote-environment-registry", () => ({ setPendingConnectionError:
205
import { Alert } from "react-native";
206
import { useThreadComposerState } from "./use-thread-composer-state";
207
import { useSelectedThreadRequests } from "./use-selected-thread-requests";
208
+import { questionAttachmentDraftKey } from "./question-attachments";
209
210
const NO_SIDE_CHATS = { startSideChat: async () => false };
211
@@ -208,6 +219,7 @@ beforeEach(() => {
219
runtimeMode: undefined,
220
};
221
state.freshPending = false;
222
+ state.questionDrafts = {};
223
state.compactModel = null;
224
state.compactDueAt = "";
225
state.thread.latestRun = null;
@@ -506,6 +518,36 @@ describe("mobile task permissions", () => {
518
});
519
});
520
521
+it.each(["cancelled", "expired"] as const)(
522
+ "returns unsent attachments to the thread composer when a question is %s",
523
+ (status) => {
524
+ const environmentId = EnvironmentId.make("secondary");
525
+ const threadId = ThreadId.make("thread");
526
+ const notes = {
527
+ id: "notes",
528
+ type: "file",
529
+ name: "notes.txt",
530
+ mimeType: "text/plain",
531
+ sizeBytes: 5,
532
+ } as unknown as DraftComposerImageAttachment;
533
+ state.questionDrafts = {
534
+ [questionAttachmentDraftKey(environmentId, threadId, "input", "language")]: {
535
+ text: "",
536
+ attachments: [notes],
537
+ },
538
+ };
539
+ makeThreadRequestsResolvedAnnouncer(
540
+ environmentId,
541
+ threadId,
542
+ )([{ id: RuntimeRequestId.make("input"), status }]);
543
+ expect(state.appendAttachments).toHaveBeenCalledWith(
544
+ "secondary:thread",
545
+ [notes],
546
+ expect.objectContaining({ appendReference: true }),
547
+ );
548
+ },
549
+);
550
+
551
describe("mobile handoff preview and submission", () => {
552
it("follows project enablement and provider availability for the selected destination", async () => {
553
state.grantedEnvironments.add("secondary");
apps/mobile/src/state/use-selected-thread-requests.ts
+34
-3
@@ -5,9 +5,12 @@ import {
5
questionAttachmentDraftPrefix,
6
questionAttachmentPreparationAtom,
7
} from "./question-attachments";
8
+import { scopedThreadKey } from "../lib/scopedEntities";
9
import {
10
+ appendComposerDraftAttachments,
11
clearComposerDraft,
12
composerDraftsAtom,
13
+ getComposerDraftSnapshot,
14
questionAttachmentDraftsAtom,
15
} from "./use-composer-drafts";
16
import {
@@ -134,11 +137,39 @@ function pruneResolvedRequestDrafts(
137
if (changed) appAtomRegistry.set(questionAttachmentPreparationAtom, counts);
138
}
139
140
+/**
141
+ * Stop cancels an open question and a provider timeout expires it; neither sent
142
+ * its attachments, so they move to the thread's own draft before pruning.
143
+ */
144
+function returnUnsentAttachmentsToThreadDraft(
145
+ environmentId: EnvironmentId,
146
+ threadId: ThreadId,
147
+ unansweredRequestIds: ReadonlyArray<RuntimeRequestId>,
148
+): void {
149
+ if (unansweredRequestIds.length === 0) return;
150
+ const prefix = questionAttachmentDraftPrefix(environmentId, threadId);
151
+ const drafts = appAtomRegistry.get(questionAttachmentDraftsAtom(prefix));
152
+ const otherRequestKeys = new Set(
153
+ resolvedQuestionDraftKeys(Object.keys(drafts), prefix, unansweredRequestIds),
154
+ );
155
+ const attachments = Object.entries(drafts).flatMap(([key, draft]) =>
156
+ otherRequestKeys.has(key) ? [] : draft.attachments,
157
+ );
158
+ const threadKey = scopedThreadKey(environmentId, threadId);
159
+ const { text } = getComposerDraftSnapshot(threadKey);
160
+ // Chips go after the draft's text, never over a selection the user left in it.
161
+ appendComposerDraftAttachments(threadKey, attachments, {
162
+ appendReference: true,
163
+ insertion: { text, start: text.length, end: text.length },
164
+ });
165
+}
166
+
167
// Resolution can come from anywhere: another device, an interrupt, the turn
168
// ending, or a reload over drafts for requests that resolved meanwhile.
139
-onThreadRequestsResolved(({ environmentId, threadId, pendingRequestIds }) =>
140
- pruneResolvedRequestDrafts(environmentId, threadId, pendingRequestIds),
141
-);
169
+onThreadRequestsResolved(({ environmentId, threadId, pendingRequestIds, unansweredRequestIds }) => {
170
+ returnUnsentAttachmentsToThreadDraft(environmentId, threadId, unansweredRequestIds);
171
+ pruneResolvedRequestDrafts(environmentId, threadId, pendingRequestIds);
172
+});
173
174
/**
175
* Drops `answeredRequestId`'s drafts as soon as the server accepts the
apps/web/src/questionAttachments.test.ts
+32
-2
@@ -169,9 +169,36 @@ it.each(["cancelled", "expired"] as const)(
169
);
170
const answer = `Purple, like ${formatInlineContextReference(threadContextReference(reference))}`;
171
store.setPrompt({ environmentId, threadId }, "Earlier draft");
172
- store.setThreadContexts(questionAttachmentDraftId(environmentId, threadId, request, "first"), [
173
- reference,
172
+ const questionDraft = questionAttachmentDraftId(environmentId, threadId, request, "first");
173
+ store.setThreadContexts(questionDraft, [reference]);
174
+ store.addFiles(questionDraft, [
175
+ {
176
+ type: "file",
177
+ id: "notes",
178
+ name: "notes.txt",
179
+ mimeType: "text/plain",
180
+ sizeBytes: 5,
181
+ file: new File(["notes"], "notes.txt", { type: "text/plain" }),
182
+ },
183
]);
184
+ const photo = {
185
+ type: "image" as const,
186
+ id: "photo",
187
+ name: "photo.png",
188
+ mimeType: "image/png",
189
+ sizeBytes: 5,
190
+ previewUrl: "blob:photo",
191
+ file: new File(["photo"], "photo.png", { type: "image/png" }),
192
+ };
193
+ store.addImages(questionDraft, [photo]);
194
+ const savedPhoto = {
195
+ id: "photo",
196
+ name: "photo.png",
197
+ mimeType: "image/png",
198
+ sizeBytes: 5,
199
+ dataUrl: "data:image/png;base64,cGhvdG8=",
200
+ };
201
+ void store.syncPersistedAttachments(questionDraft, [savedPhoto]);
202
usePendingUserInputAnswers.setState({
203
[userInputDraftKey(environmentId, threadId, request)]: {
204
first: { customAnswer: answer },
@@ -185,6 +212,9 @@ it.each(["cancelled", "expired"] as const)(
212
expect(store.getComposerDraft({ environmentId, threadId })).toMatchObject({
213
prompt: `Earlier draft\n\n${answer}\n\nAlso this`,
214
threadContexts: [reference],
215
+ files: [expect.objectContaining({ name: "notes.txt" })],
216
+ images: [expect.objectContaining({ id: "photo" })],
217
+ persistedAttachments: [savedPhoto],
218
});
219
expect(usePendingUserInputAnswers.getState()).toEqual({});
220
},
apps/web/src/questionAttachments.ts
+41
-7
@@ -99,13 +99,19 @@ export function changeQuestionAttachmentPreparation(key: DraftId, delta: number)
99
);
100
}
101
102
-export function clearQuestionAttachmentDraft(key: DraftId): void {
102
+/** Releases the draft's attachments except `keepIds`, which have moved to another draft. */
103
+export function clearQuestionAttachmentDraft(
104
+ key: DraftId,
105
+ keepIds: ReadonlySet<string> = new Set(),
106
+): void {
107
const store = useComposerDraftStore.getState();
108
const draft = store.getComposerDraft(key);
109
if (draft) {
106
- releaseDraftAttachments([...draft.images, ...draft.files]);
110
+ releaseDraftAttachments([...draft.images, ...draft.files].filter(({ id }) => !keepIds.has(id)));
111
for (const image of draft.images) {
108
- if (image.previewUrl.startsWith("blob:")) URL.revokeObjectURL(image.previewUrl);
112
+ if (!keepIds.has(image.id) && image.previewUrl.startsWith("blob:")) {
113
+ URL.revokeObjectURL(image.previewUrl);
114
+ }
115
}
116
}
117
store.clearComposerContent(key);
@@ -135,7 +141,8 @@ function pruneResolvedQuestionAttachmentDrafts(
141
/**
142
* Stop cancels an open question and a provider timeout expires it; neither sent
143
* the typed answers, so they move to the thread's own draft (not a queued-message
138
- * edit the composer may be showing) with the thread chips they reference.
144
+ * edit the composer may be showing) with the thread chips they reference and
145
+ * every file or image attached to them.
146
*/
147
function returnUnsentAnswersToThreadDraft(
148
environmentId: EnvironmentId,
@@ -165,9 +172,36 @@ function returnUnsentAnswersToThreadDraft(
172
);
173
}
174
}
168
- if (prompt === currentPrompt) return;
169
- store.setPrompt(target, prompt);
170
- store.addThreadContexts(target, threadContexts, { appendReference: false });
175
+ if (prompt !== currentPrompt) {
176
+ store.setPrompt(target, prompt);
177
+ store.addThreadContexts(target, threadContexts, { appendReference: false });
178
+ }
179
+
180
+ const threadPrefix = questionAttachmentDraftPrefix(environmentId, threadId);
181
+ const requestPrefixes = unansweredRequestIds.map(
182
+ (requestId) =>
183
+ `${threadPrefix}${encodeURIComponent(`${JSON.stringify([requestId]).slice(0, -1)},`)}`,
184
+ );
185
+ const questionDrafts = Object.entries(store.draftsByThreadKey).filter(([key]) =>
186
+ requestPrefixes.some((requestPrefix) => key.startsWith(requestPrefix)),
187
+ );
188
+ const images = questionDrafts.flatMap(([, draft]) => draft.images);
189
+ const files = questionDrafts.flatMap(([, draft]) => draft.files);
190
+ if (images.length === 0 && files.length === 0) return;
191
+ store.addImages(target, images);
192
+ store.addFiles(target, files);
193
+ const moved = useComposerDraftStore.getState().getComposerDraft(target);
194
+ const movedIds = new Set([...(moved?.images ?? []), ...(moved?.files ?? [])].map(({ id }) => id));
195
+ // Only a mounted composer re-serializes images, so carry their saved copies for a reload.
196
+ const persisted = moved?.persistedAttachments ?? [];
197
+ const persistedIds = new Set(persisted.map(({ id }) => id));
198
+ const movedPersisted = questionDrafts
199
+ .flatMap(([, draft]) => draft.persistedAttachments)
200
+ .filter(({ id }) => movedIds.has(id) && !persistedIds.has(id));
201
+ if (movedPersisted.length > 0) {
202
+ void store.syncPersistedAttachments(target, [...persisted, ...movedPersisted]);
203
+ }
204
+ for (const [key] of questionDrafts) clearQuestionAttachmentDraft(DraftId.make(key), movedIds);
205
}
206
207
// Resolution can come from anywhere: another device, an interrupt, the turn