rho-coding-agent 3.7.0

A fast Rust agent harness with a small footprint and opinionated defaults
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
//! A loaded session's background work: the processes, workflow runs and
//! subagents its turns started that run on their own. Its clients follow it
//! as [`wire::SessionEvent::Background`], list every session's with
//! [`wire::SESSION_BACKGROUND_LIST`], and stop one piece with
//! [`wire::SESSION_BACKGROUND_STOP`].
//!
//! The work is reported as a turn begins, so a client hears how the work a
//! delivery turn takes ended first; as a turn ends, so it hears what the
//! turn left running first; and by one watcher per loaded session as work
//! starts or ends, turn or no turn. Each report reads the work under the
//! feed's lock, so two reporters never publish out of order. The watcher
//! reports the session's activity with it (see `settle`), since work that
//! starts or ends may change what the session waits on.

use std::sync::{Arc, Mutex, PoisonError, Weak};

use rho_protocol::{self as wire, ErrorCode, RpcError};
use rho_sdk::CancellationToken;
use tokio::sync::watch;

use super::{awaited::AwaitedWork, LoadedSession, Sessions};
use crate::{
    app::{
        peer_sessions::PeerInbox,
        pull_requests::PullRequestInbox,
        session_runtime::{BackgroundWait, SessionRuntime},
        subagent_manager::{BackgroundChild, SubagentManager},
    },
    subagent::RunState,
    tools::{
        process::{LiveProcessSummary, ProcessManager, State, STOP_GRACE},
        ui::SurfaceHost,
        workflow_tracker::{WorkflowRailSummary, WorkflowRunTracker},
    },
    workflow::{RunLifecycle, WorkflowOutcome},
};

/// A loaded session's background work, read without its runtime's lock.
pub(super) struct SessionBackground {
    /// The parent id the session's workflow runs and subagents report to.
    session_id: String,
    /// How the session waits for its work, which decides what of it wakes
    /// the idle session.
    wait: BackgroundWait,
    sources: Mutex<Sources>,
    workflows: WorkflowRunTracker,
    /// Counts replacements of the sources, so the watchers that read them
    /// wait on the new ones.
    replaced: watch::Sender<u64>,
    /// Cancelled when the session is dropped, which ends the watcher.
    dropped: CancellationToken,
}

/// The session's tools that run work of their own, when it has them. A
/// reconfigured tool set may replace them.
#[derive(Clone, Default)]
pub(super) struct Sources {
    pub(super) processes: Option<ProcessManager>,
    pub(super) subagents: Option<SubagentManager>,
    pub(super) surfaces: Option<SurfaceHost>,
    pub(super) pull_requests: Option<PullRequestInbox>,
    /// Notices of the turns it started in other sessions (see `peer_wake`).
    pub(super) peers: Option<PeerInbox>,
}

impl Sources {
    fn of(runtime: &SessionRuntime) -> Self {
        Self {
            processes: runtime.tools().processes().cloned(),
            subagents: runtime.tools().subagents().cloned(),
            surfaces: runtime.tools().surfaces().cloned(),
            pull_requests: runtime.tools().pull_requests().cloned(),
            peers: runtime.peer_sessions().cloned(),
        }
    }
}

impl SessionBackground {
    pub(super) fn of(runtime: &SessionRuntime) -> Self {
        Self {
            session_id: runtime.session_id().to_string(),
            wait: runtime.background_wait(),
            sources: Mutex::new(Sources::of(runtime)),
            workflows: runtime.tools().workflow_tracker().clone(),
            replaced: watch::Sender::new(0),
            dropped: CancellationToken::new(),
        }
    }

    /// The background work of session `session_id`, which waits for it as
    /// `wait`, from these managers rather than a runtime's tools.
    #[cfg(test)]
    pub(super) fn for_tests(
        session_id: &str,
        wait: BackgroundWait,
        processes: Option<ProcessManager>,
        subagents: Option<SubagentManager>,
    ) -> Self {
        Self {
            session_id: session_id.to_owned(),
            wait,
            sources: Mutex::new(Sources {
                processes,
                subagents,
                ..Sources::default()
            }),
            workflows: WorkflowRunTracker::new(),
            replaced: watch::Sender::new(0),
            dropped: CancellationToken::new(),
        }
    }

    /// Takes the tools `runtime` has now, as after a configure.
    pub(super) fn refresh(&self, runtime: &SessionRuntime) {
        *self.sources.lock().unwrap_or_else(PoisonError::into_inner) = Sources::of(runtime);
        self.replaced.send_modify(|count| *count += 1);
    }

    /// Follows replacements of the sources, as after a configure: one
    /// that follows this call marks the receiver changed.
    pub(super) fn replacements(&self) -> watch::Receiver<u64> {
        self.replaced.subscribe()
    }

    /// The session's A2UI surfaces, when it offers the `ui` tool.
    pub(super) fn surfaces(&self) -> Option<SurfaceHost> {
        self.sources().surfaces
    }

    /// The session's process manager, when its tools have one.
    pub(super) fn processes(&self) -> Option<ProcessManager> {
        self.sources().processes
    }

    /// The workflow runs the session started.
    pub(super) fn workflows(&self) -> &WorkflowRunTracker {
        &self.workflows
    }

    /// The work the daemon or the session's host wakes the idle session
    /// for, from the session's tools now; `None` for a session nothing
    /// wakes, whose prompts hold until their work finishes.
    pub(super) fn awaited(&self) -> Option<AwaitedWork> {
        AwaitedWork::new(
            self.wait,
            self.session_id.clone(),
            self.workflows.clone(),
            self.sources(),
        )
    }

    fn sources(&self) -> Sources {
        self.sources
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clone()
    }

    /// The work as a client sees it: processes, then workflow runs, then
    /// subagents, each oldest first.
    fn snapshot(&self) -> Vec<wire::BackgroundWork> {
        let sources = self.sources();
        let processes = sources
            .processes
            .iter()
            .flat_map(ProcessManager::live_summaries)
            .map(process_work);
        let workflows = self
            .workflows
            .rail_summaries(&self.session_id)
            .into_iter()
            .map(workflow_work);
        let subagents = sources
            .subagents
            .iter()
            .flat_map(|subagents| subagents.background(&self.session_id))
            .map(subagent_work);
        processes.chain(workflows).chain(subagents).collect()
    }

    /// Resolves once a process, subagent or workflow run may have started
    /// or ended from now on.
    fn changes(&self) -> impl std::future::Future<Output = ()> + Send + 'static {
        // Before reading the sources, so a replacement after the read still
        // resolves it.
        let mut replaced = self.replacements();
        let sources = self.sources();
        // Subscribed here, not at the first poll.
        let exited = sources
            .processes
            .as_ref()
            .map(ProcessManager::notified_owned);
        let started = sources
            .processes
            .as_ref()
            .map(ProcessManager::started_owned);
        let subagents = sources.subagents.as_ref().map(SubagentManager::changed);
        let finished = self.workflows.notified_owned();
        let launched = self.workflows.started_owned();
        async move {
            let replaced = async move {
                // The sender lives as long as the session.
                let _ = replaced.changed().await;
            };
            tokio::select! {
                () = or_pending(exited) => {}
                () = or_pending(started) => {}
                () = or_pending(subagents) => {}
                () = finished => {}
                () = launched => {}
                () = replaced => {}
            }
        }
    }

    /// Asks `item` to stop. Fails with invalid params for work the session
    /// does not know or that already ended.
    async fn stop(&self, item: &wire::BackgroundItem) -> Result<(), String> {
        let sources = self.sources();
        match item {
            wire::BackgroundItem::Process { id } => {
                let processes = sources.processes.ok_or("the session runs no processes")?;
                processes.stop(id, STOP_GRACE).await
            }
            wire::BackgroundItem::Workflow { run } => {
                let row = self
                    .workflows
                    .rail_summaries(&self.session_id)
                    .into_iter()
                    .find(|row| row.run_id == *run && row.is_live())
                    .ok_or_else(|| format!("no running workflow run '{run}'"))?;
                let run_id = run.parse().map_err(|error| format!("{error}"))?;
                let rho_home = crate::paths::rho_dir().map_err(|error| format!("{error:#}"))?;
                crate::app::workflow_cli::request_cancellation(&rho_home, run_id, row.lifecycle)
                    .await
                    .map(drop)
                    .map_err(|error| error.to_string())
            }
            wire::BackgroundItem::Subagent { run, .. } => sources
                .subagents
                .ok_or("the session runs no subagents")?
                .cancel(&self.session_id, run)
                .map_err(|error| error.to_string()),
            _ => Err("this daemon does not know that kind of work".into()),
        }
    }
}

impl Drop for SessionBackground {
    fn drop(&mut self) {
        self.dropped.cancel();
    }
}

async fn or_pending(future: Option<impl std::future::Future<Output = ()>>) {
    match future {
        Some(future) => future.await,
        None => std::future::pending().await,
    }
}

impl LoadedSession {
    /// Tells the session's clients its background work, if it changed
    /// since they were last told.
    pub(super) fn report_background(&self) {
        self.feed.update_background(|| self.background.snapshot());
    }
}

impl Sessions {
    /// Starts `session`'s background watcher. Call once, as the session is
    /// added.
    pub(super) fn watch_background(&self, session: &Arc<LoadedSession>) {
        let watcher = watch(Arc::downgrade(session));
        let dropped = session.background.dropped.clone();
        tokio::spawn(async move {
            tokio::select! {
                () = dropped.cancelled() => {}
                () = watcher => {}
            }
        });
    }

    /// `rho/session/background/list`: each loaded session with background
    /// work, by session id.
    pub(in crate::app::daemon_host) fn background_list(
        &self,
    ) -> wire::SessionBackgroundListResponse {
        // Copied out first, so the daemon-wide map is not held while each
        // feed's list is cloned.
        let loaded: Vec<_> = self
            .loaded()
            .iter()
            .map(|(session, loaded)| (session.clone(), Arc::clone(loaded)))
            .collect();
        let mut sessions: Vec<_> = loaded
            .into_iter()
            .map(|(session, loaded)| wire::SessionBackground {
                session: wire::SessionId::new(session.to_string()),
                work: loaded.feed.background(),
            })
            .filter(|listed| !listed.work.is_empty())
            .collect();
        sessions.sort_by(|a, b| a.session.cmp(&b.session));
        wire::SessionBackgroundListResponse { sessions }
    }

    /// `rho/session/background/stop`.
    pub(in crate::app::daemon_host) async fn background_stop(
        &self,
        params: wire::SessionBackgroundStopParams,
    ) -> Result<wire::SessionBackgroundStopResponse, RpcError> {
        let session = self.background_session(&params.session).await?;
        session
            .background
            .stop(&params.work)
            .await
            .map_err(|message| {
                RpcError::new(
                    ErrorCode::InvalidParams,
                    format!("could not stop background work: {message}"),
                )
            })?;
        Ok(wire::SessionBackgroundStopResponse {})
    }

    /// `rho/session/background/hold`.
    pub(in crate::app::daemon_host) async fn background_hold(
        &self,
        params: wire::SessionBackgroundHoldParams,
    ) -> Result<wire::SessionBackgroundHoldResponse, RpcError> {
        self.background_session(&params.session).await?.hold_wake();
        Ok(wire::SessionBackgroundHoldResponse {})
    }

    /// The loaded `session` whose background work a request is about.
    async fn background_session(
        &self,
        session: &wire::SessionId,
    ) -> Result<Arc<LoadedSession>, RpcError> {
        self.refuse_when_stopping()?;
        self.loaded_session(session).await?.ok_or_else(|| {
            RpcError::new(
                ErrorCode::SessionNotLoaded,
                "the daemon has not loaded the session; attach to it first",
            )
        })
    }
}

/// Reports `session`'s background work each time it may have changed,
/// until the session is gone. Holds the session weakly, so it never keeps
/// one loaded.
async fn watch(session: Weak<LoadedSession>) {
    loop {
        // Subscribed before the report, so a change in between still wakes
        // the next look.
        let changes = {
            let Some(session) = session.upgrade() else {
                return;
            };
            let changes = session.background.changes();
            session.report_background();
            session.report_activity();
            changes
        };
        changes.await;
    }
}

fn process_work(process: LiveProcessSummary) -> wire::BackgroundWork {
    let status = match process.state {
        State::Starting | State::Running => wire::BackgroundStatus::Running,
        State::Exited => wire::BackgroundStatus::Completed,
        State::Terminated => wire::BackgroundStatus::Cancelled,
        State::TimedOut | State::FailedToStart => wire::BackgroundStatus::Failed,
    };
    wire::BackgroundWork {
        item: wire::BackgroundItem::Process {
            id: process.process_id,
        },
        title: process.command,
        status,
        exit_code: process.exit_code,
    }
}

fn workflow_work(run: WorkflowRailSummary) -> wire::BackgroundWork {
    let status = if run.is_live() {
        wire::BackgroundStatus::Running
    } else if run.outcome == Some(WorkflowOutcome::Cancellation) {
        wire::BackgroundStatus::Cancelled
    } else if run.is_failure() || run.lifecycle != RunLifecycle::Completed {
        // A run that stopped short of completing failed, whatever its
        // outcome says.
        wire::BackgroundStatus::Failed
    } else {
        wire::BackgroundStatus::Completed
    };
    wire::BackgroundWork {
        item: wire::BackgroundItem::Workflow { run: run.run_id },
        title: run.workflow_name,
        status,
        exit_code: None,
    }
}

fn subagent_work(child: BackgroundChild) -> wire::BackgroundWork {
    let status = match child.state {
        RunState::Queued | RunState::Starting | RunState::Running => {
            wire::BackgroundStatus::Running
        }
        RunState::Ok => wire::BackgroundStatus::Completed,
        RunState::Error => wire::BackgroundStatus::Failed,
        RunState::Stopped => wire::BackgroundStatus::Cancelled,
    };
    wire::BackgroundWork {
        item: wire::BackgroundItem::Subagent {
            run: child.run,
            session: child
                .session
                .map(|session| wire::SessionId::new(session.to_string())),
        },
        title: child.title,
        status,
        exit_code: None,
    }
}