rpi-cli 0.32.0

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
//! Session-free native owner. Results are bounded, volatile and incarnation
//! scoped; no admission journal, Harness, Session, writer or replay exists here.
//! Only HostService owns this backend. A client is a weak observer, never an
//! abort handle for the supervised prompt future or a second owner registry.

use super::{
    bindings::{NativeBindingProfile, NativeMiniAgent},
    ordinary::{BackendError, BackendResult},
    AgentId, AgentMode, AgentRecord, AgentState, CatalogGuard, HostCatalog, HostEpoch,
    NativeExtensionCustody, RequestId, RunId,
};
use futures::FutureExt;
use sha2::{Digest, Sha256};
use std::{
    collections::BTreeMap,
    future::Future,
    panic::AssertUnwindSafe,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc, Weak,
    },
};
use tokio::sync::{watch, Mutex};
use tokio_util::sync::CancellationToken;

#[derive(Clone, Copy)]
pub struct MiniLimits {
    pub max_input_bytes: usize,
    pub max_results: usize,
    pub max_output_bytes: usize,
}
impl Default for MiniLimits {
    fn default() -> Self {
        Self {
            max_input_bytes: 256 * 1024,
            max_results: 64,
            max_output_bytes: 1024 * 1024,
        }
    }
}

/// A volatile identity, NOT a durable admission/receipt or execution success.
/// The random run ID also fences reuse of an Agent identity within one epoch.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MiniReceipt {
    pub epoch: HostEpoch,
    pub agent_id: AgentId,
    pub request_id: RequestId,
    pub run_id: RunId,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MiniOutcome {
    Running,
    Succeeded,
    Failed,
    Aborted,
    OutputLimit,
    Faulted,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MiniResult {
    pub receipt: MiniReceipt,
    pub outcome: MiniOutcome,
    /// Exact final assistant text. Oversized results are explicitly OutputLimit,
    /// never truncated or silently evicted. Provider diagnostics stay private.
    pub answer: String,
}
struct Entry {
    digest: [u8; 32],
    result: MiniResult,
}
struct Active {
    receipt: MiniReceipt,
    cancel: CancellationToken,
}
struct Control {
    record: AgentRecord,
    results: BTreeMap<RequestId, Entry>,
    active: Option<Active>,
    output_bytes: usize,
    closing: bool,
    faulted: bool,
    closed: bool,
}
struct Backend {
    native: NativeMiniAgent,
    catalog: HostCatalog,
    limits: MiniLimits,
    stopping: Arc<AtomicBool>,
    control: Mutex<Control>,
    close_gate: Mutex<()>,
    changed: watch::Sender<()>,
}
pub(super) struct MiniAgent {
    inner: Arc<Backend>,
}
#[derive(Clone)]
pub struct MiniClient {
    inner: Weak<Backend>,
}

impl MiniAgent {
    pub(super) async fn start(
        catalog: HostCatalog,
        id: AgentId,
        guard: CatalogGuard,
        profile: NativeBindingProfile,
        limits: MiniLimits,
        custody: Arc<NativeExtensionCustody>,
        stopping: Arc<AtomicBool>,
    ) -> BackendResult<Self> {
        if limits.max_input_bytes == 0
            || limits.max_input_bytes > 8 * 1024 * 1024
            || limits.max_results == 0
            || limits.max_results > 64
            || limits.max_output_bytes == 0
            || limits.max_output_bytes > 1024 * 1024
        {
            return Err(BackendError::Binding("invalid Mini budgets"));
        }
        let record = catalog.get(id).await?;
        if record.spec.mode != AgentMode::Mini || record.spec.session.is_some() {
            return Err(BackendError::Binding(
                "Mini requires explicit Session-free metadata",
            ));
        }
        let record = catalog.transition(id, guard, AgentState::Starting).await?;
        // Service has already reserved Starting and retained this exact custody.
        // Any error/panic leaves reconciliation with that same service entry.
        let native = profile
            .bind_mini_agent(record.spec.clone(), catalog.clone(), custody)
            .await?;
        let record = catalog
            .transition(id, catalog.guard(&record), AgentState::Idle)
            .await?;
        let (changed, _) = watch::channel(());
        Ok(Self {
            inner: Arc::new(Backend {
                native,
                catalog,
                limits,
                stopping,
                control: Mutex::new(Control {
                    record,
                    results: BTreeMap::new(),
                    active: None,
                    output_bytes: 0,
                    closing: false,
                    faulted: false,
                    closed: false,
                }),
                close_gate: Mutex::new(()),
                changed,
            }),
        })
    }
    pub(super) fn client(&self) -> MiniClient {
        MiniClient {
            inner: Arc::downgrade(&self.inner),
        }
    }
    pub(super) async fn close(&self) -> BackendResult<()> {
        self.inner.close().await
    }
}
impl MiniClient {
    fn upgrade(&self) -> BackendResult<Arc<Backend>> {
        self.inner.upgrade().ok_or(BackendError::Closed)
    }
    pub async fn submit(
        &self,
        epoch: HostEpoch,
        request_id: RequestId,
        prompt: String,
    ) -> BackendResult<MiniReceipt> {
        let inner = self.upgrade()?;
        owned(async move { inner.submit(epoch, request_id, prompt).await }).await
    }
    /// Lookup only within this already-owned incarnation; never loads or
    /// replays input when an admission reply was lost.
    pub async fn request(
        &self,
        epoch: HostEpoch,
        agent_id: AgentId,
        request_id: RequestId,
    ) -> BackendResult<Option<MiniResult>> {
        let inner = self.upgrade()?;
        let control = inner.control.lock().await;
        inner.validate(&control, epoch, agent_id)?;
        Ok(control
            .results
            .get(&request_id)
            .map(|entry| entry.result.clone()))
    }
    pub async fn result(&self, receipt: MiniReceipt) -> BackendResult<Option<MiniResult>> {
        let inner = self.upgrade()?;
        let control = inner.control.lock().await;
        inner.validate(&control, receipt.epoch, receipt.agent_id)?;
        Ok(control
            .results
            .get(&receipt.request_id)
            .filter(|entry| entry.result.receipt == receipt)
            .map(|entry| entry.result.clone()))
    }
    /// Only signal this exact volatile run. Neither true nor cancellation of a
    /// query proves settlement; late callbacks still belong to the owner.
    pub async fn cancel(&self, receipt: MiniReceipt) -> BackendResult<bool> {
        let inner = self.upgrade()?;
        let control = inner.control.lock().await;
        inner.validate(&control, receipt.epoch, receipt.agent_id)?;
        match &control.active {
            Some(active) if active.receipt == receipt => {
                active.cancel.cancel();
                Ok(true)
            }
            _ => Ok(false),
        }
    }
}
impl Backend {
    fn validate(&self, control: &Control, epoch: HostEpoch, id: AgentId) -> BackendResult<()> {
        if epoch != self.catalog.epoch() || id != control.record.spec.id {
            return Err(BackendError::Binding("Mini owner identity mismatch"));
        }
        Ok(())
    }
    async fn submit(
        self: Arc<Self>,
        epoch: HostEpoch,
        request_id: RequestId,
        prompt: String,
    ) -> BackendResult<MiniReceipt> {
        let mut control = self.control.lock().await;
        self.validate(&control, epoch, control.record.spec.id)?;
        if control.closing || control.closed || self.stopping.load(Ordering::Acquire) {
            return Err(BackendError::Closed);
        }
        if control.faulted {
            return Err(BackendError::Faulted);
        }
        if prompt.trim().is_empty() || prompt.len() > self.limits.max_input_bytes {
            return Err(BackendError::InputTooLarge);
        }
        let digest: [u8; 32] = Sha256::digest(prompt.as_bytes()).into();
        if let Some(entry) = control.results.get(&request_id) {
            return if entry.digest == digest {
                Ok(entry.result.receipt)
            } else {
                Err(BackendError::Binding("Mini request intent conflict"))
            };
        }
        if control.active.is_some() {
            return Err(BackendError::Busy);
        }
        if control.results.len() >= self.limits.max_results {
            return Err(BackendError::Binding("Mini result capacity exhausted"));
        }
        control.record = self
            .catalog
            .transition(
                control.record.spec.id,
                self.catalog.guard(&control.record),
                AgentState::Running,
            )
            .await?;
        let receipt = MiniReceipt {
            epoch,
            agent_id: control.record.spec.id,
            request_id,
            run_id: RunId::new(),
        };
        let cancel = CancellationToken::new();
        control.active = Some(Active {
            receipt,
            cancel: cancel.clone(),
        });
        control.results.insert(
            request_id,
            Entry {
                digest,
                result: MiniResult {
                    receipt,
                    outcome: MiniOutcome::Running,
                    answer: String::new(),
                },
            },
        );
        // No await between publishing Active and transferring the entire prompt
        // future to its owner. Caller cancellation can only lose the response.
        let owner = self.clone();
        tokio::spawn(async move {
            owner.run(receipt, prompt, cancel).await;
        });
        Ok(receipt)
    }
    async fn run(self: Arc<Self>, receipt: MiniReceipt, prompt: String, cancel: CancellationToken) {
        let completed = AssertUnwindSafe(async {
            let first_message = self.native.agent().state().messages.len();
            let prompt = self.native.agent().prompt(prompt);
            tokio::pin!(prompt);
            let result = tokio::select! {
                // Poll native start BEFORE observing a pre-existing cancel. An
                // abort before start_active_run would miss the new run entirely.
                biased;
                result = &mut prompt => result,
                _ = cancel.cancelled() => { self.native.cancel(); prompt.await }
            };
            let state = self.native.agent().state();
            let last = state.messages[first_message..].iter().rev().find_map(|m| {
                if let rpi_agent::AgentMessage::Assistant(m) = m {
                    Some(m)
                } else {
                    None
                }
            });
            let outcome = match (&result, last.map(|m| m.stop_reason)) {
                (Err(rpi_agent::AgentError::Abort), _) | (_, Some(rpi_ai::StopReason::Aborted)) => {
                    MiniOutcome::Aborted
                }
                (Err(_), _) | (_, Some(rpi_ai::StopReason::Error)) => MiniOutcome::Failed,
                (Ok(()), Some(rpi_ai::StopReason::Stop | rpi_ai::StopReason::Length))
                    if state.error_message.is_none() =>
                {
                    MiniOutcome::Succeeded
                }
                _ => MiniOutcome::Failed,
            };
            let mut answer = String::new();
            if let Some(last) = last {
                for block in &last.content {
                    if let rpi_ai::Content::Text(text) = block {
                        if answer.len().saturating_add(text.text.len())
                            > self.limits.max_output_bytes
                        {
                            return (MiniOutcome::OutputLimit, String::new());
                        }
                        answer.push_str(&text.text);
                    }
                }
            }
            (outcome, answer)
        })
        .catch_unwind()
        .await;
        let mut control = self.control.lock().await;
        let (mut outcome, mut answer) = completed.unwrap_or_else(|_| {
            control.faulted = true;
            (MiniOutcome::Faulted, String::new())
        });
        if !control.faulted {
            match self
                .catalog
                .transition(
                    receipt.agent_id,
                    self.catalog.guard(&control.record),
                    AgentState::Idle,
                )
                .await
            {
                Ok(record) => control.record = record,
                Err(_) => {
                    control.faulted = true;
                    outcome = MiniOutcome::Faulted;
                    answer.clear();
                }
            }
        }
        if control.output_bytes.saturating_add(answer.len()) > self.limits.max_output_bytes {
            outcome = MiniOutcome::OutputLimit;
            answer.clear();
        }
        control.output_bytes += answer.len();
        if let Some(entry) = control.results.get_mut(&receipt.request_id) {
            entry.result.outcome = outcome;
            entry.result.answer = answer;
        }
        control.active = None;
        self.changed.send_replace(());
    }
    async fn close(&self) -> BackendResult<()> {
        let _close = self.close_gate.lock().await;
        let mut changed = self.changed.subscribe();
        loop {
            let mut control = self.control.lock().await;
            control.closing = true;
            if control.closed {
                return Ok(());
            }
            if let Some(active) = &control.active {
                active.cancel.cancel();
                drop(control);
                changed
                    .changed()
                    .await
                    .map_err(|_| BackendError::TaskStopped)?;
                continue;
            }
            // A panic is not native settlement, even if the request task exited.
            if control.faulted {
                return Err(BackendError::Faulted);
            }
            if control.record.state != AgentState::Closing {
                control.record = self
                    .catalog
                    .transition(
                        control.record.spec.id,
                        self.catalog.guard(&control.record),
                        AgentState::Closing,
                    )
                    .await?;
            }
            drop(control);
            self.native.close().await?;
            let mut control = self.control.lock().await;
            control.record = self
                .catalog
                .transition(
                    control.record.spec.id,
                    self.catalog.guard(&control.record),
                    AgentState::Lost,
                )
                .await?;
            control.closed = true;
            return Ok(());
        }
    }
}
async fn owned<T: Send + 'static>(
    future: impl Future<Output = BackendResult<T>> + Send + 'static,
) -> BackendResult<T> {
    tokio::spawn(AssertUnwindSafe(future).catch_unwind())
        .await
        .map_err(|_| BackendError::TaskStopped)?
        .map_err(|_| BackendError::TaskStopped)?
}