arf-console 0.3.2

A cross-platform R console written in Rust
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
//! IPC client for the `arf ipc` subcommand.
//!
//! Uses synchronous std I/O — no tokio runtime needed on the client side.
//!
//! All commands output JSON to stdout (pretty-printed when stdout is a
//! terminal, compact when piped). Errors are written to stderr as JSON
//! with `{"error": {"code": "ERROR_CODE", "message": "...", "hint": "...", "data": ...}}`.

use crate::ipc::protocol::JsonRpcResponse;
use crate::ipc::session::{find_session, list_sessions};
use anyhow::{Context, Result};

/// Default transport timeout for client-side socket reads (5 minutes).
const DEFAULT_TRANSPORT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(300);

// ── Exit codes ───────────────────────────────────────────────────────
//
// 0 = success
// 2 = IPC transport error
// 3 = session resolution error
// 4 = JSON-RPC protocol error

/// IPC transport error (socket connection failed, timeout, etc.).
const EXIT_TRANSPORT: i32 = 2;
/// Session resolution error (no session found, ambiguous PID, etc.).
const EXIT_SESSION: i32 = 3;
/// JSON-RPC protocol error (R_BUSY, INPUT_ALREADY_PENDING, etc.).
const EXIT_PROTOCOL: i32 = 4;

// ── Output helpers ───────────────────────────────────────────────────

/// Serialize a JSON value to a string, using pretty-printing when the
/// given stream is a terminal.
fn format_json(value: &serde_json::Value, is_tty: bool) -> String {
    if is_tty {
        serde_json::to_string_pretty(value).expect("format_json: serialization failed")
    } else {
        serde_json::to_string(value).expect("format_json: serialization failed")
    }
}

/// Print a JSON value to stdout. Pretty-prints when stdout is a
/// terminal, compact when piped.
fn print_json(value: &serde_json::Value) {
    let is_tty = std::io::IsTerminal::is_terminal(&std::io::stdout());
    println!("{}", format_json(value, is_tty));
}

/// Print a structured error to stderr as JSON and exit with the given code.
///
/// The error format is (all fields are always present):
/// `{"error": {"code": "ERROR_CODE", "message": "...", "hint": ..., "data": ...}}`
///
/// Error codes are strings for stable matching by consumers:
/// - JSON-RPC codes (negative integers) are mapped to descriptive names
///   (e.g. `"R_BUSY"`, `"PARSE_ERROR"`)
/// - Application codes use uppercase snake_case (e.g. `"TRANSPORT_ERROR"`,
///   `"SESSION_NOT_FOUND"`)
///
/// NOTE: This function calls `std::process::exit()`, so it cannot be
/// tested in-process. Error paths are covered by integration tests in
/// `headless_tests.rs` which run `arf ipc` as a subprocess.
fn exit_error(
    exit_code: i32,
    code: &str,
    message: &str,
    hint: Option<&str>,
    data: Option<&serde_json::Value>,
) -> ! {
    let error = serde_json::json!({
        "error": {
            "code": code,
            "message": message,
            "hint": hint,
            "data": data,
        }
    });
    let is_tty = std::io::IsTerminal::is_terminal(&std::io::stderr());
    eprintln!("{}", format_json(&error, is_tty));
    std::process::exit(exit_code);
}

/// Map a JSON-RPC numeric error code to a string identifier and hint.
fn rpc_error_info(code: i32) -> (&'static str, Option<&'static str>) {
    use crate::ipc::protocol::*;
    match code {
        R_BUSY => (
            "R_BUSY",
            Some(
                "R is executing code. Wait for it to finish, or use \
                 'arf ipc session' to check status.",
            ),
        ),
        R_NOT_AT_PROMPT => (
            "R_NOT_AT_PROMPT",
            Some(
                "R is in browser/debug mode or a menu. If triggered by \
                 browser(), use 'arf ipc send \"Q\"' to exit the debugger.",
            ),
        ),
        INPUT_ALREADY_PENDING => (
            "INPUT_ALREADY_PENDING",
            Some(
                "Another IPC input is already queued. Wait for it to \
                 be processed before sending more.",
            ),
        ),
        USER_IS_TYPING => (
            "USER_IS_TYPING",
            Some(
                "The user is typing in the REPL. Check error.data.buffer \
                 for the current input. Wait for them to finish or clear \
                 their input.",
            ),
        ),
        PARSE_ERROR => ("PARSE_ERROR", None),
        INVALID_REQUEST => ("INVALID_REQUEST", None),
        METHOD_NOT_FOUND => ("METHOD_NOT_FOUND", None),
        INVALID_PARAMS => ("INVALID_PARAMS", None),
        INTERNAL_ERROR => ("INTERNAL_ERROR", None),
        _ => ("PROTOCOL_ERROR", None),
    }
}

/// Handle a JSON-RPC response: print result or exit with structured error.
///
/// This is the common response handler for all IPC commands that send a
/// JSON-RPC request and print the result. On success, prints the result
/// JSON to stdout. On error, prints a structured error to stderr and exits.
fn handle_response(response: JsonRpcResponse) {
    if let Some(ref error) = response.error {
        let (code, hint) = rpc_error_info(error.code);
        exit_error(
            EXIT_PROTOCOL,
            code,
            &error.message,
            hint,
            error.data.as_ref(),
        );
    }

    match response.result {
        Some(result) => print_json(&result),
        None => {
            // JSON-RPC 2.0 requires exactly one of `result` or `error` to be
            // present. Reaching here indicates a server-side bug.
            exit_error(
                EXIT_PROTOCOL,
                "EMPTY_RESPONSE",
                "Server returned a response with neither result nor error (possible server bug)",
                None,
                None,
            );
        }
    }
}

/// List all active arf sessions as JSON.
///
/// Uses `serde_json::to_value` on `SessionInfo` (which derives Serialize)
/// so that new fields are automatically included without manual sync.
pub fn cmd_list() {
    let sessions = list_sessions();

    let sessions_json: Vec<serde_json::Value> = sessions
        .iter()
        .map(|s| {
            serde_json::to_value(s).unwrap_or_else(|e| {
                exit_error(
                    EXIT_PROTOCOL,
                    "SERIALIZATION_ERROR",
                    &format!("Failed to serialize session info: {e}"),
                    Some("This is likely a bug in arf."),
                    None,
                );
            })
        })
        .collect();

    print_json(&serde_json::json!({ "sessions": sessions_json }));
}

/// Resolve a session or exit with a structured JSON error.
fn resolve_session(pid: Option<u32>) -> crate::ipc::session::SessionInfo {
    match find_session(pid) {
        Some(session) => session,
        None => {
            if let Some(p) = pid {
                exit_error(
                    EXIT_SESSION,
                    "SESSION_NOT_FOUND",
                    &format!("No active arf session with PID {p}"),
                    Some("Use 'arf ipc list' to see active sessions."),
                    None,
                );
            } else {
                let sessions = list_sessions();
                if sessions.is_empty() {
                    exit_error(
                        EXIT_SESSION,
                        "SESSION_NOT_FOUND",
                        "No active arf sessions found",
                        Some("Start arf with --with-ipc to enable IPC."),
                        None,
                    );
                } else {
                    exit_error(
                        EXIT_SESSION,
                        "SESSION_AMBIGUOUS",
                        "Multiple arf sessions running",
                        Some(
                            "Specify --pid to select one. Use 'arf ipc list' \
                             to see active sessions.",
                        ),
                        None,
                    );
                }
            }
        }
    }
}

/// Evaluate R code in a running arf session.
///
/// On success, prints the structured result as JSON to stdout.
/// R evaluation errors are returned as part of the JSON result (exit 0)
/// — they are a normal response, not an IPC failure.
pub fn cmd_eval(code: &str, pid: Option<u32>, visible: bool, timeout_ms: Option<u64>) {
    let session = resolve_session(pid);

    let mut params = serde_json::json!({ "code": code, "visible": visible });
    if let Some(ms) = timeout_ms {
        params["timeout_ms"] = serde_json::json!(ms);
    }

    let request = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "evaluate",
        "params": params
    });

    // Client transport timeout: match the server-side timeout with a small buffer
    // so the server can respond with a proper timeout error before the client gives up.
    let transport_timeout = match timeout_ms {
        Some(ms) => std::time::Duration::from_millis(ms.saturating_add(5000)),
        None => DEFAULT_TRANSPORT_TIMEOUT + std::time::Duration::from_secs(5),
    };

    let response = send_request(&session.socket_path, &request, transport_timeout);
    handle_response(response);
}

/// Send code as user input to a running arf session.
pub fn cmd_send(code: &str, pid: Option<u32>) {
    let session = resolve_session(pid);

    let request = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "user_input",
        "params": { "code": code }
    });

    let response = send_request(&session.socket_path, &request, DEFAULT_TRANSPORT_TIMEOUT);
    handle_response(response);
}

/// Shut down a running arf headless session.
pub fn cmd_shutdown(pid: Option<u32>) {
    let session = resolve_session(pid);

    let request = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "shutdown",
        "params": {}
    });

    let response = send_request(&session.socket_path, &request, DEFAULT_TRANSPORT_TIMEOUT);
    handle_response(response);
}

/// Get session information as JSON via the `session` IPC method.
///
/// Output is pretty-printed when stdout is a terminal, compact when piped.
pub fn cmd_session(pid: Option<u32>) {
    let session = resolve_session(pid);

    let request = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "session",
        "params": {}
    });

    // Session info collection is lightweight; use a short transport timeout.
    let transport_timeout = std::time::Duration::from_secs(15);

    let response = send_request(&session.socket_path, &request, transport_timeout);
    handle_response(response);
}

/// Query command history via the `history` IPC method.
///
/// Output is pretty-printed when stdout is a terminal, compact when piped.
pub fn cmd_history(
    pid: Option<u32>,
    limit: i64,
    all_sessions: bool,
    cwd: Option<&str>,
    grep: Option<&str>,
    since: Option<&str>,
) {
    let session = resolve_session(pid);

    let mut params = serde_json::json!({
        "limit": limit,
        "all_sessions": all_sessions,
    });
    if let Some(cwd) = cwd {
        params["cwd"] = serde_json::Value::String(cwd.to_string());
    }
    if let Some(grep) = grep {
        params["grep"] = serde_json::Value::String(grep.to_string());
    }
    if let Some(since) = since {
        params["since"] = serde_json::Value::String(since.to_string());
    }

    let request = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "history",
        "params": params
    });

    let transport_timeout = std::time::Duration::from_secs(15);
    let response = send_request(&session.socket_path, &request, transport_timeout);
    handle_response(response);
}

/// Send a JSON-RPC request to the socket and return the response.
///
/// On transport errors (connection refused, timeout, etc.), exits with
/// a structured JSON error on stderr.
fn send_request(
    socket_path: &str,
    request: &serde_json::Value,
    timeout: std::time::Duration,
) -> JsonRpcResponse {
    match send_request_inner(socket_path, request, timeout) {
        Ok(response) => response,
        Err(e) => {
            // Distinguish protocol-level errors (malformed JSON-RPC responses)
            // from transport-level errors (connection refused, timeout, etc.)
            // so that exit codes match the documented categories.
            // Walk the full anyhow error chain to find serde_json::Error even
            // when it's wrapped by context().
            let is_protocol = e
                .chain()
                .any(|cause| cause.downcast_ref::<serde_json::Error>().is_some());
            if is_protocol {
                exit_error(
                    EXIT_PROTOCOL,
                    "PROTOCOL_ERROR",
                    &format!("{e:#}"),
                    Some("Received an invalid or malformed response from the arf session."),
                    None,
                );
            } else {
                exit_error(
                    EXIT_TRANSPORT,
                    "TRANSPORT_ERROR",
                    &format!("{e:#}"),
                    Some("Check that the arf session is running and IPC is enabled."),
                    None,
                );
            }
        }
    }
}

/// Inner transport implementation that returns Result for ergonomic error handling.
fn send_request_inner(
    socket_path: &str,
    request: &serde_json::Value,
    timeout: std::time::Duration,
) -> Result<JsonRpcResponse> {
    let body = serde_json::to_string(request)?;

    #[cfg(unix)]
    {
        use std::io::{Read, Write};
        use std::os::unix::net::UnixStream;

        let http_request = format!(
            "POST / HTTP/1.1\r\n\
             Host: localhost\r\n\
             Content-Type: application/json\r\n\
             Content-Length: {}\r\n\
             Connection: close\r\n\
             \r\n{}",
            body.len(),
            body
        );

        let mut stream = UnixStream::connect(socket_path)
            .with_context(|| format!("Failed to connect to {socket_path}"))?;
        stream.set_read_timeout(Some(timeout))?;
        stream.write_all(http_request.as_bytes())?;
        stream.shutdown(std::net::Shutdown::Write)?;

        let mut response_buf = Vec::new();
        stream.read_to_end(&mut response_buf)?;

        parse_http_response(&response_buf)
    }

    #[cfg(windows)]
    {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};
        use tokio::net::windows::named_pipe::ClientOptions;

        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .context("Failed to create tokio runtime")?;

        rt.block_on(async {
            let mut pipe = ClientOptions::new()
                .open(socket_path)
                .with_context(|| format!("Failed to connect to {socket_path}"))?;

            // Send raw JSON (no HTTP wrapping) — the server detects complete
            // JSON and stops reading, so no write shutdown is needed.
            pipe.write_all(body.as_bytes()).await?;
            pipe.flush().await?;

            // Read response with timeout
            let mut response_buf = Vec::new();
            match tokio::time::timeout(timeout, pipe.read_to_end(&mut response_buf)).await {
                Ok(result) => result?,
                Err(_) => anyhow::bail!("Request timed out after {}s", timeout.as_secs()),
            };

            parse_http_response(&response_buf)
        })
    }
}

/// Parse an HTTP response and extract the JSON body.
fn parse_http_response(data: &[u8]) -> Result<JsonRpcResponse> {
    let text = String::from_utf8_lossy(data);

    let body = if let Some(pos) = text.find("\r\n\r\n") {
        &text[pos + 4..]
    } else {
        &text
    };

    serde_json::from_str(body).context("Failed to parse JSON-RPC response")
}