Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion apps/web/src/components/ChatView.logic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ProjectId, type ModelSelection, type ThreadId, type TurnId } from "@t3tools/contracts";
import {
ProjectId,
type MessageId,
type ModelSelection,
type ThreadId,
type TurnId,
} from "@t3tools/contracts";
import { type ChatMessage, type SessionPhase, type Thread, type ThreadSession } from "../types";
import { randomUUID } from "~/lib/utils";
import { type ComposerImageAttachment, type DraftThreadState } from "../composerDraftStore";
Expand All @@ -11,6 +17,7 @@ import {
} from "../lib/terminalContext";

export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "t3code:last-invoked-script-by-project";
export const EDIT_REVERT_SYNC_TIMEOUT_MS = 3000;
const WORKTREE_BRANCH_PREFIX = "t3code";

export const LastInvokedScriptByProjectSchema = Schema.Record(ProjectId, Schema.String);
Expand Down Expand Up @@ -119,6 +126,80 @@ export function cloneComposerImageForRetry(
}
}

export function waitForThreadMessageRemoval(
threadId: ThreadId,
messageId: MessageId,
timeoutMs = EDIT_REVERT_SYNC_TIMEOUT_MS,
): Promise<void> {
return new Promise((resolve) => {
const hasMessage = () =>
useStore
.getState()
.threads.find((thread) => thread.id === threadId)
?.messages.some((message) => message.id === messageId) ?? false;

if (!hasMessage()) {
resolve();
return;
}

let settled = false;
const finish = () => {
if (settled) {
return;
}
settled = true;
unsubscribe();
globalThis.clearTimeout(timeoutId);
resolve();
};

const unsubscribe = useStore.subscribe((state) => {
const stillPresent =
state.threads
.find((thread) => thread.id === threadId)
?.messages.some((message) => message.id === messageId) ?? false;
if (!stillPresent) {
finish();
}
});
Comment on lines +157 to +165
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium components/ChatView.logic.ts:157

waitForThreadMessageRemoval has a race condition: if the message is removed between the initial hasMessage() check and the useStore.subscribe() call, the subscription callback never fires and the function waits the full timeout before resolving. After subscribing, re-check the condition immediately to handle the case where the state already changed.

-    const unsubscribe = useStore.subscribe((state) => {
+    const unsubscribe = useStore.subscribe((state) => {
       const stillPresent =
         state.threads
           .find((thread) => thread.id === threadId)
@@ -168,6 +168,9 @@ export function waitForThreadMessageRemoval(
       if (!stillPresent) {
         finish();
       }
     });
+
+    if (!hasMessage()) {
+      finish();
+    }
🤖 Copy this AI Prompt to have your agent fix this:
In file apps/web/src/components/ChatView.logic.ts around lines 157-165:

`waitForThreadMessageRemoval` has a race condition: if the message is removed between the initial `hasMessage()` check and the `useStore.subscribe()` call, the subscription callback never fires and the function waits the full timeout before resolving. After subscribing, re-check the condition immediately to handle the case where the state already changed.

Evidence trail:
apps/web/src/components/ChatView.logic.ts lines 128-170 at REVIEWED_COMMIT. The function `waitForThreadMessageRemoval` calls `hasMessage()` at line 140, then subscribes at line 156 without re-checking the condition after subscribing. The subscription callback (lines 157-163) only fires on future state changes, not on current state, confirming the race condition exists.


const timeoutId = globalThis.setTimeout(() => {
finish();
}, timeoutMs);
});
}

export async function materializeMessageImageAttachmentForEdit(
attachment: Extract<NonNullable<ChatMessage["attachments"]>[number], { type: "image" }>,
): Promise<ComposerImageAttachment | null> {
if (!attachment.previewUrl) {
return null;
}

try {
const response = await fetch(attachment.previewUrl);
if (!response.ok) {
throw new Error(`Failed to load ${attachment.name}.`);
}
const blob = await response.blob();
const file = new File([blob], attachment.name, {
type: blob.type || attachment.mimeType,
});
return {
type: "image",
id: attachment.id,
name: attachment.name,
mimeType: file.type || attachment.mimeType,
sizeBytes: blob.size || attachment.sizeBytes,
previewUrl: attachment.previewUrl,
file,
};
} catch {
return null;
}
}

export function deriveComposerSendState(options: {
prompt: string;
imageCount: number;
Expand Down
Loading
Loading