fix(web): stalled sends fail and preserve the next message #24419
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -1424,0 +1483,21 @@1483+ routeThreadKey,1484+ sendInFlight: sendInFlightRef.current,1485+ runningSends: runningSendsRef.current,1486+ queued,1487+ });1488+ if (behind !== null) {1489+ e?.preventDefault();1490+ const draft = useComposerDraftStore.getState().getComposerDraft(composerDraftTarget);1491+ setQueuedSubmit(1492+ composerDraftHasUserContent(draft)1493+ ? {1494+ routeThreadKey,1495+ draft,1496+ // Written as a follow-up to the message still sending, so it waits behind that1497+ // turn instead of steering it.1498+ dispatchMode: dispatchMode === "auto" ? "queue" : dispatchMode,1499+ submissionIntent,1500+ ...behind,1501+ }1502+ : null,1503+ );
juliusmarminge added labelsbugfix
juliusmarminge added labelspr:needs-julius
- @@ -281,0 +293,21 @@293+const MESSAGE_DISPATCH_ANSWER_TIMEOUT = "2 minutes";294+295+const requireMessageAnswer = Effect.fn("EnvironmentCommands.requireMessageAnswer")(function* <296+ A,297+ E,298+ R,299+>(effect: Effect.Effect<A, E, R>) {300+ const answer = yield* Effect.forkDetach(effect, { startImmediately: true });301+ return yield* Fiber.join(answer).pipe(302+ Effect.timeoutOrElse({303+ duration: MESSAGE_DISPATCH_ANSWER_TIMEOUT,304+ orElse: () =>305+ Effect.fail(306+ new MessageDispatchUnansweredError({307+ message:308+ "The server did not confirm this message. It may still arrive; check the thread before sending it again.",309+ }),310+ ),311+ }),312+ );313+});
tanstack-source added labelsbug
juliusmarminge added labelstakeover:in-progress
- @@ -1492,2 +1533,21 @@1533+ // Finishing sends read the ref from their own closure; the state only re-renders the effect.1534+ type QueuedSubmit = QueuedComposerSubmit<ComposerDispatchMode, ComposerSubmissionIntent>;1535+ const queuedSubmitRef = useRef<QueuedSubmit | null>(null);1536+ const [queuedSubmit, setQueuedSubmitState] = useState<QueuedSubmit | null>(null);1537+ // The generation of the send running on each thread. The hook outlives navigation, so a1538+ // submit has to wait behind its own thread's send, not whichever send started last.1539+ const runningSendsRef = useRef(new Map<string, number>());1540+ const setQueuedSubmit = (next: QueuedSubmit | null) => {1541+ queuedSubmitRef.current = next;1542+ setQueuedSubmitState(next);14921543 };149315441545+ const onSend: typeof sendComposerDraft = async (1546+ e,1547+ dispatchMode = "auto",1548+ submissionIntent = "foreground",1549+ directAnnotation,1550+ ) => {1551+ const queued =1552+ queuedSubmitRef.current?.routeThreadKey === routeThreadKey ? queuedSubmitRef.current : null;1553+ const behind = directAnnotation
- @@ -1391,11 +1425,7 @@13911425 }1392142613931427 if (failure !== null) {1428+ const failureError = squashAtomCommandFailure(failure);1429+ // An unanswered launch may still create the thread, so the draft stays promoted to it.13941430 if (isLocalDraftThread && draftId && draftThread) {1395- restoreFailedDraftThread(1396- draftId,1397- draftThread,1398- wasBootstrapThreadDeleted(squashAtomCommandFailure(failure))1399- ? newThreadId()1400- : threadIdForSend,1401- );1431+ if (!isThreadLaunchUnanswered(failureError)) {
- @@ -1179,6 +1202,11 @@11791202 : null;11801203 // Keep new-thread flows from reusing this draft while attachments and worktree setup settle.11811204 if (sentDraftThreadRef) markPromotedDraftThreadByRef(sentDraftThreadRef);1182- promptRef.current = "";1183- clearComposerDraftContent(composerDraftTarget);1184- composerRef.current?.resetCursorState();1205+ // Uploads and the dock transition above are awaited; anything typed meanwhile is the next1206+ // message, not this one. A direct annotation writes into the draft as it sends, so it1207+ // always clears.1208+ if (1209+ directAnnotation ||1210+ composerDraftContentUnchanged(1211+ draftAtSend,1212+ useComposerDraftStore.getState().getComposerDraft(composerDraftTarget),@@ -1507,0 +1632,21 @@1632+ const pendingRequest = pendingApprovals.length > 0 || Boolean(activePendingProgress);1633+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- fires the deferred Enter once server state (send settled, connection, pending requests) lets it; no user action marks that moment1634+ useEffect(() => {1635+ const next = queuedSubmit;1636+ if (next === null) return;1637+ const action = resolveQueuedComposerSubmit({1638+ ready: next.ready,1639+ leftThread: next.routeThreadKey !== routeThreadKey,1640+ sendBlocked: queuedSubmitBlocked || sendInFlightRef.current,1641+ pendingRequest: pendingRequest || pendingApprovalsRef.current.length > 0,1642+ draftUnchanged: composerDraftContentUnchanged(1643+ next.draft,1644+ useComposerDraftStore.getState().getComposerDraft(composerDraftTarget),1645+ ),1646+ });1647+ if (action === "wait") return;1648+ queuedSubmitRef.current = null;1649+ setQueuedSubmitState(null);1650+ if (action === "cancel") notifyQueuedSubmitNotSent();1651+ if (action === "send")1652+ void onSendWithRoutingLatest(undefined, next.dispatchMode, next.submissionIntent);@@ -1262,0 +1340,21 @@1340+ waiting.afterSend !== sendGeneration1341+ ) {1342+ return "unrelated";1343+ }1344+ return input.started ? "ready" : "cancel";1345+}1346+1347+/**1348+ * Whether a ready queued submit sends now. It is the user's earlier Enter, so it only sends1349+ * as the plain message they wrote: an edit, or an approval or question that arrived meanwhile1350+ * (which would turn it into an answer they never saw), cancels it instead.1351+ */1352+export function resolveQueuedComposerSubmit(input: {1353+ readonly ready: boolean;1354+ readonly leftThread: boolean;1355+ readonly sendBlocked: boolean;1356+ readonly pendingRequest: boolean;1357+ readonly draftUnchanged: boolean;1358+}): "wait" | "send" | "drop" | "cancel" {1359+ if (input.leftThread) return "drop";1360+ if (!input.ready || input.sendBlocked) return "wait";
- @@ -1391,11 +1425,13 @@13911425 }1392142613931427 if (failure !== null) {1428+ const failureError = squashAtomCommandFailure(failure);13941429 if (isLocalDraftThread && draftId && draftThread) {1395- restoreFailedDraftThread(1396- draftId,1397- draftThread,1398- wasBootstrapThreadDeleted(squashAtomCommandFailure(failure))1399- ? newThreadId()1400- : threadIdForSend,1401- );1430+ // An unanswered launch whose thread already arrived keeps the draft promoted to it; one1431+ // that has not arrived restores the draft, which promotes again if the thread lands.1432+ const launchedThreadArrived =1433+ isThreadLaunchUnanswered(failureError) &&1434+ appAtomRegistry.get(1435+ environmentThreadShells.threadShellAtom(scopeThreadRef(environmentId, threadIdForSend)),1436+ ) !== null;1437+ if (!launchedThreadArrived) {
juliusmarminge added labelstakeover:needs-julius
juliusmarminge removed labelstakeover:in-progress
Sign in to join this conversation.

Approvability#
Verdict: Needs Human Approval
Macroscope's review found this PR requires human approval — The change bounds stalled launch waits and preserves or defers the next message. It adds a lint suppression requiring human review, and unresolved concerns remain about deferred-send routing and retained attachments.
Adjust the Minimum Blocking Severity for this repo — including turning it Off — in [Settings](https://app.macroscope.com/pingdotgg/settings#Repos). You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).