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 22, 05:45 UTC
fix(mobile): drafts on threads with colons in their ids upload attachments early (#29717)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 2, 2026 at 17:59 UTC
927a2c60505a7e0d780b46ec26d29ddde7398c0d
11 files changed
+138
-28
apps/mobile/src/features/cloud/cloud-drafts.ts
+7
-1
@@ -30,7 +30,13 @@ export const removeCloudEnvironments = createRuntimeCommand(connectionAtomRuntim
30
);
31
// Backup failure must retain local file owners so a later sign-in can retry.
32
yield* Effect.tryPromise({
33
- try: () => archiveCloudComposerDrafts(accountId, environmentIds),
33
+ // Resolve draft owners against every environment, not just the removed ones.
34
+ try: () =>
35
+ archiveCloudComposerDrafts(
36
+ accountId,
37
+ environmentIds,
38
+ [...entries.values()].map((entry) => entry.target.environmentId),
39
+ ),
40
catch: (cause) =>
41
new CloudDraftArchiveError({
42
environmentCount: environmentIds.size,
apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts
+27
-11
@@ -253,7 +253,7 @@ describe("draft upload scope and offline submission", () => {
253
const threadId = ThreadId.make("thread:1");
254
const prefix = questionAttachmentDraftPrefix(environment, threadId);
255
const key = questionAttachmentDraftKey(environment, threadId, requestId, "question:1");
256
- expect(composerDraftEnvironmentId(key, [])).toBe(environment);
256
+ expect(composerDraftEnvironmentId(key, [environment], [])).toBe(environment);
257
expect(key.startsWith(prefix)).toBe(true);
258
for (const suffix of ["-extra", ":extra", "/extra", "%extra"]) {
259
const otherKey = questionAttachmentDraftKey(
@@ -263,20 +263,34 @@ describe("draft upload scope and offline submission", () => {
263
"question:1",
264
);
265
expect(otherKey.startsWith(prefix)).toBe(false);
266
- expect(composerDraftEnvironmentId(otherKey, [])).toBe(environment);
266
+ expect(composerDraftEnvironmentId(otherKey, [environment], [])).toBe(environment);
267
}
268
}
269
});
270
it("resolves thread, new-task, and queued-task drafts without crossing environments", () => {
271
- expect(composerDraftEnvironmentId("environment-1:thread", [])).toBe(environmentId);
272
- expect(composerDraftEnvironmentId("new-task:environment-1:project", [])).toBe(environmentId);
271
+ expect(composerDraftEnvironmentId("environment-1:thread", [environmentId], [])).toBe(
272
+ environmentId,
273
+ );
274
+ expect(composerDraftEnvironmentId("new-task:environment-1:project", [], [])).toBe(
275
+ environmentId,
276
+ );
277
expect(
274
- composerDraftEnvironmentId("pending-task:message", [{ messageId: "message", environmentId }]),
278
+ composerDraftEnvironmentId(
279
+ "pending-task:message",
280
+ [],
281
+ [{ messageId: "message", environmentId }],
282
+ ),
283
).toBe(environmentId);
276
- expect(composerDraftEnvironmentId("pending-task:missing", [])).toBeNull();
284
+ expect(composerDraftEnvironmentId("pending-task:missing", [], [])).toBeNull();
285
const colonEnvironment = EnvironmentId.make("a:vcs-status:b");
278
- expect(composerDraftEnvironmentId(`${colonEnvironment}:thread`, [])).toBe(colonEnvironment);
279
- expect(composerDraftEnvironmentId(`new-task:${colonEnvironment}:project`, [])).toBe(
286
+ expect(
287
+ composerDraftEnvironmentId(
288
+ `${colonEnvironment}:thread`,
289
+ [EnvironmentId.make("a"), colonEnvironment],
290
+ [],
291
+ ),
292
+ ).toBe(colonEnvironment);
293
+ expect(composerDraftEnvironmentId(`new-task:${colonEnvironment}:project`, [], [])).toBe(
294
colonEnvironment,
295
);
296
});
@@ -289,9 +303,11 @@ describe("draft upload scope and offline submission", () => {
303
createdAt: "2026-09-05T00:00:00.000Z",
304
},
305
};
292
- expect(composerDraftEnvironmentId("new-task:abc123-def456", [], stamped)).toBe(environmentId);
293
- expect(composerDraftEnvironmentId("new-task:abc123-def456", [])).toBeNull();
294
- expect(composerDraftEnvironmentId("new-task:environment-2:project", [], stamped)).toBe(
306
+ expect(composerDraftEnvironmentId("new-task:abc123-def456", [], [], stamped)).toBe(
307
+ environmentId,
308
+ );
309
+ expect(composerDraftEnvironmentId("new-task:abc123-def456", [], [])).toBeNull();
310
+ expect(composerDraftEnvironmentId("new-task:environment-2:project", [], [], stamped)).toBe(
311
environmentId,
312
);
313
});
apps/mobile/src/lib/composerAttachmentUploadQueue.ts
+12
-2
@@ -21,8 +21,10 @@ export function composerAttachmentUploadKey(
21
return `${environmentId}:${attachmentId}`;
22
}
23
24
+/** Environment and thread ids may both contain colons, so a thread draft key resolves to the longest known environment it starts with. */
25
export function composerDraftEnvironmentId(
26
draftKey: string,
27
+ environmentIds: Iterable<EnvironmentId>,
28
queuedMessages: ReadonlyArray<{
29
readonly messageId: string;
30
readonly environmentId: EnvironmentId;
@@ -42,8 +44,16 @@ export function composerDraftEnvironmentId(
44
const legacy = parseLegacyNewTaskDraftKey(draftKey);
45
return legacy === null ? null : EnvironmentId.make(legacy.environmentId);
46
}
45
- const separator = draftKey.lastIndexOf(":");
46
- return separator > 0 ? EnvironmentId.make(draftKey.slice(0, separator)) : null;
47
+ let match: EnvironmentId | null = null;
48
+ for (const environmentId of environmentIds) {
49
+ if (
50
+ draftKey.startsWith(`${environmentId}:`) &&
51
+ (match === null || environmentId.length > match.length)
52
+ ) {
53
+ match = environmentId;
54
+ }
55
+ }
56
+ return match;
57
}
58
59
type UploadServerConfig = {
apps/mobile/src/lib/composerContextClipboard.test.ts
+7
-1
@@ -29,9 +29,15 @@ vi.mock("expo-file-system", () => ({
29
}));
30
vi.mock("../state/atom-registry", () => ({
31
appAtomRegistry: {
32
- get: () => ({ _tag: "Some", value: { httpBaseUrl: "https://source.example" } }),
32
+ get: (atom: unknown) =>
33
+ atom === "environments"
34
+ ? []
35
+ : { _tag: "Some", value: { httpBaseUrl: "https://source.example" } },
36
},
37
}));
38
+vi.mock("../state/workspace", () => ({
39
+ workspaceConnections: { environmentsAtom: "environments" },
40
+}));
41
vi.mock("../state/session", () => ({
42
environmentSession: { preparedConnectionValueAtom: vi.fn() },
43
}));
apps/mobile/src/lib/composerContextClipboard.ts
+8
-1
@@ -20,6 +20,7 @@ import * as Option from "effect/Option";
20
import { appAtomRegistry } from "../state/atom-registry";
21
import { assetEnvironment } from "../state/assets";
22
import { environmentSession } from "../state/session";
23
+import { workspaceConnections } from "../state/workspace";
24
import { downloadAttachmentForPreview } from "./attachmentDownload";
25
import {
26
persistComposerAttachmentFile,
@@ -137,7 +138,13 @@ async function importAttachment(
138
): Promise<DraftComposerAttachment> {
139
await waitForComposerDraftsLoaded();
140
checkAborted(signal);
140
- const local = findLocalComposerClipboardAttachment(environmentId, record.attachmentId);
141
+ const local = findLocalComposerClipboardAttachment(
142
+ appAtomRegistry
143
+ .get(workspaceConnections.environmentsAtom)
144
+ .map((environment) => environment.environmentId),
145
+ environmentId,
146
+ record.attachmentId,
147
+ );
148
if (local) {
149
if (!local.fileUri && local.type === "image" && local.dataUrl)
150
return {
apps/mobile/src/state/composer-attachment-uploads.test.ts
+41
-1
@@ -102,7 +102,12 @@ vi.mock("./entities", () => ({
102
useVisibleThreadShells: () => [],
103
}));
104
vi.mock("./projects", () => ({ environmentProjects: {} }));
105
-vi.mock("./workspace", () => ({ workspaceConnections: {} }));
105
+vi.mock("./workspace", async () => {
106
+ const { Atom } = await import("effect/reactivity");
107
+ return {
108
+ workspaceConnections: { environmentsAtom: Atom.make([{ environmentId: "environment-1" }]) },
109
+ };
110
+});
111
vi.mock("./server", async () => {
112
const { Atom } = await import("effect/reactivity");
113
return { serverEnvironment: { configValueAtom: Atom.family(() => Atom.make(null)) } };
@@ -210,6 +215,7 @@ const requestsAtom = Atom.make((get) =>
215
composerAttachmentUploadRequests({
216
drafts: get(drafts.composerDraftUploadSourcesAtom),
217
queuedMessagesByThreadKey: get(threadOutboxManager.queuedMessagesByThreadKeyAtom),
218
+ knownEnvironmentIds: [environmentId],
219
connectedEnvironmentIds: new Set([environmentId]),
220
operableEnvironmentIds: new Set([environmentId]),
221
serverConfigs: new Map([[environmentId, config]]),
@@ -337,3 +343,37 @@ it("keeps an unexpected draft flush error blocking Send", async () => {
343
);
344
expect(native.deleted).toEqual(["pending-1"]);
345
});
346
+
347
+it("uploads draft attachments for threads whose ids contain colons", () => {
348
+ const keys = [
349
+ "environment-1:thread-1",
350
+ "environment-1:thread:mcp:namespace:key:0",
351
+ "environment-1:command-1:fork",
352
+ "environment-1:thread-1~queued-edit~run:1",
353
+ ];
354
+ const requests = composerAttachmentUploadRequests({
355
+ drafts: Object.fromEntries(keys.map((key) => [key, { attachments: [{ ...image, id: key }] }])),
356
+ queuedMessagesByThreadKey: {},
357
+ knownEnvironmentIds: [environmentId],
358
+ connectedEnvironmentIds: new Set([environmentId]),
359
+ operableEnvironmentIds: new Set([environmentId]),
360
+ serverConfigs: new Map([[environmentId, config]]),
361
+ });
362
+ expect(requests.map((request) => [request.attachment.id, request.environmentId])).toEqual(
363
+ keys.map((key) => [key, environmentId]),
364
+ );
365
+});
366
+
367
+it("does not upload a disconnected colon environment's draft to its connected prefix", () => {
368
+ const connected = EnvironmentId.make("a");
369
+ const disconnected = EnvironmentId.make("a:vcs-status:b");
370
+ const requests = composerAttachmentUploadRequests({
371
+ drafts: { [`${disconnected}:thread-1`]: { attachments: [image] } },
372
+ queuedMessagesByThreadKey: {},
373
+ knownEnvironmentIds: [connected, disconnected],
374
+ connectedEnvironmentIds: new Set([connected]),
375
+ operableEnvironmentIds: new Set([connected]),
376
+ serverConfigs: new Map([[connected, config]]),
377
+ });
378
+ expect(requests).toEqual([]);
379
+});
apps/mobile/src/state/composer-attachment-uploads.ts
+11
-3
@@ -66,6 +66,8 @@ type ComposerAttachmentUploadRequest = {
66
export function composerAttachmentUploadRequests(input: {
67
readonly drafts: Readonly<Record<string, Pick<ComposerDraft, "attachments" | "project">>>;
68
readonly queuedMessagesByThreadKey: Parameters<typeof flattenQueuedThreadMessages>[0];
69
+ /** Every environment the client knows, so a draft key never resolves to a shorter connected prefix. */
70
+ readonly knownEnvironmentIds: Iterable<EnvironmentId>;
71
readonly connectedEnvironmentIds: ReadonlySet<EnvironmentId>;
72
readonly operableEnvironmentIds: ReadonlySet<EnvironmentId>;
73
readonly serverConfigs: ReadonlyMap<
@@ -75,7 +77,7 @@ export function composerAttachmentUploadRequests(input: {
77
}): ReadonlyArray<ComposerAttachmentUploadRequest> {
78
const queued = flattenQueuedThreadMessages(input.queuedMessagesByThreadKey);
79
return Object.entries(input.drafts).flatMap(([key, draft]) => {
78
- const environmentId = composerDraftEnvironmentId(key, queued, draft);
80
+ const environmentId = composerDraftEnvironmentId(key, input.knownEnvironmentIds, queued, draft);
81
if (
82
environmentId === null ||
83
!input.connectedEnvironmentIds.has(environmentId) ||
@@ -93,7 +95,8 @@ export function composerAttachmentUploadRequests(input: {
95
// Each connected environment's session is read here so a granted or revoked
96
// operate scope re-syncs the queue.
97
const composerAttachmentUploadRequestsAtom = Atom.make((get) => {
96
- const connected = get(workspaceConnections.environmentsAtom).filter(
98
+ const environments = get(workspaceConnections.environmentsAtom);
99
+ const connected = environments.filter(
100
(environment) => environment.connectionState === "connected",
101
);
102
const operableEnvironmentIds = new Set<EnvironmentId>();
@@ -110,6 +113,7 @@ const composerAttachmentUploadRequestsAtom = Atom.make((get) => {
113
return composerAttachmentUploadRequests({
114
drafts: get(composerDraftUploadSourcesAtom),
115
queuedMessagesByThreadKey: get(threadOutboxManager.queuedMessagesByThreadKeyAtom),
116
+ knownEnvironmentIds: environments.map((environment) => environment.environmentId),
117
connectedEnvironmentIds: new Set(connected.map((environment) => environment.environmentId)),
118
operableEnvironmentIds,
119
serverConfigs: get(environmentServerConfigsAtom),
@@ -144,10 +148,14 @@ export function createComposerAttachmentUploadWorker(
148
const queued = flattenQueuedThreadMessages(
149
appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom),
150
);
151
+ const knownEnvironmentIds = appAtomRegistry
152
+ .get(workspaceConnections.environmentsAtom)
153
+ .map((environment) => environment.environmentId);
154
let retained = false;
155
for (const [key, draft] of Object.entries(appAtomRegistry.get(composerDraftsAtom))) {
156
if (
150
- composerDraftEnvironmentId(key, queued, draft) === environmentId &&
157
+ composerDraftEnvironmentId(key, knownEnvironmentIds, queued, draft) ===
158
+ environmentId &&
159
draft.attachments.some((candidate) => candidate.id === attachment.id)
160
) {
161
retained = setComposerDraftAttachmentUpload(key, uploaded) || retained;
apps/mobile/src/state/queued-edit-draft-key.ts
+1
-1
@@ -1,4 +1,4 @@
1
-/** Keep edit keys separate to preserve original drafts; the marker cannot contain colons because environment parsing uses the last one. */
1
+/** Keep edit keys separate to preserve original drafts. */
2
const QUEUED_EDIT_DRAFT_MARKER = "~queued-edit~";
3
4
export function queuedEditDraftKey(threadKey: string, runId: string): string {
apps/mobile/src/state/threadOutboxDelivery.test.ts
+7
-2
@@ -367,7 +367,9 @@ describe("thread outbox drain delivery cleanup", () => {
367
it("removes an acknowledged outbox item even when the sign-out archive write fails", async () => {
368
const message = queuedMessage({ messageId: "archive-write-failure", text: "Delivered" });
369
await harness.manager.enqueue(message);
370
- await composerDrafts.archiveCloudComposerDrafts("account-a", new Set([message.environmentId]));
370
+ await composerDrafts.archiveCloudComposerDrafts("account-a", new Set([message.environmentId]), [
371
+ message.environmentId,
372
+ ]);
373
harness.draftFile.setWriteError(new Error("Draft storage unavailable"));
374
375
await expect(
@@ -399,6 +401,7 @@ describe("thread outbox drain delivery cleanup", () => {
401
await composerDrafts.archiveCloudComposerDrafts(
402
"account-a",
403
new Set([message.environmentId]),
404
+ [message.environmentId],
405
);
406
expect(
407
appAtomRegistry.get(composerDrafts.composerCloudDraftsAtom).signedOut["account-a"]
@@ -426,7 +429,9 @@ describe("thread outbox drain delivery cleanup", () => {
429
const deliveryRevision = harness.manager.revisionOf(message.messageId);
430
const edited = { ...message, text: "Keep this edit" };
431
await harness.manager.update(edited);
429
- await composerDrafts.archiveCloudComposerDrafts("account-a", new Set([message.environmentId]));
432
+ await composerDrafts.archiveCloudComposerDrafts("account-a", new Set([message.environmentId]), [
433
+ message.environmentId,
434
+ ]);
435
await harness.manager.clearEnvironment(message.environmentId);
436
await expect(completeQueuedMessageDelivery(message, deliveryRevision, undefined)).resolves.toBe(
437
"edited",
apps/mobile/src/state/use-composer-drafts.test.ts
+10
-3
@@ -1281,7 +1281,10 @@ describe("mobile composer drafts", () => {
1281
"pending-task:queued-1": { text: "Edited queued task", attachments: [file] },
1282
});
1283
appAtomRegistry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, { queued: [queued] });
1284
- await archiveCloudComposerDrafts("account-a", new Set([environmentId]));
1284
+ await archiveCloudComposerDrafts("account-a", new Set([environmentId]), [
1285
+ environmentId,
1286
+ EnvironmentId.make("direct-environment"),
1287
+ ]);
1288
expect(appAtomRegistry.get(composerDraftsAtom)).toEqual({
1289
"direct-environment:thread-1": DRAFT,
1290
});
@@ -1347,7 +1350,9 @@ describe("mobile composer drafts", () => {
1350
appAtomRegistry.set(composerDraftsAtom, { "environment-1:thread-1": DRAFT });
1351
composerDraftFileMocks.setWriteError(new Error("Storage is full"));
1352
await expect(
1350
- archiveCloudComposerDrafts("account-a", new Set([EnvironmentId.make("environment-1")])),
1353
+ archiveCloudComposerDrafts("account-a", new Set([EnvironmentId.make("environment-1")]), [
1354
+ EnvironmentId.make("environment-1"),
1355
+ ]),
1356
).rejects.toThrow();
1357
expect(
1358
appAtomRegistry.get(composerCloudDraftsAtom).signedOut["account-a"]?.drafts[
@@ -1356,7 +1361,9 @@ describe("mobile composer drafts", () => {
1361
).toEqual(DRAFT);
1362
expect(composerAttachmentCleanupMocks.remove).not.toHaveBeenCalled();
1363
composerDraftFileMocks.setWriteError(null);
1359
- await archiveCloudComposerDrafts(null, new Set([EnvironmentId.make("environment-1")]));
1364
+ await archiveCloudComposerDrafts(null, new Set([EnvironmentId.make("environment-1")]), [
1365
+ EnvironmentId.make("environment-1"),
1366
+ ]);
1367
expect(
1368
decodePersistedComposerState(JSON.parse(composerDraftFileMocks.getDocument())).cloudDrafts
1369
.signedOut["account-a"]?.drafts["environment-1:thread-1"],
apps/mobile/src/state/use-composer-drafts.ts
+7
-2
@@ -828,6 +828,7 @@ function signedOutAttachmentOwners() {
828
}
829
830
export function findLocalComposerClipboardAttachment(
831
+ knownEnvironmentIds: Iterable<EnvironmentId>,
832
environmentId: EnvironmentId,
833
id: string,
834
): DraftComposerAttachment | undefined {
@@ -835,7 +836,10 @@ export function findLocalComposerClipboardAttachment(
836
appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom),
837
).flat();
838
for (const [key, draft] of Object.entries(appAtomRegistry.get(composerDraftsAtom))) {
838
- if (composerDraftEnvironmentId(key, queuedMessages, draft) !== environmentId) continue;
839
+ if (
840
+ composerDraftEnvironmentId(key, knownEnvironmentIds, queuedMessages, draft) !== environmentId
841
+ )
842
+ continue;
843
const attachment = draft.attachments.find((entry) => entry.id === id);
844
if (attachment) return attachment;
845
}
@@ -1084,6 +1088,7 @@ export async function getComposerCloudAccountId(): Promise<string | null> {
1088
export async function archiveCloudComposerDrafts(
1089
accountId: string | null,
1090
environmentIds: ReadonlySet<EnvironmentId>,
1091
+ knownEnvironmentIds: Iterable<EnvironmentId>,
1092
): Promise<void> {
1093
await waitForComposerDraftsLoaded();
1094
if (!(await threadOutboxManager.load())) throw new Error("Could not preserve queued messages.");
@@ -1098,7 +1103,7 @@ export async function archiveCloudComposerDrafts(
1103
const remaining = { ...current };
1104
const savedDrafts = { ...cloud.signedOut[owner]?.drafts };
1105
for (const [key, draft] of Object.entries(current)) {
1101
- const environmentId = composerDraftEnvironmentId(key, queued, draft);
1106
+ const environmentId = composerDraftEnvironmentId(key, knownEnvironmentIds, queued, draft);
1107
if (environmentId !== null && environmentIds.has(environmentId)) {
1108
savedDrafts[key] = draft;
1109
delete remaining[key];