From 3cbec3a139deb6e9ae1ab604d348995e32dd01e9 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 06:27:11 +0000 Subject: [PATCH] Introduce shared websocket close code and reason constants Co-authored-by: Theo Browne --- apps/t3/src/runtimeApiServer.ts | 9 +++++++-- packages/contracts/src/ws.ts | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/t3/src/runtimeApiServer.ts b/apps/t3/src/runtimeApiServer.ts index 2e5747174e..c900bec9e2 100644 --- a/apps/t3/src/runtimeApiServer.ts +++ b/apps/t3/src/runtimeApiServer.ts @@ -14,6 +14,8 @@ import { type WsClientMessage, type WsResponseMessage, WS_EVENT_CHANNELS, + WS_CLOSE_CODES, + WS_CLOSE_REASONS, agentConfigSchema, agentSessionIdSchema, newTodoInputSchema, @@ -611,12 +613,15 @@ export async function startRuntimeApiServer( wss.on("connection", (socket, request) => { if (!isAuthorizedConnection(request.url)) { - socket.close(4001, "unauthorized"); + socket.close(WS_CLOSE_CODES.unauthorized, WS_CLOSE_REASONS.unauthorized); return; } if (activeClient && activeClient !== socket) { - activeClient.close(4000, "replaced-by-new-client"); + activeClient.close( + WS_CLOSE_CODES.replacedByNewClient, + WS_CLOSE_REASONS.replacedByNewClient, + ); } activeClient = socket; diff --git a/packages/contracts/src/ws.ts b/packages/contracts/src/ws.ts index ddee99ae7c..cb59be8449 100644 --- a/packages/contracts/src/ws.ts +++ b/packages/contracts/src/ws.ts @@ -6,6 +6,16 @@ export const WS_EVENT_CHANNELS = { agentExit: "agent:exit", } as const; +export const WS_CLOSE_CODES = { + replacedByNewClient: 4000, + unauthorized: 4001, +} as const; + +export const WS_CLOSE_REASONS = { + replacedByNewClient: "replaced-by-new-client", + unauthorized: "unauthorized", +} as const; + const wsRequestSchema = z.object({ type: z.literal("request"), id: z.string().min(1),