Skip to content

Commit 2c7d991

Browse files
fix: make pending-task edit flush safe across sessions
- Hold the drain lock (and keep the draft) when the outbox save fails or the edits are currently unsendable, so a stale queued payload can never auto-send content the user just changed or removed - Guard the flush completion with a module-scope editing-session generation so an in-flight save from a dismissed session cannot clear the draft or drain lock owned by a newer session (including one in a fresh provider instance) - Never persist the stand-in project's placeholder title/cwd back into the queued creation; snapshot fields pass through unchanged so the drain cannot dispatch a fabricated workspace path Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 9c6d47e commit 2c7d991

1 file changed

Lines changed: 56 additions & 23 deletions

File tree

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ import {
4444
updateThreadOutboxMessage,
4545
type QueuedThreadMessage,
4646
} from "../../state/thread-outbox";
47-
import { setEditingQueuedMessageId, useThreadOutboxMessages } from "../../state/use-thread-outbox";
47+
import {
48+
clearEditingQueuedMessageId,
49+
setEditingQueuedMessageId,
50+
useThreadOutboxMessages,
51+
} from "../../state/use-thread-outbox";
4852
import {
4953
setPendingConnectionError,
5054
useSavedRemoteConnections,
@@ -58,6 +62,11 @@ function pendingTaskDraftKey(messageId: string): string {
5862
return `pending-task:${messageId}`;
5963
}
6064

65+
// Bumped on every beginEditingPendingTask, across provider instances. An
66+
// in-flight flush from a dismissed session compares against it so it never
67+
// clears the draft or drain lock out from under a newer editing session.
68+
let editingSessionGeneration = 0;
69+
6170
function findQueuedPendingTask(messageId: string): QueuedThreadMessage | null {
6271
const message = flattenQueuedThreadMessages(
6372
appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom),
@@ -579,6 +588,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
579588
}
580589
setSelectedEnvironmentId(message.environmentId);
581590
setSelectedProjectKey(scopedProjectKey(message.environmentId, message.creation.projectId));
591+
editingSessionGeneration += 1;
582592
editingPendingTaskRef.current = message;
583593
setEditingPendingTask(message);
584594
// Hold the outbox drain off this task while it is open in the editor.
@@ -599,6 +609,17 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
599609
}
600610
const workspaceSelection = draft.workspaceSelection;
601611
const mode = workspaceSelection?.mode ?? "local";
612+
// When the selection is the stand-in built from the queued snapshot,
613+
// persist the original (possibly absent) snapshot values — the
614+
// stand-in's placeholder title/workspaceRoot must never be written back
615+
// as if they were real project metadata.
616+
const usingPendingSnapshot = selectedProject === editingPendingProject;
617+
const projectTitle = usingPendingSnapshot
618+
? editingPendingTask?.creation?.projectTitle
619+
: selectedProject.title;
620+
const projectCwd = usingPendingSnapshot
621+
? editingPendingTask?.creation?.projectCwd
622+
: selectedProject.workspaceRoot;
602623
return {
603624
environmentId: selectedProject.environmentId,
604625
threadId: ThreadId.make(metadata.threadId),
@@ -611,8 +632,8 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
611632
interactionMode: draft.interactionMode ?? DEFAULT_PROVIDER_INTERACTION_MODE,
612633
creation: {
613634
projectId: selectedProject.id,
614-
projectTitle: selectedProject.title,
615-
projectCwd: selectedProject.workspaceRoot,
635+
...(projectTitle !== undefined ? { projectTitle } : {}),
636+
...(projectCwd !== undefined ? { projectCwd } : {}),
616637
workspaceMode: mode,
617638
branch: workspaceSelection?.branch ?? null,
618639
worktreePath: mode === "worktree" ? null : (workspaceSelection?.worktreePath ?? null),
@@ -621,7 +642,13 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
621642
createdAt: metadata.createdAt,
622643
};
623644
},
624-
[selectedModel, selectedProject, selectedProjectDraftKey],
645+
[
646+
editingPendingProject,
647+
editingPendingTask,
648+
selectedModel,
649+
selectedProject,
650+
selectedProjectDraftKey,
651+
],
625652
);
626653

627654
const finishEditingPendingTask = useCallback(() => {
@@ -671,26 +698,32 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
671698
createdAt: editing.createdAt,
672699
});
673700

674-
if (message) {
675-
// update() rewrites the task only if it is still queued — a concurrent
676-
// delete or delivery wins, so the flush cannot resurrect it. The drain
677-
// lock is released only once the updated payload is durable, and the
678-
// editing draft is discarded only once the save succeeded (a failed
679-
// save keeps it around so the edits rehydrate on the next open).
680-
void updateThreadOutboxMessage(message)
681-
.then(() => {
682-
clearComposerDraft(pendingTaskDraftKey(editing.messageId));
683-
})
684-
.catch((error) => {
685-
console.warn("[new-task] failed to save edited pending task", error);
686-
})
687-
.finally(() => {
688-
setEditingQueuedMessageId(null);
689-
});
690-
} else {
691-
clearComposerDraft(pendingTaskDraftKey(editing.messageId));
692-
setEditingQueuedMessageId(null);
701+
if (!message) {
702+
// The edits are currently unsendable (e.g. the prompt was cleared).
703+
// Keep both the draft and the drain lock: the stale queued payload
704+
// must not auto-send content the user just removed, and reopening the
705+
// task resumes from the saved draft.
706+
return;
693707
}
708+
709+
// update() rewrites the task only if it is still queued — a concurrent
710+
// delete or delivery wins, so the flush cannot resurrect it.
711+
const flushGeneration = editingSessionGeneration;
712+
void updateThreadOutboxMessage(message)
713+
.then(() => {
714+
// If any editing session started (possibly in a fresh provider)
715+
// while the save was in flight, it owns the draft and drain lock.
716+
if (editingSessionGeneration !== flushGeneration) {
717+
return;
718+
}
719+
clearComposerDraft(pendingTaskDraftKey(editing.messageId));
720+
clearEditingQueuedMessageId(editing.messageId);
721+
})
722+
.catch((error) => {
723+
// Keep the drain lock and the draft: delivering the stale payload
724+
// would silently drop the newer edits. Reopening the task retries.
725+
console.warn("[new-task] failed to save edited pending task", error);
726+
});
694727
};
695728
}, [buildPendingTaskMessage]);
696729
const cancelEditingPendingTask = useCallback(() => {

0 commit comments

Comments
 (0)