Prevent draft thread detail polling before shell registration (#4670)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
6e3b73884b
commit
e77f42c111
6 files changed
+79
-16
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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[]>([]);
|
||||
|
||||
@@ -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("");
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user