agentos-execution 0.2.13

Native execution plane scaffold for secure-exec
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
//! V8 runtime host — manages a shared embedded V8 runtime with session multiplexing.

use crate::v8_ipc::{self, BinaryFrame};
use agentos_runtime::RuntimeContext;
use agentos_v8_runtime::embedded_runtime::{
    shared_embedded_runtime, EmbeddedV8Runtime, EmbeddedV8SessionHandle,
};
use agentos_v8_runtime::runtime_protocol::{RuntimeCommand, RuntimeEvent, WarmSessionHint};
use agentos_v8_runtime::session::RuntimeEventOutputReceiver;
use std::io::{self, Cursor};
use std::sync::{Arc, Mutex, OnceLock};

/// V8 polyfill bridge code generated by `build.rs`.
const V8_BRIDGE_CODE: &str = concat!(
    include_str!(concat!(env!("OUT_DIR"), "/v8-bridge.js")),
    "\n",
    include_str!(concat!(env!("OUT_DIR"), "/v8-bridge-zlib.js"))
);

/// Manages an embedded V8 runtime with session multiplexing.
pub struct V8RuntimeHost {
    shared: Arc<SharedEmbeddedRuntimeClient>,
}

struct SharedEmbeddedRuntimeClient {
    runtime: Arc<EmbeddedV8Runtime>,
}

/// Direct view of the V8 runtime's one bounded per-session output lane. Mapping
/// the typed in-process event to the legacy binary frame is allocation-local and
/// no longer requires a relay queue or an OS thread per session.
pub struct V8SessionFrameReceiver {
    inner: RuntimeEventOutputReceiver,
}

impl std::fmt::Debug for V8SessionFrameReceiver {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("V8SessionFrameReceiver")
            .finish_non_exhaustive()
    }
}

impl V8SessionFrameReceiver {
    pub fn recv(&self) -> Result<BinaryFrame, flume::RecvError> {
        self.inner.recv().map(from_runtime_event)
    }

    pub async fn recv_async(&self) -> Result<BinaryFrame, flume::RecvError> {
        self.inner.recv_async().await.map(from_runtime_event)
    }
}

impl V8RuntimeHost {
    /// Connect to the process-global embedded V8 runtime client.
    pub fn spawn(runtime: &RuntimeContext) -> io::Result<Self> {
        Ok(V8RuntimeHost {
            shared: shared_embedded_runtime_client(runtime)?,
        })
    }

    /// Register a session and return a receiver for its frames.
    pub fn register_session(
        &self,
        session_id: &str,
        runtime: &RuntimeContext,
    ) -> io::Result<V8SessionFrameReceiver> {
        self.shared
            .runtime
            .register_session_with_runtime(session_id, runtime)
            .map(|(inner, _registration)| inner)
            .map(|inner| V8SessionFrameReceiver { inner })
    }

    /// Unregister a session.
    pub fn unregister_session(&self, session_id: &str) {
        self.shared.runtime.unregister_session(session_id);
    }

    pub fn create_session(
        &self,
        session_id: String,
        heap_limit_mb: u32,
        cpu_time_limit_ms: u32,
        wall_clock_limit_ms: u32,
        warm_hint: Option<WarmSessionHint>,
    ) -> io::Result<()> {
        self.shared.runtime.dispatch(RuntimeCommand::CreateSession {
            session_id,
            heap_limit_mb: non_zero_option(heap_limit_mb),
            cpu_time_limit_ms: non_zero_option(cpu_time_limit_ms),
            wall_clock_limit_ms: non_zero_option(wall_clock_limit_ms),
            warm_hint,
        })
    }

    pub fn create_session_from_command(&self, command: RuntimeCommand) -> io::Result<()> {
        self.shared.runtime.dispatch(command)
    }

    pub fn create_session_from_command_with_runtime(
        &self,
        command: RuntimeCommand,
        runtime: &RuntimeContext,
        ready_batch_handle_limit: usize,
        bridge_call_timeout: std::time::Duration,
    ) -> io::Result<()> {
        self.shared.runtime.dispatch_create_session_with_runtime(
            command,
            runtime.clone(),
            ready_batch_handle_limit,
            bridge_call_timeout,
        )
    }

    /// Send a frame to the V8 runtime.
    pub fn send_frame(&self, frame: &BinaryFrame) -> io::Result<()> {
        self.shared.runtime.dispatch(to_runtime_command(frame)?)
    }

    /// Get the pre-bundled bridge code (polyfills).
    pub fn bridge_code() -> &'static str {
        V8_BRIDGE_CODE
    }

    /// Pre-build the per-sidecar snapshot for an agent-SDK `userland_code` bundle
    /// into the process-wide cache, so the FIRST session that uses it is already
    /// warm (no cold-build penalty on the session-create path). Blocks until the
    /// snapshot is built; idempotent (a cache hit returns immediately). A no-op for
    /// empty `userland_code`.
    pub fn pre_warm_snapshot(&self, userland_code: &str) -> io::Result<()> {
        if userland_code.is_empty() {
            return Ok(());
        }
        self.shared.runtime.dispatch(RuntimeCommand::WarmSnapshot {
            bridge_code: Self::bridge_code().to_owned(),
            userland_code: userland_code.to_owned(),
        })
    }

    pub fn pre_warm_workers(&self, userland_code: &str, heap_limit_mb: u32, count: usize) {
        self.shared.runtime.pre_warm_workers(
            Self::bridge_code().to_owned(),
            userland_code.to_owned(),
            non_zero_option(heap_limit_mb),
            count,
        );
    }

    pub fn seed_default_warm_workers_async(&self) {
        static DEFAULT_WARM_STARTED: OnceLock<()> = OnceLock::new();
        let _ = DEFAULT_WARM_STARTED.get_or_init(|| {
            self.shared.runtime.pre_warm_workers(
                V8_BRIDGE_CODE.to_owned(),
                String::new(),
                None,
                warm_worker_count(),
            );
        });
    }

    /// True when the process-wide snapshot cache already has this userland
    /// bundle. This is a lookup only; it never creates a snapshot.
    pub fn snapshot_ready(&self, userland_code: &str) -> bool {
        self.shared
            .runtime
            .snapshot_ready(Self::bridge_code(), userland_code)
    }

    /// Kick a process-wide async warm for the wasm runner snapshot. At most one
    /// warm thread is spawned per process; blocking callers should use
    /// [`pre_warm_snapshot`](Self::pre_warm_snapshot).
    pub fn warm_snapshot_async(runtime: &RuntimeContext, userland_code: String) {
        if userland_code.is_empty() {
            return;
        }
        static WASM_RUNNER_WARM_STARTED: OnceLock<()> = OnceLock::new();
        let _ = WASM_RUNNER_WARM_STARTED.get_or_init(|| {
            let requested_bytes = userland_code.len();
            let runtime_for_job = runtime.clone();
            if let Err(error) = runtime.blocking().submit(requested_bytes, move || {
                let result = run_v8_maintenance("agentos-wasm-snapshot-prewarm", move || {
                    let host = V8RuntimeHost::spawn(&runtime_for_job)?;
                    host.pre_warm_snapshot(&userland_code)
                });
                if let Err(error) = result {
                    eprintln!("ERR_AGENTOS_V8_MAINTENANCE: wasm snapshot warm failed: {error}");
                }
            }) {
                eprintln!("ERR_AGENTOS_V8_MAINTENANCE: bounded executor rejected warm: {error}");
            }
        });
    }

    /// Create a session handle for sending session-scoped frames and cleanup.
    pub fn session_handle(&self, session_id: String) -> V8SessionHandle {
        V8SessionHandle::new(session_id, Arc::clone(&self.shared.runtime))
    }

    pub fn child_pid(&self) -> u32 {
        0
    }

    pub fn is_alive(&mut self) -> io::Result<bool> {
        Ok(self.shared.runtime.is_alive())
    }

    #[cfg(test)]
    fn runtime_ptr(&self) -> usize {
        Arc::as_ptr(&self.shared.runtime) as usize
    }
}

fn non_zero_option(value: u32) -> Option<u32> {
    (value > 0).then_some(value)
}

fn warm_worker_count() -> usize {
    std::env::var("AGENTOS_V8_WARM_ISOLATES")
        .ok()
        .and_then(|value| value.parse::<usize>().ok())
        .unwrap_or(2)
}

/// A handle to a single V8 session within the shared runtime.
/// Provides methods for sending frames specific to this session.
pub struct V8SessionHandle {
    inner: EmbeddedV8SessionHandle,
}

impl std::fmt::Debug for V8SessionHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("V8SessionHandle")
            .field("session_id", &self.inner.session_id())
            .finish()
    }
}

impl V8SessionHandle {
    pub fn new(session_id: String, runtime: Arc<EmbeddedV8Runtime>) -> Self {
        Self {
            inner: runtime.session_handle(session_id),
        }
    }

    /// Send a bridge response back to the V8 isolate.
    pub fn send_bridge_response(
        &self,
        call_id: u64,
        status: u8,
        payload: Vec<u8>,
    ) -> io::Result<()> {
        self.inner.send_bridge_response(call_id, status, payload)
    }

    /// Send a stream event to the V8 isolate (stdin data, timer, etc.).
    pub fn send_stream_event(&self, event_type: &str, payload: Vec<u8>) -> io::Result<()> {
        self.inner.send_stream_event(event_type, payload)
    }

    /// Publish coalesced readiness for a registered sidecar capability. This
    /// carries no subsystem payload; the guest drain targets durable sidecar
    /// state by capability identity.
    pub fn publish_readiness(
        &self,
        capability_id: u64,
        capability_generation: u64,
        flags: agentos_runtime::readiness::ReadyFlags,
    ) -> io::Result<()> {
        self.inner
            .publish_readiness(capability_id, capability_generation, flags)
    }

    pub fn remove_readiness(
        &self,
        capability_id: u64,
        capability_generation: u64,
    ) -> io::Result<()> {
        self.inner
            .remove_readiness(capability_id, capability_generation)
    }

    pub fn set_application_read_interest(
        &self,
        capability_id: u64,
        capability_generation: u64,
        enabled: bool,
    ) -> io::Result<()> {
        self.inner
            .set_application_read_interest(capability_id, capability_generation, enabled)
    }

    pub fn publish_signal(&self, signal: i32) -> io::Result<()> {
        self.inner.publish_signal(signal)
    }

    pub fn publish_timer(&self, timer_id: u64) -> io::Result<()> {
        self.inner.publish_timer(timer_id)
    }

    /// Install a direct module-source reader on this session's V8 thread so module
    /// loads read source directly instead of round-tripping the bridge.
    pub fn set_module_reader(
        &self,
        reader: Box<dyn agentos_v8_runtime::execution::GuestModuleReader>,
    ) -> io::Result<()> {
        self.inner.set_module_reader(reader)
    }

    /// Execute bridge code + user code in this V8 session without routing through
    /// the legacy binary frame encode/decode path.
    #[allow(clippy::too_many_arguments)] // mirrors the CreateSession frame
    pub fn execute(
        &self,
        mode: u8,
        file_path: String,
        bridge_code: String,
        post_restore_script: String,
        userland_code: String,
        high_resolution_time: bool,
        user_code: String,
        wasm_module_bytes: Option<Arc<Vec<u8>>>,
    ) -> io::Result<()> {
        self.inner.execute(
            mode,
            file_path,
            bridge_code,
            post_restore_script,
            userland_code,
            high_resolution_time,
            user_code,
            wasm_module_bytes,
        )
    }

    /// Terminate execution in this session.
    pub fn terminate(&self) -> io::Result<()> {
        self.inner.terminate()
    }

    /// Suspend guest execution while preserving the V8 stack and session state.
    pub fn pause(&self) -> io::Result<()> {
        self.inner.pause()
    }

    /// Resume a session previously suspended with [`Self::pause`].
    pub fn resume(&self) -> io::Result<()> {
        self.inner.resume()
    }

    /// Destroy this session in the embedded runtime and remove its receiver.
    pub fn destroy(&self) -> io::Result<()> {
        self.inner.destroy()
    }

    pub fn session_id(&self) -> &str {
        self.inner.session_id()
    }
}

impl Clone for V8SessionHandle {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

/// Pre-build the per-sidecar snapshot for an agent-SDK `userland_code` bundle into
/// the process-wide cache, so the FIRST session that uses it is already warm. Uses
/// the shared embedded runtime directly (no per-call host lifecycle). Blocks until
/// built; idempotent (cache hit returns immediately); no-op for empty input.
/// Eagerly initialize the process-wide embedded V8 runtime (and its process-lifetime
/// platform owner) before accepting VM work. Calling this at sidecar startup keeps
/// initialization failures on the entrypoint, although correctness no longer
/// depends on the first caller itself being a long-lived thread.
pub fn ensure_runtime_initialized(runtime: &RuntimeContext) -> io::Result<()> {
    shared_embedded_runtime_client(runtime).map(|_| ())
}

pub fn pre_warm_agent_snapshot(runtime: &RuntimeContext, userland_code: &str) -> io::Result<()> {
    if userland_code.is_empty() {
        return Ok(());
    }
    let userland = userland_code.to_owned();
    let runtime = runtime.clone();
    run_v8_maintenance("agentos-snapshot-prewarm", move || {
        let client = shared_embedded_runtime_client(&runtime)?;
        client.runtime.dispatch(RuntimeCommand::WarmSnapshot {
            bridge_code: V8_BRIDGE_CODE.to_owned(),
            userland_code: userland,
        })
    })
}

/// V8 snapshot construction is the one admitted ephemeral maintenance-thread
/// exception. A process-wide mutex serializes it, and every thread is joined
/// before the next job can start so thread-local V8 state cannot leak.
fn run_v8_maintenance<T: Send + 'static>(
    thread_name: &str,
    operation: impl FnOnce() -> io::Result<T> + Send + 'static,
) -> io::Result<T> {
    static V8_MAINTENANCE_LOCK: Mutex<()> = Mutex::new(());
    let _exclusive = V8_MAINTENANCE_LOCK
        .lock()
        .map_err(|_| io::Error::other("V8 maintenance lock poisoned"))?;
    // AGENTOS_THREAD_SITE: serialized-v8-maintenance
    let handle = std::thread::Builder::new()
        .name(thread_name.to_owned())
        .spawn(operation)?;
    handle
        .join()
        .map_err(|_| io::Error::other("V8 maintenance thread panicked"))?
}

fn shared_embedded_runtime_client(
    runtime_context: &RuntimeContext,
) -> io::Result<Arc<SharedEmbeddedRuntimeClient>> {
    static SHARED_RUNTIME: OnceLock<Arc<SharedEmbeddedRuntimeClient>> = OnceLock::new();
    static SHARED_RUNTIME_INIT_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    if let Some(shared) = SHARED_RUNTIME.get() {
        return Ok(Arc::clone(shared));
    }

    let _guard = SHARED_RUNTIME_INIT_LOCK
        .lock()
        .expect("shared embedded runtime init lock poisoned");
    if let Some(shared) = SHARED_RUNTIME.get() {
        return Ok(Arc::clone(shared));
    }

    let shared = Arc::new(SharedEmbeddedRuntimeClient {
        runtime: shared_embedded_runtime(runtime_context.clone())?,
    });
    let _ = SHARED_RUNTIME.set(Arc::clone(&shared));
    Ok(shared)
}

fn to_runtime_command(frame: &BinaryFrame) -> io::Result<RuntimeCommand> {
    let bytes = v8_ipc::encode_frame(frame)?;
    let runtime_frame = agentos_v8_runtime::ipc_binary::read_frame(&mut Cursor::new(bytes))?;
    RuntimeCommand::try_from(runtime_frame)
}

fn from_runtime_event(event: RuntimeEvent) -> BinaryFrame {
    match event {
        RuntimeEvent::BridgeCall {
            session_id,
            call_id,
            method,
            payload,
        } => BinaryFrame::BridgeCall {
            session_id,
            call_id,
            method,
            payload,
        },
        RuntimeEvent::ExecutionResult {
            session_id,
            exit_code,
            exports,
            error,
        } => BinaryFrame::ExecutionResult {
            session_id,
            exit_code,
            exports,
            error: error.map(from_runtime_execution_error),
        },
        RuntimeEvent::Log {
            session_id,
            channel,
            message,
        } => BinaryFrame::Log {
            session_id,
            channel,
            message,
        },
        RuntimeEvent::StreamCallback {
            session_id,
            callback_type,
            payload,
        } => BinaryFrame::StreamCallback {
            session_id,
            callback_type,
            payload,
        },
    }
}

fn from_runtime_execution_error(
    error: agentos_v8_runtime::ipc_binary::ExecutionErrorBin,
) -> v8_ipc::ExecutionErrorBin {
    v8_ipc::ExecutionErrorBin {
        error_type: error.error_type,
        message: error.message,
        stack: error.stack,
        code: error.code,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU64, Ordering};

    static NEXT_TEST_SESSION_ID: AtomicU64 = AtomicU64::new(1);

    fn next_session_id() -> String {
        format!(
            "embedded-runtime-host-{}",
            NEXT_TEST_SESSION_ID.fetch_add(1, Ordering::Relaxed)
        )
    }

    fn test_runtime_context() -> RuntimeContext {
        agentos_runtime::SidecarRuntime::process(&agentos_runtime::RuntimeConfig::default())
            .expect("test process runtime")
            .context()
    }

    #[test]
    fn embedded_runtime_host_reuses_shared_runtime_service() {
        let runtime = test_runtime_context();
        let first = V8RuntimeHost::spawn(&runtime).expect("spawn V8 runtime host");
        let second = V8RuntimeHost::spawn(&runtime).expect("spawn V8 runtime host");
        assert_eq!(
            first.runtime_ptr(),
            second.runtime_ptr(),
            "V8 runtime hosts should reuse the same embedded runtime service"
        );
    }

    #[test]
    fn embedded_runtime_host_create_destroy_recycles_session_ids() {
        let runtime = test_runtime_context();
        let host = V8RuntimeHost::spawn(&runtime).expect("spawn V8 runtime host");
        let session_id = next_session_id();

        let _first_receiver = host
            .register_session(&session_id, &runtime)
            .expect("register session output");
        host.send_frame(&BinaryFrame::CreateSession {
            session_id: session_id.clone(),
            heap_limit_mb: 0,
            cpu_time_limit_ms: 0,
            wall_clock_limit_ms: 0,
        })
        .expect("create embedded runtime session");

        let duplicate_error = host
            .send_frame(&BinaryFrame::CreateSession {
                session_id: session_id.clone(),
                heap_limit_mb: 0,
                cpu_time_limit_ms: 0,
                wall_clock_limit_ms: 0,
            })
            .expect_err("duplicate session ids should be rejected");
        assert_eq!(duplicate_error.kind(), io::ErrorKind::Other);

        host.session_handle(session_id.clone())
            .destroy()
            .expect("destroy embedded runtime session");

        let _second_receiver = host
            .register_session(&session_id, &runtime)
            .expect("re-register session output");
        host.send_frame(&BinaryFrame::CreateSession {
            session_id: session_id.clone(),
            heap_limit_mb: 0,
            cpu_time_limit_ms: 0,
            wall_clock_limit_ms: 0,
        })
        .expect("recreate embedded runtime session");

        host.session_handle(session_id)
            .destroy()
            .expect("destroy recreated session");
    }
}