Skip to content

Open-source SEO, GEO, and marketing skills for AI agents.

fix(chat): de-dupe auto-kickoff under StrictMode + race-safe shared gateway

Yuting Zhong96d6176a77fe
diff --git a/src/components/agent-chat.tsx b/src/components/agent-chat.tsx
index 7c71d32f..58259660 100644
--- a/src/components/agent-chat.tsx
+++ b/src/components/agent-chat.tsx
@@ -55,6 +55,13 @@ export type InitialMessage = {
body: string;
};
+/**
+ * Module-level dedup for the auto-kickoff. Keyed by sessionId so different
+ * threads kick off independently, but a single thread's StrictMode-induced
+ * double-mount only fires once.
+ */
+const KICKOFF_FIRED = new Set<string>();
+
type Props = {
projectSlug: string;
agentSlug: string;
@@ -134,22 +141,25 @@ export function AgentChat({
// Auto-kickoff: on a brand-new thread when the server told us FIRST_TURN.md
// is pending in the agent workspace, send a hidden first message so the
- // agent runs and produces its greeting (per D19). Fire exactly once per
- // mount; once messages.length is non-zero (any path) we never re-fire.
- const kickoffFiredRef = useRef(false);
+ // agent runs and produces its greeting (per D19). Dedup via a MODULE-level
+ // Set keyed by sessionId — useRef doesn't survive React StrictMode dev
+ // remounts (refs are reinitialized on every mount), so a ref guard fires
+ // twice and triggers two concurrent chat requests, which can race the
+ // gateway-client's connect handshake. The module-level set survives
+ // remounts because the module isn't re-evaluated.
useEffect(() => {
if (!autoKickoff) return;
- if (kickoffFiredRef.current) return;
+ if (KICKOFF_FIRED.has(sessionId)) return;
if (messages.length > 0) return;
if (pending) return;
- kickoffFiredRef.current = true;
+ KICKOFF_FIRED.add(sessionId);
// Short, neutral kickoff — the agent's FIRST_TURN.md instruction does the
// rest. Hidden so the user sees the greeting land on a clean canvas.
send("(session start)", { hidden: true });
// Intentionally do not depend on `send` (stable enough across renders) or
// `messages` (would re-fire as the greeting streams in).
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [autoKickoff]);
+ }, [autoKickoff, sessionId]);
// Keep the message thread pinned to the bottom on send / streaming updates.
// Use the scroll container directly (not scrollIntoView on a sentinel) so we
diff --git a/src/server/openclaw/gateway-client.ts b/src/server/openclaw/gateway-client.ts
index 0eb91561..1cdf576c 100644
--- a/src/server/openclaw/gateway-client.ts
+++ b/src/server/openclaw/gateway-client.ts
@@ -338,12 +338,32 @@ export type ChatStreamEvent =
* returns false and we lazily reconnect on the next call.
*/
let sharedClient: GatewayClient | null = null;
+let sharedClientPromise: Promise<GatewayClient> | null = null;
+/**
+ * Returns the shared singleton gateway client, lazily creating it on first
+ * call. The promise cache makes concurrent callers await the SAME in-flight
+ * connect — without it, two callers arriving before the first open()
+ * completes would each create a fresh GatewayClient, opening two WebSockets
+ * and racing each other's connect handshake against the OpenClaw gateway
+ * (which rejects any frame before a connect with "invalid handshake: first
+ * request must be connect"). React StrictMode double-mounting client
+ * effects in Next dev was the easiest reproducer.
+ */
async function getSharedClient(): Promise<GatewayClient> {
if (sharedClient && sharedClient.isOpen()) return sharedClient;
- sharedClient = new GatewayClient();
- await sharedClient.open();
- return sharedClient;
+ if (sharedClientPromise) return sharedClientPromise;
+ sharedClientPromise = (async () => {
+ const candidate = new GatewayClient();
+ try {
+ await candidate.open();
+ sharedClient = candidate;
+ return candidate;
+ } finally {
+ sharedClientPromise = null;
+ }
+ })();
+ return sharedClientPromise;
}
/**