rho-coding-agent 1.48.0

A lightweight agent harness inspired by Pi
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
//! Composes selected SDK tool bundles and shuts them down through one contract.

use std::{future::Future, path::PathBuf, pin::Pin, sync::Arc};

use rho_sdk::tool::Tool;

use crate::{
    agent::{AgentCapabilities, ToolCapability},
    config::Config,
    diagnostics::RuntimeDiagnostics,
};

use super::{
    advisor::AdvisorSessionStore,
    agent::{
        BackgroundSubagents, DelegationBundleOptions, DelegationToolSelection, SubagentManager,
    },
};

/// A feature-owned group of tools and any resources they need.
///
/// Bundles keep lifecycle ownership in the feature that creates each tool. The
/// boxed future is `Send` so callers can shut bundles down from async runtimes.
pub(crate) trait ToolBundle: Send + Sync {
    fn tools(&self) -> &[Arc<dyn Tool>];

    fn shutdown(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
        Box::pin(async {})
    }
}

/// A bundle for features which need no shutdown work.
pub(crate) struct StaticToolBundle {
    tools: Vec<Arc<dyn Tool>>,
}

impl StaticToolBundle {
    pub(crate) fn new(tools: Vec<Arc<dyn Tool>>) -> Self {
        Self { tools }
    }
}

impl ToolBundle for StaticToolBundle {
    fn tools(&self) -> &[Arc<dyn Tool>] {
        &self.tools
    }
}

#[derive(Clone, Debug)]
pub struct DelegationConfig {
    cwd: PathBuf,
    config_path: PathBuf,
    background: BackgroundSubagents,
    /// Catalog already discovered for `cwd`; the agent tool rediscovers when
    /// absent.
    catalog: Option<Arc<crate::agent::AgentCatalog>>,
}

impl DelegationConfig {
    pub fn new(
        cwd: PathBuf,
        config_path: PathBuf,
        background: BackgroundSubagents,
        catalog: Option<Arc<crate::agent::AgentCatalog>>,
    ) -> Self {
        Self {
            cwd,
            config_path,
            background,
            catalog,
        }
    }
}

#[derive(Clone)]
pub struct ToolSetOptions {
    capabilities: AgentCapabilities,
    advisor: Option<AdvisorSessionStore>,
    delegation: Option<DelegationConfig>,
    workflow: Option<Arc<dyn super::workflow::WorkflowToolService>>,
    workflow_tracker: super::workflow_tracker::WorkflowRunTracker,
}

impl Default for ToolSetOptions {
    fn default() -> Self {
        Self::new(AgentCapabilities::all_host_tools())
    }
}

impl ToolSetOptions {
    pub fn new(capabilities: AgentCapabilities) -> Self {
        Self {
            capabilities,
            advisor: None,
            delegation: None,
            workflow: None,
            workflow_tracker: super::workflow_tracker::WorkflowRunTracker::new(),
        }
    }

    /// Supplies the session store the `advisor` tool reads. Without it the
    /// capability alone registers no tool, so runs with no live session to
    /// review cannot offer the advisor.
    ///
    /// The store is kept whether or not advisor mode is on, so a later
    /// `/advisor on` registers the tool without rebuilding the tool set.
    pub fn advisor(mut self, store: AdvisorSessionStore) -> Self {
        self.advisor = Some(store);
        self
    }

    pub fn delegation(mut self, config: DelegationConfig) -> Self {
        self.delegation = Some(config);
        self
    }

    pub(crate) fn workflow(
        mut self,
        service: Arc<dyn super::workflow::WorkflowToolService>,
    ) -> Self {
        self.workflow = Some(service);
        self
    }

    pub(crate) fn workflow_tracker(
        mut self,
        tracker: super::workflow_tracker::WorkflowRunTracker,
    ) -> Self {
        self.workflow_tracker = tracker;
        self
    }
}

/// The `advisor` tool and the store it reads, held whether or not the tool is
/// currently advertised.
///
/// Advisor mode toggles mid-session, and the executor must never see a tool the
/// run does not have, so registration is a state transition on the built tool
/// set rather than a reason to rebuild it.
struct AdvisorTools {
    store: AdvisorSessionStore,
    tool: Arc<dyn Tool>,
    registered: bool,
}

pub struct AppToolSet {
    tools: Vec<Arc<dyn Tool>>,
    bundles: Vec<Box<dyn ToolBundle>>,
    advisor: Option<AdvisorTools>,
    subagents: Option<SubagentManager>,
    processes: Option<super::process::ProcessManager>,
    workflow_tracker: super::workflow_tracker::WorkflowRunTracker,
    checkpoint_tracker: Arc<crate::session::workspace_checkpoint::WorkspaceCheckpointTracker>,
    web_access: super::web::WebAccessStore,
    mcp_report: super::mcp::McpSessionReport,
    mcp_catalog: super::mcp::McpCatalog,
    file_view: rho_tools::FileViewPolicy,
}

impl AppToolSet {
    pub fn disabled() -> Self {
        Self {
            tools: Vec::new(),
            bundles: Vec::new(),
            advisor: None,
            subagents: None,
            processes: None,
            workflow_tracker: super::workflow_tracker::WorkflowRunTracker::new(),
            checkpoint_tracker: Arc::new(
                crate::session::workspace_checkpoint::WorkspaceCheckpointTracker::new(false),
            ),
            web_access: super::web::WebAccessStore::new(),
            mcp_report: super::mcp::McpSessionReport::default(),
            mcp_catalog: super::mcp::McpCatalog::default(),
            file_view: rho_tools::FileViewPolicy::default(),
        }
    }

    pub fn new(config: &Config, diagnostics: RuntimeDiagnostics, options: ToolSetOptions) -> Self {
        let ToolSetOptions {
            capabilities,
            advisor,
            delegation,
            workflow,
            workflow_tracker,
        } = options;
        let mut tool_set = Self::disabled();
        tool_set.workflow_tracker = workflow_tracker;
        tool_set.checkpoint_tracker = Arc::new(
            crate::session::workspace_checkpoint::WorkspaceCheckpointTracker::new(
                config.experimental_workspace_rewind,
            ),
        );
        // Compose one child-process environment policy for every process tool.
        // Provider credential env vars are excluded so agent commands cannot
        // read host API keys from the ambient environment.
        let process_environment =
            rho_sdk::ProcessEnvironment::inherit_except(rho_providers::credential_env_vars());

        let edit_format = config.edit_tool.resolve(&config.provider);
        tool_set.file_view.set(edit_format);
        tool_set.add_bundle(super::coding::sdk_bundle(
            &capabilities,
            config.max_output_bytes,
            process_environment.clone(),
            tool_set.checkpoint_tracker.clone(),
            tool_set.file_view.clone(),
        ));
        if capabilities.contains(&ToolCapability::Process) {
            let bundle = super::process::sdk_bundle(
                config.max_output_bytes,
                process_environment.clone(),
                tool_set.checkpoint_tracker.clone(),
            );
            tool_set.processes = Some(bundle.manager_handle());
            tool_set.add_bundle(bundle);
        }
        if capabilities.contains(&ToolCapability::Skill) {
            tool_set.add_bundle(super::sdk_features::skill_bundle(config.max_output_bytes));
        }
        if capabilities.contains(&ToolCapability::Rho) {
            tool_set.add_bundle(super::rho::sdk_bundle(diagnostics, config.max_output_bytes));
        }
        if capabilities.contains(&ToolCapability::Questionnaire) {
            tool_set.add_bundle(super::sdk_features::questionnaire_bundle());
        }
        if let (true, Some(store)) = (capabilities.contains(&ToolCapability::Advisor), advisor) {
            // The tool set owns advisor initialization: the model and the
            // registration state both come from the same config read.
            store.set_model(super::advisor::advisor_model(config).cloned());
            tool_set.advisor = Some(AdvisorTools {
                tool: super::advisor::advisor_tool(store.clone()),
                store,
                registered: false,
            });
            tool_set.set_advisor_registered(super::advisor::advisor_available(config));
        }
        if let (true, Some(service)) = (capabilities.contains(&ToolCapability::Workflow), workflow)
        {
            tool_set.add_bundle(super::workflow::sdk_bundle(
                service,
                config.max_output_bytes,
            ));
        }
        #[cfg(debug_assertions)]
        if capabilities.contains(&ToolCapability::Extension(super::tui_fixture::NAME.into())) {
            if let Some(bundle) = super::tui_fixture::sdk_bundle() {
                tool_set.add_bundle(bundle);
            }
        }
        let web_access = tool_set.web_access.clone();
        tool_set.add_bundle(super::web::sdk_bundle(
            config,
            &capabilities,
            process_environment,
            web_access,
        ));

        let delegation_tools = DelegationToolSelection::from_capabilities(&capabilities);
        if let (Some(selection), Some(delegation)) = (delegation_tools, delegation) {
            let bundle = super::agent::sdk_bundle(
                config,
                DelegationBundleOptions {
                    cwd: delegation.cwd,
                    tools: selection,
                    config_path: delegation.config_path,
                    background: delegation.background,
                    catalog: delegation.catalog,
                },
                tool_set.checkpoint_tracker.clone(),
            );
            tool_set.subagents = Some(bundle.manager_handle());
            tool_set.add_bundle(bundle);
        }

        tool_set
    }

    /// Attach MCP inventory and any live tool bundle from session assembly.
    pub(crate) fn with_mcp(mut self, outcome: super::mcp::McpConnectOutcome) -> Self {
        self.attach_mcp(outcome);
        self
    }

    /// Attach a finished MCP connect onto an already-built tool set.
    pub(crate) fn attach_mcp(&mut self, outcome: super::mcp::McpConnectOutcome) {
        self.mcp_report = outcome.report;
        self.mcp_catalog = outcome.catalog;
        if let Some(bundle) = outcome.bundle {
            self.add_bundle(bundle);
        }
    }

    pub(crate) fn add_bundle(&mut self, bundle: impl ToolBundle + 'static) {
        self.tools.extend(bundle.tools().iter().cloned());
        self.bundles.push(Box::new(bundle));
    }

    /// Prompts and resources connected servers offer the user.
    pub(crate) fn mcp_catalog(&self) -> &super::mcp::McpCatalog {
        &self.mcp_catalog
    }

    pub(crate) fn mcp_report(&self) -> &super::mcp::McpSessionReport {
        &self.mcp_report
    }

    pub fn tools(&self) -> &[Arc<dyn Tool>] {
        &self.tools
    }

    pub fn specs(&self) -> Vec<rho_sdk::model::ToolSpec> {
        self.tools.iter().map(|tool| tool.spec()).collect()
    }

    /// Returns registry names without applying any additional capability filter.
    pub fn unfiltered_names(&self) -> impl Iterator<Item = String> + '_ {
        self.tools.iter().map(|tool| tool.spec().name)
    }

    pub fn contains(&self, name: &str) -> bool {
        self.unfiltered_names().any(|registered| registered == name)
    }

    /// Test-only: append a tool without capability filtering.
    #[cfg(test)]
    pub(crate) fn push_tool_for_tests(&mut self, tool: Arc<dyn Tool>) {
        self.tools.push(tool);
    }

    /// The advisor's session store, present whenever the run may offer the
    /// advisor, even while advisor mode is off.
    pub fn advisor(&self) -> Option<&AdvisorSessionStore> {
        self.advisor.as_ref().map(|advisor| &advisor.store)
    }

    /// Whether the `advisor` tool is currently advertised to the model.
    pub fn advisor_registered(&self) -> bool {
        self.advisor
            .as_ref()
            .is_some_and(|advisor| advisor.registered)
    }

    /// Adds or removes the `advisor` tool, so `/advisor` reaches the next
    /// runtime build without disturbing tools that hold live state.
    ///
    /// Returns whether the advertised tool list changed; callers rebuild the
    /// runtime only then.
    pub fn set_advisor_registered(&mut self, registered: bool) -> bool {
        let Some(advisor) = self.advisor.as_mut() else {
            return false;
        };
        if advisor.registered == registered {
            return false;
        }
        advisor.registered = registered;
        let tool = Arc::clone(&advisor.tool);
        if registered {
            self.tools.push(tool);
        } else {
            self.tools.retain(|existing| !Arc::ptr_eq(existing, &tool));
        }
        true
    }

    /// Replaces the advertised built-in file edit tool.
    ///
    /// Returns the previous format when the advertised tool list changed so
    /// callers can rebuild the runtime and tell the model. No-ops when this
    /// run has no edit tool or the selection is already active.
    ///
    /// Matches only canonical built-in names (`edit`, `apply_patch`,
    /// `str_replace`). Legacy aliases such as `edit_file` still classify as
    /// edit for transcripts via [`rho_tools::EditFormat::is_edit_tool_name`],
    /// but are never treated as the swappable built-in slot.
    pub fn set_edit_tool(
        &mut self,
        edit_tool: rho_tools::EditFormat,
        max_output_bytes: usize,
    ) -> Option<rho_tools::EditFormat> {
        let previous = self.edit_tool()?;
        if previous == edit_tool {
            return None;
        }
        let position = self
            .tools
            .iter()
            .position(|tool| is_canonical_edit_tool_name(tool.spec().name.as_str()))?;
        let mutation_observer: Arc<dyn rho_tools::WorkspaceMutationObserver> =
            self.checkpoint_tracker.clone();
        self.tools[position] =
            super::coding::edit_tool(edit_tool, max_output_bytes, mutation_observer);
        self.file_view.set(edit_tool);
        Some(previous)
    }

    #[cfg(test)]
    pub(crate) fn file_view_style(&self) -> rho_tools::FileViewStyle {
        self.file_view.style()
    }

    /// The currently advertised built-in edit format, when this run exposes one.
    pub fn edit_tool(&self) -> Option<rho_tools::EditFormat> {
        self.tools.iter().find_map(|tool| {
            let name = tool.spec().name;
            rho_tools::EditFormat::ALL
                .iter()
                .copied()
                .find(|format| format.tool_name() == name.as_str())
        })
    }

    pub fn subagents(&self) -> Option<&SubagentManager> {
        self.subagents.as_ref()
    }

    pub fn processes(&self) -> Option<&super::process::ProcessManager> {
        self.processes.as_ref()
    }

    pub fn workflow_tracker(&self) -> &super::workflow_tracker::WorkflowRunTracker {
        &self.workflow_tracker
    }

    pub fn checkpoint_tracker(
        &self,
    ) -> &Arc<crate::session::workspace_checkpoint::WorkspaceCheckpointTracker> {
        &self.checkpoint_tracker
    }

    pub fn web_access(&self) -> &super::web::WebAccessStore {
        &self.web_access
    }

    pub async fn shutdown(&self) {
        for bundle in &self.bundles {
            bundle.shutdown().await;
        }
    }
}

/// Whether `name` is a canonical built-in edit tool name (`edit`,
/// `apply_patch`, `str_replace`). Excludes legacy aliases such as `edit_file`.
fn is_canonical_edit_tool_name(name: &str) -> bool {
    rho_tools::EditFormat::ALL
        .iter()
        .any(|format| format.tool_name() == name)
}

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