rpi-cli 0.35.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
//! Private adapter for already-built, exclusively handed-over Harnesses.
//! No construction/discovery, catalog, IPC, replay or extra Agent loop.
//! The optional trusted cleanup barrier runs after Harness close but before
//! writer release. It must prove actual resource settlement, not merely a
//! lifecycle acknowledgement. Concrete CLI/process/DLL integration is still
//! pending; bindings with external work require such a barrier. Callers must
//! not keep driving raw Harness clones after successful attachment.

use super::{
    runtime::{HostRuntime, OwnerEntry, OwnerPhase},
    AgentId,
};
use futures::{stream::FuturesUnordered, FutureExt, StreamExt};
use rpi_harness::{
    agent_harness::{AgentHarness, AgentLane, HarnessWriteFailure, RunResult},
    result::HarnessError,
};
use std::{
    future::Future,
    panic::AssertUnwindSafe,
    sync::{Arc, Mutex, Weak},
    time::Duration,
};
use tokio::sync::Mutex as AsyncMutex;
use tokio_util::sync::CancellationToken;

type Result<T> = std::result::Result<T, InProcessError>;
pub(super) type HarnessRuntime = HostRuntime<ManagedHarness>;

/// Trusted, retry-safe cleanup over retained resources. Ok means every callback
/// and external worker has actually settled; uncertainty must return an error.
/// A rejected attach borrows (does not consume) this cleanup capability.
/// Do not capture a strong runtime reference and create an ownership cycle.
pub(super) type HarnessCleanup =
    Arc<dyn Fn() -> futures::future::BoxFuture<'static, Result<()>> + Send + Sync>;

#[derive(Clone, Debug, thiserror::Error)]
pub(super) enum InProcessError {
    #[error("Host or Agent is stopping")]
    Stopping,
    #[error("Agent has no live Host owner")]
    NotLoaded,
    #[error("Agent or Session already has a Host owner")]
    AlreadyOwned,
    #[error("Agent is busy")]
    Busy,
    #[error("Harness binding has no exclusive durable writer")]
    WriterRequired,
    #[error("native settlement is not proved; retain owner and reconcile")]
    Unsettled,
    #[error("owned Host task panicked or stopped")]
    TaskStopped,
    #[error("Harness operation failed")]
    Harness(#[source] Arc<HarnessError>),
}
impl From<HarnessError> for InProcessError {
    fn from(value: HarnessError) -> Self {
        Self::Harness(Arc::new(value))
    }
}

#[derive(Clone, Copy, Default, PartialEq, Eq)]
enum CloseStage {
    #[default]
    Open,
    HarnessClosed,
    BindingsRetired,
    ResourcesReleased,
    Complete,
}

#[derive(Default)]
struct Control {
    retiring: bool,
    close_stage: CloseStage,
    uncertain: bool,
    active: Option<CancellationToken>,
    write_failures: Vec<HarnessWriteFailure>,
}
pub(super) struct ManagedHarness {
    harness: AgentHarness,
    cleanup: Option<HarnessCleanup>,
    operation: AsyncMutex<()>,
    control: Mutex<Control>,
}
impl ManagedHarness {
    fn control(&self) -> std::sync::MutexGuard<'_, Control> {
        self.control
            .lock()
            .unwrap_or_else(|poison| poison.into_inner())
    }
    fn require_saved(&self) -> Result<()> {
        let failures = self.harness.take_write_failures();
        if !self.harness.unsaved_messages().is_empty() || !failures.is_empty() {
            let mut control = self.control();
            control.write_failures.extend(failures);
            control.uncertain = true;
            return Err(InProcessError::Unsettled);
        }
        Ok(())
    }
    fn retire(&self) {
        let mut control = self.control();
        control.retiring = true;
        if let Some(cancel) = &control.active {
            cancel.cancel();
        }
    }
    async fn prompt(&self, text: String) -> Result<RunResult> {
        let _operation = self
            .operation
            .try_lock()
            .map_err(|_| InProcessError::Busy)?;
        let cancel = {
            let mut control = self.control();
            if control.retiring {
                return Err(InProcessError::Stopping);
            }
            if control.uncertain {
                return Err(InProcessError::Unsettled);
            }
            let cancel = CancellationToken::new();
            control.active = Some(cancel.clone());
            cancel
        };
        // Same cooperative pre-admission cancellation pattern as Ordinary.
        // Never abort/drop the executing Harness future on cancellation.
        let execute = AssertUnwindSafe(self.harness.prompt_text(&text, vec![])).catch_unwind();
        tokio::pin!(execute);
        let request_cancel = async {
            cancel.cancelled().await;
            loop {
                if let Some(active) = self.harness.active_operation_snapshot() {
                    if self
                        .harness
                        .request_abort(&active.run_id)
                        .await
                        .unwrap_or(false)
                    {
                        break;
                    }
                }
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        };
        tokio::pin!(request_cancel);
        let result = tokio::select! {
            result = &mut execute => result,
            _ = &mut request_cancel => execute.await,
        };
        let result = result
            .map_err(|_| InProcessError::TaskStopped)
            .and_then(|result| result.map_err(InProcessError::from));
        let settled = self.harness.wait_for_idle().await;
        self.harness.drain_session().await;
        let result = settled.map_err(InProcessError::from).and(result);
        let write_failures = self.harness.take_write_failures();
        let mut control = self.control();
        control.active = None;
        control.uncertain |= !write_failures.is_empty()
            || !self.harness.unsaved_messages().is_empty()
            || matches!(&result, Err(InProcessError::TaskStopped))
            || matches!(&result, Err(InProcessError::Harness(error)) if matches!(error.as_ref(), HarnessError::Io { .. } | HarnessError::SessionStorage { .. }));
        control.write_failures.extend(write_failures);
        result
    }
    async fn close(&self) -> Result<()> {
        self.retire();
        let _operation = self.operation.lock().await;
        {
            let control = self.control();
            if control.close_stage == CloseStage::Complete {
                return Ok(());
            }
            if control.uncertain {
                return Err(InProcessError::Unsettled);
            }
        }
        let stage = self.control().close_stage;
        if stage == CloseStage::Open {
            self.harness.wait_for_idle().await?;
            self.harness.drain_session().await;
            self.require_saved()?;
            self.harness.close().await?;
            self.control().close_stage = CloseStage::HarnessClosed;
        }
        let stage = self.control().close_stage;
        if stage == CloseStage::HarnessClosed {
            // close only seals operations. Relinquish this Harness's own
            // executable references before waiting for final plugin cleanup.
            self.harness.retire_runtime_bindings().await?;
            self.control().close_stage = CloseStage::BindingsRetired;
        }
        let stage = self.control().close_stage;
        if stage != CloseStage::ResourcesReleased {
            if let Some(cleanup) = &self.cleanup {
                cleanup().await?;
            }
            self.control().close_stage = CloseStage::ResourcesReleased;
        }
        let storage = self.harness.session().storage();
        storage.drain().await;
        // Resource teardown can itself finish accepted writes. Recheck after
        // its barrier and drain, before surrendering the durable writer.
        self.require_saved()?;
        storage.release_writer_lease();
        if storage.writer_lease_held() {
            return Err(InProcessError::Unsettled);
        }
        self.control().close_stage = CloseStage::Complete;
        Ok(())
    }
}

pub(super) struct HarnessClient {
    runtime: Weak<HarnessRuntime>,
    owner: Weak<ManagedHarness>,
    id: AgentId,
}
impl HarnessClient {
    /// Native diagnostics remain inspectable even after failed close/stop.
    pub fn write_failures(&self) -> Result<Vec<HarnessWriteFailure>> {
        let runtime = self.runtime.upgrade().ok_or(InProcessError::NotLoaded)?;
        let owner = runtime
            .entries()
            .get(&self.id)
            .and_then(|entry| entry.owner.clone())
            .ok_or(InProcessError::NotLoaded)?;
        if !self.owner.ptr_eq(&Arc::downgrade(&owner)) {
            return Err(InProcessError::NotLoaded);
        }
        let failures = owner.control().write_failures.clone();
        Ok(failures)
    }
    pub async fn prompt_text(&self, text: String) -> Result<RunResult> {
        let runtime = self.runtime.upgrade().ok_or(InProcessError::NotLoaded)?;
        let id = self.id;
        let expected = self.owner.clone();
        owned(async move {
            let owner = runtime.live_owner(id)?;
            if !expected.ptr_eq(&Arc::downgrade(&owner)) {
                return Err(InProcessError::NotLoaded);
            }
            owner.prompt(text).await
        })
        .await
    }
    /// Signal the current managed call only; it cannot poison a later call.
    pub fn cancel(&self) -> Result<bool> {
        let runtime = self.runtime.upgrade().ok_or(InProcessError::NotLoaded)?;
        let owner = runtime.live_owner(self.id)?;
        if !self.owner.ptr_eq(&Arc::downgrade(&owner)) {
            return Err(InProcessError::NotLoaded);
        }
        let control = owner.control();
        if let Some(cancel) = &control.active {
            cancel.cancel();
            return Ok(true);
        }
        Ok(false)
    }
}

impl HarnessRuntime {
    /// Rejection returns the original Harness so the caller keeps cleanup
    /// responsibility. A clone is never registered as a second Session owner.
    pub fn attach(
        self: &Arc<Self>,
        id: AgentId,
        harness: AgentHarness,
    ) -> std::result::Result<HarnessClient, (InProcessError, Box<AgentHarness>)> {
        self.attach_inner(id, harness, None)
    }
    pub fn attach_with_cleanup(
        self: &Arc<Self>,
        id: AgentId,
        harness: AgentHarness,
        cleanup: &HarnessCleanup,
    ) -> std::result::Result<HarnessClient, (InProcessError, Box<AgentHarness>)> {
        self.attach_inner(id, harness, Some(cleanup))
    }
    fn attach_inner(
        self: &Arc<Self>,
        id: AgentId,
        harness: AgentHarness,
        cleanup: Option<&HarnessCleanup>,
    ) -> std::result::Result<HarnessClient, (InProcessError, Box<AgentHarness>)> {
        let mut entries = self.entries();
        let error = if self.is_stopping() {
            Some(InProcessError::Stopping)
        } else if entries.contains_key(&id)
            || entries.values().any(|entry| {
                entry
                    .owner
                    .as_ref()
                    .is_some_and(|owner| owner.harness.session_id() == harness.session_id())
            })
        {
            Some(InProcessError::AlreadyOwned)
        } else if harness.active_operation_snapshot().is_some() {
            Some(InProcessError::Busy)
        } else {
            let storage = harness.session().storage();
            (!storage.is_ephemeral() && !storage.writer_lease_held())
                .then_some(InProcessError::WriterRequired)
        };
        if let Some(error) = error {
            return Err((error, Box::new(harness)));
        }
        let owner = Arc::new(ManagedHarness {
            harness,
            cleanup: cleanup.cloned(),
            operation: AsyncMutex::new(()),
            control: Mutex::new(Control::default()),
        });
        entries.insert(
            id,
            OwnerEntry {
                phase: OwnerPhase::Live,
                owner: Some(owner.clone()),
                failed_start: None,
            },
        );
        Ok(HarnessClient {
            runtime: Arc::downgrade(self),
            owner: Arc::downgrade(&owner),
            id,
        })
    }
    fn live_owner(&self, id: AgentId) -> Result<Arc<ManagedHarness>> {
        if self.is_stopping() {
            return Err(InProcessError::Stopping);
        }
        let entries = self.entries();
        let entry = entries.get(&id).ok_or(InProcessError::NotLoaded)?;
        if entry.phase != OwnerPhase::Live {
            return Err(InProcessError::Stopping);
        }
        entry.owner.clone().ok_or(InProcessError::Unsettled)
    }
    pub async fn close_harness(self: &Arc<Self>, id: AgentId) -> Result<()> {
        let runtime = self.clone();
        owned(async move {
            let _reservation = runtime.lifecycle.read().await;
            runtime.close_harness_inner(id).await
        })
        .await
    }
    async fn close_harness_inner(&self, id: AgentId) -> Result<()> {
        let owner = {
            let mut entries = self.entries();
            let entry = entries.get_mut(&id).ok_or(InProcessError::NotLoaded)?;
            if entry.phase == OwnerPhase::Closing {
                return Err(InProcessError::Busy);
            }
            let owner = entry.owner.clone().ok_or(InProcessError::Unsettled)?;
            owner.retire();
            entry.phase = OwnerPhase::Closing;
            owner
        };
        let result = AssertUnwindSafe(owner.close())
            .catch_unwind()
            .await
            .unwrap_or(Err(InProcessError::TaskStopped));
        let mut entries = self.entries();
        if result.is_ok() {
            entries.remove(&id);
        } else if let Some(entry) = entries.get_mut(&id) {
            entry.phase = OwnerPhase::CloseFailed;
        }
        result
    }
    pub async fn shutdown_harnesses(self: &Arc<Self>) -> Result<()> {
        self.begin_stopping();
        let runtime = self.clone();
        owned(async move {
            // Signal before waiting for the lifecycle writer, including calls
            // that have not yet reached native admission.
            for entry in runtime.entries().values() {
                if let Some(owner) = &entry.owner {
                    owner.retire();
                }
            }
            let _reservation = runtime.lifecycle.write().await;
            if runtime.is_closed() {
                return Ok(());
            }
            let ids: Vec<_> = runtime.entries().keys().copied().collect();
            let mut closes = FuturesUnordered::new();
            for id in ids {
                closes.push(runtime.close_harness_inner(id));
            }
            while closes.next().await.is_some() {}
            if runtime.owner_count() != 0 {
                return Err(InProcessError::Unsettled);
            }
            runtime.mark_closed();
            Ok(())
        })
        .await
    }
}

async fn owned<T: Send + 'static>(
    future: impl Future<Output = Result<T>> + Send + 'static,
) -> Result<T> {
    tokio::spawn(AssertUnwindSafe(future).catch_unwind())
        .await
        .map_err(|_| InProcessError::TaskStopped)?
        .map_err(|_| InProcessError::TaskStopped)?
}

#[cfg(test)]
mod tests;