Skip to content

Commit b3a9eab

Browse files
committed
fix(web): wait for a racing client's chat project before failing
When the server rejects the create because another client made the chats project first, wait briefly for that project to reach the store instead of reporting a failure. The headline guard also compares the target environment, an interrupted repo probe is no longer cached, and the docs say the controls hide only while the folder is not a repo.
1 parent a8eff87 commit b3a9eab

4 files changed

Lines changed: 33 additions & 11 deletions

File tree

‎apps/server/src/ws.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,12 +1766,15 @@ const makeWsRpcLayer = (
17661766
// folder is only offered when the data dir is outside any work tree.
17671767
// Detection failures fail closed and hide the folder, never the config.
17681768
// Probed once per connection: a negative VCS detection is not cached.
1769+
// An interrupted probe stays uncached so the next config load retries.
17691770
const resolveChatWorkspaceRoot = yield* Effect.cached(
17701771
gitWorkflow.isRepository(config.baseDir).pipe(
17711772
Effect.map((isRepository) =>
17721773
isRepository ? undefined : path.join(config.baseDir, "chats"),
17731774
),
1774-
Effect.catchCause(() => Effect.succeed(undefined)),
1775+
Effect.catchCause((cause) =>
1776+
Cause.hasInterruptsOnly(cause) ? Effect.interrupt : Effect.succeed(undefined),
1777+
),
17751778
),
17761779
);
17771780

‎apps/web/src/components/chat/DraftHeroHeadline.tsx‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,22 @@ export function DraftHeroHeadline({
157157

158158
// The picker can change the draft's target while "Just chat" is still
159159
// creating its project; a stale continuation must not retarget it again.
160-
const latestTargetRef = useRef({ draftId, activeProjectKey });
160+
const latestTargetRef = useRef({ draftId, activeProjectKey, chatTargetEnvironmentId });
161161
useEffect(() => {
162-
latestTargetRef.current = { draftId, activeProjectKey };
163-
}, [activeProjectKey, draftId]);
162+
latestTargetRef.current = { draftId, activeProjectKey, chatTargetEnvironmentId };
163+
}, [activeProjectKey, chatTargetEnvironmentId, draftId]);
164164
// Project selection changes the target of the open draft in place. The
165165
// prompt stays in the same composer session, so the sidebar only gets a
166166
// draft row if the user later navigates away.
167167
const selectProject = (project: (typeof projects)[number], logicalProjectKey: string) => {
168168
if (!draftId) {
169169
return;
170170
}
171-
latestTargetRef.current = { draftId, activeProjectKey: logicalProjectKey };
171+
latestTargetRef.current = {
172+
draftId,
173+
activeProjectKey: logicalProjectKey,
174+
chatTargetEnvironmentId: project.environmentId,
175+
};
172176
const currentDraft = getComposerDraft(draftId);
173177
setLogicalProjectDraftThreadId(
174178
logicalProjectKey,
@@ -195,13 +199,14 @@ export function DraftHeroHeadline({
195199
if (chatTargetEnvironmentId === null || isChatDraft) {
196200
return;
197201
}
198-
const requested = { draftId, activeProjectKey };
202+
const requested = { draftId, activeProjectKey, chatTargetEnvironmentId };
199203
const project = await ensureChatProject(chatTargetEnvironmentId);
200204
const latest = latestTargetRef.current;
201205
if (
202206
!project ||
203207
latest.draftId !== requested.draftId ||
204-
latest.activeProjectKey !== requested.activeProjectKey
208+
latest.activeProjectKey !== requested.activeProjectKey ||
209+
latest.chatTargetEnvironmentId !== requested.chatTargetEnvironmentId
205210
) {
206211
return;
207212
}

‎apps/web/src/hooks/useChatProject.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ import { useAtomCommand } from "~/state/use-atom-command";
1818
// project.create is in flight would be rejected as a duplicate workspace root.
1919
const inFlightByEnvironment = new Map<EnvironmentId, Promise<EnvironmentProject | null>>();
2020

21+
async function waitForChatProject(
22+
find: () => EnvironmentProject | null,
23+
attempts = 12,
24+
intervalMs = 250,
25+
): Promise<EnvironmentProject | null> {
26+
for (let attempt = 0; attempt < attempts; attempt++) {
27+
const project = find();
28+
if (project) return project;
29+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
30+
}
31+
return null;
32+
}
33+
2134
function reportChatStartFailure(error: unknown) {
2235
toastManager.add(
2336
stackedThreadToast({
@@ -93,8 +106,9 @@ export function useChatProject() {
93106
},
94107
});
95108
if (result._tag === "Failure") {
96-
// Another client may have created it first; that project is fine to use.
97-
const raced = findExisting();
109+
// Another client may have created it first. Its project event can
110+
// land after this rejection, so give the store a moment to catch up.
111+
const raced = await waitForChatProject(findExisting);
98112
if (raced) return raced;
99113
if (!isAtomCommandInterrupted(result)) {
100114
reportChatStartFailure(squashAtomCommandFailure(result));

‎docs/user/thread-sidebar.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ if that project exists there. Otherwise it selects an environment that has it.
1616
To talk to an agent without picking a repository, choose **Just chat** from the
1717
project picker, the empty-state screen, or the add-project menu. Chat threads
1818
share one **Chats** project rooted in a plain `chats` folder inside the T3 data
19-
directory, so branch, worktree, and diff controls stay hidden. Any files the
20-
agent writes land in that folder. The option is unavailable when the data
19+
directory. Branch, worktree, and diff controls stay hidden while that folder is
20+
not a Git repository. Any files the agent writes land in that folder. The option is unavailable when the data
2121
directory itself sits inside a Git checkout.
2222

2323
### Start in the background

0 commit comments

Comments
 (0)