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(mobile): recovering a failed task keeps a paste that lands while it saves (#38031)
Co-authored-by: Grok 4.7 <noreply@x.ai> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 17, 2026 at 11:18 UTC
134580067f3c811f32630667bd57f048d0bff9ee
3 files changed
+134
-13
apps/mobile/src/state/recover-failed-thread-draft.ts
+31
-8
@@ -1,5 +1,7 @@
1
import type { QueuedThreadMessage } from "./thread-outbox-model";
2
+import type { DraftComposerAttachment } from "../lib/composerImages";
3
import { scopedThreadKey } from "../lib/scopedEntities";
4
+import { abortComposerDraftOperations } from "./composerDraftOperations";
5
import { restoredNewTaskDraftKey } from "./new-task-draft-key";
6
import {
7
appendComposerDraftAttachments,
@@ -13,22 +15,43 @@ export async function recoverFailedThreadDraft(message: QueuedThreadMessage): Pr
15
const sourceKey = scopedThreadKey(message.environmentId, message.threadId);
16
const targetKey = restoredNewTaskDraftKey(message.messageId);
17
const source = getComposerDraftSnapshot(sourceKey);
16
- if (source.text.length === 0 && source.attachments.length === 0) return;
18
+ if (source.text.length === 0 && source.attachments.length === 0) {
19
+ abortComposerDraftOperations(sourceKey);
20
+ return;
21
+ }
22
23
await mergeComposerDraftContent(targetKey, {
24
text: source.text,
25
context: source.context,
26
attachments: [],
27
});
23
- const existingIds = new Set(
24
- getComposerDraftSnapshot(targetKey).attachments.map((attachment) => attachment.id),
25
- );
28
+ // Recovery may exceed caps; preserve every file and leave trimming to the editor.
29
+ appendComposerDraftAttachments(targetKey, attachmentsMissingFrom(targetKey, source.attachments), {
30
+ restore: true,
31
+ });
32
+ await flushComposerDrafts();
33
+ abortComposerDraftOperations(sourceKey);
34
+ // A paste still preparing can land in the source during the saves; move it with its reference.
35
+ const sourceIds = new Set(source.attachments.map((attachment) => attachment.id));
36
appendComposerDraftAttachments(
37
targetKey,
28
- source.attachments.filter((attachment) => !existingIds.has(attachment.id)),
29
- { restore: true },
38
+ attachmentsMissingFrom(
39
+ targetKey,
40
+ getComposerDraftSnapshot(sourceKey).attachments.filter(
41
+ (attachment) => !sourceIds.has(attachment.id),
42
+ ),
43
+ ),
44
+ { restore: true, appendReference: true },
45
);
31
- // Recovery may exceed caps; preserve every file and leave trimming to the editor.
32
- await flushComposerDrafts();
46
clearComposerDraftContent(sourceKey);
47
}
48
+
49
+function attachmentsMissingFrom(
50
+ draftKey: string,
51
+ attachments: ReadonlyArray<DraftComposerAttachment>,
52
+) {
53
+ const existingIds = new Set(
54
+ getComposerDraftSnapshot(draftKey).attachments.map((attachment) => attachment.id),
55
+ );
56
+ return attachments.filter((attachment) => !existingIds.has(attachment.id));
57
+}
apps/mobile/src/state/threadOutboxDelivery.test.ts
+95
@@ -6,6 +6,7 @@ import * as Effect from "effect/Effect";
6
import * as Fiber from "effect/Fiber";
7
import {
8
CommandId,
9
+ ComposerContextId,
10
EnvironmentId,
11
MessageId,
12
ProjectId,
@@ -158,6 +159,7 @@ import { pendingThreadCreationOutcomesAtom } from "./pending-thread-creation";
159
import { clearThreadComposerError, threadComposerErrorsAtom } from "./thread-composer-error";
160
import type { QueuedThreadMessage } from "./thread-outbox-model";
161
import * as composerDrafts from "./use-composer-drafts";
162
+import { startComposerDraftOperation } from "./composerDraftOperations";
163
import { recoverFailedThreadDraft } from "./recover-failed-thread-draft";
164
import { editingQueuedMessageIdsAtom } from "./use-thread-outbox";
165
import {
@@ -789,6 +791,99 @@ describe("thread outbox recovery rollback", () => {
791
);
792
});
793
794
+ it("keeps a late attachment and its reference when recovery fills the context limit", async () => {
795
+ const message: QueuedThreadMessage = {
796
+ ...queuedMessage({ messageId: "failed-full-context", text: "Original prompt" }),
797
+ modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" },
798
+ creation: {
799
+ projectId: ProjectId.make("project-1"),
800
+ workspaceMode: "local",
801
+ branch: null,
802
+ worktreePath: null,
803
+ },
804
+ };
805
+ const sourceKey = `${message.environmentId}:${message.threadId}`;
806
+ const targetKey = "new-task:restored-failed-full-context";
807
+ await composerDrafts.waitForComposerDraftsLoaded();
808
+ const contextDraft = (start: number) => {
809
+ const records = Array.from({ length: 100 }, (_, index) => ({
810
+ version: 1 as const,
811
+ contextId: ComposerContextId.make(`skill-${start + index}`),
812
+ kind: "skill" as const,
813
+ label: "Skill",
814
+ name: "skill",
815
+ }));
816
+ return {
817
+ text: records
818
+ .map((record) => `[Skill](t3-context://v1/skill/${record.contextId})`)
819
+ .join(" "),
820
+ context: { version: 1 as const, records },
821
+ attachments: [],
822
+ };
823
+ };
824
+ appAtomRegistry.set(composerDrafts.composerDraftsAtom, {
825
+ [targetKey]: contextDraft(0),
826
+ [sourceKey]: contextDraft(100),
827
+ });
828
+ const saving = Promise.withResolvers<void>();
829
+ const resume = Promise.withResolvers<void>();
830
+ const write = harness.draftFile.File.prototype.write;
831
+ const writeSpy = vi
832
+ .spyOn(harness.draftFile.File.prototype, "write")
833
+ .mockImplementationOnce(async function (value) {
834
+ write(value);
835
+ saving.resolve();
836
+ await resume.promise;
837
+ });
838
+ const attachment = {
839
+ id: "late-paste",
840
+ type: "image" as const,
841
+ name: "pasted.png",
842
+ mimeType: "image/png",
843
+ sizeBytes: 1,
844
+ dataUrl: "data:image/png;base64,AA==",
845
+ previewUri: "data:image/png;base64,AA==",
846
+ };
847
+ try {
848
+ const recovering = recoverFailedThreadDraft(message);
849
+ await saving.promise;
850
+ composerDrafts.appendComposerDraftAttachments(sourceKey, [attachment], {
851
+ appendReference: true,
852
+ });
853
+ resume.resolve();
854
+ await recovering;
855
+ const restored = composerDrafts.getComposerDraftSnapshot(targetKey);
856
+ expect(restored.attachments).toContainEqual(attachment);
857
+ expect(restored.context?.records).toHaveLength(201);
858
+ const imageRecord = restored.context?.records.find(
859
+ (record) => "attachmentId" in record && record.attachmentId === attachment.id,
860
+ );
861
+ expect(imageRecord).toBeDefined();
862
+ expect(restored.text).toContain(`/${imageRecord!.contextId})`);
863
+ expect(composerDrafts.getComposerDraftSnapshot(sourceKey).attachments).toEqual([]);
864
+ } finally {
865
+ resume.resolve();
866
+ writeSpy.mockRestore();
867
+ await composerDrafts.flushComposerDrafts();
868
+ }
869
+ });
870
+
871
+ it("cancels a paste that finishes preparing after recovery returns", async () => {
872
+ const message = queuedMessage({ messageId: "failed-unfinished-paste", text: "Original" });
873
+ const sourceKey = `${message.environmentId}:${message.threadId}`;
874
+ appAtomRegistry.set(composerDrafts.composerDraftsAtom, {
875
+ [sourceKey]: { text: "Follow-up", attachments: [] },
876
+ });
877
+ const paste = startComposerDraftOperation(sourceKey);
878
+ try {
879
+ await recoverFailedThreadDraft(message);
880
+ expect(paste.signal.aborted).toBe(true);
881
+ } finally {
882
+ paste.finish();
883
+ await composerDrafts.flushComposerDrafts();
884
+ }
885
+ });
886
+
887
it("keeps setup edits recoverable when saving their recovery draft fails", async () => {
888
const message = queuedMessage({ messageId: "failed-save", text: "Original prompt" });
889
const sourceKey = `${message.environmentId}:${message.threadId}`;
apps/mobile/src/state/use-composer-drafts.ts
+8
-5
@@ -403,6 +403,7 @@ function draftWithInsertedContext(
403
draft: ComposerDraft,
404
content: { text: string; context: OrchestrationMessageContext },
405
target?: ComposerDraftInsertion,
406
+ maxContextRecords = COMPOSER_CONTEXT_MAX_RECORDS,
407
): ComposerDraft | null {
408
const { start, end } = contextInsertionRange(draftKey, draft, target);
409
const before = draft.text.slice(0, start);
@@ -412,7 +413,7 @@ function draftWithInsertedContext(
413
const records = new Map(draft.context?.records.map((record) => [record.contextId, record]));
414
for (const record of content.context.records) records.set(record.contextId, record);
415
const context = referencedComposerContext(text, { version: 1, records: [...records.values()] });
415
- if ((context?.records.length ?? 0) > COMPOSER_CONTEXT_MAX_RECORDS) return null;
416
+ if ((context?.records.length ?? 0) > maxContextRecords) return null;
417
rememberComposerDraftSelection(
418
draftKey,
419
text,
@@ -1465,9 +1466,10 @@ export function appendComposerDraftAttachments(
1466
options?.maxAttachments ?? PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
1467
) - retained.attachments.length,
1468
);
1468
- const contextCapacity = options?.appendReference
1469
- ? Math.max(0, COMPOSER_CONTEXT_MAX_RECORDS - (retained.context?.records.length ?? 0))
1470
- : attachments.length;
1469
+ const contextCapacity =
1470
+ options?.appendReference && !options.restore
1471
+ ? Math.max(0, COMPOSER_CONTEXT_MAX_RECORDS - (retained.context?.records.length ?? 0))
1472
+ : attachments.length;
1473
const accepted = attachments.slice(0, Math.min(remaining, contextCapacity));
1474
rejected = attachments.slice(accepted.length);
1475
if (accepted.length === 0) {
@@ -1483,7 +1485,8 @@ export function appendComposerDraftAttachments(
1485
text: records.map(formatComposerContextReference).join(" "),
1486
context: { version: 1, records },
1487
},
1486
- options?.insertion,
1488
+ options.insertion,
1489
+ options.restore ? Infinity : COMPOSER_CONTEXT_MAX_RECORDS,
1490
);
1491
if (!inserted) {
1492
rejected = attachments;