fix: race condition in auto-project bootstrap + cleanup

- wsNativeApi: Cache last welcome payload and replay to late subscribers.
  Fixes race where WS connect + welcome arrives before React effect fires.
- App.tsx: Add bootstrappedRef to prevent duplicate project creation,
  skip bootstrap in Electron mode
- wsTransport: Use standard import.meta.env for VITE_WS_URL access
- CLAUDE.md: Fix header (was incorrectly '# AGENTS.md')

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-09 22:03:53 +00:00
1 parent bb4f4d57e8
commit 56349ee830
4 files changed
+32 -8

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
# AGENTS.md
# CLAUDE.md
## Project Snapshot
CodeThing is a minimal web GUI for using code agents like Codex and Claude Code (coming soon).
+11 -1
View File
@@ -3,6 +3,7 @@ import { useEffect, useMemo, useRef } from "react";
import ChatView from "./components/ChatView";
import DiffPanel from "./components/DiffPanel";
import Sidebar from "./components/Sidebar";
import { isElectron } from "./env";
import { DEFAULT_MODEL } from "./model-logic";
import { readNativeApi } from "./session-logic";
import { StoreProvider, useStore } from "./store";
@@ -29,13 +30,20 @@ function EventRouter() {
function AutoProjectBootstrap() {
const { state, dispatch } = useStore();
const bootstrappedRef = useRef(false);
useEffect(() => {
// Only relevant in browser mode — Electron doesn't send server welcome
if (isElectron) return;
return onServerWelcome((payload) => {
if (bootstrappedRef.current) return;
// Don't create duplicate projects for the same cwd
const existing = state.projects.find((p) => p.cwd === payload.cwd);
if (existing) {
// If there are threads, ensure one is active
bootstrappedRef.current = true;
// Ensure a thread is active
const existingThread = state.threads.find(
(t) => t.projectId === existing.id,
);
@@ -48,6 +56,8 @@ function AutoProjectBootstrap() {
return;
}
bootstrappedRef.current = true;
// Create project + thread from server cwd
const projectId = crypto.randomUUID();
dispatch({
+19 -1
View File
@@ -9,11 +9,27 @@ import { WsTransport } from "./wsTransport";
let instance: { api: NativeApi; transport: WsTransport } | null = null;
const welcomeListeners = new Set<(payload: WsWelcomePayload) => void>();
let lastWelcome: WsWelcomePayload | null = null;
/**
* Subscribe to the server welcome message. If a welcome was already received
* before this call, the listener fires synchronously with the cached payload.
* This avoids the race between WebSocket connect and React effect registration.
*/
export function onServerWelcome(
listener: (payload: WsWelcomePayload) => void,
): () => void {
welcomeListeners.add(listener);
// Replay cached welcome for late subscribers
if (lastWelcome) {
try {
listener(lastWelcome);
} catch {
// Swallow listener errors
}
}
return () => {
welcomeListeners.delete(listener);
};
@@ -24,9 +40,11 @@ export function createWsNativeApi(): NativeApi {
const transport = new WsTransport();
// Listen for server welcome and forward to registered listeners
// Listen for server welcome and forward to registered listeners.
// Also cache it so late subscribers (React effects) get it immediately.
transport.subscribe(WS_CHANNELS.serverWelcome, (data) => {
const payload = data as WsWelcomePayload;
lastWelcome = payload;
for (const listener of welcomeListeners) {
try {
listener(payload);
+1 -5
View File
@@ -24,11 +24,7 @@ export class WsTransport {
constructor(url?: string) {
// In dev mode, VITE_WS_URL points to the server's WebSocket endpoint.
// In production, the page is served by the WS server on the same host:port.
const envUrl =
typeof import.meta !== "undefined"
? (import.meta as unknown as { env?: { VITE_WS_URL?: string } }).env
?.VITE_WS_URL
: undefined;
const envUrl = import.meta.env.VITE_WS_URL as string | undefined;
this.url =
url ??
(envUrl && envUrl.length > 0