Skip to content

Commit 34f3a2c

Browse files
fix: hold per-task drain locks instead of a single editing slot
- The editing/held drain lock is now a set of message ids, so opening a new pending task no longer overwrites the lock still protecting a dismissed task whose latest edits could not be written back (which let the drain send its stale original content) - Track the active editing message id at module scope so an in-flight flush releases exactly its own lock: a reopened session for the same task keeps ownership, while unrelated sessions no longer block release - Stand-in projects no longer fabricate a workspace root from the project id; branch queries skip the empty value instead of issuing VCS calls against a non-path Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 2c7d991 commit 34f3a2c

4 files changed

Lines changed: 58 additions & 34 deletions

File tree

‎apps/mobile/src/features/home/usePendingTaskListActions.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Alert } from "react-native";
44

55
import { removeThreadOutboxMessage } from "../../state/thread-outbox";
66
import type { PendingNewTask } from "../../state/use-pending-new-tasks";
7-
import { clearEditingQueuedMessageId } from "../../state/use-thread-outbox";
7+
import { releaseEditingQueuedMessage } from "../../state/use-thread-outbox";
88

99
export function usePendingTaskListActions(): {
1010
readonly openPendingTask: (pendingTask: PendingNewTask) => void;
@@ -40,7 +40,7 @@ export function usePendingTaskListActions(): {
4040
// it is held for THIS task — clearing it up front (or for another
4141
// task) would let the drain deliver a mid-edit payload.
4242
void removeThreadOutboxMessage(pendingTask.message)
43-
.then(() => clearEditingQueuedMessageId(pendingTask.message.messageId))
43+
.then(() => releaseEditingQueuedMessage(pendingTask.message.messageId))
4444
.catch((error) => {
4545
Alert.alert(
4646
"Could not delete pending task",

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ import {
4545
type QueuedThreadMessage,
4646
} from "../../state/thread-outbox";
4747
import {
48-
clearEditingQueuedMessageId,
49-
setEditingQueuedMessageId,
48+
holdEditingQueuedMessage,
49+
releaseEditingQueuedMessage,
5050
useThreadOutboxMessages,
5151
} from "../../state/use-thread-outbox";
5252
import {
@@ -62,10 +62,11 @@ function pendingTaskDraftKey(messageId: string): string {
6262
return `pending-task:${messageId}`;
6363
}
6464

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;
65+
// The message id owned by the currently active editing session, tracked
66+
// across provider instances. An in-flight flush from a dismissed session
67+
// consults it so it never drops the draft or releases the drain lock out from
68+
// under a newer session editing the same task.
69+
let activeEditingMessageId: string | null = null;
6970

7071
function findQueuedPendingTask(messageId: string): QueuedThreadMessage | null {
7172
const message = flattenQueuedThreadMessages(
@@ -219,9 +220,15 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
219220
setSubmitting(false);
220221
setBranchQuery("");
221222
setExpandedProvider(null);
223+
const editing = editingPendingTaskRef.current;
222224
editingPendingTaskRef.current = null;
223225
setEditingPendingTask(null);
224-
setEditingQueuedMessageId(null);
226+
if (editing) {
227+
if (activeEditingMessageId === editing.messageId) {
228+
activeEditingMessageId = null;
229+
}
230+
releaseEditingQueuedMessage(editing.messageId);
231+
}
225232
}, []);
226233

227234
const environments = useMemo(
@@ -279,7 +286,10 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
279286
environmentId: editingPendingTask.environmentId,
280287
id: creation.projectId,
281288
title: creation.projectTitle ?? "Unknown project",
282-
workspaceRoot: creation.projectCwd ?? String(creation.projectId),
289+
// Deliberately empty when the snapshot has no cwd — downstream consumers
290+
// (branch queries, worktree bootstrap) must skip it, not receive a
291+
// fabricated path.
292+
workspaceRoot: creation.projectCwd ?? "",
283293
repositoryIdentity: null,
284294
defaultModelSelection: editingPendingTask.modelSelection ?? null,
285295
scripts: [],
@@ -435,7 +445,8 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
435445
const branchTarget = useMemo(
436446
() => ({
437447
environmentId: selectedProject?.environmentId ?? null,
438-
cwd: selectedProject?.workspaceRoot ?? null,
448+
// `|| null` also skips the stand-in project's empty workspaceRoot.
449+
cwd: selectedProject?.workspaceRoot || null,
439450
query: null,
440451
}),
441452
[selectedProject?.environmentId, selectedProject?.workspaceRoot],
@@ -588,11 +599,11 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
588599
}
589600
setSelectedEnvironmentId(message.environmentId);
590601
setSelectedProjectKey(scopedProjectKey(message.environmentId, message.creation.projectId));
591-
editingSessionGeneration += 1;
602+
activeEditingMessageId = message.messageId;
592603
editingPendingTaskRef.current = message;
593604
setEditingPendingTask(message);
594605
// Hold the outbox drain off this task while it is open in the editor.
595-
setEditingQueuedMessageId(message.messageId);
606+
holdEditingQueuedMessage(message.messageId);
596607
return true;
597608
}, []);
598609

@@ -655,10 +666,13 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
655666
const editing = editingPendingTaskRef.current;
656667
editingPendingTaskRef.current = null;
657668
if (editing) {
669+
if (activeEditingMessageId === editing.messageId) {
670+
activeEditingMessageId = null;
671+
}
658672
clearComposerDraft(pendingTaskDraftKey(editing.messageId));
673+
releaseEditingQueuedMessage(editing.messageId);
659674
}
660675
setEditingPendingTask(null);
661-
setEditingQueuedMessageId(null);
662676
}, []);
663677

664678
// If the queued task disappears mid-edit (deleted from the list, or
@@ -690,6 +704,9 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
690704
}
691705
editingPendingTaskRef.current = null;
692706
setEditingPendingTask(null);
707+
if (activeEditingMessageId === editing.messageId) {
708+
activeEditingMessageId = null;
709+
}
693710

694711
const message = buildPendingTaskMessage({
695712
threadId: editing.threadId,
@@ -708,16 +725,15 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
708725

709726
// update() rewrites the task only if it is still queued — a concurrent
710727
// delete or delivery wins, so the flush cannot resurrect it.
711-
const flushGeneration = editingSessionGeneration;
712728
void updateThreadOutboxMessage(message)
713729
.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) {
730+
// If this task was reopened (possibly in a fresh provider) while
731+
// the save was in flight, that session owns the draft and the lock.
732+
if (activeEditingMessageId === editing.messageId) {
717733
return;
718734
}
719735
clearComposerDraft(pendingTaskDraftKey(editing.messageId));
720-
clearEditingQueuedMessageId(editing.messageId);
736+
releaseEditingQueuedMessage(editing.messageId);
721737
})
722738
.catch((error) => {
723739
// Keep the drain lock and the draft: delivering the stale payload

‎apps/mobile/src/state/use-thread-outbox-drain.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
import { threadEnvironment } from "./threads";
3636
import { useAtomCommand } from "./use-atom-command";
3737
import {
38-
editingQueuedMessageIdAtom,
38+
editingQueuedMessageIdsAtom,
3939
useThreadOutboxMessages,
4040
useThreadOutboxShellStatuses,
4141
} from "./use-thread-outbox";
@@ -92,7 +92,7 @@ export function useThreadOutboxDrain(): void {
9292
reportFailure: false,
9393
});
9494
const dispatchingQueuedMessageId = useAtomValue(dispatchingQueuedMessageIdAtom);
95-
const editingQueuedMessageId = useAtomValue(editingQueuedMessageIdAtom);
95+
const editingQueuedMessageIds = useAtomValue(editingQueuedMessageIdsAtom);
9696
const queuedMessagesByThreadKey = useThreadOutboxMessages();
9797
const shellStatuses = useThreadOutboxShellStatuses();
9898
const threads = useThreadShells();
@@ -287,7 +287,7 @@ export function useThreadOutboxDrain(): void {
287287
if (!nextQueuedMessage) {
288288
continue;
289289
}
290-
if (nextQueuedMessage.messageId === editingQueuedMessageId) {
290+
if (editingQueuedMessageIds[nextQueuedMessage.messageId]) {
291291
continue;
292292
}
293293
if ((retryNotBeforeRef.current.get(nextQueuedMessage.messageId) ?? 0) > Date.now()) {
@@ -393,7 +393,7 @@ export function useThreadOutboxDrain(): void {
393393
}, [
394394
connectedEnvironments,
395395
dispatchingQueuedMessageId,
396-
editingQueuedMessageId,
396+
editingQueuedMessageIds,
397397
projects,
398398
queuedMessagesByThreadKey,
399399
retryTick,

‎apps/mobile/src/state/use-thread-outbox.ts‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,32 @@ const threadOutboxShellStatusesAtom = Atom.make(
2121
).pipe(Atom.withLabel("mobile:thread-outbox:shell-statuses"));
2222

2323
/**
24-
* The queued pending task currently open in the new-task editor. The outbox
25-
* drain must not deliver it mid-edit; the editor flushes or removes it when
26-
* editing ends.
24+
* Queued pending tasks the outbox drain must not deliver right now: the one
25+
* open in the new-task editor, plus any whose latest edits could not be saved
26+
* back yet (delivering those would send stale content). Editing sessions hold
27+
* their message id here and release it once the queued payload is current.
2728
*/
28-
export const editingQueuedMessageIdAtom = Atom.make<MessageId | null>(null).pipe(
29+
export const editingQueuedMessageIdsAtom = Atom.make<Readonly<Record<MessageId, true>>>({}).pipe(
2930
Atom.keepAlive,
30-
Atom.withLabel("mobile:thread-outbox:editing-message-id"),
31+
Atom.withLabel("mobile:thread-outbox:editing-message-ids"),
3132
);
3233

33-
export function setEditingQueuedMessageId(messageId: MessageId | null): void {
34-
appAtomRegistry.set(editingQueuedMessageIdAtom, messageId);
34+
export function holdEditingQueuedMessage(messageId: MessageId): void {
35+
const current = appAtomRegistry.get(editingQueuedMessageIdsAtom);
36+
if (current[messageId]) {
37+
return;
38+
}
39+
appAtomRegistry.set(editingQueuedMessageIdsAtom, { ...current, [messageId]: true });
3540
}
3641

37-
/** Release the edit lock only if it is held for this specific message. */
38-
export function clearEditingQueuedMessageId(messageId: MessageId): void {
39-
if (appAtomRegistry.get(editingQueuedMessageIdAtom) === messageId) {
40-
appAtomRegistry.set(editingQueuedMessageIdAtom, null);
42+
export function releaseEditingQueuedMessage(messageId: MessageId): void {
43+
const current = appAtomRegistry.get(editingQueuedMessageIdsAtom);
44+
if (!current[messageId]) {
45+
return;
4146
}
47+
const next = { ...current };
48+
delete next[messageId];
49+
appAtomRegistry.set(editingQueuedMessageIdsAtom, next);
4250
}
4351

4452
export function useThreadOutboxMessages() {

0 commit comments

Comments
 (0)