Skip to main content

secure_exec_sidecar/
wire.rs

1//! Generated Secure Exec sidecar wire protocol surface.
2//!
3//! This module is the public generated protocol entrypoint. The hand-written
4//! `protocol` module remains an internal compatibility layer while callers move
5//! to generated wire frames.
6
7use std::error::Error;
8use std::fmt;
9
10pub use crate::generated_protocol::v1::*;
11
12// The generated BARE types intentionally omit `Copy`/`Default`; restore them on the
13// crate-local generated types so the wider sidecar keeps the ergonomics it relies on
14// after the hand-written protocol types were replaced with these aliases. These live in
15// `wire` (not `protocol`) because `protocol.rs` is `#[path]`-included by integration
16// tests, where the generated types would be foreign and the impls would break the orphan rule.
17impl Copy for crate::generated_protocol::v1::GuestFilesystemOperation {}
18impl Copy for crate::generated_protocol::v1::RootFilesystemMode {}
19impl Copy for crate::generated_protocol::v1::WasmPermissionTier {}
20
21// `derive(Default)` cannot be added: these are foreign generated types, so the
22// `Default` impl must be written by hand here (orphan rule).
23#[allow(clippy::derivable_impls)]
24impl Default for crate::generated_protocol::v1::RootFilesystemEntryKind {
25    fn default() -> Self {
26        Self::File
27    }
28}
29
30impl Default for crate::generated_protocol::v1::RootFilesystemEntry {
31    fn default() -> Self {
32        Self {
33            path: String::new(),
34            kind: crate::generated_protocol::v1::RootFilesystemEntryKind::File,
35            mode: None,
36            uid: None,
37            gid: None,
38            content: None,
39            encoding: None,
40            target: None,
41            executable: false,
42        }
43    }
44}
45
46#[allow(clippy::derivable_impls)]
47impl Default for crate::generated_protocol::v1::RootFilesystemMode {
48    fn default() -> Self {
49        Self::Ephemeral
50    }
51}
52
53#[allow(clippy::derivable_impls)]
54impl Default for crate::generated_protocol::v1::RootFilesystemDescriptor {
55    fn default() -> Self {
56        Self {
57            mode: crate::generated_protocol::v1::RootFilesystemMode::default(),
58            disable_default_base_layer: false,
59            lowers: Vec::new(),
60            bootstrap_entries: Vec::new(),
61        }
62    }
63}
64
65impl crate::generated_protocol::v1::PermissionsPolicy {
66    pub fn deny_all() -> Self {
67        use crate::generated_protocol::v1::{
68            FsPermissionScope, PatternPermissionScope, PermissionMode,
69        };
70        Self {
71            fs: Some(FsPermissionScope::PermissionMode(PermissionMode::Deny)),
72            network: Some(PatternPermissionScope::PermissionMode(PermissionMode::Deny)),
73            child_process: Some(PatternPermissionScope::PermissionMode(PermissionMode::Deny)),
74            process: Some(PatternPermissionScope::PermissionMode(PermissionMode::Deny)),
75            env: Some(PatternPermissionScope::PermissionMode(PermissionMode::Deny)),
76            binding: Some(PatternPermissionScope::PermissionMode(PermissionMode::Deny)),
77        }
78    }
79
80    pub fn allow_all() -> Self {
81        use crate::generated_protocol::v1::{
82            FsPermissionScope, PatternPermissionScope, PermissionMode,
83        };
84        Self {
85            fs: Some(FsPermissionScope::PermissionMode(PermissionMode::Allow)),
86            network: Some(PatternPermissionScope::PermissionMode(
87                PermissionMode::Allow,
88            )),
89            child_process: Some(PatternPermissionScope::PermissionMode(
90                PermissionMode::Allow,
91            )),
92            process: Some(PatternPermissionScope::PermissionMode(
93                PermissionMode::Allow,
94            )),
95            env: Some(PatternPermissionScope::PermissionMode(
96                PermissionMode::Allow,
97            )),
98            binding: Some(PatternPermissionScope::PermissionMode(
99                PermissionMode::Allow,
100            )),
101        }
102    }
103}
104
105impl Default for crate::generated_protocol::v1::PermissionsPolicy {
106    fn default() -> Self {
107        Self::allow_all()
108    }
109}
110
111impl crate::generated_protocol::v1::CreateVmRequest {
112    pub fn json_config(
113        runtime: crate::generated_protocol::v1::GuestRuntimeKind,
114        config: secure_exec_vm_config::CreateVmConfig,
115    ) -> Self {
116        Self {
117            runtime,
118            config: serde_json::to_string(&config).expect("serialize create VM config"),
119        }
120    }
121
122    pub fn legacy_test_config(
123        runtime: crate::generated_protocol::v1::GuestRuntimeKind,
124        metadata: std::collections::HashMap<String, String>,
125        root_filesystem: crate::generated_protocol::v1::RootFilesystemDescriptor,
126        permissions: Option<crate::generated_protocol::v1::PermissionsPolicy>,
127    ) -> Self {
128        let metadata: std::collections::BTreeMap<_, _> = metadata.into_iter().collect();
129        let mut config = secure_exec_vm_config::CreateVmConfig {
130            cwd: metadata.get("cwd").cloned(),
131            env: legacy_env_config(&metadata),
132            root_filesystem: legacy_root_filesystem_config(root_filesystem),
133            permissions: permissions.map(legacy_permissions_config),
134            limits: legacy_limits_config(&metadata),
135            dns: legacy_dns_config(&metadata),
136            native_root: legacy_native_root_config(&metadata),
137            listen: legacy_listen_config(&metadata),
138            ..Default::default()
139        };
140        config.loopback_exempt_ports = legacy_loopback_exempt_ports(&config.env);
141        Self::json_config(runtime, config)
142    }
143}
144
145fn legacy_env_config(
146    metadata: &std::collections::BTreeMap<String, String>,
147) -> std::collections::BTreeMap<String, String> {
148    metadata
149        .iter()
150        .filter_map(|(key, value)| {
151            key.strip_prefix("env.")
152                .map(|name| (name.to_string(), value.clone()))
153        })
154        .collect()
155}
156
157fn legacy_root_filesystem_config(
158    descriptor: crate::generated_protocol::v1::RootFilesystemDescriptor,
159) -> secure_exec_vm_config::RootFilesystemConfig {
160    secure_exec_vm_config::RootFilesystemConfig {
161        mode: match descriptor.mode {
162            crate::generated_protocol::v1::RootFilesystemMode::Ephemeral => {
163                secure_exec_vm_config::RootFilesystemMode::Ephemeral
164            }
165            crate::generated_protocol::v1::RootFilesystemMode::ReadOnly => {
166                secure_exec_vm_config::RootFilesystemMode::ReadOnly
167            }
168        },
169        disable_default_base_layer: descriptor.disable_default_base_layer,
170        lowers: descriptor
171            .lowers
172            .into_iter()
173            .map(legacy_root_lower_config)
174            .collect(),
175        bootstrap_entries: descriptor
176            .bootstrap_entries
177            .into_iter()
178            .map(legacy_root_entry_config)
179            .collect(),
180    }
181}
182
183fn legacy_root_lower_config(
184    lower: crate::generated_protocol::v1::RootFilesystemLowerDescriptor,
185) -> secure_exec_vm_config::RootFilesystemLowerDescriptor {
186    match lower {
187        crate::generated_protocol::v1::RootFilesystemLowerDescriptor::SnapshotRootFilesystemLower(
188            snapshot,
189        ) => secure_exec_vm_config::RootFilesystemLowerDescriptor::Snapshot {
190            entries: snapshot
191                .entries
192                .into_iter()
193                .map(legacy_root_entry_config)
194                .collect(),
195        },
196        crate::generated_protocol::v1::RootFilesystemLowerDescriptor::BundledBaseFilesystemLower => {
197            secure_exec_vm_config::RootFilesystemLowerDescriptor::BundledBaseFilesystem
198        }
199    }
200}
201
202fn legacy_root_entry_config(
203    entry: crate::generated_protocol::v1::RootFilesystemEntry,
204) -> secure_exec_vm_config::RootFilesystemEntry {
205    secure_exec_vm_config::RootFilesystemEntry {
206        path: entry.path,
207        kind: match entry.kind {
208            crate::generated_protocol::v1::RootFilesystemEntryKind::File => {
209                secure_exec_vm_config::RootFilesystemEntryKind::File
210            }
211            crate::generated_protocol::v1::RootFilesystemEntryKind::Directory => {
212                secure_exec_vm_config::RootFilesystemEntryKind::Directory
213            }
214            crate::generated_protocol::v1::RootFilesystemEntryKind::Symlink => {
215                secure_exec_vm_config::RootFilesystemEntryKind::Symlink
216            }
217        },
218        mode: entry.mode,
219        uid: entry.uid,
220        gid: entry.gid,
221        content: entry.content,
222        encoding: entry.encoding.map(|encoding| match encoding {
223            crate::generated_protocol::v1::RootFilesystemEntryEncoding::Utf8 => {
224                secure_exec_vm_config::RootFilesystemEntryEncoding::Utf8
225            }
226            crate::generated_protocol::v1::RootFilesystemEntryEncoding::Base64 => {
227                secure_exec_vm_config::RootFilesystemEntryEncoding::Base64
228            }
229        }),
230        target: entry.target,
231        executable: entry.executable,
232    }
233}
234
235fn legacy_permissions_config(
236    permissions: crate::generated_protocol::v1::PermissionsPolicy,
237) -> secure_exec_vm_config::PermissionsPolicy {
238    secure_exec_vm_config::PermissionsPolicy {
239        fs: permissions.fs.map(legacy_fs_permission_scope_config),
240        network: permissions
241            .network
242            .map(legacy_pattern_permission_scope_config),
243        child_process: permissions
244            .child_process
245            .map(legacy_pattern_permission_scope_config),
246        process: permissions
247            .process
248            .map(legacy_pattern_permission_scope_config),
249        env: permissions.env.map(legacy_pattern_permission_scope_config),
250        binding: permissions
251            .binding
252            .map(legacy_pattern_permission_scope_config),
253    }
254}
255
256fn legacy_permission_mode_config(
257    mode: crate::generated_protocol::v1::PermissionMode,
258) -> secure_exec_vm_config::PermissionMode {
259    match mode {
260        crate::generated_protocol::v1::PermissionMode::Allow => {
261            secure_exec_vm_config::PermissionMode::Allow
262        }
263        crate::generated_protocol::v1::PermissionMode::Ask => {
264            secure_exec_vm_config::PermissionMode::Ask
265        }
266        crate::generated_protocol::v1::PermissionMode::Deny => {
267            secure_exec_vm_config::PermissionMode::Deny
268        }
269    }
270}
271
272fn legacy_fs_permission_scope_config(
273    scope: crate::generated_protocol::v1::FsPermissionScope,
274) -> secure_exec_vm_config::FsPermissionScope {
275    match scope {
276        crate::generated_protocol::v1::FsPermissionScope::PermissionMode(mode) => {
277            secure_exec_vm_config::FsPermissionScope::Mode(legacy_permission_mode_config(mode))
278        }
279        crate::generated_protocol::v1::FsPermissionScope::FsPermissionRuleSet(rules) => {
280            secure_exec_vm_config::FsPermissionScope::Rules(
281                secure_exec_vm_config::FsPermissionRuleSet {
282                    default: rules.default.map(legacy_permission_mode_config),
283                    rules: rules
284                        .rules
285                        .into_iter()
286                        .map(|rule| secure_exec_vm_config::FsPermissionRule {
287                            mode: legacy_permission_mode_config(rule.mode),
288                            operations: rule.operations,
289                            paths: rule.paths,
290                        })
291                        .collect(),
292                },
293            )
294        }
295    }
296}
297
298fn legacy_pattern_permission_scope_config(
299    scope: crate::generated_protocol::v1::PatternPermissionScope,
300) -> secure_exec_vm_config::PatternPermissionScope {
301    match scope {
302        crate::generated_protocol::v1::PatternPermissionScope::PermissionMode(mode) => {
303            secure_exec_vm_config::PatternPermissionScope::Mode(legacy_permission_mode_config(mode))
304        }
305        crate::generated_protocol::v1::PatternPermissionScope::PatternPermissionRuleSet(rules) => {
306            secure_exec_vm_config::PatternPermissionScope::Rules(
307                secure_exec_vm_config::PatternPermissionRuleSet {
308                    default: rules.default.map(legacy_permission_mode_config),
309                    rules: rules
310                        .rules
311                        .into_iter()
312                        .map(|rule| secure_exec_vm_config::PatternPermissionRule {
313                            mode: legacy_permission_mode_config(rule.mode),
314                            operations: rule.operations,
315                            patterns: rule.patterns,
316                        })
317                        .collect(),
318                },
319            )
320        }
321    }
322}
323
324fn legacy_dns_config(
325    metadata: &std::collections::BTreeMap<String, String>,
326) -> Option<secure_exec_vm_config::VmDnsConfig> {
327    let mut dns = secure_exec_vm_config::VmDnsConfig::default();
328    if let Some(value) = metadata.get("network.dns.servers") {
329        dns.name_servers = value
330            .split(',')
331            .map(str::trim)
332            .filter(|entry| !entry.is_empty())
333            .map(str::to_string)
334            .collect();
335    }
336    for (key, value) in metadata {
337        let Some(hostname) = key.strip_prefix("network.dns.override.") else {
338            continue;
339        };
340        dns.overrides.insert(
341            hostname.to_string(),
342            value
343                .split(',')
344                .map(str::trim)
345                .filter(|entry| !entry.is_empty())
346                .map(str::to_string)
347                .collect(),
348        );
349    }
350    if dns.name_servers.is_empty() && dns.overrides.is_empty() {
351        None
352    } else {
353        Some(dns)
354    }
355}
356
357fn legacy_native_root_config(
358    metadata: &std::collections::BTreeMap<String, String>,
359) -> Option<secure_exec_vm_config::NativeRootFilesystemConfig> {
360    let id = metadata.get("rootFilesystem.nativePlugin.id")?;
361    let config = metadata
362        .get("rootFilesystem.nativePlugin.config")
363        .map(|value| serde_json::from_str(value).expect("parse native root plugin config"))
364        .unwrap_or_else(|| serde_json::Value::Object(serde_json::Map::new()));
365    let read_only = metadata
366        .get("rootFilesystem.nativePlugin.readOnly")
367        .map(|value| value.parse::<bool>().expect("parse native root readOnly"))
368        .unwrap_or(false);
369    Some(secure_exec_vm_config::NativeRootFilesystemConfig {
370        plugin: secure_exec_vm_config::MountPluginDescriptor {
371            id: id.clone(),
372            config,
373        },
374        read_only,
375    })
376}
377
378fn legacy_listen_config(
379    metadata: &std::collections::BTreeMap<String, String>,
380) -> Option<secure_exec_vm_config::VmListenPolicyConfig> {
381    let listen = secure_exec_vm_config::VmListenPolicyConfig {
382        port_min: metadata
383            .get("network.listen.port_min")
384            .map(|value| value.parse::<u16>().expect("parse network.listen.port_min")),
385        port_max: metadata
386            .get("network.listen.port_max")
387            .map(|value| value.parse::<u16>().expect("parse network.listen.port_max")),
388        allow_privileged: metadata
389            .get("network.listen.allow_privileged")
390            .map(|value| {
391                value
392                    .parse::<bool>()
393                    .expect("parse network.listen.allow_privileged")
394            }),
395    };
396    if listen.port_min.is_none() && listen.port_max.is_none() && listen.allow_privileged.is_none() {
397        None
398    } else {
399        Some(listen)
400    }
401}
402
403fn legacy_loopback_exempt_ports(env: &std::collections::BTreeMap<String, String>) -> Vec<u16> {
404    let Some(value) = env.get("AGENT_OS_LOOPBACK_EXEMPT_PORTS") else {
405        return Vec::new();
406    };
407    serde_json::from_str::<Vec<serde_json::Value>>(value)
408        .unwrap_or_default()
409        .into_iter()
410        .filter_map(|value| match value {
411            serde_json::Value::Number(number) => number.as_u64(),
412            serde_json::Value::String(value) => value.parse::<u64>().ok(),
413            _ => None,
414        })
415        .filter_map(|port| u16::try_from(port).ok())
416        .collect()
417}
418
419fn legacy_limits_config(
420    metadata: &std::collections::BTreeMap<String, String>,
421) -> Option<secure_exec_vm_config::VmLimitsConfig> {
422    let resources = secure_exec_vm_config::ResourceLimitsConfig {
423        cpu_count: legacy_u64(metadata, "resource.cpu_count"),
424        max_processes: legacy_u64(metadata, "resource.max_processes"),
425        max_open_fds: legacy_u64(metadata, "resource.max_open_fds"),
426        max_pipes: legacy_u64(metadata, "resource.max_pipes"),
427        max_ptys: legacy_u64(metadata, "resource.max_ptys"),
428        max_sockets: legacy_u64(metadata, "resource.max_sockets"),
429        max_connections: legacy_u64(metadata, "resource.max_connections"),
430        max_socket_buffered_bytes: legacy_u64(metadata, "resource.max_socket_buffered_bytes"),
431        max_socket_datagram_queue_len: legacy_u64(
432            metadata,
433            "resource.max_socket_datagram_queue_len",
434        ),
435        max_filesystem_bytes: legacy_u64(metadata, "resource.max_filesystem_bytes"),
436        max_inode_count: legacy_u64(metadata, "resource.max_inode_count"),
437        max_blocking_read_ms: legacy_u64(metadata, "resource.max_blocking_read_ms"),
438        max_pread_bytes: legacy_u64(metadata, "resource.max_pread_bytes"),
439        max_fd_write_bytes: legacy_u64(metadata, "resource.max_fd_write_bytes"),
440        max_process_argv_bytes: legacy_u64(metadata, "resource.max_process_argv_bytes"),
441        max_process_env_bytes: legacy_u64(metadata, "resource.max_process_env_bytes"),
442        max_readdir_entries: legacy_u64(metadata, "resource.max_readdir_entries"),
443        max_wasm_fuel: legacy_u64(metadata, "resource.max_wasm_fuel"),
444        max_wasm_memory_bytes: legacy_u64(metadata, "resource.max_wasm_memory_bytes"),
445        max_wasm_stack_bytes: legacy_u64(metadata, "resource.max_wasm_stack_bytes"),
446    };
447    let http = secure_exec_vm_config::HttpLimitsConfig {
448        max_fetch_response_bytes: legacy_u64(metadata, "limits.http.max_fetch_response_bytes"),
449    };
450    let tools = secure_exec_vm_config::ToolLimitsConfig {
451        default_tool_timeout_ms: legacy_u64(metadata, "limits.tools.default_tool_timeout_ms"),
452        max_tool_timeout_ms: legacy_u64(metadata, "limits.tools.max_tool_timeout_ms"),
453        max_registered_toolkits: legacy_u64(metadata, "limits.tools.max_registered_toolkits"),
454        max_registered_tools_per_vm: legacy_u64(
455            metadata,
456            "limits.tools.max_registered_tools_per_vm",
457        ),
458        max_tools_per_toolkit: legacy_u64(metadata, "limits.tools.max_tools_per_toolkit"),
459        max_tool_schema_bytes: legacy_u64(metadata, "limits.tools.max_tool_schema_bytes"),
460        max_tool_examples_per_tool: legacy_u64(metadata, "limits.tools.max_tool_examples_per_tool"),
461        max_tool_example_input_bytes: legacy_u64(
462            metadata,
463            "limits.tools.max_tool_example_input_bytes",
464        ),
465    };
466    let plugins = secure_exec_vm_config::PluginLimitsConfig {
467        max_persisted_manifest_bytes: legacy_u64(
468            metadata,
469            "limits.plugins.max_persisted_manifest_bytes",
470        ),
471        max_persisted_manifest_file_bytes: legacy_u64(
472            metadata,
473            "limits.plugins.max_persisted_manifest_file_bytes",
474        ),
475    };
476    let acp = secure_exec_vm_config::AcpLimitsConfig {
477        max_read_line_bytes: legacy_u64(metadata, "limits.acp.max_read_line_bytes"),
478        stdout_buffer_byte_limit: legacy_u64(metadata, "limits.acp.stdout_buffer_byte_limit"),
479    };
480    let js_runtime = secure_exec_vm_config::JsRuntimeLimitsConfig {
481        v8_heap_limit_mb: legacy_u64(metadata, "limits.js_runtime.v8_heap_limit_mb"),
482        sync_rpc_wait_timeout_ms: legacy_u64(
483            metadata,
484            "limits.js_runtime.sync_rpc_wait_timeout_ms",
485        ),
486        captured_output_limit_bytes: legacy_u64(
487            metadata,
488            "limits.js_runtime.captured_output_limit_bytes",
489        ),
490        stdin_buffer_limit_bytes: legacy_u64(
491            metadata,
492            "limits.js_runtime.stdin_buffer_limit_bytes",
493        ),
494        event_payload_limit_bytes: legacy_u64(
495            metadata,
496            "limits.js_runtime.event_payload_limit_bytes",
497        ),
498        v8_ipc_max_frame_bytes: legacy_u64(metadata, "limits.js_runtime.v8_ipc_max_frame_bytes"),
499    };
500    let python = secure_exec_vm_config::PythonLimitsConfig {
501        output_buffer_max_bytes: legacy_u64(metadata, "limits.python.output_buffer_max_bytes"),
502        execution_timeout_ms: legacy_u64(metadata, "limits.python.execution_timeout_ms"),
503        max_old_space_mb: legacy_u64(metadata, "limits.python.max_old_space_mb"),
504        vfs_rpc_timeout_ms: legacy_u64(metadata, "limits.python.vfs_rpc_timeout_ms"),
505    };
506    let wasm = secure_exec_vm_config::WasmLimitsConfig {
507        max_module_file_bytes: legacy_u64(metadata, "limits.wasm.max_module_file_bytes"),
508        captured_output_limit_bytes: legacy_u64(
509            metadata,
510            "limits.wasm.captured_output_limit_bytes",
511        ),
512        sync_read_limit_bytes: legacy_u64(metadata, "limits.wasm.sync_read_limit_bytes"),
513    };
514
515    let config = secure_exec_vm_config::VmLimitsConfig {
516        resources: legacy_has_resource_limits(&resources).then_some(resources),
517        http: http.max_fetch_response_bytes.is_some().then_some(http),
518        tools: legacy_has_tool_limits(&tools).then_some(tools),
519        plugins: legacy_has_plugin_limits(&plugins).then_some(plugins),
520        acp: legacy_has_acp_limits(&acp).then_some(acp),
521        js_runtime: legacy_has_js_runtime_limits(&js_runtime).then_some(js_runtime),
522        python: legacy_has_python_limits(&python).then_some(python),
523        wasm: legacy_has_wasm_limits(&wasm).then_some(wasm),
524    };
525
526    if config.resources.is_none()
527        && config.http.is_none()
528        && config.tools.is_none()
529        && config.plugins.is_none()
530        && config.acp.is_none()
531        && config.js_runtime.is_none()
532        && config.python.is_none()
533        && config.wasm.is_none()
534    {
535        None
536    } else {
537        Some(config)
538    }
539}
540
541fn legacy_u64(metadata: &std::collections::BTreeMap<String, String>, key: &str) -> Option<u64> {
542    metadata.get(key).map(|value| {
543        value
544            .parse::<u64>()
545            .unwrap_or_else(|error| panic!("parse {key}: {error}"))
546    })
547}
548
549fn legacy_has_resource_limits(config: &secure_exec_vm_config::ResourceLimitsConfig) -> bool {
550    config.cpu_count.is_some()
551        || config.max_processes.is_some()
552        || config.max_open_fds.is_some()
553        || config.max_pipes.is_some()
554        || config.max_ptys.is_some()
555        || config.max_sockets.is_some()
556        || config.max_connections.is_some()
557        || config.max_socket_buffered_bytes.is_some()
558        || config.max_socket_datagram_queue_len.is_some()
559        || config.max_filesystem_bytes.is_some()
560        || config.max_inode_count.is_some()
561        || config.max_blocking_read_ms.is_some()
562        || config.max_pread_bytes.is_some()
563        || config.max_fd_write_bytes.is_some()
564        || config.max_process_argv_bytes.is_some()
565        || config.max_process_env_bytes.is_some()
566        || config.max_readdir_entries.is_some()
567        || config.max_wasm_fuel.is_some()
568        || config.max_wasm_memory_bytes.is_some()
569        || config.max_wasm_stack_bytes.is_some()
570}
571
572fn legacy_has_tool_limits(config: &secure_exec_vm_config::ToolLimitsConfig) -> bool {
573    config.default_tool_timeout_ms.is_some()
574        || config.max_tool_timeout_ms.is_some()
575        || config.max_registered_toolkits.is_some()
576        || config.max_registered_tools_per_vm.is_some()
577        || config.max_tools_per_toolkit.is_some()
578        || config.max_tool_schema_bytes.is_some()
579        || config.max_tool_examples_per_tool.is_some()
580        || config.max_tool_example_input_bytes.is_some()
581}
582
583fn legacy_has_plugin_limits(config: &secure_exec_vm_config::PluginLimitsConfig) -> bool {
584    config.max_persisted_manifest_bytes.is_some()
585        || config.max_persisted_manifest_file_bytes.is_some()
586}
587
588fn legacy_has_acp_limits(config: &secure_exec_vm_config::AcpLimitsConfig) -> bool {
589    config.max_read_line_bytes.is_some() || config.stdout_buffer_byte_limit.is_some()
590}
591
592fn legacy_has_js_runtime_limits(config: &secure_exec_vm_config::JsRuntimeLimitsConfig) -> bool {
593    config.v8_heap_limit_mb.is_some()
594        || config.sync_rpc_wait_timeout_ms.is_some()
595        || config.captured_output_limit_bytes.is_some()
596        || config.stdin_buffer_limit_bytes.is_some()
597        || config.event_payload_limit_bytes.is_some()
598        || config.v8_ipc_max_frame_bytes.is_some()
599}
600
601fn legacy_has_python_limits(config: &secure_exec_vm_config::PythonLimitsConfig) -> bool {
602    config.output_buffer_max_bytes.is_some()
603        || config.execution_timeout_ms.is_some()
604        || config.max_old_space_mb.is_some()
605        || config.vfs_rpc_timeout_ms.is_some()
606}
607
608fn legacy_has_wasm_limits(config: &secure_exec_vm_config::WasmLimitsConfig) -> bool {
609    config.max_module_file_bytes.is_some()
610        || config.captured_output_limit_bytes.is_some()
611        || config.sync_read_limit_bytes.is_some()
612}
613
614// Ownership-scope constructor ergonomics. The generated BARE union exposes only the
615// tuple-wrapped variants (`ConnectionOwnership`/`SessionOwnership`/`VmOwnership`); restore
616// the hand-written `connection`/`session`/`vm` helpers the sidecar relies on. These live in
617// `wire` (not `protocol`) for the same orphan-rule reason as the impls above: `protocol.rs`
618// is `#[path]`-included by integration tests where the generated type is foreign.
619impl crate::generated_protocol::v1::OwnershipScope {
620    pub fn connection(connection_id: impl Into<String>) -> Self {
621        Self::ConnectionOwnership(crate::generated_protocol::v1::ConnectionOwnership {
622            connection_id: connection_id.into(),
623        })
624    }
625
626    pub fn session(connection_id: impl Into<String>, session_id: impl Into<String>) -> Self {
627        Self::SessionOwnership(crate::generated_protocol::v1::SessionOwnership {
628            connection_id: connection_id.into(),
629            session_id: session_id.into(),
630        })
631    }
632
633    pub fn vm(
634        connection_id: impl Into<String>,
635        session_id: impl Into<String>,
636        vm_id: impl Into<String>,
637    ) -> Self {
638        Self::VmOwnership(crate::generated_protocol::v1::VmOwnership {
639            connection_id: connection_id.into(),
640            session_id: session_id.into(),
641            vm_id: vm_id.into(),
642        })
643    }
644}
645
646pub const PROTOCOL_NAME: &str = "secure-exec-sidecar";
647pub const PROTOCOL_VERSION: u16 = 7;
648pub const DEFAULT_MAX_FRAME_BYTES: usize = 1024 * 1024;
649
650#[derive(Debug, Clone, PartialEq, Eq)]
651pub enum ProtocolCodecError {
652    TruncatedFrame {
653        actual: usize,
654    },
655    LengthPrefixMismatch {
656        declared: usize,
657        actual: usize,
658    },
659    FrameTooLarge {
660        size: usize,
661        max: usize,
662    },
663    UnsupportedSchema {
664        name: String,
665        version: u16,
666    },
667    InvalidRequestId,
668    InvalidRequestDirection {
669        request_id: RequestId,
670        expected: RequestDirection,
671    },
672    EmptyOwnershipField {
673        field: &'static str,
674    },
675    EmptyAuthToken,
676    InvalidOwnershipScope {
677        required: OwnershipRequirement,
678        actual: OwnershipRequirement,
679    },
680    SerializeFailure(String),
681    DeserializeFailure(String),
682}
683
684impl fmt::Display for ProtocolCodecError {
685    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
686        match self {
687            Self::TruncatedFrame { actual } => {
688                write!(
689                    f,
690                    "protocol frame is truncated: only {actual} bytes provided"
691                )
692            }
693            Self::LengthPrefixMismatch { declared, actual } => write!(
694                f,
695                "protocol frame length prefix mismatch: declared {declared} bytes, got {actual}",
696            ),
697            Self::FrameTooLarge { size, max } => {
698                write!(f, "protocol frame is {size} bytes, limit is {max}")
699            }
700            Self::UnsupportedSchema { name, version } => write!(
701                f,
702                "unsupported protocol schema {name}@{version}; expected {PROTOCOL_NAME}@{PROTOCOL_VERSION}",
703            ),
704            Self::InvalidRequestId => write!(f, "protocol request identifiers must be non-zero"),
705            Self::InvalidRequestDirection {
706                request_id,
707                expected,
708            } => write!(f, "protocol request id {request_id} must be {expected}",),
709            Self::EmptyOwnershipField { field } => {
710                write!(f, "protocol ownership field `{field}` cannot be empty")
711            }
712            Self::EmptyAuthToken => {
713                write!(f, "authenticate requests require a non-empty auth token")
714            }
715            Self::InvalidOwnershipScope { required, actual } => write!(
716                f,
717                "protocol frame requires {required} ownership but carried {actual}",
718            ),
719            Self::SerializeFailure(message) => {
720                write!(f, "protocol frame serialization failed: {message}")
721            }
722            Self::DeserializeFailure(message) => {
723                write!(f, "protocol frame deserialization failed: {message}")
724            }
725        }
726    }
727}
728
729impl Error for ProtocolCodecError {}
730
731#[derive(Debug, Clone, Copy, PartialEq, Eq)]
732pub enum OwnershipRequirement {
733    Any,
734    Connection,
735    Session,
736    Vm,
737    SessionOrVm,
738}
739
740impl fmt::Display for OwnershipRequirement {
741    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
742        match self {
743            Self::Any => write!(f, "any"),
744            Self::Connection => write!(f, "connection"),
745            Self::Session => write!(f, "session"),
746            Self::Vm => write!(f, "vm"),
747            Self::SessionOrVm => write!(f, "session-or-vm"),
748        }
749    }
750}
751
752#[derive(Debug, Clone, Copy, PartialEq, Eq)]
753pub enum RequestDirection {
754    Host,
755    Sidecar,
756}
757
758impl fmt::Display for RequestDirection {
759    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
760        match self {
761            Self::Host => write!(f, "positive"),
762            Self::Sidecar => write!(f, "negative"),
763        }
764    }
765}
766
767#[derive(Debug, Clone, PartialEq, Eq)]
768pub struct WireDispatchResult {
769    pub response: ResponseFrame,
770    pub events: Vec<EventFrame>,
771}
772
773#[derive(Debug, Clone)]
774pub struct WireFrameCodec {
775    max_frame_bytes: usize,
776}
777
778impl WireFrameCodec {
779    pub fn new(max_frame_bytes: usize) -> Self {
780        Self { max_frame_bytes }
781    }
782
783    pub fn max_frame_bytes(&self) -> usize {
784        self.max_frame_bytes
785    }
786
787    pub fn encode(&self, frame: &ProtocolFrame) -> Result<Vec<u8>, ProtocolCodecError> {
788        validate_frame(frame)?;
789
790        let payload = serde_bare::to_vec(frame)
791            .map_err(|error| ProtocolCodecError::SerializeFailure(error.to_string()))?;
792        if payload.len() > self.max_frame_bytes {
793            return Err(ProtocolCodecError::FrameTooLarge {
794                size: payload.len(),
795                max: self.max_frame_bytes,
796            });
797        }
798
799        let length =
800            u32::try_from(payload.len()).map_err(|_| ProtocolCodecError::FrameTooLarge {
801                size: payload.len(),
802                max: u32::MAX as usize,
803            })?;
804
805        let mut encoded = Vec::with_capacity(4 + payload.len());
806        encoded.extend_from_slice(&length.to_be_bytes());
807        encoded.extend_from_slice(&payload);
808        Ok(encoded)
809    }
810
811    pub fn decode(&self, bytes: &[u8]) -> Result<ProtocolFrame, ProtocolCodecError> {
812        let payload = self.checked_payload(bytes)?;
813        let frame = serde_bare::from_slice(payload)
814            .map_err(|error| ProtocolCodecError::DeserializeFailure(error.to_string()))?;
815        validate_frame(&frame)?;
816        Ok(frame)
817    }
818
819    fn checked_payload<'a>(&self, bytes: &'a [u8]) -> Result<&'a [u8], ProtocolCodecError> {
820        if bytes.len() < 4 {
821            return Err(ProtocolCodecError::TruncatedFrame {
822                actual: bytes.len(),
823            });
824        }
825
826        let declared =
827            u32::from_be_bytes(bytes[..4].try_into().expect("length prefix is four bytes"))
828                as usize;
829        if declared > self.max_frame_bytes {
830            return Err(ProtocolCodecError::FrameTooLarge {
831                size: declared,
832                max: self.max_frame_bytes,
833            });
834        }
835
836        let actual = bytes.len() - 4;
837        if declared != actual {
838            return Err(ProtocolCodecError::LengthPrefixMismatch { declared, actual });
839        }
840
841        Ok(&bytes[4..])
842    }
843}
844
845impl Default for WireFrameCodec {
846    fn default() -> Self {
847        Self::new(DEFAULT_MAX_FRAME_BYTES)
848    }
849}
850
851pub fn protocol_schema() -> ProtocolSchema {
852    ProtocolSchema::current()
853}
854
855impl ProtocolSchema {
856    pub fn current() -> Self {
857        Self {
858            name: PROTOCOL_NAME.to_string(),
859            version: PROTOCOL_VERSION,
860        }
861    }
862}
863
864impl Default for ProtocolSchema {
865    fn default() -> Self {
866        Self::current()
867    }
868}
869
870pub(crate) fn request_frame_to_compat(
871    request: RequestFrame,
872) -> Result<crate::protocol::RequestFrame, ProtocolCodecError> {
873    match crate::protocol::from_generated_protocol_frame(ProtocolFrame::RequestFrame(request))? {
874        crate::protocol::ProtocolFrame::Request(request) => Ok(request),
875        crate::protocol::ProtocolFrame::Response(_)
876        | crate::protocol::ProtocolFrame::Event(_)
877        | crate::protocol::ProtocolFrame::SidecarRequest(_)
878        | crate::protocol::ProtocolFrame::SidecarResponse(_) => {
879            Err(ProtocolCodecError::DeserializeFailure(String::from(
880                "wire request frame converted to non-request compatibility frame",
881            )))
882        }
883    }
884}
885
886pub(crate) fn ownership_scope_to_compat(
887    ownership: OwnershipScope,
888) -> crate::protocol::OwnershipScope {
889    crate::protocol::from_generated_ownership_scope(ownership)
890}
891
892pub(crate) fn request_payload_to_compat(
893    ownership: &crate::protocol::OwnershipScope,
894    payload: RequestPayload,
895) -> Result<crate::protocol::RequestPayload, ProtocolCodecError> {
896    match crate::protocol::from_generated_protocol_frame(ProtocolFrame::RequestFrame(
897        RequestFrame {
898            schema: protocol_schema(),
899            request_id: 1,
900            ownership: crate::protocol::to_generated_ownership_scope(ownership),
901            payload,
902        },
903    ))? {
904        crate::protocol::ProtocolFrame::Request(request) => Ok(request.payload),
905        crate::protocol::ProtocolFrame::Response(_)
906        | crate::protocol::ProtocolFrame::Event(_)
907        | crate::protocol::ProtocolFrame::SidecarRequest(_)
908        | crate::protocol::ProtocolFrame::SidecarResponse(_) => {
909            Err(ProtocolCodecError::DeserializeFailure(String::from(
910                "wire request payload converted to non-request compatibility frame",
911            )))
912        }
913    }
914}
915
916pub(crate) fn response_payload_from_compat(
917    ownership: &crate::protocol::OwnershipScope,
918    payload: crate::protocol::ResponsePayload,
919) -> Result<ResponsePayload, ProtocolCodecError> {
920    match crate::protocol::to_generated_protocol_frame(&crate::protocol::ProtocolFrame::Response(
921        crate::protocol::ResponseFrame::new(1, ownership.clone(), payload),
922    ))? {
923        ProtocolFrame::ResponseFrame(response) => Ok(response.payload),
924        ProtocolFrame::RequestFrame(_)
925        | ProtocolFrame::EventFrame(_)
926        | ProtocolFrame::SidecarRequestFrame(_)
927        | ProtocolFrame::SidecarResponseFrame(_) => Err(ProtocolCodecError::SerializeFailure(
928            String::from("compatibility response payload converted to non-response wire frame"),
929        )),
930    }
931}
932
933pub(crate) fn event_frame_from_compat(
934    event: crate::protocol::EventFrame,
935) -> Result<EventFrame, ProtocolCodecError> {
936    match crate::protocol::to_generated_protocol_frame(&crate::protocol::ProtocolFrame::Event(
937        event,
938    ))? {
939        ProtocolFrame::EventFrame(event) => Ok(event),
940        ProtocolFrame::RequestFrame(_)
941        | ProtocolFrame::ResponseFrame(_)
942        | ProtocolFrame::SidecarRequestFrame(_)
943        | ProtocolFrame::SidecarResponseFrame(_) => Err(ProtocolCodecError::SerializeFailure(
944            String::from("compatibility event converted to non-event wire frame"),
945        )),
946    }
947}
948
949pub(crate) fn event_frame_to_compat(
950    event: EventFrame,
951) -> Result<crate::protocol::EventFrame, ProtocolCodecError> {
952    match crate::protocol::from_generated_protocol_frame(ProtocolFrame::EventFrame(event))? {
953        crate::protocol::ProtocolFrame::Event(event) => Ok(event),
954        crate::protocol::ProtocolFrame::Request(_)
955        | crate::protocol::ProtocolFrame::Response(_)
956        | crate::protocol::ProtocolFrame::SidecarRequest(_)
957        | crate::protocol::ProtocolFrame::SidecarResponse(_) => {
958            Err(ProtocolCodecError::DeserializeFailure(String::from(
959                "wire event converted to non-event compatibility frame",
960            )))
961        }
962    }
963}
964
965pub(crate) fn sidecar_request_frame_from_compat(
966    request: crate::protocol::SidecarRequestFrame,
967) -> Result<SidecarRequestFrame, ProtocolCodecError> {
968    match crate::protocol::to_generated_protocol_frame(
969        &crate::protocol::ProtocolFrame::SidecarRequest(request),
970    )? {
971        ProtocolFrame::SidecarRequestFrame(request) => Ok(request),
972        ProtocolFrame::RequestFrame(_)
973        | ProtocolFrame::ResponseFrame(_)
974        | ProtocolFrame::EventFrame(_)
975        | ProtocolFrame::SidecarResponseFrame(_) => {
976            Err(ProtocolCodecError::SerializeFailure(String::from(
977                "compatibility sidecar request converted to non-sidecar-request wire frame",
978            )))
979        }
980    }
981}
982
983pub(crate) fn sidecar_request_payload_to_compat(
984    ownership: &crate::protocol::OwnershipScope,
985    payload: SidecarRequestPayload,
986) -> Result<crate::protocol::SidecarRequestPayload, ProtocolCodecError> {
987    match crate::protocol::from_generated_protocol_frame(ProtocolFrame::SidecarRequestFrame(
988        SidecarRequestFrame {
989            schema: protocol_schema(),
990            request_id: -1,
991            ownership: crate::protocol::to_generated_ownership_scope(ownership),
992            payload,
993        },
994    ))? {
995        crate::protocol::ProtocolFrame::SidecarRequest(request) => Ok(request.payload),
996        crate::protocol::ProtocolFrame::Request(_)
997        | crate::protocol::ProtocolFrame::Response(_)
998        | crate::protocol::ProtocolFrame::Event(_)
999        | crate::protocol::ProtocolFrame::SidecarResponse(_) => {
1000            Err(ProtocolCodecError::DeserializeFailure(String::from(
1001                "wire sidecar request payload converted to non-sidecar-request compatibility frame",
1002            )))
1003        }
1004    }
1005}
1006
1007pub(crate) fn sidecar_response_frame_to_compat(
1008    response: SidecarResponseFrame,
1009) -> Result<crate::protocol::SidecarResponseFrame, ProtocolCodecError> {
1010    match crate::protocol::from_generated_protocol_frame(ProtocolFrame::SidecarResponseFrame(
1011        response,
1012    ))? {
1013        crate::protocol::ProtocolFrame::SidecarResponse(response) => Ok(response),
1014        crate::protocol::ProtocolFrame::Request(_)
1015        | crate::protocol::ProtocolFrame::Response(_)
1016        | crate::protocol::ProtocolFrame::Event(_)
1017        | crate::protocol::ProtocolFrame::SidecarRequest(_) => {
1018            Err(ProtocolCodecError::DeserializeFailure(String::from(
1019                "wire sidecar response converted to non-sidecar-response compatibility frame",
1020            )))
1021        }
1022    }
1023}
1024
1025pub(crate) fn sidecar_response_frame_from_compat(
1026    response: crate::protocol::SidecarResponseFrame,
1027) -> Result<SidecarResponseFrame, ProtocolCodecError> {
1028    match crate::protocol::to_generated_protocol_frame(
1029        &crate::protocol::ProtocolFrame::SidecarResponse(response),
1030    )? {
1031        ProtocolFrame::SidecarResponseFrame(response) => Ok(response),
1032        ProtocolFrame::RequestFrame(_)
1033        | ProtocolFrame::ResponseFrame(_)
1034        | ProtocolFrame::EventFrame(_)
1035        | ProtocolFrame::SidecarRequestFrame(_) => {
1036            Err(ProtocolCodecError::SerializeFailure(String::from(
1037                "compatibility sidecar response converted to non-sidecar-response wire frame",
1038            )))
1039        }
1040    }
1041}
1042
1043pub(crate) fn dispatch_result_from_compat(
1044    result: crate::state::DispatchResult,
1045) -> Result<WireDispatchResult, ProtocolCodecError> {
1046    let response = match crate::protocol::to_generated_protocol_frame(
1047        &crate::protocol::ProtocolFrame::Response(result.response),
1048    )? {
1049        ProtocolFrame::ResponseFrame(response) => response,
1050        ProtocolFrame::RequestFrame(_)
1051        | ProtocolFrame::EventFrame(_)
1052        | ProtocolFrame::SidecarRequestFrame(_)
1053        | ProtocolFrame::SidecarResponseFrame(_) => {
1054            return Err(ProtocolCodecError::SerializeFailure(String::from(
1055                "compatibility dispatch response converted to non-response wire frame",
1056            )));
1057        }
1058    };
1059
1060    let events = result
1061        .events
1062        .into_iter()
1063        .map(|event| {
1064            match crate::protocol::to_generated_protocol_frame(
1065                &crate::protocol::ProtocolFrame::Event(event),
1066            )? {
1067                ProtocolFrame::EventFrame(event) => Ok(event),
1068                ProtocolFrame::RequestFrame(_)
1069                | ProtocolFrame::ResponseFrame(_)
1070                | ProtocolFrame::SidecarRequestFrame(_)
1071                | ProtocolFrame::SidecarResponseFrame(_) => {
1072                    Err(ProtocolCodecError::SerializeFailure(String::from(
1073                        "compatibility dispatch event converted to non-event wire frame",
1074                    )))
1075                }
1076            }
1077        })
1078        .collect::<Result<Vec<_>, _>>()?;
1079
1080    Ok(WireDispatchResult { response, events })
1081}
1082
1083fn validate_frame(frame: &ProtocolFrame) -> Result<(), ProtocolCodecError> {
1084    match frame {
1085        ProtocolFrame::RequestFrame(frame) => {
1086            validate_schema(&frame.schema)?;
1087            validate_request_id(frame.request_id)
1088        }
1089        ProtocolFrame::ResponseFrame(frame) => {
1090            validate_schema(&frame.schema)?;
1091            validate_request_id(frame.request_id)
1092        }
1093        ProtocolFrame::EventFrame(frame) => validate_schema(&frame.schema),
1094        ProtocolFrame::SidecarRequestFrame(frame) => {
1095            validate_schema(&frame.schema)?;
1096            validate_request_id(frame.request_id)
1097        }
1098        ProtocolFrame::SidecarResponseFrame(frame) => {
1099            validate_schema(&frame.schema)?;
1100            validate_request_id(frame.request_id)
1101        }
1102    }
1103}
1104
1105fn validate_schema(schema: &ProtocolSchema) -> Result<(), ProtocolCodecError> {
1106    if schema.name != PROTOCOL_NAME || schema.version != PROTOCOL_VERSION {
1107        return Err(ProtocolCodecError::UnsupportedSchema {
1108            name: schema.name.clone(),
1109            version: schema.version,
1110        });
1111    }
1112    Ok(())
1113}
1114
1115fn validate_request_id(request_id: RequestId) -> Result<(), ProtocolCodecError> {
1116    if request_id == 0 {
1117        return Err(ProtocolCodecError::InvalidRequestId);
1118    }
1119    Ok(())
1120}