fix(web): keep worktree setup state when leaving a thread and returning #11769
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
- @@ -7445,5 +7495,16 @@7445- createdAt: messageCreatedAt,7446- updatedAt: messageCreatedAt,7447- streaming: false,7448- },7449- ]);7495+ const optimisticUserMessage: ChatMessage = {7496+ id: messageIdForSend,7497+ role: "user",7498+ text: outgoingMessageText,7499+ ...(optimisticAttachments.length > 0 ? { attachments: optimisticAttachments } : {}),7500+ ...(outgoingMessageContext !== undefined ? { context: outgoingMessageContext } : {}),7501+ turnId: null,7502+ createdAt: messageCreatedAt,7503+ updatedAt: messageCreatedAt,7504+ streaming: false,7505+ };7506+ setOptimisticUserMessages((existing) => [...existing, optimisticUserMessage]);7507+ if (baseBranchForWorktree) {7508+ // Hold the send at module scope so a remount restores the message and7509+ // re-subscribes to the setup the server is still running.7510+ rememberPendingWorktreeSetup({@@ -355,0 +369,21 @@369+ readonly ownerKey: string;370+ readonly threadKey: string;371+ readonly environmentId: EnvironmentId;372+ readonly threadId: ThreadId;373+ readonly messages: ReadonlyArray<ChatMessage>;374+}375+376+let pendingWorktreeSetups: PendingWorktreeSetup[] = [];377+378+export function rememberPendingWorktreeSetup(input: {379+ readonly ownerKey: string;380+ readonly threadKey: string;381+ readonly environmentId: EnvironmentId;382+ readonly threadId: ThreadId;383+ readonly messages: ReadonlyArray<ChatMessage>;384+}): void {385+ pendingWorktreeSetups = [386+ ...pendingWorktreeSetups.filter(387+ (entry) => entry.ownerKey !== input.ownerKey && entry.threadKey !== input.threadKey,388+ ),389+ input,
- apps/web/src/components/ChatView.tsx lines 7514 to 7519Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -7449,0 +7495,21 @@7495+ const optimisticUserMessage: ChatMessage = {7496+ id: messageIdForSend,7497+ role: "user",7498+ text: outgoingMessageText,7499+ ...(optimisticAttachments.length > 0 ? { attachments: optimisticAttachments } : {}),7500+ ...(outgoingMessageContext !== undefined ? { context: outgoingMessageContext } : {}),7501+ turnId: null,7502+ createdAt: messageCreatedAt,7503+ updatedAt: messageCreatedAt,7504+ streaming: false,7505+ };7506+ setOptimisticUserMessages((existing) => [...existing, optimisticUserMessage]);7507+ if (baseBranchForWorktree) {7508+ // Hold the send at module scope so a remount restores the message and7509+ // re-subscribes to the setup the server is still running.7510+ rememberPendingWorktreeSetup({7511+ ownerKey: worktreeSetupOwnerKey,7512+ threadKey: routeThreadKey,7513+ environmentId,7514+ threadId: threadIdForSend,7515+ messages: [optimisticUserMessage],
- apps/web/src/components/ChatView.logic.ts line 399Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -355,0 +367,21 @@367+ */368+interface PendingWorktreeSetup {369+ readonly ownerKey: string;370+ readonly threadKey: string;371+ readonly environmentId: EnvironmentId;372+ readonly threadId: ThreadId;373+ readonly messages: ReadonlyArray<ChatMessage>;374+}375+376+let pendingWorktreeSetups: PendingWorktreeSetup[] = [];377+/** Bounds records left behind by setups the user started and navigated away from. */378+const MAX_PENDING_WORKTREE_SETUPS = 8;379+380+export function rememberPendingWorktreeSetup(input: {381+ readonly ownerKey: string;382+ readonly threadKey: string;383+ readonly environmentId: EnvironmentId;384+ readonly threadId: ThreadId;385+ readonly messages: ReadonlyArray<ChatMessage>;386+}): void {387+ const next = [
- @@ -355,0 +387,21 @@387+ const newMessageIds = new Set(input.messages.map((message) => message.id));388+ const displaced = pendingWorktreeSetups.filter(389+ (entry) => entry.ownerKey === input.ownerKey || entry.threadKey === input.threadKey,390+ );391+ // A record this send supersedes is no longer reachable from a remount, so392+ // release the preview URLs it held apart from any message still carried over.393+ for (const record of displaced) {394+ for (const message of record.messages) {395+ if (newMessageIds.has(message.id)) continue;396+ revokeUserMessagePreviewUrls(message);397+ }398+ }399+ const next = [400+ ...pendingWorktreeSetups.filter(401+ (entry) => entry.ownerKey !== input.ownerKey && entry.threadKey !== input.threadKey,402+ ),403+ input,404+ ];405+ // Drop the oldest abandoned records; their messages are no longer rendered406+ // or handed off, so their preview URLs can be released.407+ for (const evicted of next.splice(0, Math.max(0, next.length - MAX_PENDING_WORKTREE_SETUPS))) {
juliusmarminge closed this
Sign in to join this conversation.
Bugbot is paused — on-demand spend limit reached
Bugbot uses usage-based billing for this team and has hit its on-demand spend limit.
A team admin can raise the spend limit in the Cursor dashboard, or wait for the next billing cycle to continue.