File size: 10,045 Bytes
ea39c0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use std::time::Instant;

use codex_api::AuthError;
use codex_api::AuthProvider;
use codex_http_client::Request;
use codex_websocket_client::WebSocketConnection;
use codex_websocket_client::WebSocketConnector;
use http::Method;
use http::StatusCode;
use serde::Deserialize;
use serde::Serialize;
use tokio::time::sleep;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tracing::info;
use tracing::warn;

use super::EnvironmentRegistryClient;
use super::RemoteEnvironmentConfig;
use crate::ExecServerError;
use crate::client::is_retryable_recovery_error;
use crate::client::registry_recovery_retry_delay;
use crate::client_api::DEFAULT_REMOTE_EXEC_SERVER_CONNECT_TIMEOUT;
use crate::client_transport::authenticate_websocket_request;
use crate::client_transport::connect_websocket_request;
use crate::connection::JsonRpcConnection;
use crate::server::ConnectionProcessor;
use crate::telemetry::ConnectionTransport;
use crate::trace_context::current_trace_context_headers;

const DIRECT_TRANSPORT: &str = "direct_jsonrpc_v1";

#[derive(Debug, Serialize)]
struct DirectRegistrationRequest {
    transport: &'static str,
}

#[derive(Debug, Deserialize)]
struct DirectRegistrationResponse {
    environment_id: String,
    transport: String,
    registration_id: String,
    url: String,
}

impl EnvironmentRegistryClient {
    #[tracing::instrument(
        name = "codex.exec_server.remote.register",
        skip_all,
        fields(
            otel.kind = "client",
            otel.name = "codex.exec_server.remote.register",
            result = tracing::field::Empty,
        )
    )]
    async fn register_direct_environment(
        &self,
        environment_id: &str,
    ) -> Result<DirectRegistrationResponse, ExecServerError> {
        let started_at = Instant::now();
        let response = async {
            let url = super::endpoint_url(
                &self.base_url,
                &format!("/cloud/environment/{environment_id}/direct/register"),
            );
            let request = Request::new(Method::POST, url)
                .with_json(&DirectRegistrationRequest {
                    transport: DIRECT_TRANSPORT,
                })
                .into_prepared()
                .map_err(ExecServerError::EnvironmentRegistryConfig)?;
            let request = self
                .auth_provider
                .apply_auth(request)
                .await
                .map_err(direct_auth_error)?;
            let prepared = request
                .prepare_body_for_send()
                .map_err(ExecServerError::EnvironmentRegistryConfig)?;
            let response = self
                .http
                .request(request.method, request.url)
                .headers(prepared.headers)
                .headers(current_trace_context_headers())
                .body(prepared.body.unwrap_or_default())
                .timeout(self.connect_timeout)
                .send()
                .await?;
            let response: DirectRegistrationResponse = self.parse_json_response(response).await?;
            if response.environment_id != environment_id {
                return Err(ExecServerError::Protocol(
                    "environment registry returned a different environment id".to_string(),
                ));
            }
            if response.transport != DIRECT_TRANSPORT {
                return Err(ExecServerError::Protocol(format!(
                    "environment registry returned unsupported direct transport `{}`",
                    response.transport
                )));
            }
            if response.registration_id.trim().is_empty() || response.url.trim().is_empty() {
                return Err(ExecServerError::Protocol(
                    "environment registry returned incomplete direct connection data".to_string(),
                ));
            }
            require_tls_or_loopback(&response.url, "wss")?;
            Ok(response)
        }
        .await;
        let result = if response.is_ok() { "success" } else { "error" };
        tracing::Span::current().record("result", result);
        self.telemetry
            .remote_registration_completed(result, started_at.elapsed());
        response
    }
}

pub(super) async fn run_direct_environment(
    config: RemoteEnvironmentConfig,
    client: EnvironmentRegistryClient,
    processor: ConnectionProcessor,
) -> Result<(), ExecServerError> {
    require_tls_or_loopback(&config.base_url, "https")?;
    let mut retry_attempt = 0;
    let mut registration = client
        .register_direct_environment(&config.environment_id)
        .await?;

    loop {
        match connect_direct(
            &registration.url,
            config.auth_provider.as_ref(),
            &config.http_client_factory,
        )
        .await
        {
            Ok(websocket) => {
                retry_attempt = 0;
                info!(
                    environment_id = registration.environment_id,
                    registration_id = registration.registration_id,
                    "direct exec-server connected"
                );
                processor
                    .run_connection(
                        JsonRpcConnection::from_websocket(
                            websocket,
                            format!(
                                "direct exec-server websocket {}",
                                websocket_diagnostic_url(&registration.url)
                            ),
                        ),
                        ConnectionTransport::WebSocket,
                    )
                    .await;
                config.telemetry.remote_reconnect("disconnected");
            }
            Err(error)
                if is_retryable_recovery_error(&error)
                    && !matches!(
                        &error,
                        ExecServerError::WebSocketConnect {
                            source: tokio_tungstenite::tungstenite::Error::Http(response),
                            ..
                        } if response.status().is_client_error()
                            && !matches!(
                                response.status(),
                                StatusCode::REQUEST_TIMEOUT
                                    | StatusCode::CONFLICT
                                    | StatusCode::TOO_MANY_REQUESTS
                            )
                    ) =>
            {
                // A handshake conflict rejects this registration; other transient failures
                // reconnect using the existing URL.
                if matches!(
                    &error,
                    ExecServerError::WebSocketConnect {
                        source: tokio_tungstenite::tungstenite::Error::Http(response),
                        ..
                    } if response.status() == StatusCode::CONFLICT
                ) {
                    registration = client
                        .register_direct_environment(&config.environment_id)
                        .await?;
                }
                warn!("direct exec-server connection failed; retrying");
                config.telemetry.remote_reconnect("connect_failed");
            }
            Err(error) => return Err(error),
        }

        sleep(registry_recovery_retry_delay(
            &config.environment_id,
            retry_attempt,
        ))
        .await;
        retry_attempt = retry_attempt.saturating_add(1);
    }
}

async fn connect_direct(
    url: &str,
    auth_provider: &dyn AuthProvider,
    http_client_factory: &codex_http_client::HttpClientFactory,
) -> Result<WebSocketConnection, ExecServerError> {
    let mut request =
        url.into_client_request()
            .map_err(|source| ExecServerError::WebSocketConnect {
                url: websocket_diagnostic_url(url),
                source,
            })?;
    request
        .headers_mut()
        .extend(current_trace_context_headers());
    authenticate_websocket_request(&mut request, auth_provider)
        .await
        .map_err(direct_auth_error)?;
    let authenticated_url = request.uri().to_string();
    require_tls_or_loopback(&authenticated_url, "wss")?;
    let connector = WebSocketConnector::new(http_client_factory)
        .map_err(|error| ExecServerError::WebSocketConfiguration(error.to_string()))?
        .with_tcp_nodelay();
    connect_websocket_request(
        request,
        websocket_diagnostic_url(&authenticated_url),
        connector,
        DEFAULT_REMOTE_EXEC_SERVER_CONNECT_TIMEOUT,
        /*use_loopback_direct*/ false,
    )
    .await
}

pub(super) fn require_tls_or_loopback(
    url: &str,
    secure_scheme: &str,
) -> Result<(), ExecServerError> {
    let parsed = url::Url::parse(url).map_err(|error| {
        ExecServerError::EnvironmentRegistryConfig(format!("invalid remote endpoint URL: {error}"))
    })?;
    if parsed.scheme() == secure_scheme {
        return Ok(());
    }

    let loopback = match parsed.host() {
        Some(url::Host::Domain(host)) => host.eq_ignore_ascii_case("localhost"),
        Some(url::Host::Ipv4(host)) => host.is_loopback(),
        Some(url::Host::Ipv6(host)) => host.is_loopback(),
        None => false,
    };
    if loopback
        && secure_scheme
            .strip_suffix('s')
            .is_some_and(|scheme| parsed.scheme() == scheme)
    {
        return Ok(());
    }

    Err(ExecServerError::EnvironmentRegistryConfig(format!(
        "remote transport requires {secure_scheme} for non-loopback endpoints"
    )))
}

fn websocket_diagnostic_url(url: &str) -> String {
    url.split(['?', '#']).next().unwrap_or(url).to_string()
}

fn direct_auth_error(error: AuthError) -> ExecServerError {
    let message = format!("failed to resolve environment registry authentication: {error}");
    match error {
        AuthError::Build(_) => ExecServerError::EnvironmentRegistryAuth(message),
        AuthError::Transient(_) => ExecServerError::Disconnected(message),
    }
}

#[cfg(test)]
#[path = "direct_tests.rs"]
mod tests;