@@ -416,16 +416,34 @@ function createServer(config, store) {
416416 } ;
417417 }
418418
419- // Also rewrite agent_id in URL query params for GET endpoints (DAK-6899)
419+ // Rewrite agent_id in URL path segments (/v1/agents/{id}/...) and query params.
420+ // Body rewriting is handled above; path segments and query params need explicit
421+ // treatment because rewriteRequestAgentId only touches JSON body fields (DAK-6901).
420422 let forwardPath = path ;
421423 try {
422- const qUrl = new URL ( req . url , "http://localhost" ) ;
423- if ( qUrl . searchParams . has ( "agent_id" ) ) {
424- const namespace = sessionNamespace ( resolved . id ) ;
425- const qAgentId = qUrl . searchParams . get ( "agent_id" ) ;
426- qUrl . searchParams . set ( "agent_id" , namespace ) ;
427- forwardPath = qUrl . pathname + "?" + qUrl . searchParams . toString ( ) ;
424+ const fUrl = new URL ( req . url , "http://localhost" ) ;
425+ const namespace = sessionNamespace ( resolved . id ) ;
426+ let modified = false ;
427+
428+ // Path-segment rewrite: /v1/agents/{agent_id}/... (DAK-6901)
429+ const segMatch = fUrl . pathname . match ( / ^ ( \/ v 1 \/ a g e n t s \/ ) ( [ ^ / ] + ) ( \/ .* ) ? $ / ) ;
430+ if ( segMatch ) {
431+ const clientId = segMatch [ 2 ] ;
432+ fUrl . pathname = segMatch [ 1 ] + namespace + ( segMatch [ 3 ] || '' ) ;
433+ if ( ! rewrite ) rewrite = { namespace, restoreTo : clientId } ;
434+ modified = true ;
435+ }
436+
437+ // Query-param rewrite: ?agent_id=... (DAK-6899)
438+ if ( fUrl . searchParams . has ( "agent_id" ) ) {
439+ const qAgentId = fUrl . searchParams . get ( "agent_id" ) ;
440+ fUrl . searchParams . set ( "agent_id" , namespace ) ;
428441 if ( ! rewrite ) rewrite = { namespace, restoreTo : qAgentId } ;
442+ modified = true ;
443+ }
444+
445+ if ( modified ) {
446+ forwardPath = fUrl . pathname + ( fUrl . search || '' ) ;
429447 }
430448 } catch ( _ ) { }
431449 forward ( config , req , res , forwardPath , bodyBuf , outHeaders , rewrite ) ;
0 commit comments