Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Prevent draft thread detail polling before shell registration (#4670)

Julius Marmingee77f42c111dc
diff --git a/apps/web/src/components/BranchToolbar.tsx b/apps/web/src/components/BranchToolbar.tsx
index 2a1d0d1ec..3a83f5c9a 100644
--- a/apps/web/src/components/BranchToolbar.tsx
+++ b/apps/web/src/components/BranchToolbar.tsx
@@ -234,10 +234,10 @@ export const BranchToolbar = memo(function BranchToolbar({
() => scopeThreadRef(environmentId, threadId),
[environmentId, threadId],
);
- const serverThread = useThread(threadRef);
const draftThread = useComposerDraftStore((store) =>
draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),
);
+ const serverThread = useThread(threadRef, { waitForShell: draftThread !== null });
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
const activeProjectRef = serverThread
? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsx
index a61427f20..74138fa52 100644
--- a/apps/web/src/components/BranchToolbarBranchSelector.tsx
+++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx
@@ -129,11 +129,11 @@ export function BranchToolbarBranchSelector({
() => scopeThreadRef(environmentId, threadId),
[environmentId, threadId],
);
- const serverThread = useThread(threadRef);
- const serverSession = serverThread?.session ?? null;
const draftThread = useComposerDraftStore((store) =>
draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),
);
+ const serverThread = useThread(threadRef, { waitForShell: draftThread !== null });
+ const serverSession = serverThread?.session ?? null;
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
const activeProjectRef = serverThread
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index b234d0765..bdde62a3e 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -620,8 +620,8 @@ const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDra
const openTerminal = useAtomCommand(terminalEnvironment.open, "terminal open");
const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write");
const closeTerminalMutation = useAtomCommand(terminalEnvironment.close, "terminal close");
- const serverThread = useThread(threadRef);
const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
+ const serverThread = useThread(threadRef, { waitForShell: draftThread !== null });
const projectRef = serverThread
? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
: draftThread
@@ -976,8 +976,8 @@ const PersistentThreadTerminalPanel = memo(function PersistentThreadTerminalPane
newShortcutLabel,
closeShortcutLabel,
}: PersistentThreadTerminalPanelProps) {
- const serverThread = useThread(threadRef);
const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
+ const serverThread = useThread(threadRef, { waitForShell: draftThread !== null });
const projectRef = serverThread
? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
: draftThread
@@ -1181,7 +1181,14 @@ function ChatViewContent(props: ChatViewProps) {
);
const composerDraftTarget: ScopedThreadRef | DraftId =
routeKind === "server" ? routeThreadRef : props.draftId;
- const serverThread = useThread(routeThreadRef);
+ const draftThread = useComposerDraftStore((store) =>
+ routeKind === "server"
+ ? store.getDraftSessionByRef(routeThreadRef)
+ : draftId
+ ? store.getDraftSession(draftId)
+ : null,
+ );
+ const serverThread = useThread(routeThreadRef, { waitForShell: draftThread !== null });
const markThreadVisited = useUiStateStore((store) => store.markThreadVisited);
const activeThreadLastVisitedAt = useUiStateStore(
(store) => store.threadLastVisitedAtById[routeThreadKey],
@@ -1234,13 +1241,6 @@ function ChatViewContent(props: ChatViewProps) {
const setLogicalProjectDraftThreadId = useComposerDraftStore(
(store) => store.setLogicalProjectDraftThreadId,
);
- const draftThread = useComposerDraftStore((store) =>
- routeKind === "server"
- ? store.getDraftSessionByRef(routeThreadRef)
- : draftId
- ? store.getDraftSession(draftId)
- : null,
- );
const promptRef = useRef("");
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
const composerTerminalContextsRef = useRef<TerminalContextDraft[]>([]);
diff --git a/apps/web/src/components/GitActionsControl.tsx b/apps/web/src/components/GitActionsControl.tsx
index 11716e935..4f2bd1995 100644
--- a/apps/web/src/components/GitActionsControl.tsx
+++ b/apps/web/src/components/GitActionsControl.tsx
@@ -986,7 +986,6 @@ export default function GitActionsControl({
() => (activeThreadRef ? { threadRef: activeThreadRef } : undefined),
[activeThreadRef],
);
- const activeServerThread = useThread(activeThreadRef);
const activeDraftThread = useComposerDraftStore((store) =>
draftId
? store.getDraftSession(draftId)
@@ -994,6 +993,9 @@ export default function GitActionsControl({
? store.getDraftThreadByRef(activeThreadRef)
: null,
);
+ const activeServerThread = useThread(activeThreadRef, {
+ waitForShell: activeDraftThread !== null,
+ });
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
const [isCommitDialogOpen, setIsCommitDialogOpen] = useState(false);
const [dialogCommitMessage, setDialogCommitMessage] = useState("");
diff --git a/apps/web/src/state/entities.test.ts b/apps/web/src/state/entities.test.ts
new file mode 100644
index 000000000..0d7611ec4
--- /dev/null
+++ b/apps/web/src/state/entities.test.ts
@@ -0,0 +1,36 @@
+import { scopeThreadRef } from "@t3tools/client-runtime/environment";
+import { EnvironmentId, ThreadId } from "@t3tools/contracts";
+import { describe, expect, it } from "vite-plus/test";
+
+import { resolveThreadDetailRef } from "./entities";
+
+const threadRef = scopeThreadRef(EnvironmentId.make("environment-1"), ThreadId.make("thread-1"));
+
+describe("resolveThreadDetailRef", () => {
+ it("does not subscribe to a reserved draft thread before it enters the shell index", () => {
+ expect(
+ resolveThreadDetailRef(threadRef, {
+ shellExists: false,
+ waitForShell: true,
+ }),
+ ).toBeNull();
+ });
+
+ it("subscribes once the reserved draft thread enters the shell index", () => {
+ expect(
+ resolveThreadDetailRef(threadRef, {
+ shellExists: true,
+ waitForShell: true,
+ }),
+ ).toBe(threadRef);
+ });
+
+ it("keeps direct server-thread lookups enabled when the shell has not loaded it", () => {
+ expect(
+ resolveThreadDetailRef(threadRef, {
+ shellExists: false,
+ waitForShell: false,
+ }),
+ ).toBe(threadRef);
+ });
+});
diff --git a/apps/web/src/state/entities.ts b/apps/web/src/state/entities.ts
index 9bd20070c..552468e04 100644
--- a/apps/web/src/state/entities.ts
+++ b/apps/web/src/state/entities.ts
@@ -152,10 +152,35 @@ export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadS
);
}
+export function resolveThreadDetailRef(
+ ref: ScopedThreadRef | null,
+ options: {
+ shellExists: boolean;
+ waitForShell: boolean;
+ },
+): ScopedThreadRef | null {
+ return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null;
+}
+
/** Detail collections composed with shell-authoritative thread/workspace metadata. */
-export function useThread(ref: ScopedThreadRef | null): EnvironmentThread | null {
+export function useThread(
+ ref: ScopedThreadRef | null,
+ options?: {
+ /**
+ * Client-reserved draft thread ids do not exist on the server until the
+ * first send. Waiting for the shell index avoids polling the detail
+ * endpoint for an intentionally missing thread during that window.
+ */
+ waitForShell?: boolean;
+ },
+): EnvironmentThread | null {
const shell = useThreadShell(ref);
- const detail = useThreadDetail(ref);
+ const detail = useThreadDetail(
+ resolveThreadDetailRef(ref, {
+ shellExists: shell !== null,
+ waitForShell: options?.waitForShell === true,
+ }),
+ );
return useMemo(() => mergeEnvironmentThread(detail, shell), [detail, shell]);
}