rho-coding-agent 2.26.0

A fast Rust agent harness with a small footprint and opinionated defaults
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
//! Local plugin install/link/remove/enable tests.

use std::path::{Path, PathBuf};

use tempfile::TempDir;

use super::{inspect_source, install, remove, set_enabled, InstallMode};
use crate::plugins::{
    discover_with_trust, state::PluginScope, PluginOrigin, PluginStatus, ProjectTrust,
};

const SCHEMA: &str = "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json";

pub(super) struct Env {
    pub(super) home: TempDir,
    pub(super) project: TempDir,
    pub(super) rho_home: TempDir,
}

pub(super) fn env() -> Env {
    let home = TempDir::new().unwrap();
    let project = TempDir::new().unwrap();
    let rho_home = TempDir::new().unwrap();
    std::fs::create_dir(project.path().join(".git")).unwrap();
    Env {
        home,
        project,
        rho_home,
    }
}

pub(super) fn write_source(root: &Path, name: &str) -> PathBuf {
    let dir = root.join(name);
    std::fs::create_dir_all(dir.join("skills").join("hello")).unwrap();
    std::fs::write(
        dir.join("plugin.json"),
        format!(
            r#"{{"$schema":"{SCHEMA}","name":"{name}","version":"1.2.3","description":"demo plugin"}}"#
        ),
    )
    .unwrap();
    std::fs::write(
        dir.join("skills/hello/SKILL.md"),
        "---\nname: hello\ndescription: greets\n---\nbody\n",
    )
    .unwrap();
    dir
}

// Covers: install validates the package then copies into the managed user root.
// Owner: plugin install policy.
#[test]
fn install_copies_into_user_root_after_validation() {
    let env = env();
    let source_root = TempDir::new().unwrap();
    let source = write_source(source_root.path(), "demo");

    let package = install(
        &source,
        PluginScope::User,
        InstallMode::Copy,
        /* force */ false,
        env.project.path(),
        env.home.path(),
        Some(env.rho_home.path()),
    )
    .unwrap();

    assert_eq!(package.name, "demo");
    assert_eq!(package.origin, PluginOrigin::Install);
    assert_eq!(package.version.as_deref(), Some("1.2.3"));
    let installed = env.home.path().join(".agents/plugins/demo/plugin.json");
    assert!(installed.is_file());
    // Source remains untouched.
    assert!(source.join("plugin.json").is_file());

    let discovery = discover_with_trust(
        env.project.path(),
        Some(env.home.path()),
        Some(env.rho_home.path()),
        ProjectTrust::Trusted,
    );
    let entry = discovery.report.find("demo").unwrap();
    assert_eq!(entry.status, PluginStatus::Loaded);
    assert_eq!(entry.origin, PluginOrigin::Install);
    assert_eq!(entry.skill_names, ["hello"]);
}

// Covers: relative source paths resolve against the invocation directory.
// Owner: plugin install source resolution.
#[test]
fn relative_source_paths_resolve_against_cwd() {
    let env = env();
    write_source(&env.project.path().join("pkg"), "copied");
    write_source(&env.project.path().join("pkg"), "linked");
    let cases = [
        ("pkg/copied", InstallMode::Copy, PluginOrigin::Install),
        ("pkg/linked", InstallMode::Link, PluginOrigin::Link),
    ];
    for (relative, mode, origin) in cases {
        let package = install(
            Path::new(relative),
            PluginScope::User,
            mode,
            /* force */ false,
            env.project.path(),
            env.home.path(),
            Some(env.rho_home.path()),
        )
        .unwrap();
        assert_eq!(package.origin, origin, "{relative}");
        assert!(package.path.join("plugin.json").is_file(), "{relative}");
    }
}

// Covers: invalid packages never reach a managed root.
// Owner: plugin install validation.
#[test]
fn install_rejects_invalid_package_before_copy() {
    let env = env();
    let source_root = TempDir::new().unwrap();
    let bad = source_root.path().join("bad");
    std::fs::create_dir_all(&bad).unwrap();
    std::fs::write(bad.join("plugin.json"), r#"{"name":"bad"}"#).unwrap();

    let error = install(
        &bad,
        PluginScope::User,
        InstallMode::Copy,
        /* force */ false,
        env.project.path(),
        env.home.path(),
        Some(env.rho_home.path()),
    )
    .unwrap_err();

    assert!(error.to_string().contains("invalid plugin package"));
    assert!(!env.home.path().join(".agents/plugins/bad").exists());
}

// Covers: conflicting destinations require an explicit --force replacement.
// Owner: plugin install conflict policy.
#[test]
fn install_conflict_requires_force() {
    let env = env();
    let source_root = TempDir::new().unwrap();
    let source = write_source(source_root.path(), "demo");
    install(
        &source,
        PluginScope::User,
        InstallMode::Copy,
        /* force */ false,
        env.project.path(),
        env.home.path(),
        Some(env.rho_home.path()),
    )
    .unwrap();

    let error = install(
        &source,
        PluginScope::User,
        InstallMode::Copy,
        /* force */ false,
        env.project.path(),
        env.home.path(),
        Some(env.rho_home.path()),
    )
    .unwrap_err();
    assert!(error.to_string().contains("--force"));

    install(
        &source,
        PluginScope::User,
        InstallMode::Copy,
        /* force */ true,
        env.project.path(),
        env.home.path(),
        Some(env.rho_home.path()),
    )
    .unwrap();
}

// Covers: project scope is refused when its project root is $HOME, before
// anything is written to either scope, including a first install where $HOME
// is spelled through a symlink but cwd is physical. User scope still installs.
// Owner: plugin install scope policy.
#[test]
fn project_install_from_home_is_refused() {
    // (label, home is a git repository, cwd relative to home, $HOME via symlink)
    let cases = [
        ("no repository", false, "", false),
        ("dotfiles repository", true, "notes", false),
        #[cfg(unix)]
        ("symlinked home", false, "", true),
    ];
    for (label, repository, relative_cwd, symlinked) in cases {
        let physical = TempDir::new().unwrap();
        let rho_home = TempDir::new().unwrap();
        let source_root = TempDir::new().unwrap();
        let source = write_source(source_root.path(), "demo");
        if repository {
            std::fs::create_dir(physical.path().join(".git")).unwrap();
        }
        let cwd = physical.path().join(relative_cwd);
        std::fs::create_dir_all(&cwd).unwrap();
        let alias = TempDir::new().unwrap();
        let home = if symlinked {
            let link = alias.path().join("home");
            #[cfg(unix)]
            std::os::unix::fs::symlink(physical.path(), &link).unwrap();
            link
        } else {
            physical.path().to_path_buf()
        };

        let install_as = |scope| {
            install(
                &source,
                scope,
                InstallMode::Copy,
                /* force */ false,
                &cwd,
                &home,
                Some(rho_home.path()),
            )
        };

        let project = install_as(PluginScope::Project);
        let untouched =
            !physical.path().join(".agents").exists() && !physical.path().join(".rho").exists();
        // Control: the same setup installs at user scope, so the refusal is the scope.
        let user = install_as(PluginScope::User);

        assert_eq!(
            (project.is_err(), untouched, user.is_ok()),
            (true, true, true),
            "{label}"
        );
    }
}

// Covers: in a repository rooted at $HOME, a nested project root keeps its
// activation state even though the repository root itself is user scope.
// Owner: plugin state store for a project that shares the user plugins root.
#[test]
fn dotfiles_nested_project_plugin_disable_applies() {
    let home = TempDir::new().unwrap();
    let rho_home = TempDir::new().unwrap();
    std::fs::create_dir(home.path().join(".git")).unwrap();
    let cwd = home.path().join("notes");
    write_source(&cwd.join(".agents/plugins"), "nested");
    let status = || {
        let discovery = discover_with_trust(
            &cwd,
            Some(home.path()),
            Some(rho_home.path()),
            ProjectTrust::Trusted,
        );
        let entry = discovery.report.find("nested").unwrap();
        (entry.scope, entry.status)
    };

    set_enabled(
        "nested",
        /* enabled */ false,
        &cwd,
        Some(home.path()),
        Some(rho_home.path()),
    )
    .unwrap();

    assert_eq!(status(), (PluginScope::Project, PluginStatus::Disabled));
}

// Covers: from $HOME with a separate RHO_HOME, project state that older builds
// wrote there keeps its disables until `enable`, and an unreadable copy is
// ignored instead of failing user-scope commands.
// Owner: plugin state store for a project that shares the user plugins root.
#[test]
fn home_project_state_from_older_builds() {
    // (label, $HOME/.rho/plugins.toml, status before enable)
    let cases = [
        (
            "disable kept",
            "version = 1\n[plugins.demo]\nenabled = false\n",
            PluginStatus::Disabled,
        ),
        ("unreadable ignored", "not toml [", PluginStatus::Loaded),
    ];
    for (label, legacy, before) in cases {
        let home = TempDir::new().unwrap();
        let rho_home = TempDir::new().unwrap();
        write_source(&home.path().join(".agents/plugins"), "demo");
        let legacy_path = home.path().join(".rho/plugins.toml");
        std::fs::create_dir_all(legacy_path.parent().unwrap()).unwrap();
        std::fs::write(&legacy_path, legacy).unwrap();
        let status = || {
            let discovery = discover_with_trust(
                home.path(),
                Some(home.path()),
                Some(rho_home.path()),
                ProjectTrust::Untrusted,
            );
            let entry = discovery.report.find("demo").unwrap();
            (entry.scope, entry.status)
        };

        let initial = status();
        set_enabled(
            "demo",
            /* enabled */ true,
            home.path(),
            Some(home.path()),
            Some(rho_home.path()),
        )
        .unwrap();

        assert_eq!(
            (initial, status()),
            (
                (PluginScope::User, before),
                (PluginScope::User, PluginStatus::Loaded)
            ),
            "{label}"
        );
    }
}

// Covers: link creates a managed symlink and records link origin.
// Owner: plugin link policy.
#[cfg(unix)]
#[test]
fn link_creates_managed_symlink() {
    let env = env();
    let source_root = TempDir::new().unwrap();
    let source = write_source(source_root.path(), "linked");

    let package = install(
        &source,
        PluginScope::Project,
        InstallMode::Link,
        /* force */ false,
        env.project.path(),
        env.home.path(),
        Some(env.rho_home.path()),
    )
    .unwrap();

    let dest = env.project.path().join(".agents/plugins/linked");
    assert_eq!(package.origin, PluginOrigin::Link);
    assert!(std::fs::symlink_metadata(&dest)
        .unwrap()
        .file_type()
        .is_symlink());
    assert_eq!(
        std::fs::canonicalize(&dest).unwrap(),
        std::fs::canonicalize(&source).unwrap()
    );
}

// Covers: disable keeps package files but drops components from discovery.
// Owner: plugin activation policy.
#[test]
fn disable_keeps_package_and_drops_session_components() {
    let env = env();
    let source_root = TempDir::new().unwrap();
    let source = write_source(source_root.path(), "demo");
    install(
        &source,
        PluginScope::User,
        InstallMode::Copy,
        /* force */ false,
        env.project.path(),
        env.home.path(),
        Some(env.rho_home.path()),
    )
    .unwrap();

    set_enabled(
        "demo",
        /* enabled */ false,
        env.project.path(),
        Some(env.home.path()),
        Some(env.rho_home.path()),
    )
    .unwrap();

    assert!(env
        .home
        .path()
        .join(".agents/plugins/demo/plugin.json")
        .is_file());

    let discovery = discover_with_trust(
        env.project.path(),
        Some(env.home.path()),
        Some(env.rho_home.path()),
        ProjectTrust::Trusted,
    );
    assert!(discovery.skills.is_empty());
    assert!(!discovery.mcp.has_enabled_servers());
    let entry = discovery.report.find("demo").unwrap();
    assert_eq!(entry.status, PluginStatus::Disabled);
    assert!(!entry.enabled);
    assert_eq!(entry.skill_count, 1);
}

// Covers: remove deletes only the package slot and preserves PLUGIN_DATA.
// Owner: plugin remove safety.
#[test]
fn remove_deletes_package_but_keeps_data_directory() {
    let env = env();
    let source_root = TempDir::new().unwrap();
    let source = write_source(source_root.path(), "demo");
    install(
        &source,
        PluginScope::User,
        InstallMode::Copy,
        /* force */ false,
        env.project.path(),
        env.home.path(),
        Some(env.rho_home.path()),
    )
    .unwrap();

    let plugins_root = env.home.path().join(".agents/plugins");
    let data = plugins_root.join("data/demo");
    std::fs::create_dir_all(&data).unwrap();
    std::fs::write(data.join("state.json"), "{}").unwrap();

    remove(
        "demo",
        env.project.path(),
        Some(env.home.path()),
        Some(env.rho_home.path()),
    )
    .unwrap();

    assert!(!plugins_root.join("demo").exists());
    assert!(data.join("state.json").is_file());
}

// Covers: inspect_source never requires component execution and surfaces metadata.
// Owner: plugin package validation.
#[test]
fn inspect_source_reads_manifest_only() {
    let root = TempDir::new().unwrap();
    let source = write_source(root.path(), "meta");
    let inspected = inspect_source(&source).unwrap();
    assert_eq!(inspected.manifest.name, "meta");
    assert_eq!(inspected.manifest.version.as_deref(), Some("1.2.3"));
    assert_eq!(
        inspected.manifest.description.as_deref(),
        Some("demo plugin")
    );
}