ctoolbox/workspace/ipc/
types.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3use std::time::Duration;
4use uuid::Uuid;
5
6/// Monotonic request identifier for RPC calls on a connection.
7pub type RequestId = u64;
8
9/// Identifier for a logical stream.
10pub type StreamId = u64;
11
12/// Opaque identifier for a process managed by the workspace.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub struct ProcessId(pub Uuid);
15
16impl Default for ProcessId {
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl ProcessId {
23    /// Generate a new random `ProcessId`.
24    pub fn new() -> Self {
25        Self(Uuid::new_v4())
26    }
27}
28
29impl fmt::Display for ProcessId {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        self.0.fmt(f)
32    }
33}
34
35/// Identifier for a connection (control plane) bound to a process/capability set.
36#[derive(
37    Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default,
38)]
39pub struct ConnectionId(pub Uuid);
40
41impl fmt::Display for ConnectionId {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        self.0.fmt(f)
44    }
45}
46
47/// Opaque identifier for a shared blob.
48#[derive(
49    Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default,
50)]
51pub struct BlobId(pub Uuid);
52
53impl fmt::Display for BlobId {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        self.0.fmt(f)
56    }
57}
58
59/// Kind of child process.
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
61pub enum ChildKind {
62    // External tools (probably should be for testing only)
63    External,
64    Formats,
65    /// IO (also currently includes the web server).
66    Io,
67    Network,
68    /// Renderer process for document display.
69    Renderer,
70    Storage,
71}
72
73/// A blob token that authorizes mapping/reading/writing to a blob on the data plane.
74#[derive(
75    Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default,
76)]
77pub struct BlobToken {
78    pub id: BlobId,
79    pub size: u64,
80    /// Optional hint for cleanup or lifetime control.
81    pub lease_ms: Option<u64>,
82}
83
84/// Create a span that identifies a workspace-managed process.
85///
86/// This is intended for attaching `process_id` to log lines emitted while
87/// handling process-scoped work.
88#[must_use]
89pub fn ipc_process_span(process_id: ProcessId) -> tracing::Span {
90    tracing::info_span!("ipc.process", process_id = %process_id)
91}
92
93/// Create a span that identifies an IPC connection.
94///
95/// This is intended for attaching `conn_id` to log lines emitted while
96/// servicing a connection.
97#[must_use]
98pub fn ipc_connection_span(connection_id: ConnectionId) -> tracing::Span {
99    tracing::info_span!("ipc.connection", conn_id = %connection_id)
100}
101
102/// Create a span that identifies a single RPC request.
103///
104/// This is intentionally independent of protocol types (only uses `RequestId`).
105#[must_use]
106pub fn ipc_request_span(request_id: RequestId) -> tracing::Span {
107    tracing::info_span!("ipc.request", request_id)
108}
109
110/// Create a span that identifies a logical stream.
111#[must_use]
112pub fn ipc_stream_span(stream_id: StreamId) -> tracing::Span {
113    tracing::info_span!("ipc.stream", stream_id)
114}
115
116/// Platform-neutral timeout settings.
117#[derive(Debug, Clone, Serialize, Deserialize)]
118pub struct Timeouts {
119    pub handshake: Duration,
120    pub request: Duration,
121    pub graceful_shutdown: Duration,
122    pub heartbeat_interval: Duration,
123    pub heartbeat_miss_tolerance: u32,
124}