zeph 0.22.2

Lightweight AI agent with hybrid inference, skills-first architecture, and multi-channel I/O
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::cli::PluginCommand;

/// Prints the resolved overlay summary for the plugins directory.
///
/// Displays contributing and skipped plugins accurately. Does NOT show the
/// post-intersection merged `allowed_commands` values — those depend on the
/// live `Config` base, which is not available here. Users who want the merged
/// live values should inspect `tools.shell.allowed_commands` in `config.toml`
/// after startup (logged at INFO level on first reload).
fn print_overlay_section(plugins_dir: &std::path::Path) -> anyhow::Result<()> {
    let mut cfg = zeph_core::config::Config::default();
    let overlay = zeph_plugins::apply_plugin_config_overlays(&mut cfg, plugins_dir)
        .map_err(|e| anyhow::anyhow!("overlay resolution failed: {e}"))?;

    if overlay.source_plugins.is_empty() && overlay.skipped_plugins.is_empty() {
        println!("No plugin overlay active.");
        return Ok(());
    }

    println!("Active plugin overlay:");

    if overlay.source_plugins.is_empty() {
        println!("  Source plugins:  (none)");
    } else {
        println!("  Source plugins:  {}", overlay.source_plugins.join(", "));
    }

    if overlay.skipped_plugins.is_empty() {
        println!("  Skipped plugins: (none)");
    } else {
        println!("  Skipped plugins:");
        for reason in &overlay.skipped_plugins {
            println!("    - {reason}");
        }
    }

    println!(
        "  Note: overlay values shown against default config — run with --config for live intersection."
    );

    Ok(())
}

/// Handle `zeph plugin` subcommands.
///
/// # Errors
///
/// Returns an error if the plugin operation fails (invalid manifest, conflicts, etc.).
// `async` is unused when compiled without the `registry` feature (the Search/Get arms'
// `.await` calls are cfg'd out, leaving the fn body synchronous) — the signature must stay
// `async` regardless, since `runner.rs` always `.await`s this call and the feature is a
// caller-invisible build-time choice (M1, critic handoff).
#[allow(clippy::unused_async)]
pub(crate) async fn handle_plugin_command(
    cmd: PluginCommand,
    config_path: Option<&std::path::Path>,
) -> anyhow::Result<()> {
    use crate::bootstrap::{load_config_or_default, resolve_config_path};

    let config_file = resolve_config_path(config_path);
    let config = load_config_or_default(&config_file);

    let plugins_dir = crate::bootstrap::plugins_dir();
    std::fs::create_dir_all(&plugins_dir)
        .map_err(|e| anyhow::anyhow!("failed to create plugins dir: {e}"))?;

    let managed_skills_dir = crate::bootstrap::managed_skills_dir();
    let mcp_allowed = config.mcp.allowed_commands.clone();
    let base_shell_allowed = config.tools.shell.allowed_commands.clone();

    let mgr = zeph_plugins::PluginManager::new(
        plugins_dir.clone(),
        managed_skills_dir,
        mcp_allowed,
        base_shell_allowed,
    );

    match cmd {
        PluginCommand::List { overlay } => {
            if overlay {
                print_overlay_section(&plugins_dir)?;
            } else {
                let installed = mgr.list_installed()?;
                if installed.is_empty() {
                    println!("No plugins installed.");
                } else {
                    for p in &installed {
                        println!("{} v{}{}", p.name, p.version, p.description);
                    }
                }
            }
        }

        PluginCommand::Add {
            source,
            strict_reputation,
        } => {
            let mgr = mgr.with_reputation_config(&config.plugins.reputation, strict_reputation);
            let result = mgr.add(&source)?;
            println!("Installed plugin \"{}\".", result.name);
            if !result.installed_skills.is_empty() {
                println!("  Skills: {}", result.installed_skills.join(", "));
            }
            if !result.mcp_server_ids.is_empty() {
                println!(
                    "  MCP servers (restart required): {}",
                    result.mcp_server_ids.join(", ")
                );
            }
            for w in &result.warnings {
                eprintln!("warning: {w}");
            }
            // Pointer to plugin add for future users.
            println!(
                "\nPlugins are managed separately. Run `zeph plugin add <source>` to install more."
            );
        }

        PluginCommand::Remove { name } => {
            let result = mgr.remove(&name)?;
            println!("Removed plugin \"{name}\".");
            if !result.removed_skills.is_empty() {
                println!("  Removed skills: {}", result.removed_skills.join(", "));
            }
            if !result.removed_mcp_ids.is_empty() {
                println!(
                    "  MCP servers removed (restart required): {}",
                    result.removed_mcp_ids.join(", ")
                );
            }
        }

        PluginCommand::Search { query } => {
            #[cfg(feature = "registry")]
            {
                registry_search(&config, &query).await?;
            }
            #[cfg(not(feature = "registry"))]
            {
                let _ = &query;
                println!(
                    "This zeph build was compiled without the `registry` feature; rebuild \
                     with `--features registry` (or `full`) to use `zeph plugin search`."
                );
            }
        }

        PluginCommand::Get { registry_id } => {
            #[cfg(feature = "registry")]
            {
                // Fetched packages install via the same `mgr.add(...)` path as `plugin add`, so
                // they get the same reputation check (spec-043, #5864). No `--strict-reputation`
                // flag on `get` — config's `enforcement` applies as-is.
                let mgr = mgr.with_reputation_config(&config.plugins.reputation, false);
                registry_get(&config, &mgr, &registry_id).await?;
            }
            #[cfg(not(feature = "registry"))]
            {
                let _ = &registry_id;
                println!(
                    "This zeph build was compiled without the `registry` feature; rebuild \
                     with `--features registry` (or `full`) to use `zeph plugin get`."
                );
            }
        }
    }

    Ok(())
}

/// `zeph plugin search <query>` (spec-045, #5869, FR-003 — shares the search implementation
/// used by `zeph skill search`, NFR-006).
///
/// Prints [`crate::commands::registry_client::REGISTRY_NOT_CONFIGURED_MSG`] and makes zero
/// network calls when `skills.registry.enabled = false` (FR-004, NFR-001). Thin wrapper around
/// [`registry_search_with`] — see that fn's tests for `MockRegistryClient`-driven coverage.
#[cfg(feature = "registry")]
#[tracing::instrument(name = "plugin.registry_search", skip(config), fields(query))]
async fn registry_search(config: &zeph_core::config::Config, query: &str) -> anyhow::Result<()> {
    use crate::commands::registry_client::{
        REGISTRY_NOT_CONFIGURED_MSG, build_registry_client, resolve_registry_token,
    };

    if !config.skills.registry.enabled {
        println!("{REGISTRY_NOT_CONFIGURED_MSG}");
        anyhow::bail!("{REGISTRY_NOT_CONFIGURED_MSG}");
    }

    let token = resolve_registry_token(config).await?;
    let client = build_registry_client(config, token);
    registry_search_with(client.as_ref(), query).await
}

/// Search logic parameterized over a [`zeph_plugins::marketplace::RegistryClient`] — split out
/// of [`registry_search`] so tests can drive it with `MockRegistryClient` without network or a
/// real `Config`/vault (review fix #4).
#[cfg(feature = "registry")]
async fn registry_search_with(
    client: &dyn zeph_plugins::marketplace::RegistryClient,
    query: &str,
) -> anyhow::Result<()> {
    use crate::commands::registry_client::print_search_results;

    let results = client
        .search(query)
        .await
        .map_err(|e| anyhow::anyhow!("registry search failed: {e}"))?;
    print_search_results(&results);
    Ok(())
}

/// `zeph plugin get <registry-id>` (spec-045, #5869, FR-003).
///
/// Fetches the package and, when it contains a `plugin.toml`, routes it through
/// [`zeph_plugins::PluginManager::add`] unchanged (NFR-002 — same manifest validation, MCP
/// allowlist, and injection scan as `zeph plugin add <local-path>`). Fails with a pointer to
/// `zeph skill get` when the fetched package has no `plugin.toml` (a bare skill package).
/// Thin wrapper around [`registry_get_with`] — see that fn's tests for `MockRegistryClient`-
/// driven coverage.
#[cfg(feature = "registry")]
#[tracing::instrument(name = "plugin.registry_get", skip(config, mgr), fields(registry_id))]
async fn registry_get(
    config: &zeph_core::config::Config,
    mgr: &zeph_plugins::PluginManager,
    registry_id: &str,
) -> anyhow::Result<()> {
    use crate::commands::registry_client::{
        REGISTRY_NOT_CONFIGURED_MSG, build_registry_client, resolve_registry_token,
    };

    if !config.skills.registry.enabled {
        println!("{REGISTRY_NOT_CONFIGURED_MSG}");
        anyhow::bail!("{REGISTRY_NOT_CONFIGURED_MSG}");
    }

    let token = resolve_registry_token(config).await?;
    let client = build_registry_client(config, token);
    registry_get_with(client.as_ref(), mgr, registry_id).await
}

/// Fetch-and-install logic parameterized over a [`zeph_plugins::marketplace::RegistryClient`] —
/// split out of [`registry_get`] so tests can drive it with `MockRegistryClient` (review fix #4).
#[cfg(feature = "registry")]
async fn registry_get_with(
    client: &dyn zeph_plugins::marketplace::RegistryClient,
    mgr: &zeph_plugins::PluginManager,
    registry_id: &str,
) -> anyhow::Result<()> {
    let archive = client
        .fetch(registry_id)
        .await
        .map_err(|e| anyhow::anyhow!("registry fetch failed: {e}"))?;

    if !archive.has_plugin_manifest {
        anyhow::bail!(
            "package {registry_id:?} is a bare skill package (no plugin.toml); use \
             `zeph skill get {registry_id}` instead"
        );
    }

    let source = archive
        .install_dir
        .to_str()
        .ok_or_else(|| anyhow::anyhow!("registry temp dir path is not valid UTF-8"))?;
    let result = mgr.add(source)?;
    println!(
        "Installed plugin \"{}\" from registry {registry_id:?}.",
        result.name
    );
    if !result.installed_skills.is_empty() {
        println!("  Skills: {}", result.installed_skills.join(", "));
    }
    if !result.mcp_server_ids.is_empty() {
        println!(
            "  MCP servers (restart required): {}",
            result.mcp_server_ids.join(", ")
        );
    }
    for w in &result.warnings {
        eprintln!("warning: {w}");
    }

    Ok(())
}

#[cfg(all(test, feature = "registry"))]
mod registry_tests {
    use super::*;
    use zeph_plugins::marketplace::RegistryEntry;
    use zeph_plugins::marketplace::mock::MockRegistryClient;

    fn sample_entry(id: &str, name: &str) -> RegistryEntry {
        RegistryEntry {
            registry_id: id.to_owned(),
            name: name.to_owned(),
            description: "a test plugin".to_owned(),
            tags: vec![],
            author: None,
            security_audit_status: None,
        }
    }

    fn test_manager(root: &std::path::Path) -> zeph_plugins::PluginManager {
        zeph_plugins::PluginManager::new(
            root.join("plugins"),
            root.join("managed-skills"),
            Vec::new(),
            Vec::new(),
        )
    }

    #[tokio::test]
    async fn registry_search_with_calls_client_and_succeeds() {
        let mock = MockRegistryClient::new().with_entry(sample_entry("acme/x", "X Plugin"));
        registry_search_with(&mock, "x").await.unwrap();
    }

    #[tokio::test]
    async fn registry_search_with_propagates_client_error() {
        let mock = MockRegistryClient::new().failing("boom");
        let err = registry_search_with(&mock, "x").await.unwrap_err();
        assert!(err.to_string().contains("registry search failed"));
    }

    #[tokio::test]
    async fn registry_get_with_installs_plugin_bundle() {
        let dir = tempfile::tempdir().unwrap();
        let mgr = test_manager(dir.path());

        let mock = MockRegistryClient::new().with_package(
            "acme/full-plugin",
            vec![
                (
                    "plugin.toml".to_owned(),
                    "[plugin]\nname = \"full-plugin\"\nversion = \"0.1.0\"".to_owned(),
                ),
                (
                    "skills/x/SKILL.md".to_owned(),
                    "---\nname: x\ndescription: a test skill\n---\nbody".to_owned(),
                ),
            ],
        );

        registry_get_with(&mock, &mgr, "acme/full-plugin")
            .await
            .unwrap();

        assert!(
            dir.path()
                .join("plugins/full-plugin/.plugin.toml")
                .is_file()
        );
    }

    #[tokio::test]
    async fn registry_get_with_rejects_bare_skill_package_with_pointer_to_skill_get() {
        let dir = tempfile::tempdir().unwrap();
        let mgr = test_manager(dir.path());

        let mock = MockRegistryClient::new().with_package(
            "acme/x",
            vec![(
                "SKILL.md".to_owned(),
                "---\nname: x\ndescription: a test skill\n---\nbody".to_owned(),
            )],
        );

        let err = registry_get_with(&mock, &mgr, "acme/x").await.unwrap_err();
        assert!(err.to_string().contains("zeph skill get acme/x"));
    }

    #[tokio::test]
    async fn registry_get_with_propagates_fetch_not_found() {
        let dir = tempfile::tempdir().unwrap();
        let mgr = test_manager(dir.path());
        let mock = MockRegistryClient::new();

        let err = registry_get_with(&mock, &mgr, "missing/id")
            .await
            .unwrap_err();
        assert!(err.to_string().contains("registry fetch failed"));
    }

    // ── #5943: disabled-registry bail must match the printed FR-004 message ──
    //
    // Regression coverage for the outer `registry_search`/`registry_get` gate (not exercised by
    // the `_with` tests above, which only drive post-gate logic): before the fix, the bail! used
    // a hardcoded `"skill registry is not configured"` string — wrong wording for a plugin
    // subcommand too — that diverged from the `REGISTRY_NOT_CONFIGURED_MSG` printed to stdout
    // just above it. Asserting exact equality (not just `contains`) pins both wording and future
    // re-divergence.

    #[tokio::test]
    async fn registry_search_bails_with_registry_not_configured_msg_when_disabled() {
        use crate::commands::registry_client::REGISTRY_NOT_CONFIGURED_MSG;

        let config = zeph_core::config::Config::default();
        assert!(!config.skills.registry.enabled);

        let err = registry_search(&config, "query").await.unwrap_err();
        assert_eq!(err.to_string(), REGISTRY_NOT_CONFIGURED_MSG);
    }

    #[tokio::test]
    async fn registry_get_bails_with_registry_not_configured_msg_when_disabled() {
        use crate::commands::registry_client::REGISTRY_NOT_CONFIGURED_MSG;

        let dir = tempfile::tempdir().unwrap();
        let mgr = test_manager(dir.path());

        let config = zeph_core::config::Config::default();
        assert!(!config.skills.registry.enabled);

        let err = registry_get(&config, &mgr, "acme/x").await.unwrap_err();
        assert_eq!(err.to_string(), REGISTRY_NOT_CONFIGURED_MSG);
    }
}