@@ -30,6 +30,8 @@ const { CLAUDE_MCP_JSON } = require('../index.js')
3030
3131const ROOT = path . resolve ( __dirname , '..' )
3232const TEMP_ROOT = path . join ( os . tmpdir ( ) , `devcodex-mcp-test-${ process . pid } ` )
33+ const PROFILE_TRACE_TIMEOUT_MS = 30_000
34+ const PROFILE_TRACE_MAX_BYTES = 1024 * 1024
3335
3436function rpcRequest ( id , method , params = { } ) {
3537 return JSON . stringify ( { jsonrpc : '2.0' , id, method, params } )
@@ -187,7 +189,12 @@ function runProfileServerWithReadTrace(requests, cwd) {
187189 "const originalReadFileSync = fs.readFileSync.bind(fs)" ,
188190 "const originalOpenSync = fs.openSync.bind(fs)" ,
189191 "const originalAppendFileSync = fs.appendFileSync.bind(fs)" ,
190- "function trace(file) { try { originalAppendFileSync(process.env.DEVCODEX_READ_TRACE, JSON.stringify(String(file)) + '\\n') } catch {} }" ,
192+ "let tracing = false" ,
193+ "function trace(file) {" ,
194+ " if (tracing || String(file) === process.env.DEVCODEX_READ_TRACE) return" ,
195+ " tracing = true" ,
196+ " try { originalAppendFileSync(process.env.DEVCODEX_READ_TRACE, JSON.stringify(String(file)) + '\\n') } catch {} finally { tracing = false }" ,
197+ "}" ,
191198 "fs.readFileSync = function tracedReadFileSync(file, ...args) {" ,
192199 " trace(file)" ,
193200 " return originalReadFileSync(file, ...args)" ,
@@ -202,9 +209,18 @@ function runProfileServerWithReadTrace(requests, cwd) {
202209 cwd : ROOT ,
203210 input,
204211 encoding : 'utf8' ,
205- env : { ...process . env , DEVCODEX_READ_TRACE : tracePath }
212+ env : { ...process . env , DEVCODEX_READ_TRACE : tracePath } ,
213+ timeout : PROFILE_TRACE_TIMEOUT_MS
206214 } )
207- if ( result . status !== 0 ) throw new Error ( ( result . stderr || result . stdout || 'profile trace server failed' ) . trim ( ) )
215+ if ( result . error ?. code === 'ETIMEDOUT' ) {
216+ throw new Error ( `profile trace server exceeded ${ PROFILE_TRACE_TIMEOUT_MS } ms` )
217+ }
218+ if ( result . status !== 0 ) throw new Error ( ( result . stderr || result . stdout || result . error ?. message || 'profile trace server failed' ) . trim ( ) )
219+ const traceBytes = fs . existsSync ( tracePath ) ? fs . statSync ( tracePath ) . size : 0
220+ assert . ok (
221+ traceBytes <= PROFILE_TRACE_MAX_BYTES ,
222+ `profile read trace exceeded ${ PROFILE_TRACE_MAX_BYTES } bytes: ${ traceBytes } `
223+ )
208224 const responses = result . stdout . trim ( ) . split ( / \r ? \n / ) . filter ( Boolean ) . map ( line => JSON . parse ( line ) )
209225 const reads = fs . existsSync ( tracePath )
210226 ? fs . readFileSync ( tracePath , 'utf8' ) . split ( / \r ? \n / ) . filter ( Boolean ) . map ( line => JSON . parse ( line ) )
0 commit comments