File size: 11,583 Bytes
afa0cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Checks that remote timing measures each observation, not client delays or cell lifetime.

use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;

use anyhow::Context;
use anyhow::Result;
use codex_code_mode::CodeModeSessionProvider;
use codex_code_mode::ExecuteRequest;
use codex_code_mode::GrpcCodeModeSessionProvider;
use codex_code_mode::NoopCodeModeSessionDelegate;
use codex_code_mode::ProcessOwnedCodeModeSessionProvider;
use codex_code_mode::RuntimeResponse;
use codex_code_mode::WaitOutcome;
use codex_code_mode::WaitRequest;
use codex_code_mode_protocol::grpc;
use codex_code_mode_protocol::grpc::code_mode_host_client::CodeModeHostClient;
use pretty_assertions::assert_eq;
use tokio::time::sleep;
use tokio::time::timeout;

#[path = "support/host.rs"]
mod host;
#[expect(dead_code, reason = "Other suites use the shared cell_id helper.")]
#[path = "support/recording_delegate.rs"]
mod recording_delegate;

use host::HostHarness;
use recording_delegate::RecordingDelegate;

const TEST_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 20);

/// Successful and failed JavaScript include computation and timers in their host timing.
/// Waiting to read a finished execution must not increase that measurement.
#[tokio::test]
async fn execution_timing_includes_javascript_but_excludes_delayed_reads() -> Result<()> {
    let host = HostHarness::start("grpc://127.0.0.1:0").await?;
    let mut client = CodeModeHostClient::connect(host.endpoint).await?;
    let mut events = client
        .open_session(grpc::OpenSessionRequest {
            cell_execution_limits: None,
        })
        .await?
        .into_inner();
    let opened = timeout(TEST_TIMEOUT, events.message())
        .await??
        .context("session ended")?;
    let Some(grpc::session_event::Event::Opened(opened)) = opened.event else {
        anyhow::bail!("expected session opening event");
    };

    let endings = ["", "throw new Error('timed failure');"];
    for (index, suffix) in endings.into_iter().enumerate() {
        let observed = Instant::now();
        let mut execution = client
            .execute(grpc::ExecuteRequest {
                session_id: opened.session_id.clone(),
                execution_id: format!("execution-{index}"),
                tool_call_id: format!("call-{index}"),
                source: format!(
                    "const until = Date.now() + 150; while (Date.now() < until) {{}} \
                     await new Promise(resolve => setTimeout(resolve, 150)); {suffix}"
                ),
                enabled_tools: Vec::new(),
                yield_time_ms: Some(/*value*/ 5_000),
                max_output_tokens: Some(/*value*/ 1_000),
            })
            .await?
            .into_inner();
        let started = timeout(TEST_TIMEOUT, execution.message())
            .await??
            .context("execution ended")?;
        let Some(grpc::execute_event::Event::Started(started)) = started.event else {
            anyhow::bail!("expected execution admission");
        };
        let closed = timeout(TEST_TIMEOUT, events.message())
            .await??
            .context("session ended")?;
        assert!(
            matches!(closed.event, Some(grpc::session_event::Event::CellClosed(closed))
            if closed.cell_id == started.cell_id)
        );

        // CellClosed confirms the JS finished before introducing a client-only delay.
        sleep(Duration::from_millis(/*millis*/ 600)).await;
        let result = timeout(TEST_TIMEOUT, execution.message())
            .await??
            .context("execution ended")?;
        let Some(grpc::execute_event::Event::Outcome(outcome)) = result.event else {
            anyhow::bail!("expected execution outcome");
        };
        let duration = Duration::from_nanos(outcome.code_mode_host_duration_ns);
        assert!(duration >= Duration::from_millis(/*millis*/ 250));
        assert!(duration + Duration::from_millis(/*millis*/ 300) <= observed.elapsed());
        let Some(grpc::execution_outcome::Outcome::Completed(completed)) = outcome.outcome else {
            anyhow::bail!("expected completed execution");
        };
        if suffix.is_empty() {
            assert_eq!(completed.error_text, None);
        } else {
            assert!(
                completed
                    .error_text
                    .context("expected JS failure")?
                    .contains("timed failure")
            );
        }
    }
    Ok(())
}

/// The process-owned host freezes successful and failed execution timing before
/// the client reads its initial response, including JavaScript computation and timers.
#[tokio::test]
async fn stdio_execution_timing_includes_javascript_but_excludes_delayed_reads() -> Result<()> {
    let provider = ProcessOwnedCodeModeSessionProvider::with_host_program(
        codex_utils_cargo_bin::cargo_bin("codex-code-mode-host")?,
    );
    let delegate = Arc::new(RecordingDelegate::default());
    let session = provider
        .create_session()
        .await
        .map_err(anyhow::Error::msg)?;

    let endings = ["", "throw new Error('timed failure');"];
    for (index, suffix) in endings.into_iter().enumerate() {
        let observed = Instant::now();
        let started = session
            .execute(
                ExecuteRequest {
                    tool_call_id: format!("call-{index}"),
                    source: format!(
                        "const until = Date.now() + 150; while (Date.now() < until) {{}} \
                     await new Promise(resolve => setTimeout(resolve, 150)); \
                     notify('finished'); {suffix}"
                    ),
                    enabled_tools: Vec::new(),
                    yield_time_ms: Some(/*value*/ 5_000),
                    max_output_tokens: Some(/*value*/ 1_000),
                },
                delegate.clone(),
            )
            .await
            .map_err(anyhow::Error::msg)?;
        timeout(TEST_TIMEOUT, delegate.notification_delivered.notified())
            .await
            .context("JS did not report completion of its timed work")?;

        // Delay only after the host has finished the computation and timer.
        sleep(Duration::from_millis(/*millis*/ 600)).await;
        let response = timeout(TEST_TIMEOUT, started.initial_response())
            .await?
            .map_err(anyhow::Error::msg)?;
        let duration = response
            .code_mode_host_duration()
            .context("missing stdio exec timing")?;
        assert!(duration >= Duration::from_millis(/*millis*/ 250));
        assert!(duration + Duration::from_millis(/*millis*/ 300) <= observed.elapsed());
        let RuntimeResponse::Result { error_text, .. } = response else {
            anyhow::bail!("expected completed execution");
        };
        if suffix.is_empty() {
            assert_eq!(error_text, None);
        } else {
            assert!(
                error_text
                    .context("expected JS failure")?
                    .contains("timed failure")
            );
        }
    }
    session.shutdown().await.map_err(anyhow::Error::msg)?;
    Ok(())
}

/// Every wait and termination measures its own request, including missing-cell outcomes.
/// Earlier execution and background time must not be charged again by later observations.
#[tokio::test]
async fn observation_timing_excludes_previous_requests_and_background_time() -> Result<()> {
    let host = HostHarness::start("grpc://127.0.0.1:0").await?;
    let providers: [Arc<dyn CodeModeSessionProvider>; 2] = [
        Arc::new(ProcessOwnedCodeModeSessionProvider::with_host_program(
            codex_utils_cargo_bin::cargo_bin("codex-code-mode-host")?,
        )),
        Arc::new(GrpcCodeModeSessionProvider::new(host.endpoint)),
    ];
    for provider in providers {
        let session = provider
            .create_session()
            .await
            .map_err(anyhow::Error::msg)?;
        let started = session
            .execute(
                ExecuteRequest {
                    tool_call_id: "call-1".to_string(),
                    source: "await new Promise(() => {});".to_string(),
                    enabled_tools: Vec::new(),
                    yield_time_ms: Some(/*value*/ 200),
                    max_output_tokens: Some(/*value*/ 1_000),
                },
                Arc::new(NoopCodeModeSessionDelegate),
            )
            .await
            .map_err(anyhow::Error::msg)?;
        let cell_id = started.cell_id.clone();
        let initial = started
            .initial_response()
            .await
            .map_err(anyhow::Error::msg)?;
        let duration = initial
            .code_mode_host_duration()
            .context("missing exec timing")?;
        assert!(duration >= Duration::from_millis(/*millis*/ 150));
        let yielded = RuntimeResponse::Yielded {
            cell_id: cell_id.clone(),
            content_items: Vec::new(),
            code_mode_host_duration: Some(duration),
        };
        assert_eq!(initial, yielded);

        for _ in 0..2 {
            sleep(Duration::from_millis(/*millis*/ 300)).await;
            let observed = Instant::now();
            let outcome = session
                .wait(WaitRequest {
                    cell_id: cell_id.clone(),
                    yield_time_ms: 50,
                })
                .await
                .map_err(anyhow::Error::msg)?;
            let duration = outcome
                .code_mode_host_duration()
                .context("missing wait timing")?;
            assert!(duration >= Duration::from_millis(/*millis*/ 40));
            assert!(
                duration <= observed.elapsed(),
                "wait included time before its request: {duration:?}"
            );
            assert_eq!(
                outcome,
                WaitOutcome::LiveCell(RuntimeResponse::Yielded {
                    cell_id: cell_id.clone(),
                    content_items: Vec::new(),
                    code_mode_host_duration: Some(duration),
                })
            );
        }

        let observed = Instant::now();
        let terminated = session
            .terminate(cell_id.clone())
            .await
            .map_err(anyhow::Error::msg)?;
        let duration = terminated
            .code_mode_host_duration()
            .context("missing termination timing")?;
        assert!(duration <= observed.elapsed());
        assert_eq!(
            terminated,
            WaitOutcome::LiveCell(RuntimeResponse::Terminated {
                cell_id: cell_id.clone(),
                content_items: Vec::new(),
                code_mode_host_duration: Some(duration),
            })
        );

        let observed = Instant::now();
        let missing = session
            .wait(WaitRequest {
                cell_id: cell_id.clone(),
                yield_time_ms: 50,
            })
            .await
            .map_err(anyhow::Error::msg)?;
        let duration = missing
            .code_mode_host_duration()
            .context("missing absent-cell timing")?;
        assert!(duration <= observed.elapsed());
        assert_eq!(
            missing,
            WaitOutcome::MissingCell(RuntimeResponse::Result {
                error_text: Some(format!("exec cell {cell_id} not found")),
                cell_id,
                content_items: Vec::new(),
                code_mode_host_duration: Some(duration),
            })
        );
        session.shutdown().await.map_err(anyhow::Error::msg)?;
    }
    Ok(())
}