ridl-cli 0.4.0

The `ridl` command-line toolchain: check, build, fmt, lock, diff, lsp, and mcp, over the shared compiler crates.
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
//! A contract expression built as a long operator chain draws a diagnostic in
//! `ridl check`, `ridl mcp` and `ridl lsp`, and aborts none of them
//! (driftsys/ridl#346).
//!
//! A chain of a few thousand terms once built a syntax tree as deep as its term
//! count, and the checker's recursion over that tree overflowed the stack. A
//! stack overflow aborts the process instead of unwinding, so every test here
//! runs the built binary as a child: a regression fails the assertion on the
//! child's exit or its missing answer, and does not abort this test binary.

use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdout, Command, ExitStatus, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc;
use std::time::{Duration, Instant};

use lsp_server::{Message, Notification, Request, RequestId};
use serde_json::json;

/// How long a spawned binary is given to answer or to exit. Generous, because
/// a loaded CI machine is slow; bounded, because `cargo test` has no per-test
/// timeout and a hung child would otherwise hang the whole run.
const TIMEOUT: Duration = Duration::from_secs(60);

/// The term count of every long chain below. `ridl mcp` checks on a 2 MiB
/// thread and aborted from about 860 terms in a debug build; `ridl check` and
/// `ridl lsp` check on the main thread and aborted below 5000.
const TERMS: usize = 5000;

/// A contract expression in a minimal ridl file that is otherwise clean: the
/// only diagnostics it draws come from the expression.
fn contract_file(expr: &str) -> String {
    format!(
        "package app\ntype N : integer [0..10]\ninterface I {{\n  command c(a: N) [ require {expr} ] @[..50ms]\n}}\n"
    )
}

/// A flat chain of `terms` operands under `op`, closed into a boolean where
/// the operator is not one already.
fn flat_chain(op: &str, terms: usize) -> String {
    match op {
        "||" | "&&" => vec!["a == 1"; terms].join(&format!(" {op} ")),
        "." => format!("a{} == 1", ".b".repeat(terms - 1)),
        _ => format!("{} == 1", vec!["a"; terms].join(&format!(" {op} "))),
    }
}

/// Every shape the tests send: one flat chain per chaining operator, plus two
/// shapes that stack chains — through parentheses, and through the precedence
/// levels — so that a bound charged per chain, not per tree, still fails.
fn shapes() -> Vec<(String, String)> {
    let mut shapes: Vec<(String, String)> = ["||", "&&", "+", "-", "*", "/", "%", "."]
        .into_iter()
        .map(|op| (format!("`{op}` chain"), flat_chain(op, TERMS)))
        .collect();
    let mut nested = "a".to_string();
    for _ in 0..120 {
        nested = format!("({nested}{})", " + a".repeat(119));
    }
    shapes.push(("nested chains".to_string(), format!("{nested} == 1")));
    shapes.push((
        "precedence ladder".to_string(),
        format!(
            "a{}{}{} == a{}{}",
            ".b".repeat(119),
            " * a".repeat(119),
            " + a".repeat(119),
            " && a == a".repeat(119),
            " || a == a".repeat(119),
        ),
    ));
    shapes
}

/// Waits for `child` to exit, killing it and failing the test if it does not
/// within [`TIMEOUT`].
fn wait_for_exit(child: &mut Child, what: &str) -> ExitStatus {
    let deadline = Instant::now() + TIMEOUT;
    loop {
        match child.try_wait().expect("poll the child") {
            Some(status) => return status,
            None if Instant::now() >= deadline => {
                let _ = child.kill();
                let _ = child.wait();
                panic!("{what} did not exit within {TIMEOUT:?}");
            }
            None => std::thread::sleep(Duration::from_millis(20)),
        }
    }
}

/// The child's stderr, for a failure message, once it has exited.
fn stderr_of(child: &mut Child) -> String {
    let mut text = String::new();
    if let Some(stderr) = child.stderr.as_mut() {
        let _ = std::io::Read::read_to_string(stderr, &mut text);
    }
    text
}

// ---------------------------------------------------------------------------
// `ridl check`
// ---------------------------------------------------------------------------

#[test]
fn ridl_check_reports_form_102_for_every_long_chain_shape() {
    let dir = TempDir::new("check");
    for (shape, expr) in shapes() {
        let path = dir.write("chain.ridl", &contract_file(&expr));
        let output = Command::new(env!("CARGO_BIN_EXE_ridl"))
            .args(["check", "--format", "json"])
            .arg(&path)
            .output()
            .expect("run ridl check");
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert_eq!(
            output.status.code(),
            Some(1),
            "the {shape} exits 1 with a diagnostic, not by a signal: {:?}\n{stderr}",
            output.status,
        );
        let diagnostics: serde_json::Value =
            serde_json::from_slice(&output.stdout).expect("stdout is JSON");
        let codes: Vec<&str> = diagnostics
            .as_array()
            .expect("a JSON array")
            .iter()
            .filter_map(|diagnostic| diagnostic["code"].as_str())
            .collect();
        // Exactly the parse refusal: the checker does not type-check the
        // truncated prefix, which would draw a false RIDL-306 on a sum whose
        // `== 1` the parser cut off.
        assert_eq!(
            codes,
            vec!["FORM-102"],
            "the {shape} draws FORM-102 and nothing else"
        );
    }
}

#[test]
fn ridl_check_accepts_a_long_chain_under_the_limit() {
    let dir = TempDir::new("check-short");
    for op in ["||", "&&", "+", "-", "*", "/", "%"] {
        let path = dir.write("chain.ridl", &contract_file(&flat_chain(op, 100)));
        let output = Command::new(env!("CARGO_BIN_EXE_ridl"))
            .arg("check")
            .arg(&path)
            .output()
            .expect("run ridl check");
        assert_eq!(
            output.status.code(),
            Some(0),
            "a 100-term `{op}` chain is clean: {}",
            String::from_utf8_lossy(&output.stderr),
        );
    }
}

// ---------------------------------------------------------------------------
// `ridl mcp`
// ---------------------------------------------------------------------------

/// Reads newline-delimited JSON-RPC messages off `stdout` on a worker thread.
fn read_json_lines(stdout: ChildStdout) -> mpsc::Receiver<serde_json::Value> {
    let (sender, receiver) = mpsc::channel();
    std::thread::spawn(move || {
        let mut stdout = BufReader::new(stdout);
        let mut line = String::new();
        loop {
            line.clear();
            match stdout.read_line(&mut line) {
                Ok(0) | Err(_) => break,
                Ok(_) => {
                    let Ok(value) = serde_json::from_str(&line) else {
                        continue;
                    };
                    if sender.send(value).is_err() {
                        break;
                    }
                }
            }
        }
    });
    receiver
}

/// Waits for the JSON-RPC response to `id`, skipping anything else.
fn json_response(messages: &mpsc::Receiver<serde_json::Value>, id: i64) -> serde_json::Value {
    let deadline = Instant::now() + TIMEOUT;
    loop {
        let remaining = deadline.saturating_duration_since(Instant::now());
        match messages.recv_timeout(remaining) {
            Ok(message) if message["id"] == id => return message,
            Ok(_) => {}
            Err(err) => panic!("no response to request {id} within {TIMEOUT:?}: {err}"),
        }
    }
}

#[test]
fn ridl_mcp_reports_form_102_for_every_long_chain_shape_and_keeps_serving() {
    let mut child = Command::new(env!("CARGO_BIN_EXE_ridl"))
        .arg("mcp")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn ridl mcp");
    let mut stdin = child.stdin.take().expect("piped stdin");
    let messages = read_json_lines(child.stdout.take().expect("piped stdout"));

    let initialize = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "initialize",
        "params": {
            "protocolVersion": "2025-06-18",
            "capabilities": {},
            "clientInfo": { "name": "deep-expression-test", "version": "0.0.0" }
        }
    });
    writeln!(stdin, "{initialize}").expect("write initialize");
    json_response(&messages, 1);
    writeln!(
        stdin,
        "{}",
        json!({ "jsonrpc": "2.0", "method": "notifications/initialized" })
    )
    .expect("write initialized");

    for (id, (shape, expr)) in (2..).zip(shapes()) {
        let call = json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": "tools/call",
            "params": {
                "name": "ridl_check",
                "arguments": { "source": contract_file(&expr), "profile": "ridl" }
            }
        });
        writeln!(stdin, "{call}").expect("write tools/call");
        let deadline = Instant::now() + TIMEOUT;
        let response = loop {
            let remaining = deadline.saturating_duration_since(Instant::now());
            match messages.recv_timeout(remaining) {
                Ok(message) if message["id"] == id => break message,
                Ok(_) => {}
                Err(_) => {
                    let _ = child.kill();
                    let status = child.wait().expect("reap ridl mcp");
                    panic!(
                        "ridl mcp gave no answer for the {shape} (status {status:?}):\n{}",
                        stderr_of(&mut child)
                    );
                }
            }
        };
        let text = response["result"]["content"][0]["text"]
            .as_str()
            .unwrap_or_else(|| panic!("a text result for the {shape}: {response}"));
        let output: serde_json::Value = serde_json::from_str(text).expect("the tool returns JSON");
        let codes: Vec<&str> = output["diagnostics"]
            .as_array()
            .unwrap_or_else(|| panic!("a diagnostics array: {output}"))
            .iter()
            .filter_map(|diagnostic| diagnostic["code"].as_str())
            .collect();
        assert!(
            codes.contains(&"FORM-102"),
            "the {shape} draws FORM-102: {codes:?}"
        );
    }

    drop(stdin);
    let status = wait_for_exit(&mut child, "ridl mcp");
    assert_eq!(status.code(), Some(0), "{}", stderr_of(&mut child));
}

// ---------------------------------------------------------------------------
// `ridl lsp`
// ---------------------------------------------------------------------------

/// Reads LSP messages off `stdout` on a worker thread.
fn read_messages(stdout: ChildStdout) -> mpsc::Receiver<Message> {
    let (sender, receiver) = mpsc::channel();
    std::thread::spawn(move || {
        let mut stdout = BufReader::new(stdout);
        while let Ok(Some(message)) = Message::read(&mut stdout) {
            if sender.send(message).is_err() {
                break;
            }
        }
    });
    receiver
}

/// A `file://` URI for `path`.
fn file_uri(path: &Path) -> String {
    format!("file://{}", path.display())
}

#[test]
fn ridl_lsp_publishes_form_102_for_every_long_chain_shape_and_keeps_serving() {
    let dir = TempDir::new("lsp");
    dir.write(
        "ridl.toml",
        "[package]\nname = \"app\"\nversion = \"0.1.0\"\n",
    );
    let mut child = Command::new(env!("CARGO_BIN_EXE_ridl"))
        .arg("lsp")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn ridl lsp");
    let mut stdin = child.stdin.take().expect("piped stdin");
    let messages = read_messages(child.stdout.take().expect("piped stdout"));

    let initialize = RequestId::from(1);
    Message::Request(Request::new(
        initialize.clone(),
        "initialize".to_string(),
        json!({ "capabilities": {}, "rootUri": file_uri(&dir.0) }),
    ))
    .write(&mut stdin)
    .expect("write initialize");
    let deadline = Instant::now() + TIMEOUT;
    loop {
        let remaining = deadline.saturating_duration_since(Instant::now());
        match messages.recv_timeout(remaining) {
            Ok(Message::Response(response)) if response.id == initialize => break,
            Ok(_) => {}
            Err(err) => panic!("no initialize response within {TIMEOUT:?}: {err}"),
        }
    }
    Message::Notification(Notification::new("initialized".to_string(), json!({})))
        .write(&mut stdin)
        .expect("write initialized");

    for (index, (shape, expr)) in shapes().into_iter().enumerate() {
        let path = dir.write(&format!("chain{index}.ridl"), &contract_file(&expr));
        let uri = file_uri(&path);
        Message::Notification(Notification::new(
            "textDocument/didOpen".to_string(),
            json!({
                "textDocument": {
                    "uri": uri,
                    "languageId": "ridl",
                    "version": 1,
                    "text": contract_file(&expr),
                }
            }),
        ))
        .write(&mut stdin)
        .expect("write didOpen");
        let deadline = Instant::now() + TIMEOUT;
        let codes: Vec<String> = loop {
            let remaining = deadline.saturating_duration_since(Instant::now());
            match messages.recv_timeout(remaining) {
                Ok(Message::Notification(notification))
                    if notification.method == "textDocument/publishDiagnostics"
                        && notification.params["uri"] == uri =>
                {
                    break notification.params["diagnostics"]
                        .as_array()
                        .expect("a diagnostics array")
                        .iter()
                        .filter_map(|diagnostic| diagnostic["code"].as_str())
                        .map(str::to_string)
                        .collect();
                }
                Ok(_) => {}
                Err(_) => {
                    let _ = child.kill();
                    let status = child.wait().expect("reap ridl lsp");
                    panic!(
                        "ridl lsp published nothing for the {shape} (status {status:?}):\n{}",
                        stderr_of(&mut child)
                    );
                }
            }
        };
        assert!(
            codes.iter().any(|code| code == "FORM-102"),
            "the {shape} draws FORM-102: {codes:?}"
        );
    }

    let shutdown = RequestId::from(2);
    Message::Request(Request::new(
        shutdown.clone(),
        "shutdown".to_string(),
        json!(null),
    ))
    .write(&mut stdin)
    .expect("write shutdown");
    let deadline = Instant::now() + TIMEOUT;
    loop {
        let remaining = deadline.saturating_duration_since(Instant::now());
        match messages.recv_timeout(remaining) {
            Ok(Message::Response(response)) if response.id == shutdown => break,
            Ok(_) => {}
            Err(err) => panic!("no shutdown response within {TIMEOUT:?}: {err}"),
        }
    }
    Message::Notification(Notification::new("exit".to_string(), json!(null)))
        .write(&mut stdin)
        .expect("write exit");
    drop(stdin);
    let status = wait_for_exit(&mut child, "ridl lsp");
    assert_eq!(status.code(), Some(0), "{}", stderr_of(&mut child));
}

// ---------------------------------------------------------------------------
// Scratch files
// ---------------------------------------------------------------------------

/// A unique directory under the system temp dir, removed on drop.
struct TempDir(PathBuf);

impl TempDir {
    fn new(label: &str) -> Self {
        static COUNTER: AtomicUsize = AtomicUsize::new(0);
        let mut path = std::env::temp_dir();
        path.push(format!(
            "ridl-deep-expression-{label}-{}-{}",
            std::process::id(),
            COUNTER.fetch_add(1, Ordering::SeqCst),
        ));
        std::fs::create_dir_all(&path).expect("create the temp dir");
        // Canonical, so the `file://` URIs the language server is sent match
        // the paths it resolves (macOS puts the temp dir behind a symlink).
        Self(path.canonicalize().expect("canonicalize the temp dir"))
    }

    fn write(&self, relative: &str, text: &str) -> PathBuf {
        let path = self.0.join(relative);
        std::fs::write(&path, text).expect("write the fixture file");
        path
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}