agent_works/plugin/install/ops.rs
1//! Plugin install lifecycle: `plugin add` / `plugin remove` (human review before
2//! install → write to disk → pin → uninstall).
3//!
4//! The channels are capped at **path + git + archive**; this does not grow into a
5//! package manager (marketplace / central catalog deferred).
6//!
7//! - **Human review before install**: [`plan_install`] only stages + produces a
8//! [`ReviewDigest`] (full manifest + capability breakdown); only **after human
9//! confirmation** does [`commit_install`] write to disk (product-level gate;
10//! plugins requested by an agent are never allowed through by default);
11//! - **Pinned values**: manifest hash, each .wasm hash, `run`/module/server paths
12//! absolutized, resolved values for capabilities' paths/hosts — all pinned into
13//! [`PluginRegistry`] (the authority **outside** the plugin directory);
14//! - **zip-slip**: reject if an archive member path escapes the extraction
15//! directory;
16//! - **git dependency made explicit** (Windows): the git URL channel depends on a
17//! `git` executable on `PATH` (may embed gix later);
18//! - **Uninstall cleans all three places**: tool table (identity_keys returned to
19//! the caller for deregistration) / ledger ([`RemovalReport::sweep_ledger`]) /
20//! registry entries.
21//!
22//! secret rule: env/headers only flow as `${ENV:VAR}` references; this module
23//! never resolves them and never writes literals.
24
25use std::path::Path;
26
27use super::super::registry::{PluginRegistry, ToolPin};
28
29use super::build::*;
30use super::source::*;
31use super::types::*;
32
33/// `plugin add` step one: materialize the source into staging, validate the
34/// manifest, compute pinned values, produce the review digest.
35///
36/// **Writes nothing to disk** (nothing into `<plugins_root>/<name>/`, nothing to
37/// the registry) — only after a human has reviewed [`InstallPlan::digest`] and
38/// confirmed does [`commit_install`] run.
39pub async fn plan_install(
40 source: &PluginSource,
41 plugins_root: &Path,
42) -> Result<InstallPlan, InstallError> {
43 let staging_dir = staging_dir_for(plugins_root);
44 std::fs::create_dir_all(&staging_dir).map_err(InstallError::Io)?;
45
46 match materialize(source, &staging_dir).await {
47 Ok(()) => {}
48 Err(e) => {
49 let _ = std::fs::remove_dir_all(&staging_dir);
50 return Err(e);
51 }
52 }
53 match build_plan(staging_dir, plugins_root) {
54 Ok(plan) => Ok(plan),
55 Err(e) => Err(e),
56 }
57}
58
59/// `plugin add` step two (**after human confirmation**): staging → final
60/// directory, pinned into the registry.
61pub fn commit_install(
62 plan: &InstallPlan,
63 registry: &mut PluginRegistry,
64) -> Result<InstalledPlugin, InstallError> {
65 // Replacement semantics for re-adding: delete the old directory first
66 // (same-name plugin slot).
67 if plan.plugin_root.exists() {
68 std::fs::remove_dir_all(&plan.plugin_root).map_err(InstallError::Io)?;
69 }
70 if let Some(parent) = plan.plugin_root.parent() {
71 std::fs::create_dir_all(parent).map_err(InstallError::Io)?;
72 }
73 std::fs::rename(&plan.staging.dir, &plan.plugin_root).map_err(InstallError::Io)?;
74
75 registry.insert(plan.pin.clone());
76 let registry_path = PluginRegistry::path_in(&plan.plugins_root);
77 registry
78 .save(®istry_path)
79 .map_err(InstallError::Registry)?;
80
81 let identity_keys = plan
82 .pin
83 .tools
84 .iter()
85 .flat_map(|t| match t {
86 // The real tool keys of the mcp form expand at load time by server
87 // tool name; here we give label keys, and the loader fills them in.
88 ToolPin::Wasm { identity, .. } | ToolPin::Script { identity, .. } => {
89 vec![identity.clone()]
90 }
91 ToolPin::Mcp { label, .. } => vec![format!("plugin.{}.{label}", plan.pin.name)],
92 })
93 .collect();
94
95 Ok(InstalledPlugin {
96 pin: plan.pin.clone(),
97 plugin_dir: plan.plugin_root.clone(),
98 identity_keys,
99 })
100}
101
102/// `plugin remove`: clean all three places (tool-table keys returned, ledger
103/// prefix sweepable, registry pins pulled), delete the install directory.
104pub fn remove_plugin(
105 plugins_root: &Path,
106 registry: &mut PluginRegistry,
107 name: &str,
108) -> Result<RemovalReport, InstallError> {
109 let pin = registry
110 .remove(name)
111 .ok_or_else(|| InstallError::NotInstalled {
112 name: name.to_string(),
113 })?;
114
115 let registry_path = PluginRegistry::path_in(plugins_root);
116 registry
117 .save(®istry_path)
118 .map_err(InstallError::Registry)?;
119
120 let plugin_dir = plugins_root.join(name);
121 let dir_removed = if plugin_dir.exists() {
122 std::fs::remove_dir_all(&plugin_dir).map_err(InstallError::Io)?;
123 true
124 } else {
125 false
126 };
127
128 let mut identity_keys = Vec::new();
129 for tool in &pin.tools {
130 match tool {
131 ToolPin::Wasm { identity, .. } | ToolPin::Script { identity, .. } => {
132 identity_keys.push(identity.clone());
133 }
134 ToolPin::Mcp { label, .. } => {
135 identity_keys.push(format!("plugin.{name}.{label}"));
136 }
137 }
138 }
139
140 Ok(RemovalReport {
141 name: name.to_string(),
142 identity_keys,
143 ledger_prefix: format!("plugin.{name}."),
144 dir_removed,
145 registry_removed: true,
146 })
147}