Skip to content

Commit 9256975

Browse files
Prevent draft thread detail polling before shell registration
- Wait for draft threads to enter the shell index before detail lookups - Add coverage for draft and direct server-thread resolution behavior
1 parent 80ead5f commit 9256975

6 files changed

Lines changed: 71 additions & 8 deletions

File tree

‎apps/web/src/components/BranchToolbar.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export const BranchToolbar = memo(function BranchToolbar({
234234
() => scopeThreadRef(environmentId, threadId),
235235
[environmentId, threadId],
236236
);
237-
const serverThread = useThread(threadRef);
237+
const serverThread = useThread(threadRef, { waitForShell: draftId !== undefined });
238238
const draftThread = useComposerDraftStore((store) =>
239239
draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),
240240
);

‎apps/web/src/components/BranchToolbarBranchSelector.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export function BranchToolbarBranchSelector({
129129
() => scopeThreadRef(environmentId, threadId),
130130
[environmentId, threadId],
131131
);
132-
const serverThread = useThread(threadRef);
132+
const serverThread = useThread(threadRef, { waitForShell: draftId !== undefined });
133133
const serverSession = serverThread?.session ?? null;
134134
const draftThread = useComposerDraftStore((store) =>
135135
draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,8 @@ const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDra
620620
const openTerminal = useAtomCommand(terminalEnvironment.open, "terminal open");
621621
const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write");
622622
const closeTerminalMutation = useAtomCommand(terminalEnvironment.close, "terminal close");
623-
const serverThread = useThread(threadRef);
624623
const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
624+
const serverThread = useThread(threadRef, { waitForShell: draftThread !== null });
625625
const projectRef = serverThread
626626
? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
627627
: draftThread
@@ -976,8 +976,8 @@ const PersistentThreadTerminalPanel = memo(function PersistentThreadTerminalPane
976976
newShortcutLabel,
977977
closeShortcutLabel,
978978
}: PersistentThreadTerminalPanelProps) {
979-
const serverThread = useThread(threadRef);
980979
const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
980+
const serverThread = useThread(threadRef, { waitForShell: draftThread !== null });
981981
const projectRef = serverThread
982982
? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
983983
: draftThread
@@ -1181,7 +1181,7 @@ function ChatViewContent(props: ChatViewProps) {
11811181
);
11821182
const composerDraftTarget: ScopedThreadRef | DraftId =
11831183
routeKind === "server" ? routeThreadRef : props.draftId;
1184-
const serverThread = useThread(routeThreadRef);
1184+
const serverThread = useThread(routeThreadRef, { waitForShell: routeKind === "draft" });
11851185
const markThreadVisited = useUiStateStore((store) => store.markThreadVisited);
11861186
const activeThreadLastVisitedAt = useUiStateStore(
11871187
(store) => store.threadLastVisitedAtById[routeThreadKey],

‎apps/web/src/components/GitActionsControl.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,9 @@ export default function GitActionsControl({
986986
() => (activeThreadRef ? { threadRef: activeThreadRef } : undefined),
987987
[activeThreadRef],
988988
);
989-
const activeServerThread = useThread(activeThreadRef);
989+
const activeServerThread = useThread(activeThreadRef, {
990+
waitForShell: draftId !== undefined,
991+
});
990992
const activeDraftThread = useComposerDraftStore((store) =>
991993
draftId
992994
? store.getDraftSession(draftId)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3+
import { describe, expect, it } from "vite-plus/test";
4+
5+
import { resolveThreadDetailRef } from "./entities";
6+
7+
const threadRef = scopeThreadRef(EnvironmentId.make("environment-1"), ThreadId.make("thread-1"));
8+
9+
describe("resolveThreadDetailRef", () => {
10+
it("does not subscribe to a reserved draft thread before it enters the shell index", () => {
11+
expect(
12+
resolveThreadDetailRef(threadRef, {
13+
shellExists: false,
14+
waitForShell: true,
15+
}),
16+
).toBeNull();
17+
});
18+
19+
it("subscribes once the reserved draft thread enters the shell index", () => {
20+
expect(
21+
resolveThreadDetailRef(threadRef, {
22+
shellExists: true,
23+
waitForShell: true,
24+
}),
25+
).toBe(threadRef);
26+
});
27+
28+
it("keeps direct server-thread lookups enabled when the shell has not loaded it", () => {
29+
expect(
30+
resolveThreadDetailRef(threadRef, {
31+
shellExists: false,
32+
waitForShell: false,
33+
}),
34+
).toBe(threadRef);
35+
});
36+
});

‎apps/web/src/state/entities.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,35 @@ export function useThreadStatus(ref: ScopedThreadRef | null): EnvironmentThreadS
152152
);
153153
}
154154

155+
export function resolveThreadDetailRef(
156+
ref: ScopedThreadRef | null,
157+
options: {
158+
shellExists: boolean;
159+
waitForShell: boolean;
160+
},
161+
): ScopedThreadRef | null {
162+
return ref !== null && (!options.waitForShell || options.shellExists) ? ref : null;
163+
}
164+
155165
/** Detail collections composed with shell-authoritative thread/workspace metadata. */
156-
export function useThread(ref: ScopedThreadRef | null): EnvironmentThread | null {
166+
export function useThread(
167+
ref: ScopedThreadRef | null,
168+
options?: {
169+
/**
170+
* Client-reserved draft thread ids do not exist on the server until the
171+
* first send. Waiting for the shell index avoids polling the detail
172+
* endpoint for an intentionally missing thread during that window.
173+
*/
174+
waitForShell?: boolean;
175+
},
176+
): EnvironmentThread | null {
157177
const shell = useThreadShell(ref);
158-
const detail = useThreadDetail(ref);
178+
const detail = useThreadDetail(
179+
resolveThreadDetailRef(ref, {
180+
shellExists: shell !== null,
181+
waitForShell: options?.waitForShell === true,
182+
}),
183+
);
159184
return useMemo(() => mergeEnvironmentThread(detail, shell), [detail, shell]);
160185
}
161186

0 commit comments

Comments
 (0)