rpi-cli 0.21.1

Terminal coding-agent CLI (the `rpi` binary) built on the rpi-* library crates
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
//! Optional native RPC text-I/O transport used by the production dispatcher.
//!
//! The RPC owner must retain `FileIOClient` and call `close` on EOF/shutdown.
//! Backends retain only a Weak reference; even in-flight strong references cannot
//! defer owner-drop cancellation. No harness, adapter or MCP ABI is retained.
use super::{
    client_requests::{ClientRequestBroker, MAX_TIMEOUT_MS},
    writer::RpcWriter,
};
use async_trait::async_trait;
use rpi_tools::file_io::{
    resolve_client_path, FileIOBackend, FileIOError, FileIOErrorCode, FileIOReadRange,
    FileIORequest, FileIOResponse, MAX_FILE_IO_BYTES,
};
use serde_json::{json, Value};
use std::{
    collections::HashMap,
    io::{self, Write},
    path::Path,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc, Mutex, Weak,
    },
    time::Duration,
};
use tokio_util::sync::CancellationToken;

// Encoded JSON, excluding newline. The native input framing limit (1 MiB)
// remains the first allocation boundary. This stricter budget is checked by a
// streaming counter before cloning a known reply. Highly escaped text can hit
// this budget before the independent 256 KiB UTF-8 content limit.
const MAX_PAYLOAD_BYTES: usize = 768 * 1024;
const MAX_ERROR_BYTES: usize = 4 * 1024;

pub(crate) fn capabilities() -> Value {
    json!({"version": 1, "maxPending": 64, "maxTextBytes": MAX_FILE_IO_BYTES,
        "maxPayloadBytes": MAX_PAYLOAD_BYTES, "timeoutMs": MAX_TIMEOUT_MS})
}

/// Unique lifecycle owner: intentionally NOT Clone (nor Arc-returning).
pub(crate) struct FileIOClient {
    inner: Arc<Inner>,
}
struct Inner {
    broker: ClientRequestBroker,
    gate: Mutex<State>,
    stop: CancellationToken,
    timeout: Duration,
    // Synchronous hook forces the race within one poll, after select has
    // checked cancellation but before the real publication gate is taken.
    #[cfg(test)]
    before_publish: Mutex<Option<Box<dyn FnOnce() + Send>>>,
}
struct State {
    closed: bool,
    writer: Option<RpcWriter>,
    // Only unresolved IDs; the broker independently retains admission slots
    // until their waiter drops, including resolved-but-unconsumed replies.
    pending: HashMap<String, Pending>,
}
struct Pending {
    expected: Expected,
    closure: Arc<Closure>,
}
#[derive(Clone, Copy)]
enum Expected {
    Read { max_bytes: usize },
    Write,
}
struct Backend(Weak<Inner>);

impl FileIOClient {
    pub(crate) fn new(writer: RpcWriter) -> Self {
        Self::with_timeout(writer, Duration::from_millis(MAX_TIMEOUT_MS))
    }
    fn with_timeout(writer: RpcWriter, timeout: Duration) -> Self {
        Self {
            inner: Arc::new(Inner {
                broker: ClientRequestBroker::default(),
                gate: Mutex::new(State {
                    closed: false,
                    writer: Some(writer),
                    pending: HashMap::new(),
                }),
                stop: CancellationToken::new(),
                timeout,
                #[cfg(test)]
                before_publish: Mutex::new(None),
            }),
        }
    }
    pub(crate) fn backend(&self) -> Arc<dyn FileIOBackend> {
        Arc::new(Backend(Arc::downgrade(&self.inner)))
    }
    pub(crate) fn close(&self) {
        // Same gate as registration AND publication: close cannot be followed
        // by a request enqueue, including one already holding queue capacity.
        let mut state = self.inner.gate.lock().expect("file I/O gate poisoned");
        state.closed = true;
        self.inner.stop.cancel();
        // Close BEFORE dropping broker senders (which wakes waiters).
        for (_, pending) in state.pending.drain() {
            pending.closure.close();
        }
        self.inner.broker.cancel_all();
        state.writer.take();
    }

    /// Native `file_io_response` envelope after the dispatcher removes only its
    /// optional correlation `id`. Unknown/late/duplicate request IDs
    /// return false BEFORE size scanning or copying any untrusted payload.
    /// Malformed known replies resolve a fixed InvalidResponse and retire.
    pub(crate) fn resolve_response(&self, message: &Value) -> bool {
        let Some(id) = message.get("requestId").and_then(Value::as_str) else {
            return false;
        };
        let mut state = self.inner.gate.lock().expect("file I/O gate poisoned");
        let Some(pending) = state.pending.remove(id) else {
            return false;
        };
        let reply = if valid_reply(message, pending.expected) {
            // Only now allocate a copy, and never preserve arbitrary metadata.
            if let Some(result) = message.get("result") {
                json!({"result": result.clone()})
            } else {
                json!({"error": message["error"].clone()})
            }
        } else {
            json!({"error": {"code": "invalid_response", "message": "invalid client file I/O response"}})
        };
        // Queue the one closure BEFORE waking the consuming broker future.
        pending.closure.close();
        self.inner.broker.resolve(id, Ok(reply))
    }
}
impl Drop for FileIOClient {
    fn drop(&mut self) {
        self.close();
    }
}

fn error(code: FileIOErrorCode) -> FileIOError {
    let message = match code {
        FileIOErrorCode::Unsupported => "file I/O pending request capacity exhausted",
        FileIOErrorCode::InvalidResponse => "invalid client file I/O response",
        FileIOErrorCode::TooLarge => "client file I/O exceeds bounded payload limit",
        FileIOErrorCode::Timeout => {
            "client file I/O timed out; a dispatched write may have committed"
        }
        FileIOErrorCode::Cancelled => {
            "client file I/O cancelled; a dispatched write may have committed"
        }
        FileIOErrorCode::InvalidPath => "unsupported or ambiguous client path",
        _ => "client file I/O disconnected; a dispatched write may have committed",
    };
    FileIOError::new(code, message)
}
fn parse_code(code: &str) -> Option<FileIOErrorCode> {
    Some(match code {
        "unsupported" => FileIOErrorCode::Unsupported,
        "denied" => FileIOErrorCode::Denied,
        "not_found" => FileIOErrorCode::NotFound,
        "invalid_response" => FileIOErrorCode::InvalidResponse,
        "too_large" => FileIOErrorCode::TooLarge,
        "timeout" => FileIOErrorCode::Timeout,
        "cancelled" => FileIOErrorCode::Cancelled,
        "disconnected" => FileIOErrorCode::Disconnected,
        "invalid_path" => FileIOErrorCode::InvalidPath,
        _ => return None,
    })
}

/// Bounded serialization without a second full JSON buffer.
fn fits_payload(value: &Value) -> bool {
    struct Counter(usize);
    impl Write for Counter {
        fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
            if bytes.len() > MAX_PAYLOAD_BYTES.saturating_sub(self.0) {
                return Err(io::Error::other("file I/O payload limit"));
            }
            self.0 += bytes.len();
            Ok(bytes.len())
        }
        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }
    serde_json::to_writer(Counter(0), value).is_ok()
}
fn valid_reply(message: &Value, expected: Expected) -> bool {
    let Some(object) = message.as_object() else {
        return false;
    };
    if object.len() != 3
        || message.get("type").and_then(Value::as_str) != Some("file_io_response")
        || !fits_payload(message)
    {
        return false;
    }
    match (message.get("result"), message.get("error")) {
        (Some(result), None) => {
            let Some(result) = result.as_object() else {
                return false;
            };
            match expected {
                Expected::Read { max_bytes } => {
                    result.len() == 1
                        && result
                            .get("content")
                            .and_then(Value::as_str)
                            .is_some_and(|text| text.len() <= max_bytes)
                }
                Expected::Write => result.is_empty(),
            }
        }
        (None, Some(remote)) => remote.as_object().is_some_and(|object| {
            object.len() == 2
                && remote
                    .get("code")
                    .and_then(Value::as_str)
                    .and_then(parse_code)
                    .is_some()
                && remote
                    .get("message")
                    .and_then(Value::as_str)
                    .is_some_and(|message| message.len() <= MAX_ERROR_BYTES)
        }),
        _ => false,
    }
}

fn encode_request(request: FileIORequest) -> Result<(Value, Expected), FileIOError> {
    let (path, mut event, expected) = match request {
        FileIORequest::ReadText {
            absolute_path,
            range,
            max_bytes,
        } => {
            let max_bytes = max_bytes.min(MAX_FILE_IO_BYTES);
            let mut event = json!({"operation": "read_text_file", "maxBytes": max_bytes});
            if let FileIOReadRange::Lines {
                offset_1based,
                limit,
            } = range
            {
                if offset_1based == 0 || limit == 0 {
                    return Err(error(FileIOErrorCode::InvalidResponse));
                }
                event["line"] = json!(offset_1based);
                event["limit"] = json!(limit);
            }
            (absolute_path, event, Expected::Read { max_bytes })
        }
        FileIORequest::WriteText {
            absolute_path,
            content,
        } => {
            if content.len() > MAX_FILE_IO_BYTES {
                return Err(error(FileIOErrorCode::TooLarge));
            }
            (
                absolute_path,
                json!({"operation": "write_text_file", "content": content}),
                Expected::Write,
            )
        }
    };
    // Empty cwd deliberately forbids relative input; use the exact neutral
    // tools resolver for POSIX, drive-absolute, UNC and lexical normalization.
    event["path"] = json!(resolve_client_path(Path::new(""), &path)?);
    event["type"] = json!("file_io_request");
    Ok((event, expected))
}

/// Shared with response/owner-close paths, without retaining Inner (no cycle).
struct Closure {
    writer: RpcWriter,
    id: String,
    published: AtomicBool,
    closed: AtomicBool,
}
impl Closure {
    // All publication and close calls hold Inner::gate. Registration alone is
    // not a client-visible request and must never produce a closed event.
    fn close(&self) {
        if !self.closed.swap(true, Ordering::AcqRel) && self.published.load(Ordering::Acquire) {
            self.writer.try_write_line(&json!({
                "type": "file_io_request_closed", "requestId": self.id,
            }));
        }
    }
}
/// Declared AFTER the pinned broker waiter. Cancellation/drop closes and removes
/// response admission BEFORE that waiter releases its broker slot. Successful
/// replies close in resolve_response BEFORE the broker sender wakes the waiter.
struct CloseRequest {
    inner: Arc<Inner>,
    closure: Arc<Closure>,
}
impl CloseRequest {
    fn close(&self) {
        let mut state = self.inner.gate.lock().expect("file I/O gate poisoned");
        state.pending.remove(&self.closure.id);
        self.closure.close();
    }
}
impl Drop for CloseRequest {
    fn drop(&mut self) {
        self.close();
    }
}

#[async_trait]
impl FileIOBackend for Backend {
    async fn request(
        &self,
        request: FileIORequest,
        cancel: CancellationToken,
    ) -> Result<FileIOResponse, FileIOError> {
        let inner = self
            .0
            .upgrade()
            .ok_or_else(|| error(FileIOErrorCode::Disconnected))?;
        // Deadline starts before validation/admission, and includes enqueue.
        let deadline = tokio::time::Instant::now() + inner.timeout;
        if cancel.is_cancelled() {
            return Err(error(FileIOErrorCode::Cancelled));
        }
        let (mut event, expected) = encode_request(request)?;
        let (request, writer, closure) = {
            let mut state = inner.gate.lock().expect("file I/O gate poisoned");
            if state.closed || inner.stop.is_cancelled() {
                return Err(error(FileIOErrorCode::Disconnected));
            }
            let writer = state
                .writer
                .as_ref()
                .ok_or_else(|| error(FileIOErrorCode::Disconnected))?
                .clone();
            if writer.failure().is_cancelled() {
                return Err(error(FileIOErrorCode::Disconnected));
            }
            if cancel.is_cancelled() {
                return Err(error(FileIOErrorCode::Cancelled));
            }
            let request = inner
                .broker
                .register()
                .map_err(|_| error(FileIOErrorCode::Unsupported))?;
            let closure = Arc::new(Closure {
                writer: writer.clone(),
                id: request.id.clone(),
                published: AtomicBool::new(false),
                closed: AtomicBool::new(false),
            });
            state.pending.insert(
                request.id.clone(),
                Pending {
                    expected,
                    closure: closure.clone(),
                },
            );
            (request, writer, closure)
        };
        event["requestId"] = json!(request.id);
        // Only the outer select owns cancellation/deadline. Letting wait()'s
        // own cancellation win could release its slot before RAII closure.
        let never_cancel = CancellationToken::new();
        let waiter = request.wait(inner.timeout + Duration::from_secs(1), &never_cancel);
        tokio::pin!(waiter);
        // Drop order is intentional; see CloseRequest. All awaits below borrow
        // waiter, so an aborted caller cannot drop the broker request first.
        let close = CloseRequest {
            inner: inner.clone(),
            closure: closure.clone(),
        };
        let failure = writer.failure();
        let operation = async {
            if !fits_payload(&event) {
                return Err(error(FileIOErrorCode::TooLarge));
            }
            // Size is checked before allocating the encoded buffer. No JSON
            // serialization or asynchronous wait occurs under the state gate.
            let mut line =
                serde_json::to_vec(&event).map_err(|_| error(FileIOErrorCode::InvalidResponse))?;
            line.push(b'\n');
            let permit = writer
                .reserve_bounded()
                .await
                .map_err(|_| error(FileIOErrorCode::Disconnected))?;
            #[cfg(test)]
            {
                let hook = inner
                    .before_publish
                    .lock()
                    .expect("file I/O publication hook poisoned")
                    .take();
                if let Some(hook) = hook {
                    hook();
                }
            }
            {
                let state = inner.gate.lock().expect("file I/O gate poisoned");
                if state.closed || inner.stop.is_cancelled() || failure.is_cancelled() {
                    return Err(error(FileIOErrorCode::Disconnected));
                }
                if cancel.is_cancelled() {
                    return Err(error(FileIOErrorCode::Cancelled));
                }
                if tokio::time::Instant::now() >= deadline {
                    return Err(error(FileIOErrorCode::Timeout));
                }
                // A known early response may already have retired this ID.
                // Consume that response below, but never publish it afterwards.
                if state.pending.contains_key(&closure.id)
                    && !closure.closed.load(Ordering::Acquire)
                {
                    // The lifecycle gate orders this synchronous commit and
                    // publication marker against every response/close/drop.
                    permit.send(line);
                    closure.published.store(true, Ordering::Release);
                } else {
                    drop(permit);
                }
            }
            (&mut waiter)
                .await
                .map_err(|message| match message.as_str() {
                    "client request cancelled" => error(FileIOErrorCode::Cancelled),
                    "client request timed out" => error(FileIOErrorCode::Timeout),
                    _ => error(FileIOErrorCode::Disconnected),
                })
        };
        let reply = tokio::select! {
            biased;
            _ = cancel.cancelled() => Err(error(FileIOErrorCode::Cancelled)),
            _ = inner.stop.cancelled() => Err(error(FileIOErrorCode::Disconnected)),
            _ = failure.cancelled() => Err(error(FileIOErrorCode::Disconnected)),
            _ = tokio::time::sleep_until(deadline) => Err(error(FileIOErrorCode::Timeout)),
            reply = operation => reply,
        };
        close.close();
        if failure.is_cancelled() || inner.stop.is_cancelled() {
            return Err(error(FileIOErrorCode::Disconnected));
        }
        let reply = reply?;
        if let Some(remote) = reply.get("error") {
            // This envelope is exclusively generated by resolve_response after
            // validation, never decoded from arbitrary broker error strings.
            let code = remote["code"]
                .as_str()
                .and_then(parse_code)
                .ok_or_else(|| error(FileIOErrorCode::InvalidResponse))?;
            let message = remote["message"]
                .as_str()
                .ok_or_else(|| error(FileIOErrorCode::InvalidResponse))?;
            return Err(FileIOError::new(code, message));
        }
        match expected {
            Expected::Read { .. } => Ok(FileIOResponse::Text {
                content: reply["result"]["content"]
                    .as_str()
                    .ok_or_else(|| error(FileIOErrorCode::InvalidResponse))?
                    .to_owned(),
            }),
            Expected::Write => Ok(FileIOResponse::Written),
        }
    }
}

#[cfg(test)]
#[path = "file_io_tests.rs"]
mod tests;