coralstack-cmd-ipc 0.2.0

Inter-Process Communication library for running typed Commands across processes and services
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
//! Wire protocol for the command registry.
//!
//! This module defines the seven [`Message`] variants exchanged between
//! registries. The JSON representation is byte-identical to the TypeScript
//! implementation's `CommandMessage` union (see
//! `packages/cmd-ipc/src/registry/command-message-schemas.ts`) so that a
//! Rust process and a Node.js process can talk to each other over any
//! channel that carries JSON.

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;
use uuid::Uuid;

use crate::error::{ExecuteErrorCode, RegisterErrorCode};

/// A unique message identifier.
///
/// Matches the TypeScript type alias `MessageID = string` — UUIDs are
/// serialized as hyphenated strings.
pub type MessageId = Uuid;

/// Description of a command as advertised across a channel.
///
/// Mirrors `CommandDefinitionBaseSchema` in the TypeScript protocol.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct CommandDef {
    pub id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub schema: Option<CommandSchema>,
}

/// A request/response JSON-Schema pair attached to a [`CommandDef`].
///
/// Both slots are optional; absent means "no payload expected" (the
/// TypeScript equivalent is `v.void()`). When populated the value is a
/// raw JSON Schema; the registry normalizes incoming schemas so wire
/// representations remain language-agnostic.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct CommandSchema {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub request: Option<Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub response: Option<Value>,
}

impl CommandSchema {
    /// Empty schema — both slots unset. Equivalent to advertising a
    /// command with `request: None, response: None` (the void/void
    /// shape). Use when the command takes no payload and returns none.
    pub fn empty() -> Self {
        Self {
            request: None,
            response: None,
        }
    }

    /// Maximally permissive schema — both request and response declared
    /// as open objects with `additionalProperties: true`. Useful for
    /// runtime plugins whose payload shape isn't known at advertise
    /// time (e.g. Flow's QuickJS `SourceChannel` when a plugin exports
    /// an `any → any` function).
    ///
    /// Prefer a real schema when you can produce one — consumers use it
    /// for validation, MCP tool schemas, and generated TS clients.
    pub fn permissive() -> Self {
        Self {
            request: Some(serde_json::json!({
                "type": "object",
                "additionalProperties": true,
            })),
            response: Some(serde_json::json!({
                "type": "object",
                "additionalProperties": true,
            })),
        }
    }

    /// Builder: set only the request schema (leaves response unset).
    pub fn with_request(mut self, schema: Value) -> Self {
        self.request = Some(schema);
        self
    }

    /// Builder: set only the response schema (leaves request unset).
    pub fn with_response(mut self, schema: Value) -> Self {
        self.response = Some(schema);
        self
    }
}

/// Body of an execute-command error response.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ExecuteError {
    pub code: ExecuteErrorCode,
    pub message: String,
}

/// Zero-sized marker that (de)serializes as the JSON literal `true`.
///
/// Used to distinguish the `ok: true` / `ok: false` variants of
/// [`RegisterResult`] and [`ExecuteResult`] while keeping them as proper
/// Rust enums rather than structs with nullable fields.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct True;

impl Serialize for True {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_bool(true)
    }
}

impl<'de> Deserialize<'de> for True {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        match bool::deserialize(d)? {
            true => Ok(True),
            false => Err(serde::de::Error::custom("expected literal `true`")),
        }
    }
}

/// Zero-sized marker that (de)serializes as the JSON literal `false`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct False;

impl Serialize for False {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_bool(false)
    }
}

impl<'de> Deserialize<'de> for False {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        match bool::deserialize(d)? {
            false => Ok(False),
            true => Err(serde::de::Error::custom("expected literal `false`")),
        }
    }
}

/// Body of a `register.command.response` message.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum RegisterResult {
    Ok { ok: True },
    Err { ok: False, error: RegisterErrorCode },
}

/// Body of an `execute.command.response` message.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum ExecuteResult {
    Ok {
        ok: True,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        result: Option<Value>,
    },
    Err {
        ok: False,
        error: ExecuteError,
    },
}

/// Discriminated union of every message type carried on a channel.
///
/// The `type` tag strings are the same dotted identifiers used by the
/// TypeScript `MessageType` enum.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(tag = "type")]
pub enum Message {
    #[serde(rename = "register.command.request")]
    RegisterCommandRequest { id: MessageId, command: CommandDef },

    #[serde(rename = "register.command.response")]
    RegisterCommandResponse {
        id: MessageId,
        thid: MessageId,
        response: RegisterResult,
    },

    #[serde(rename = "list.commands.request")]
    ListCommandsRequest { id: MessageId },

    #[serde(rename = "list.commands.response")]
    ListCommandsResponse {
        id: MessageId,
        thid: MessageId,
        commands: Vec<CommandDef>,
    },

    #[serde(rename = "execute.command.request")]
    ExecuteCommandRequest {
        id: MessageId,
        #[serde(rename = "commandId")]
        command_id: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        request: Option<Value>,
    },

    #[serde(rename = "execute.command.response")]
    ExecuteCommandResponse {
        id: MessageId,
        thid: MessageId,
        response: ExecuteResult,
    },

    #[serde(rename = "event")]
    Event {
        id: MessageId,
        #[serde(rename = "eventId")]
        event_id: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        payload: Option<Value>,
    },
}

impl Message {
    /// Returns the message's `id` field.
    pub fn id(&self) -> MessageId {
        match self {
            Self::RegisterCommandRequest { id, .. }
            | Self::RegisterCommandResponse { id, .. }
            | Self::ListCommandsRequest { id, .. }
            | Self::ListCommandsResponse { id, .. }
            | Self::ExecuteCommandRequest { id, .. }
            | Self::ExecuteCommandResponse { id, .. }
            | Self::Event { id, .. } => *id,
        }
    }

    /// Returns the `thid` field for messages that carry one.
    pub fn thid(&self) -> Option<MessageId> {
        match self {
            Self::RegisterCommandResponse { thid, .. }
            | Self::ListCommandsResponse { thid, .. }
            | Self::ExecuteCommandResponse { thid, .. } => Some(*thid),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn uid(s: &str) -> Uuid {
        Uuid::parse_str(s).unwrap()
    }

    /// Every fixture below is paired JSON that the TypeScript library
    /// produces (or accepts). Round-tripping through `Message` must leave
    /// the semantic JSON tree unchanged.
    fn check_roundtrip(msg: &Message, expected: Value) {
        let serialized = serde_json::to_value(msg).unwrap();
        assert_eq!(
            serialized, expected,
            "serialized form does not match fixture"
        );
        let parsed: Message = serde_json::from_value(expected.clone()).unwrap();
        assert_eq!(&parsed, msg, "fixture did not deserialize back to input");
    }

    #[test]
    fn register_command_request_roundtrip() {
        let msg = Message::RegisterCommandRequest {
            id: uid("11111111-1111-1111-1111-111111111111"),
            command: CommandDef {
                id: "math.add".to_string(),
                description: Some("Adds two numbers".to_string()),
                schema: Some(CommandSchema {
                    request: Some(json!({
                        "type": "object",
                        "properties": { "a": { "type": "number" }, "b": { "type": "number" } },
                        "required": ["a", "b"]
                    })),
                    response: Some(json!({ "type": "number" })),
                }),
            },
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "register.command.request",
                "id": "11111111-1111-1111-1111-111111111111",
                "command": {
                    "id": "math.add",
                    "description": "Adds two numbers",
                    "schema": {
                        "request": {
                            "type": "object",
                            "properties": { "a": { "type": "number" }, "b": { "type": "number" } },
                            "required": ["a", "b"]
                        },
                        "response": { "type": "number" }
                    }
                }
            }),
        );
    }

    #[test]
    fn register_command_response_ok_roundtrip() {
        let msg = Message::RegisterCommandResponse {
            id: uid("22222222-2222-2222-2222-222222222222"),
            thid: uid("11111111-1111-1111-1111-111111111111"),
            response: RegisterResult::Ok { ok: True },
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "register.command.response",
                "id": "22222222-2222-2222-2222-222222222222",
                "thid": "11111111-1111-1111-1111-111111111111",
                "response": { "ok": true }
            }),
        );
    }

    #[test]
    fn register_command_response_err_roundtrip() {
        let msg = Message::RegisterCommandResponse {
            id: uid("22222222-2222-2222-2222-222222222222"),
            thid: uid("11111111-1111-1111-1111-111111111111"),
            response: RegisterResult::Err {
                ok: False,
                error: RegisterErrorCode::DuplicateCommand,
            },
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "register.command.response",
                "id": "22222222-2222-2222-2222-222222222222",
                "thid": "11111111-1111-1111-1111-111111111111",
                "response": { "ok": false, "error": "duplicate_command" }
            }),
        );
    }

    #[test]
    fn list_commands_request_roundtrip() {
        let msg = Message::ListCommandsRequest {
            id: uid("33333333-3333-3333-3333-333333333333"),
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "list.commands.request",
                "id": "33333333-3333-3333-3333-333333333333"
            }),
        );
    }

    #[test]
    fn list_commands_response_roundtrip() {
        let msg = Message::ListCommandsResponse {
            id: uid("44444444-4444-4444-4444-444444444444"),
            thid: uid("33333333-3333-3333-3333-333333333333"),
            commands: vec![CommandDef {
                id: "user.create".to_string(),
                description: None,
                schema: None,
            }],
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "list.commands.response",
                "id": "44444444-4444-4444-4444-444444444444",
                "thid": "33333333-3333-3333-3333-333333333333",
                "commands": [{ "id": "user.create" }]
            }),
        );
    }

    #[test]
    fn execute_command_request_roundtrip() {
        let msg = Message::ExecuteCommandRequest {
            id: uid("55555555-5555-5555-5555-555555555555"),
            command_id: "math.add".to_string(),
            request: Some(json!({ "a": 1, "b": 2 })),
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "execute.command.request",
                "id": "55555555-5555-5555-5555-555555555555",
                "commandId": "math.add",
                "request": { "a": 1, "b": 2 }
            }),
        );
    }

    #[test]
    fn execute_command_request_no_payload() {
        let msg = Message::ExecuteCommandRequest {
            id: uid("55555555-5555-5555-5555-555555555555"),
            command_id: "system.ping".to_string(),
            request: None,
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "execute.command.request",
                "id": "55555555-5555-5555-5555-555555555555",
                "commandId": "system.ping"
            }),
        );
    }

    #[test]
    fn execute_command_response_ok_roundtrip() {
        let msg = Message::ExecuteCommandResponse {
            id: uid("66666666-6666-6666-6666-666666666666"),
            thid: uid("55555555-5555-5555-5555-555555555555"),
            response: ExecuteResult::Ok {
                ok: True,
                result: Some(json!(3)),
            },
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "execute.command.response",
                "id": "66666666-6666-6666-6666-666666666666",
                "thid": "55555555-5555-5555-5555-555555555555",
                "response": { "ok": true, "result": 3 }
            }),
        );
    }

    #[test]
    fn execute_command_response_err_roundtrip() {
        let msg = Message::ExecuteCommandResponse {
            id: uid("66666666-6666-6666-6666-666666666666"),
            thid: uid("55555555-5555-5555-5555-555555555555"),
            response: ExecuteResult::Err {
                ok: False,
                error: ExecuteError {
                    code: ExecuteErrorCode::NotFound,
                    message: "no such command".to_string(),
                },
            },
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "execute.command.response",
                "id": "66666666-6666-6666-6666-666666666666",
                "thid": "55555555-5555-5555-5555-555555555555",
                "response": {
                    "ok": false,
                    "error": { "code": "not_found", "message": "no such command" }
                }
            }),
        );
    }

    #[test]
    fn event_roundtrip() {
        let msg = Message::Event {
            id: uid("77777777-7777-7777-7777-777777777777"),
            event_id: "user.created".to_string(),
            payload: Some(json!({ "userId": "u1" })),
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "event",
                "id": "77777777-7777-7777-7777-777777777777",
                "eventId": "user.created",
                "payload": { "userId": "u1" }
            }),
        );
    }

    #[test]
    fn event_private_prefix_preserved() {
        // Private events (leading underscore) are still valid wire-level;
        // privacy is enforced by the registry, not the message schema.
        let msg = Message::Event {
            id: uid("77777777-7777-7777-7777-777777777777"),
            event_id: "_internal.tick".to_string(),
            payload: None,
        };
        check_roundtrip(
            &msg,
            json!({
                "type": "event",
                "id": "77777777-7777-7777-7777-777777777777",
                "eventId": "_internal.tick"
            }),
        );
    }

    #[test]
    fn unknown_type_rejected() {
        let bad =
            json!({ "type": "not.a.real.type", "id": "00000000-0000-0000-0000-000000000000" });
        assert!(serde_json::from_value::<Message>(bad).is_err());
    }

    #[test]
    fn register_result_err_requires_ok_false() {
        // `ok: true` with an `error` field must not parse as Err.
        let bad = json!({ "ok": true, "error": "duplicate_command" });
        let parsed: RegisterResult = serde_json::from_value(bad).unwrap();
        assert!(matches!(parsed, RegisterResult::Ok { .. }));
    }
}