@@ -6,6 +6,36 @@ import { streamResponse, createTelemetryRecord } from "./stream.mjs";
66import { loadExtensions , snapshotRegistry , runOnRequest , runOnResponseStart , runOnResponse } from "./pipeline.mjs" ;
77import { startWatcher } from "./watcher.mjs" ;
88
9+ // --------------------------------------------------------------------------
10+ // Debug logging (writes to ~/.claude/cache-fix-debug.log)
11+ // Set CACHE_FIX_DEBUG=1 to enable
12+ // --------------------------------------------------------------------------
13+
14+ import { appendFileSync , readFileSync , writeFileSync , mkdirSync , renameSync } from "node:fs" ;
15+ import { homedir } from "node:os" ;
16+ import { join } from "node:path" ;
17+ import util from 'util' ;
18+
19+ const LOG_PATH = join ( homedir ( ) , ".claude" , "cache-fix-debug.log" ) ;
20+
21+ function debugLog ( ...args ) {
22+ if ( ! config . debug ) return ;
23+ const line = `[${ new Date ( ) . toISOString ( ) } ] ${ util . format ( ...args ) } \n` ;
24+ try { appendFileSync ( LOG_PATH , line ) ; } catch { }
25+ }
26+
27+ // --------------------------------------------------------------------------
28+
29+ console . log ( `config.port='${ config . port } '` ) ;
30+ console . log ( `config.bind='${ config . bind } '` ) ;
31+ console . log ( `config.upstream='${ config . upstream } '` ) ;
32+ console . log ( `config.debug='${ config . debug } '` ) ;
33+ console . log ( `config.httpsProxy='${ config . httpsProxy } '` ) ;
34+ console . log ( `config.httpProxy='${ config . httpProxy } '` ) ;
35+ console . log ( `config.noProxy='${ config . noProxy } '` ) ;
36+ console . log ( `config.caFile='${ config . caFile } '` ) ;
37+ console . log ( `config.rejectUnauthorized='${ config . rejectUnauthorized } '` ) ;
38+
939function collectBody ( req ) {
1040 return new Promise ( ( resolve , reject ) => {
1141 const chunks = [ ] ;
@@ -74,7 +104,18 @@ async function handleMessages(clientReq, clientRes) {
74104 } ) ;
75105
76106 const pre = await preForward ( clientReq , clientRes , abortController , extSnapshot , "messages" ) ;
77- if ( pre . handled ) return ;
107+ if ( pre . handled ) {
108+ debugLog ( "========================================" ) ;
109+ debugLog ( "[PROXY] handled internally without upstream request" ) ;
110+ debugLog ( "[PROXY -> CLAUDE] RESPONSE" ) ;
111+ debugLog ( "url:" , clientRes . url ) ;
112+ debugLog ( "method:" , clientRes . method ) ;
113+ debugLog ( "status:" , clientRes . statusCode ) ;
114+ debugLog ( "message:" , clientRes . statusMessage ) ;
115+ debugLog ( "bytes:" , clientRes . socket ?. bytesWritten || 0 ) ;
116+ debugLog ( "headers:" , clientRes . getHeaders ( ) ) ;
117+ return ;
118+ }
78119 const { parsed, forwardBody, meta } = pre ;
79120
80121 const requestedModel = parsed ?. model || null ;
@@ -88,6 +129,8 @@ async function handleMessages(clientReq, clientRes) {
88129 abortController . signal
89130 ) ) ;
90131 } catch ( err ) {
132+ console . error ( "[PROXY] forwardRequest error:" , err . message ) ;
133+ debugLog ( "[PROXY] forwardRequest error:" , err . message ) ;
91134 if ( abortController . signal . aborted ) return ;
92135 clientRes . writeHead ( 502 , { "content-type" : "application/json" } ) ;
93136 clientRes . end ( JSON . stringify ( { error : "upstream_error" , message : err . message } ) ) ;
@@ -99,6 +142,18 @@ async function handleMessages(clientReq, clientRes) {
99142 // socket carried the request without each one re-instrumenting upstream.
100143 meta . _upstreamConnectionId = upstreamConnectionId ?? null ;
101144
145+ debugLog ( "" ) ;
146+ debugLog ( "========================================" ) ;
147+ debugLog ( "[UPSTREAM -> PROXY -> CLAUDE] RESPONSE" ) ;
148+ debugLog ( "url:" , upstreamRes . url ) ;
149+ debugLog ( "method:" , upstreamRes . method ) ;
150+ debugLog ( "status1:" , statusCode ) ;
151+ debugLog ( "status2:" , upstreamRes . statusCode ) ;
152+ debugLog ( "message:" , upstreamRes . statusMessage ) ;
153+ debugLog ( "bytes:" , upstreamRes . socket ?. bytesWritten || 0 ) ;
154+ debugLog ( "upstream headers:" , upstreamRes . headers ) ;
155+ debugLog ( "proxy headers:" , responseHeaders ) ;
156+
102157 if ( extSnapshot . length > 0 ) {
103158 const resCtx = { status : statusCode , headers : responseHeaders , meta } ;
104159 await runOnResponseStart ( resCtx , extSnapshot ) ;
@@ -259,16 +314,52 @@ function handleNotFound(_req, res) {
259314 */
260315export function createProxyServer ( ) {
261316 return http . createServer ( ( req , res ) => {
262- if ( req . method === "GET" && req . url === "/health" ) {
263- return handleHealth ( req , res ) ;
264- }
265- if ( req . method === "POST" && req . url ?. startsWith ( "/v1/messages" ) ) {
266- return handleMessages ( req , res ) ;
317+ try {
318+ debugLog ( "" ) ;
319+ debugLog ( "========================================" ) ;
320+ debugLog ( "[CLAUDE -> PROXY] REQUEST" ) ;
321+ debugLog ( "method:" , req . method ) ;
322+ debugLog ( "url:" , req . url ) ;
323+ debugLog ( "headers:" , req . headers ) ;
324+
325+ const originalWrite = res . write ;
326+ const originalEnd = res . end ;
327+
328+ res . write = function ( chunk , ...args ) {
329+ debugLog ( `[PROXY -> CLAUDE] Send chunk. Size: ${ chunk ? chunk . length : 0 } bytes` ) ;
330+ return originalWrite . apply ( res , [ chunk , ...args ] ) ;
331+ } ;
332+
333+ res . end = function ( chunk , ...args ) {
334+ debugLog ( "[PROXY -> CLAUDE] Close connection (res.end)" ) ;
335+ return originalEnd . apply ( res , [ chunk , ...args ] ) ;
336+ } ;
337+
338+ if ( req . method === "GET" && req . url === "/health" ) {
339+ return handleHealth ( req , res ) ;
340+ }
341+ if ( req . method === "POST" && req . url ?. startsWith ( "/v1/messages" ) ) {
342+ return handleMessages ( req , res ) ;
343+ }
344+ if ( req . url ?. startsWith ( "/api/claude_cli/bootstrap" ) ) {
345+ return handleBootstrap ( req , res ) ;
346+ }
347+ console . error ( `ERROR: handler not found for req.url='${ req . url } ' req.method='${ req . method } '` ) ;
348+ debugLog ( `ERROR: handler not found for req.url='${ req . url } ' req.method='${ req . method } '` ) ;
349+ handleNotFound ( req , res ) ;
267350 }
268- if ( req . url ?. startsWith ( "/api/claude_cli/bootstrap" ) ) {
269- return handleBootstrap ( req , res ) ;
351+ catch ( error ) {
352+ console . error ( "!!! REQUEST HANDLER ERROR !!!" ) ;
353+ console . error ( error ) ;
354+ debugLog ( "!!! REQUEST HANDLER ERROR !!!" ) ;
355+ debugLog ( error ) ;
356+ // Reply error 500 to prevent claude hanging
357+ if ( ! res . headersSent ) {
358+ res . writeHead ( 500 , { 'Content-Type' : 'application/json' } ) ;
359+ res . end ( JSON . stringify ( { error : "Internal Proxy Error" , message : error . message } ) ) ;
360+ }
270361 }
271- handleNotFound ( req , res ) ;
362+
272363 } ) ;
273364}
274365
0 commit comments