@@ -40,6 +40,7 @@ import {
4040import type { EffectiveSetting , GetSandboxConfigResponse , SandboxPolicy , SettingValue } from './gen/sandbox_pb.js' ;
4141import { PolicySource , type SandboxPolicySchema , SettingScope , type SettingValueSchema } from './gen/sandbox_pb.js' ;
4242import { ProviderClient } from './providers.js' ;
43+ import { heldUntilReady , OWNER_REPLICA_HEADER , OWNER_REQUEST_HEADER , routedStream } from './routing.js' ;
4344import { validateSshResponse } from './ssh-validate.js' ;
4445import { buildTransport , type ConnectOptions } from './transport.js' ;
4546
@@ -1282,6 +1283,29 @@ export class SandboxClient {
12821283 }
12831284 }
12841285
1286+ // get() that also asks the gateway which replica owns the supervisor session.
1287+ private async getWithOwner (
1288+ name : string ,
1289+ options ?: SandboxCallOptions | null ,
1290+ ) : Promise < { ref : SandboxRef ; owner ?: string } > {
1291+ let owner : string | undefined ;
1292+ try {
1293+ const resp = await this . grpc . getSandbox (
1294+ { ...namedTarget ( name , options ) } ,
1295+ {
1296+ ...requestCallOptions ( options ) ,
1297+ headers : { [ OWNER_REQUEST_HEADER ] : '1' } ,
1298+ onHeader : ( header ) => {
1299+ owner = header . get ( OWNER_REPLICA_HEADER ) || undefined ;
1300+ } ,
1301+ } ,
1302+ ) ;
1303+ return { ref : sandboxRef ( resp . sandbox ) , owner } ;
1304+ } catch ( e ) {
1305+ throw fromConnect ( e ) ;
1306+ }
1307+ }
1308+
12851309 list ( options ?: ListOptions | null ) : Pager < SandboxRef > {
12861310 return new Pager ( async ( pageToken ) => {
12871311 try {
@@ -1385,22 +1409,22 @@ export class SandboxClient {
13851409 ) : AsyncGenerator < ExecStreamEvent , void , void > {
13861410 try {
13871411 // Preserve the existing preflight so lookup failures surface before the stream starts.
1388- await this . get ( name , {
1412+ const { owner } = await this . getWithOwner ( name , {
13891413 workspace : options ?. workspace ,
13901414 ...( options ?. signal ? { signal : options . signal } : { } ) ,
13911415 } ) ;
1392- const stream = this . grpc . execSandbox (
1393- {
1394- ... sandboxTarget ( name , options ) ,
1395- command ,
1396- workdir : options ?. workdir ?? '' ,
1397- environment : options ?. environment ?? { } ,
1398- executionTimeout : durationFromSeconds ( options ?. timeoutSecs ?? 0 ) ,
1399- stdin : options ?. stdin ? new Uint8Array ( options . stdin ) : new Uint8Array ( ) ,
1400- tty : false ,
1401- noLoginShell : options ?. noLoginShell ?? false ,
1402- } ,
1403- { signal : options ?. signal } ,
1416+ const request = {
1417+ ... sandboxTarget ( name , options ) ,
1418+ command ,
1419+ workdir : options ?. workdir ?? '' ,
1420+ environment : options ?. environment ?? { } ,
1421+ executionTimeout : durationFromSeconds ( options ?. timeoutSecs ?? 0 ) ,
1422+ stdin : options ?. stdin ? new Uint8Array ( options . stdin ) : new Uint8Array ( ) ,
1423+ tty : false ,
1424+ noLoginShell : options ?. noLoginShell ?? false ,
1425+ } ;
1426+ const stream = routedStream ( owner , ( { headers , onHeader } ) =>
1427+ this . grpc . execSandbox ( request , { signal : options ?. signal , headers , onHeader } ) ,
14041428 ) ;
14051429
14061430 let sawExit = false ;
@@ -1541,14 +1565,18 @@ export class SandboxClient {
15411565 command : string [ ] ,
15421566 options ?: ExecInteractiveOptions | null ,
15431567 ) : Promise < ExecInteractiveSessionControl > {
1568+ let owner : string | undefined ;
15441569 try {
1545- await this . get ( name , { workspace : options ?. workspace , ...( options ?. signal ? { signal : options . signal } : { } ) } ) ;
1570+ ( { owner } = await this . getWithOwner ( name , {
1571+ workspace : options ?. workspace ,
1572+ ...( options ?. signal ? { signal : options . signal } : { } ) ,
1573+ } ) ) ;
15461574 } catch ( e ) {
15471575 throw e instanceof SdkError ? e : fromConnect ( e ) ;
15481576 }
15491577
15501578 const input = new Pushable < MessageInitShape < typeof ExecSandboxInputSchema > > ( ) ;
1551- input . push ( {
1579+ const start : MessageInitShape < typeof ExecSandboxInputSchema > = {
15521580 payload : {
15531581 case : 'start' ,
15541582 value : {
@@ -1564,7 +1592,7 @@ export class SandboxClient {
15641592 noLoginShell : options ?. noLoginShell ?? false ,
15651593 } ,
15661594 } ,
1567- } ) ;
1595+ } ;
15681596
15691597 const controller = new AbortController ( ) ;
15701598 const signal = options ?. signal ? AbortSignal . any ( [ options . signal , controller . signal ] ) : controller . signal ;
@@ -1618,7 +1646,9 @@ export class SandboxClient {
16181646 if ( signal . aborted ) throw new SdkError ( 'canceled' , 'exec cancelled' ) ;
16191647 // Start and observe the transport immediately, independently of output
16201648 // consumption. Backpressure bounds the queue to 16 chunks of 64 KiB.
1621- const stream = grpc . execSandboxInteractive ( input , { signal } ) ;
1649+ const stream = routedStream ( owner , ( { headers, onHeader, ready } ) =>
1650+ grpc . execSandboxInteractive ( heldUntilReady ( start , input , ready ) , { signal, headers, onHeader } ) ,
1651+ ) ;
16221652 for await ( const event of stream ) {
16231653 if ( signal . aborted ) throw new SdkError ( 'canceled' , 'exec cancelled' ) ;
16241654 if ( exitCode !== undefined ) {
@@ -1713,12 +1743,18 @@ export class SandboxClient {
17131743 const localPort = opts . localPort ?? 0 ;
17141744
17151745 let sandboxId : string ;
1746+ let owner : string | undefined ;
17161747 try {
1717- const ref = await this . get ( name , { workspace : opts . workspace , ...( opts . signal ? { signal : opts . signal } : { } ) } ) ;
1748+ const found = await this . getWithOwner ( name , {
1749+ workspace : opts . workspace ,
1750+ ...( opts . signal ? { signal : opts . signal } : { } ) ,
1751+ } ) ;
1752+ const ref = found . ref ;
17181753 if ( ref . phase !== 'ready' ) {
17191754 throw new SdkError ( 'connect' , `sandbox '${ name } ' is not ready (phase: ${ ref . phase } )` ) ;
17201755 }
17211756 sandboxId = ref . id ;
1757+ owner = found . owner ;
17221758 } catch ( e ) {
17231759 throw e instanceof SdkError ? e : fromConnect ( e ) ;
17241760 }
@@ -1744,6 +1780,7 @@ export class SandboxClient {
17441780 opts . workspace ,
17451781 targetHost ,
17461782 targetPort ,
1783+ owner ,
17471784 controller . signal ,
17481785 )
17491786 . catch ( ( error : unknown ) => {
@@ -1828,6 +1865,7 @@ export class SandboxClient {
18281865 workspace : string | undefined ,
18291866 targetHost : string ,
18301867 targetPort : number ,
1868+ owner : string | undefined ,
18311869 signal : AbortSignal ,
18321870 ) : Promise < void > {
18331871 let token : string | undefined ;
@@ -1840,7 +1878,7 @@ export class SandboxClient {
18401878 // tears down this one socket via the catch below.
18411879 validateSshResponse ( session , sandboxId ) ;
18421880 token = session . token ;
1843- input . push ( {
1881+ const init : MessageInitShape < typeof TcpForwardFrameSchema > = {
18441882 payload : {
18451883 case : 'init' ,
18461884 value : {
@@ -1853,7 +1891,7 @@ export class SandboxClient {
18531891 authorizationToken : token ,
18541892 } ,
18551893 } ,
1856- } ) ;
1894+ } ;
18571895
18581896 socket . on ( 'data' , ( chunk : Buffer ) => {
18591897 for ( let off = 0 ; off < chunk . length ; off += FORWARD_CHUNK ) {
@@ -1875,7 +1913,10 @@ export class SandboxClient {
18751913 signal . addEventListener ( 'abort' , onAbort , { once : true } ) ;
18761914
18771915 try {
1878- for await ( const frame of this . grpc . forwardTcp ( input , { signal } ) ) {
1916+ const frames = routedStream ( owner , ( { headers, onHeader, ready } ) =>
1917+ this . grpc . forwardTcp ( heldUntilReady ( init , input , ready ) , { signal, headers, onHeader } ) ,
1918+ ) ;
1919+ for await ( const frame of frames ) {
18791920 if ( frame . payload . case !== 'data' ) continue ;
18801921 const data = frame . payload . value ;
18811922 if ( data . length === 0 ) continue ;
0 commit comments