@@ -14,6 +14,7 @@ import {
1414import { VALID_MESSAGE_TYPES , HEARTBEAT_INTERVAL , MAX_MISSED_PINGS , RATE_LIMITED_TYPES , WS_RATE_LIMIT_MAX , WS_RATE_LIMIT_WINDOW_MS } from './constants.js' ;
1515import { messageHandlers } from './message-handlers/index.js' ;
1616import { chatStateStore } from '../chat-state-singleton.js' ;
17+ import { debug } from '../logger.js' ;
1718import type { SessionMiddleware , MessageContext } from './types.js' ;
1819
1920export { cleanupAllSessions , cleanupUserSessions } from './session-pool.js' ;
@@ -40,7 +41,7 @@ export function setupWebSocket(
4041 wss . on ( 'close' , ( ) => clearInterval ( heartbeat ) ) ;
4142
4243 wss . on ( 'connection' , async ( ws : WebSocket , req : IncomingMessage ) => {
43- console . log ( '[WS-SERVER] New connection attempt from' , req . socket . remoteAddress ) ;
44+ console . log ( '[WS-SERVER] New connection from' , req . socket . remoteAddress ) ;
4445 ( ws as any ) . missedPings = 0 ;
4546 ws . on ( 'pong' , ( ) => { ( ws as any ) . missedPings = 0 ; } ) ;
4647
@@ -61,7 +62,7 @@ export function setupWebSocket(
6162 } ) ;
6263
6364 const session = ( req as any ) . session ;
64- console . log ( '[WS-SERVER] Session extracted:' , ! ! session , 'token:' , ! ! session ?. githubToken , 'user:' , session ?. githubUser ?. login ) ;
65+ debug ( '[WS-SERVER] Session extracted:' , ! ! session , 'token:' , ! ! session ?. githubToken , 'user:' , session ?. githubUser ?. login ) ;
6566
6667 // Restore auth from encrypted cookie when session file is missing (e.g. after EmptyDir wipe)
6768 if ( session && ! session . githubToken ) {
@@ -73,13 +74,13 @@ export function setupWebSocket(
7374 session . githubUser = data . githubUser ;
7475 session . githubAuthTime = data . githubAuthTime ;
7576 session . save ( ( ) => { } ) ;
76- console . log ( `[WS-SERVER] Restored auth from cookie for user=${ data . githubUser . login } ` ) ;
77+ debug ( `[WS-SERVER] Restored auth from cookie for user=${ data . githubUser . login } ` ) ;
7778 }
7879 }
7980 }
8081
8182 const auth = checkAuth ( session ) ;
82- console . log ( '[WS-SERVER] Auth check result :' , auth . authenticated , auth . error || 'ok' ) ;
83+ debug ( '[WS-SERVER] Auth check:' , auth . authenticated , auth . error || 'ok' ) ;
8384 if ( ! auth . authenticated ) {
8485 logSecurity ( 'warn' , 'ws_unauthorized' , {
8586 ip : req . socket . remoteAddress ,
@@ -110,11 +111,11 @@ export function setupWebSocket(
110111 const tabId = isValidTabId ( rawTabId ) ? rawTabId : 'default' ;
111112 const lastSeq = parseInt ( reqUrl . searchParams . get ( 'lastSeq' ) || '-1' , 10 ) ;
112113 const poolKey = `${ userLogin } :${ tabId } ` ;
113- console . log ( '[WS-SERVER] Authenticated user :' , userLogin , 'tab:' , tabId , 'lastSeq:' , lastSeq , 'checking pool...' ) ;
114+ console . log ( '[WS-SERVER] Authenticated:' , userLogin , 'tab:' , tabId ) ;
114115 let entry = sessionPool . get ( poolKey ) ;
115116
116117 if ( entry ) {
117- console . log ( '[WS-SERVER] Existing pool entry found for' , poolKey ) ;
118+ debug ( '[WS-SERVER] Existing pool entry for' , poolKey ) ;
118119 // Reattach to existing pool entry
119120 if ( entry . ws && entry . ws !== ws && entry . ws . readyState === WebSocket . OPEN ) {
120121 entry . ws . close ( 4002 , 'Replaced by new connection' ) ;
@@ -136,7 +137,7 @@ export function setupWebSocket(
136137 }
137138 }
138139
139- console . log ( '[WS-SERVER] Sending session_reconnected to' , poolKey , 'hasSession:' , ! ! entry . session ) ;
140+ debug ( '[WS-SERVER] Sending session_reconnected to' , poolKey , 'hasSession:' , ! ! entry . session ) ;
140141 poolSend ( entry , {
141142 type : 'session_reconnected' ,
142143 user : userLogin ,
@@ -170,10 +171,10 @@ export function setupWebSocket(
170171 } else {
171172 // Create new pool entry — enforce per-user session cap
172173 if ( countUserSessions ( userLogin ) >= config . maxSessionsPerUser ) {
173- console . log ( '[WS-SERVER] Session cap reached for' , userLogin , '— evicting oldest' ) ;
174+ debug ( '[WS-SERVER] Session cap reached for' , userLogin , '— evicting oldest' ) ;
174175 await evictOldestUserSession ( userLogin ) ;
175176 }
176- console . log ( '[WS-SERVER] Creating new pool entry for' , poolKey ) ;
177+ debug ( '[WS-SERVER] New pool entry for' , poolKey ) ;
177178 const client = createCopilotClient ( githubToken , config . copilotConfigDir ) ;
178179 entry = createPoolEntry ( client , ws ) ;
179180 sessionPool . set ( poolKey , entry ) ;
@@ -189,7 +190,7 @@ export function setupWebSocket(
189190 }
190191
191192 const hasPersistedState = ! ! ( persistedState && persistedState . messages . length > 0 ) ;
192- console . log ( '[WS-SERVER] Sending connected to' , poolKey , 'persisted:' , hasPersistedState ) ;
193+ debug ( '[WS-SERVER] Sending connected to' , poolKey , 'persisted:' , hasPersistedState ) ;
193194 poolSend ( entry , {
194195 type : 'connected' ,
195196 user : userLogin ,
@@ -213,7 +214,7 @@ export function setupWebSocket(
213214 const connectionEntry = entry ;
214215
215216 ws . on ( 'close' , ( code : number , reason : Buffer ) => {
216- console . log ( '[WS-SERVER] Client disconnected :' , poolKey , 'code:' , code , 'reason:' , reason ?. toString ( ) ) ;
217+ console . log ( '[WS-SERVER] Disconnected :' , poolKey , 'code:' , code ) ;
217218 if ( connectionEntry . ws === ws ) {
218219 connectionEntry . ws = null ;
219220 connectionEntry . ttlTimer = setTimeout ( async ( ) => {
@@ -231,7 +232,7 @@ export function setupWebSocket(
231232 ws . on ( 'message' , async ( raw ) => {
232233 try {
233234 const msg = JSON . parse ( raw . toString ( ) ) ;
234- console . log ( '[WS-SERVER] Message from' , userLogin , ':' , msg . type ) ;
235+ debug ( '[WS-SERVER] Message from' , userLogin , ':' , msg . type ) ;
235236
236237 if ( ! msg . type || ! VALID_MESSAGE_TYPES . has ( msg . type ) ) {
237238 poolSend ( connectionEntry , { type : 'error' , message : 'Unknown message type' } ) ;
0 commit comments