microclaw-engine 0.2.0

Channel-independent Agent Engine and default services for MicroClaw
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! Authenticated WebSocket transport and host for MicroClaw Workers.

use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};

use crate::internal::runtime::{RunController, Worker, WorkerConnection, WorkerTransport};
use async_trait::async_trait;
use futures_util::{SinkExt, StreamExt};
use http::header::AUTHORIZATION;
use microclaw_core::run_protocol::{
    RuntimeError, RuntimeErrorCode, WorkerCommand, WorkerFrame, WORKER_PROTOCOL_VERSION,
};
use tokio::net::{TcpListener, TcpStream};
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::handshake::server::{Request, Response};
use tokio_tungstenite::tungstenite::http::StatusCode;
use tokio_tungstenite::tungstenite::{Error as WebSocketError, Message};
use tokio_tungstenite::{accept_hdr_async, connect_async, MaybeTlsStream, WebSocketStream};

/// Creates authenticated WebSocket connections to a MicroClaw Worker host.
#[derive(Debug, Clone)]
pub struct WebSocketWorkerTransport {
    url: String,
    bearer_token: Option<String>,
}

impl WebSocketWorkerTransport {
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            bearer_token: None,
        }
    }

    pub fn bearer_token(mut self, token: impl Into<String>) -> Self {
        self.bearer_token = Some(token.into());
        self
    }
}

struct WebSocketConnection {
    socket: WebSocketStream<MaybeTlsStream<TcpStream>>,
}

#[async_trait]
impl WorkerTransport for WebSocketWorkerTransport {
    async fn connect(&self) -> Result<Box<dyn WorkerConnection>, RuntimeError> {
        let mut request = self
            .url
            .as_str()
            .into_client_request()
            .map_err(configuration_error)?;
        if let Some(token) = &self.bearer_token {
            let value = format!("Bearer {token}")
                .parse()
                .map_err(|_| configuration_error("invalid Worker bearer token"))?;
            request.headers_mut().insert(AUTHORIZATION, value);
        }
        let (socket, _) = connect_async(request).await.map_err(connection_error)?;
        Ok(Box::new(WebSocketConnection { socket }))
    }
}

#[async_trait]
impl WorkerConnection for WebSocketConnection {
    async fn send(&mut self, command: WorkerCommand) -> Result<(), RuntimeError> {
        let payload = serde_json::to_string(&command).map_err(protocol_error)?;
        self.socket
            .send(Message::Text(payload))
            .await
            .map_err(connection_error)
    }

    async fn receive(&mut self) -> Result<Option<WorkerFrame>, RuntimeError> {
        while let Some(message) = self.socket.next().await {
            match message.map_err(connection_error)? {
                Message::Text(payload) => {
                    return serde_json::from_str(&payload)
                        .map(Some)
                        .map_err(protocol_error);
                }
                Message::Binary(payload) => {
                    return serde_json::from_slice(&payload)
                        .map(Some)
                        .map_err(protocol_error);
                }
                Message::Ping(payload) => self
                    .socket
                    .send(Message::Pong(payload))
                    .await
                    .map_err(connection_error)?,
                Message::Close(_) => return Ok(None),
                Message::Pong(_) | Message::Frame(_) => {}
            }
        }
        Ok(None)
    }
}

/// Authenticated WebSocket host for a Local or custom Worker implementation.
pub struct WorkerHost {
    worker: Arc<dyn Worker>,
    bearer_token: Option<String>,
    runs: Arc<tokio::sync::Mutex<HostedRunRegistry>>,
    max_retained_runs: usize,
    max_events_per_run: usize,
}

#[derive(Default)]
struct HostedRunRegistry {
    runs: HashMap<String, Arc<HostedRun>>,
    completed: VecDeque<String>,
}

struct HostedRun {
    request_fingerprint: String,
    controller: RunController,
    state: Mutex<HostedRunState>,
    changed: tokio::sync::Notify,
}

#[derive(Default)]
struct HostedRunState {
    events: VecDeque<microclaw_core::runtime_event::RuntimeEventEnvelope>,
    discarded_through: Option<u64>,
    result: Option<microclaw_core::run_protocol::RunResult>,
}

impl WorkerHost {
    pub fn new(worker: impl Worker + 'static) -> Self {
        Self {
            worker: Arc::new(worker),
            bearer_token: None,
            runs: Arc::new(tokio::sync::Mutex::new(HostedRunRegistry::default())),
            max_retained_runs: 256,
            max_events_per_run: 4096,
        }
    }

    pub fn shared(worker: Arc<dyn Worker>) -> Self {
        Self {
            worker,
            bearer_token: None,
            runs: Arc::new(tokio::sync::Mutex::new(HostedRunRegistry::default())),
            max_retained_runs: 256,
            max_events_per_run: 4096,
        }
    }

    pub fn bearer_token(mut self, token: impl Into<String>) -> Self {
        self.bearer_token = Some(token.into());
        self
    }

    pub fn replay_limits(mut self, retained_runs: usize, events_per_run: usize) -> Self {
        self.max_retained_runs = retained_runs.max(1);
        self.max_events_per_run = events_per_run.max(1);
        self
    }

    /// Serve until the listener is closed or the task is cancelled.
    pub async fn serve(self, listener: TcpListener) -> Result<(), RuntimeError> {
        let worker = self.worker;
        let runs = self.runs;
        let max_retained_runs = self.max_retained_runs;
        let max_events_per_run = self.max_events_per_run;
        let bearer_token = self.bearer_token.map(|token| format!("Bearer {token}"));
        loop {
            let (stream, _) = listener.accept().await.map_err(connection_error)?;
            let worker = worker.clone();
            let runs = runs.clone();
            let bearer_token = bearer_token.clone();
            tokio::spawn(async move {
                let _ = serve_connection(
                    stream,
                    worker,
                    runs,
                    bearer_token,
                    max_retained_runs,
                    max_events_per_run,
                )
                .await;
            });
        }
    }
}

#[allow(clippy::result_large_err)]
async fn serve_connection(
    stream: TcpStream,
    worker: Arc<dyn Worker>,
    runs: Arc<tokio::sync::Mutex<HostedRunRegistry>>,
    bearer_token: Option<String>,
    max_retained_runs: usize,
    max_events_per_run: usize,
) -> Result<(), RuntimeError> {
    let socket = accept_hdr_async(stream, move |request: &Request, mut response: Response| {
        let authorized = bearer_token.as_ref().is_none_or(|expected| {
            request
                .headers()
                .get(AUTHORIZATION)
                .and_then(|value| value.to_str().ok())
                .is_some_and(|actual| constant_time_eq(actual.as_bytes(), expected.as_bytes()))
        });
        if authorized {
            Ok(response)
        } else {
            *response.status_mut() = StatusCode::UNAUTHORIZED;
            Err(response.map(|_| Some("unauthorized".into())))
        }
    })
    .await
    .map_err(connection_error)?;
    let (mut sink, mut stream) = socket.split();

    while let Some(message) = stream.next().await {
        let command = decode_command(message.map_err(connection_error)?)?;
        command.validate_protocol()?;
        match command {
            WorkerCommand::Describe { .. } => {
                send_frame(
                    &mut sink,
                    WorkerFrame::Descriptor {
                        protocol_version: WORKER_PROTOCOL_VERSION,
                        descriptor: worker.descriptor(),
                    },
                )
                .await?;
            }
            WorkerCommand::Health { .. } => {
                send_frame(
                    &mut sink,
                    WorkerFrame::Health {
                        protocol_version: WORKER_PROTOCOL_VERSION,
                        health: worker.health(),
                    },
                )
                .await?;
            }
            WorkerCommand::Submit {
                profile, request, ..
            } => {
                let run_id = request
                    .run_id
                    .clone()
                    .ok_or_else(|| protocol_error("remote submission requires run_id"))?;
                let request_fingerprint =
                    serde_json::to_string(&(profile.clone(), &request)).map_err(protocol_error)?;
                let mut registry = runs.lock().await;
                let hosted = if let Some(existing) = registry.runs.get(run_id.as_str()).cloned() {
                    if existing.request_fingerprint != request_fingerprint {
                        send_frame(
                            &mut sink,
                            WorkerFrame::Error {
                                protocol_version: WORKER_PROTOCOL_VERSION,
                                run_id: Some(run_id),
                                error: protocol_error(
                                    "run_id was already submitted with a different request",
                                ),
                            },
                        )
                        .await?;
                        continue;
                    }
                    existing
                } else {
                    let run = worker.submit(profile, *request).await?;
                    let hosted = Arc::new(HostedRun {
                        request_fingerprint,
                        controller: run.controller(),
                        state: Mutex::new(HostedRunState::default()),
                        changed: tokio::sync::Notify::new(),
                    });
                    registry.runs.insert(run_id.to_string(), hosted.clone());
                    tokio::spawn(pump_run(
                        run,
                        hosted.clone(),
                        runs.clone(),
                        max_retained_runs,
                        max_events_per_run,
                    ));
                    hosted
                };
                drop(registry);
                send_frame(
                    &mut sink,
                    WorkerFrame::Accepted {
                        protocol_version: WORKER_PROTOCOL_VERSION,
                        run_id: run_id.clone(),
                    },
                )
                .await?;
                stream_hosted_run(&mut sink, &mut stream, hosted, run_id, None).await?;
                return Ok(());
            }
            WorkerCommand::Control { .. } => {
                return Err(protocol_error("control command has no active run"));
            }
            WorkerCommand::ResumeEvents {
                run_id,
                after_sequence,
                ..
            } => {
                let hosted = runs.lock().await.runs.get(run_id.as_str()).cloned();
                let Some(hosted) = hosted else {
                    send_frame(
                        &mut sink,
                        WorkerFrame::Error {
                            protocol_version: WORKER_PROTOCOL_VERSION,
                            run_id: Some(run_id),
                            error: RuntimeError {
                                code: RuntimeErrorCode::Unavailable,
                                message: "run is not known by this Worker host".into(),
                                retryable: true,
                            },
                        },
                    )
                    .await?;
                    continue;
                };
                stream_hosted_run(&mut sink, &mut stream, hosted, run_id, after_sequence).await?;
                return Ok(());
            }
        }
    }
    Ok(())
}

async fn pump_run(
    mut run: crate::internal::runtime::RunHandle,
    hosted: Arc<HostedRun>,
    registry: Arc<tokio::sync::Mutex<HostedRunRegistry>>,
    max_retained_runs: usize,
    max_events_per_run: usize,
) {
    while let Some(envelope) = run.next_event().await {
        let mut state = hosted
            .state
            .lock()
            .unwrap_or_else(|error| error.into_inner());
        state.events.push_back(envelope);
        while state.events.len() > max_events_per_run {
            state.discarded_through = state.events.pop_front().map(|event| event.sequence);
        }
        drop(state);
        hosted.changed.notify_waiters();
    }
    let result =
        run.result()
            .await
            .unwrap_or_else(|error| microclaw_core::run_protocol::RunResult {
                run_id: hosted.controller.id().clone(),
                session_id: microclaw_core::run_protocol::SessionId::new(""),
                status: microclaw_core::run_protocol::RunStatus::Failed,
                final_text: String::new(),
                error: Some(error),
                metadata: Default::default(),
            });
    hosted
        .state
        .lock()
        .unwrap_or_else(|error| error.into_inner())
        .result = Some(result);
    hosted.changed.notify_waiters();
    let run_id = hosted.controller.id().to_string();
    let mut registry = registry.lock().await;
    registry.completed.push_back(run_id);
    while registry.completed.len() > max_retained_runs {
        if let Some(expired) = registry.completed.pop_front() {
            registry.runs.remove(&expired);
        }
    }
}

async fn stream_hosted_run<S, R>(
    sink: &mut S,
    stream: &mut R,
    hosted: Arc<HostedRun>,
    run_id: microclaw_core::run_protocol::RunId,
    after_sequence: Option<u64>,
) -> Result<(), RuntimeError>
where
    S: futures_util::Sink<Message, Error = WebSocketError> + Unpin,
    R: futures_util::Stream<Item = Result<Message, WebSocketError>> + Unpin,
{
    let mut cursor = after_sequence;
    loop {
        let (events, result, history_lost) = {
            let state = hosted
                .state
                .lock()
                .unwrap_or_else(|error| error.into_inner());
            (
                state
                    .events
                    .iter()
                    .filter(|event| cursor.is_none_or(|value| event.sequence > value))
                    .cloned()
                    .collect::<Vec<_>>(),
                state.result.clone(),
                state
                    .discarded_through
                    .is_some_and(|discarded| cursor.is_none_or(|sequence| sequence < discarded)),
            )
        };
        if history_lost {
            send_frame(
                sink,
                WorkerFrame::Error {
                    protocol_version: WORKER_PROTOCOL_VERSION,
                    run_id: Some(run_id),
                    error: RuntimeError {
                        code: RuntimeErrorCode::Unavailable,
                        message: "requested Worker event history has expired".into(),
                        retryable: true,
                    },
                },
            )
            .await?;
            return Ok(());
        }
        for envelope in events {
            cursor = Some(envelope.sequence);
            send_frame(
                sink,
                WorkerFrame::Event {
                    protocol_version: WORKER_PROTOCOL_VERSION,
                    envelope,
                },
            )
            .await?;
        }
        if let Some(result) = result {
            send_frame(
                sink,
                WorkerFrame::Result {
                    protocol_version: WORKER_PROTOCOL_VERSION,
                    result,
                },
            )
            .await?;
            return Ok(());
        }
        tokio::select! {
            _ = hosted.changed.notified() => {}
            message = stream.next() => {
                let Some(message) = message else { return Err(connection_error("Worker client disconnected")); };
                let command = decode_command(message.map_err(connection_error)?)?;
                command.validate_protocol()?;
                let WorkerCommand::Control { control, .. } = command else {
                    send_frame(sink, WorkerFrame::Error { protocol_version: WORKER_PROTOCOL_VERSION, run_id: Some(run_id.clone()), error: protocol_error("only control commands are accepted during a run") }).await?;
                    continue;
                };
                let outcome = match control {
                    microclaw_core::run_protocol::RuntimeControl::Cancel { run_id: target } if target == run_id => hosted.controller.cancel_confirmed().await,
                    microclaw_core::run_protocol::RuntimeControl::Steer { run_id: target, message } if target == run_id => hosted.controller.steer_confirmed(message).await,
                    microclaw_core::run_protocol::RuntimeControl::ResolveApproval { run_id: target, approval_id, decision } if target == run_id => hosted.controller.resolve_approval_confirmed(approval_id, decision).await,
                    _ => Err(protocol_error("control command targets a different run")),
                };
                match outcome {
                    Ok(()) => send_frame(sink, WorkerFrame::ControlAcknowledged { protocol_version: WORKER_PROTOCOL_VERSION, run_id: run_id.clone() }).await?,
                    Err(error) => send_frame(sink, WorkerFrame::Error { protocol_version: WORKER_PROTOCOL_VERSION, run_id: Some(run_id.clone()), error }).await?,
                }
            }
        }
    }
}

fn decode_command(message: Message) -> Result<WorkerCommand, RuntimeError> {
    match message {
        Message::Text(payload) => serde_json::from_str(&payload).map_err(protocol_error),
        Message::Binary(payload) => serde_json::from_slice(&payload).map_err(protocol_error),
        _ => Err(protocol_error("expected a JSON Worker command")),
    }
}

async fn send_frame<S>(sink: &mut S, frame: WorkerFrame) -> Result<(), RuntimeError>
where
    S: futures_util::Sink<Message, Error = WebSocketError> + Unpin,
{
    let payload = serde_json::to_string(&frame).map_err(protocol_error)?;
    sink.send(Message::Text(payload))
        .await
        .map_err(connection_error)
}

fn constant_time_eq(left: &[u8], right: &[u8]) -> bool {
    if left.len() != right.len() {
        return false;
    }
    left.iter()
        .zip(right)
        .fold(0_u8, |diff, (left, right)| diff | (left ^ right))
        == 0
}

fn configuration_error(error: impl std::fmt::Display) -> RuntimeError {
    RuntimeError {
        code: RuntimeErrorCode::Configuration,
        message: error.to_string(),
        retryable: false,
    }
}

fn protocol_error(error: impl std::fmt::Display) -> RuntimeError {
    RuntimeError {
        code: RuntimeErrorCode::InvalidRequest,
        message: error.to_string(),
        retryable: false,
    }
}

fn connection_error(error: impl std::fmt::Display) -> RuntimeError {
    RuntimeError {
        code: RuntimeErrorCode::Unavailable,
        message: error.to_string(),
        retryable: true,
    }
}

#[cfg(test)]
mod tests {
    use crate::internal::runtime::{
        ExecutionContext, ExecutionResult, LocalWorker, RemoteWorker, RunExecutor, Runtime,
    };
    use microclaw_core::run_protocol::{
        AgentProfile, RunRequest, RuntimeCapabilities, SessionId, WorkerHealthStatus,
    };
    use microclaw_core::runtime_event::RuntimeEvent;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use super::*;

    struct Echo;

    #[async_trait]
    impl RunExecutor for Echo {
        async fn execute(
            &self,
            _profile: AgentProfile,
            request: RunRequest,
            context: ExecutionContext,
        ) -> Result<ExecutionResult, RuntimeError> {
            context.emit(RuntimeEvent::TextDelta {
                delta: "from worker".into(),
            })?;
            Ok(ExecutionResult::new(
                request
                    .session_id
                    .unwrap_or_else(|| SessionId::new("remote-session")),
                request.prompt,
            ))
        }

        fn capabilities(&self) -> RuntimeCapabilities {
            RuntimeCapabilities {
                streaming: true,
                cancellation: true,
                remote_workers: true,
                ..RuntimeCapabilities::default()
            }
        }
    }

    struct CountingEcho(Arc<AtomicUsize>);

    #[async_trait]
    impl RunExecutor for CountingEcho {
        async fn execute(
            &self,
            _profile: AgentProfile,
            request: RunRequest,
            context: ExecutionContext,
        ) -> Result<ExecutionResult, RuntimeError> {
            self.0.fetch_add(1, Ordering::SeqCst);
            tokio::time::sleep(std::time::Duration::from_millis(30)).await;
            context.emit(RuntimeEvent::TextDelta {
                delta: "persisted".into(),
            })?;
            Ok(ExecutionResult::new(
                SessionId::new("resumed-session"),
                request.prompt,
            ))
        }
    }

    async fn start_host(token: &str) -> (String, tokio::task::JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let runtime = Runtime::builder().executor(Echo).build().unwrap();
        let worker = LocalWorker::new(runtime, "worker-1", "Test worker");
        let host = WorkerHost::new(worker).bearer_token(token);
        let task = tokio::spawn(async move {
            let _ = host.serve(listener).await;
        });
        (format!("ws://{address}"), task)
    }

    #[tokio::test]
    async fn authenticated_websocket_worker_runs_and_reports_health() {
        let (url, host) = start_host("secret").await;
        let transport = WebSocketWorkerTransport::new(url).bearer_token("secret");
        let worker = RemoteWorker::connect(transport).await.unwrap();
        assert_eq!(worker.descriptor().name, "Test worker");

        let health = worker.refresh_health().await.unwrap();
        assert_eq!(health.status, WorkerHealthStatus::Ready);
        let mut run = worker
            .submit(AgentProfile::default(), RunRequest::new("hello remote"))
            .await
            .unwrap();
        assert!(matches!(
            run.next_event().await.unwrap().event,
            RuntimeEvent::TextDelta { .. }
        ));
        assert!(matches!(
            run.next_event().await.unwrap().event,
            RuntimeEvent::FinalResponse { .. }
        ));
        assert_eq!(run.result().await.unwrap().final_text, "hello remote");
        host.abort();
    }

    #[tokio::test]
    async fn websocket_worker_rejects_invalid_credentials() {
        let (url, host) = start_host("secret").await;
        let transport = WebSocketWorkerTransport::new(url).bearer_token("wrong");
        let error = match RemoteWorker::connect(transport).await {
            Ok(_) => panic!("invalid Worker credentials should fail"),
            Err(error) => error,
        };
        assert_eq!(error.code, RuntimeErrorCode::Unavailable);
        host.abort();
    }

    #[tokio::test]
    async fn websocket_worker_keeps_run_alive_and_resumes_after_disconnect() {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let executions = Arc::new(AtomicUsize::new(0));
        let runtime = Runtime::builder()
            .executor(CountingEcho(executions.clone()))
            .build()
            .unwrap();
        let host = WorkerHost::new(LocalWorker::new(runtime, "worker-1", "Test worker"));
        let host_task = tokio::spawn(async move {
            let _ = host.serve(listener).await;
        });
        let transport = WebSocketWorkerTransport::new(format!("ws://{address}"));
        let run_id = microclaw_core::run_protocol::RunId::new("durable-run");
        let mut first = transport.connect().await.unwrap();
        let mut request = RunRequest::new("survive disconnect");
        request.run_id = Some(run_id.clone());
        first
            .send(WorkerCommand::Submit {
                protocol_version: WORKER_PROTOCOL_VERSION,
                profile: AgentProfile::default(),
                request: Box::new(request),
            })
            .await
            .unwrap();
        assert!(matches!(
            first.receive().await.unwrap(),
            Some(WorkerFrame::Accepted { .. })
        ));
        drop(first);

        tokio::time::sleep(std::time::Duration::from_millis(60)).await;
        let mut resumed = transport.connect().await.unwrap();
        resumed
            .send(WorkerCommand::ResumeEvents {
                protocol_version: WORKER_PROTOCOL_VERSION,
                run_id: run_id.clone(),
                after_sequence: None,
            })
            .await
            .unwrap();
        let result = loop {
            match resumed.receive().await.unwrap() {
                Some(WorkerFrame::Event { .. }) => {}
                Some(WorkerFrame::Result { result, .. }) => break result,
                frame => panic!("unexpected resumed frame: {frame:?}"),
            }
        };
        assert_eq!(result.run_id, run_id);
        assert_eq!(executions.load(Ordering::SeqCst), 1);
        host_task.abort();
    }
}