feat: M1.1 完成 + M1.2 启动 — 全量更新

M1.1 收尾:
- 24项 P0/P1/P2 bug 修复 (Rust 107 tests + Flutter 15 tests)
- Flutter App v0.3: cupertino_icons 修复, 单元测试, 调试面板, APK 52.6MB
- 示例插件完善: manifest.json + 请求/响应示范 + 7个测试
- API 文档重写 (以 routes.rs 为唯一权威)
- MILESTONES.md 更新至 100%

M1.2 启动:
- P0: 插件管理 API 闭环 (handle_manager_message Custom 分支 + broadcast_plugin_states)
- ServiceManager 集成测试 8/8 (tests/m1_2_service_manager.rs)
- M1.2 测试计划 (docs/M1.2_TEST_PLAN.md, 18个E2E场景)
- 动态插件系统: auto_rollback + version_manager GC + 路径穿越防护

总计: Rust 115/115 测试, Flutter 15/15 测试, 零 warning

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
showen
2026-03-14 18:12:42 +08:00
parent 8ed9cb2d9d
commit d30c111c71
68 changed files with 8115 additions and 1201 deletions

View File

@@ -1,12 +1,29 @@
use crate::core::config::AppConfig;
use crate::core::message::{Destination, Envelope, Message};
use crate::core::plugin::{CapabilityTestResult, Plugin, PluginContext};
use crate::core::plugin_loader::ErrorPolicy;
use crate::core::plugin_loader::{ErrorPolicy, PluginLoader, PluginRegistryEntry};
use crate::core::plugin_repo::PluginRepository;
use crate::core::version_manager::VersionManager;
use anyhow::{anyhow, Result};
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::sync::{mpsc, Arc};
const DEFAULT_PLUGIN_REPO_URL: &str = "https://plugins.example.com";
#[derive(Deserialize)]
struct PluginSwitchCommand {
id: String,
version: String,
}
#[derive(Deserialize)]
struct PluginInstallCommand {
id: String,
#[serde(default)]
version: Option<String>,
}
/// 插件运行时状态包装
struct PluginState {
plugin: Box<dyn Plugin>,
@@ -92,6 +109,46 @@ pub struct ServiceManager {
}
impl ServiceManager {
fn plugin_context(&self) -> PluginContext {
PluginContext {
tx: self.tx.clone(),
config: Arc::clone(&self.config),
}
}
fn init_and_start_plugin_with_context(
state: &mut PluginState,
ctx: PluginContext,
) -> Result<()> {
if let Err(init_error) = state.plugin.init(ctx) {
let cleanup_error = state.plugin.stop().err();
return match cleanup_error {
Some(stop_error) => Err(anyhow!(
"plugin '{}' init failed: {}; cleanup stop failed: {}",
state.id(),
init_error,
stop_error
)),
None => Err(init_error),
};
}
if let Err(start_error) = state.plugin.start() {
let cleanup_error = state.plugin.stop().err();
return match cleanup_error {
Some(stop_error) => Err(anyhow!(
"plugin '{}' start failed: {}; cleanup stop failed: {}",
state.id(),
start_error,
stop_error
)),
None => Err(start_error),
};
}
Ok(())
}
pub fn new(config: AppConfig) -> Self {
let (tx, rx) = mpsc::channel();
Self {
@@ -240,15 +297,17 @@ impl ServiceManager {
match &state.error_policy {
ErrorPolicy::AutoRollback => {
eprintln!(
"[ServiceManager] 动态插件 '{}' 必须能力自测失败,禁用 (待回退)",
"[ServiceManager] 动态插件 '{}' 必须能力自测失败,尝试自动回退到稳定版本",
state.id()
);
state.needs_rollback = true;
}
ErrorPolicy::DisableAndLog => {
eprintln!(
"[ServiceManager] 动态插件 '{}' 必须能力自测失败,禁用",
state.id()
);
state.needs_rollback = false;
}
}
state.enabled = false;
@@ -258,6 +317,15 @@ impl ServiceManager {
}
}
for idx in 0..self.plugins.len() {
if !self.plugins[idx].needs_rollback {
continue;
}
let plugin_id = self.plugins[idx].id().to_string();
self.rollback_dynamic_plugin(idx, &plugin_id);
}
// Phase 3: start
for state in &mut self.plugins {
if !state.enabled {
@@ -279,6 +347,8 @@ impl ServiceManager {
}
}
self.broadcast_plugin_states();
Ok(())
}
@@ -329,18 +399,29 @@ impl ServiceManager {
/// 启用/禁用指定插件
pub fn set_plugin_enabled(&mut self, plugin_id: &str, enabled: bool) -> Result<()> {
let state = self
let idx = self
.plugins
.iter_mut()
.find(|s| s.id() == plugin_id)
.iter()
.position(|s| s.id() == plugin_id)
.ok_or_else(|| anyhow!("plugin '{plugin_id}' not found"))?;
if enabled && !state.enabled {
// 重新启用reset 错误计数
if enabled && !self.plugins[idx].enabled {
let ctx = self.plugin_context();
let state = &mut self.plugins[idx];
state.error_count = 0;
state.enabled = true;
println!("[ServiceManager] 插件 '{plugin_id}' 已启用");
} else if !enabled && state.enabled {
match Self::init_and_start_plugin_with_context(state, ctx) {
Ok(()) => {
state.enabled = true;
println!("[ServiceManager] 插件 '{plugin_id}' 已启用");
}
Err(error) => {
state.enabled = false;
return Err(anyhow!("failed to enable plugin '{plugin_id}': {error}"));
}
}
} else if !enabled && self.plugins[idx].enabled {
let state = &mut self.plugins[idx];
state.plugin.stop()?;
state.enabled = false;
println!("[ServiceManager] 插件 '{plugin_id}' 已禁用");
}
@@ -348,6 +429,23 @@ impl ServiceManager {
Ok(())
}
pub fn rollback_plugin(&mut self, plugin_id: &str) -> Result<()> {
let idx = self
.plugins
.iter()
.position(|state| state.id() == plugin_id)
.ok_or_else(|| anyhow!("plugin '{plugin_id}' not found"))?;
if !self.plugins[idx].is_dynamic {
return Err(anyhow!(
"plugin '{plugin_id}' is not dynamic and cannot be rolled back"
));
}
self.rollback_dynamic_plugin(idx, plugin_id);
Ok(())
}
/// 查询插件状态信息(供 HTTP API 使用)
pub fn plugin_states(&self) -> Vec<PluginStateInfo> {
self.plugins
@@ -389,18 +487,50 @@ impl ServiceManager {
new_state.capabilities = capabilities;
new_state.auto_test = auto_test;
let ctx = PluginContext {
tx: self.tx.clone(),
config: Arc::clone(&self.config),
};
new_state.plugin.init(ctx)?;
new_state.plugin.start()?;
let ctx = self.plugin_context();
let mut old_state = self.plugins.remove(idx);
let old_was_enabled = old_state.enabled;
if self.plugins[idx].enabled {
let _ = self.plugins[idx].plugin.stop();
if old_was_enabled {
// 先停旧插件,避免热替换窗口内新旧实例同时持有端口、文件句柄等独占资源。
old_state.plugin.stop()?;
}
let replace_result = Self::init_and_start_plugin_with_context(&mut new_state, ctx);
match replace_result {
Ok(()) => {
new_state.enabled = true;
self.plugins.insert(idx, new_state);
}
Err(new_error) => {
if old_was_enabled {
let restore_ctx = self.plugin_context();
match Self::init_and_start_plugin_with_context(&mut old_state, restore_ctx) {
Ok(()) => {
old_state.enabled = true;
self.plugins.insert(idx, old_state);
return Err(anyhow!(
"failed to replace plugin '{plugin_id}': {new_error}; restored previous plugin"
));
}
Err(restore_error) => {
old_state.enabled = false;
self.plugins.insert(idx, old_state);
return Err(anyhow!(
"failed to replace plugin '{plugin_id}': {new_error}; failed to restore previous plugin: {restore_error}"
));
}
}
}
old_state.enabled = false;
self.plugins.insert(idx, old_state);
return Err(anyhow!(
"failed to replace plugin '{plugin_id}': {new_error}"
));
}
}
self.plugins[idx] = new_state;
println!("[ServiceManager] 插件 '{plugin_id}' 热替换成功");
Ok(())
}
@@ -441,6 +571,192 @@ impl ServiceManager {
)
}
fn plugin_loader(&self) -> Result<PluginLoader> {
let version_manager = self
.version_manager
.as_ref()
.ok_or_else(|| anyhow!("plugin version manager is not configured"))?;
Ok(PluginLoader::new(version_manager.loader().store_path()))
}
fn plugin_repository(&self) -> Result<PluginRepository> {
Ok(PluginRepository::new(
&std::env::var("SHOWEN_PLUGIN_REPO_URL")
.ok()
.filter(|value| !value.trim().is_empty())
.unwrap_or_else(|| DEFAULT_PLUGIN_REPO_URL.to_string()),
self.plugin_loader()?,
))
}
fn register_dynamic_plugin_runtime(
&mut self,
plugin_id: &str,
plugin: Box<dyn Plugin>,
error_policy: ErrorPolicy,
max_errors: u32,
required_capabilities: Vec<String>,
capabilities: Vec<String>,
auto_test: bool,
) -> Result<()> {
let mut state = PluginState::new_dynamic(plugin, error_policy, max_errors);
state.required_capabilities = required_capabilities;
state.capabilities = capabilities;
state.auto_test = auto_test;
let ctx = self.plugin_context();
Self::init_and_start_plugin_with_context(&mut state, ctx)
.map_err(|error| anyhow!("failed to start installed plugin '{plugin_id}': {error}"))?;
state.enabled = true;
self.plugins.push(state);
println!("[ServiceManager] 插件 '{plugin_id}' 已安装并启动");
Ok(())
}
fn switch_plugin_version(&mut self, plugin_id: &str, version: &str) -> Result<()> {
let version_manager = self
.version_manager
.as_ref()
.ok_or_else(|| anyhow!("plugin version manager is not configured"))?;
version_manager.switch_version(plugin_id, version)?;
let (plugin, manifest) = version_manager
.loader()
.load_plugin(plugin_id, Some(version))?;
let loader = PluginLoader::new(version_manager.loader().store_path());
let registry = loader.load_registry()?;
let entry = registry
.plugins
.get(plugin_id)
.ok_or_else(|| anyhow!("plugin '{plugin_id}' not in registry"))?;
if let Some(idx) = self
.plugins
.iter()
.position(|state| state.id() == plugin_id)
{
self.replace_dynamic_plugin_at_index(
idx,
plugin_id,
Box::new(plugin),
manifest.error_policy,
entry.max_errors,
manifest.required_capabilities,
manifest.capabilities,
manifest.auto_test,
)?;
} else {
self.register_dynamic_plugin_runtime(
plugin_id,
Box::new(plugin),
manifest.error_policy,
entry.max_errors,
manifest.required_capabilities,
manifest.capabilities,
manifest.auto_test,
)?;
}
println!("[ServiceManager] 插件 '{plugin_id}' 已切换到版本 {version}");
Ok(())
}
fn install_plugin(&mut self, request: PluginInstallCommand) -> Result<()> {
let plugin_id = request.id.clone();
let repo = self.plugin_repository()?;
let version = match request.version.clone() {
Some(version) => version,
None => repo.check_update(&plugin_id, "0.0.0")?.ok_or_else(|| {
anyhow!(
"repo did not report an installable version for '{}'",
plugin_id
)
})?,
};
repo.download_and_install(&plugin_id, &version)?;
let loader = self.plugin_loader()?;
let (plugin, manifest) = loader.load_plugin(&plugin_id, Some(&version))?;
let mut registry = loader.load_registry()?;
let existing = registry.plugins.get(&plugin_id).cloned();
let entry = PluginRegistryEntry {
active_version: version.clone(),
last_stable_version: existing
.as_ref()
.and_then(|entry| entry.last_stable_version.clone()),
enabled: true,
error_policy: existing
.as_ref()
.map(|entry| entry.error_policy.clone())
.unwrap_or_else(|| manifest.error_policy.clone()),
max_errors: existing.as_ref().map(|entry| entry.max_errors).unwrap_or(5),
};
registry.plugins.insert(plugin_id.clone(), entry.clone());
loader.save_registry(&registry)?;
if let Some(idx) = self
.plugins
.iter()
.position(|state| state.id() == plugin_id)
{
self.replace_dynamic_plugin_at_index(
idx,
&plugin_id,
Box::new(plugin),
manifest.error_policy,
entry.max_errors,
manifest.required_capabilities,
manifest.capabilities,
manifest.auto_test,
)?;
} else {
self.register_dynamic_plugin_runtime(
&plugin_id,
Box::new(plugin),
manifest.error_policy,
entry.max_errors,
manifest.required_capabilities,
manifest.capabilities,
manifest.auto_test,
)?;
}
Ok(())
}
fn check_plugin_updates(&self) -> Result<()> {
let repo = self.plugin_repository()?;
let registry = self.plugin_loader()?.load_registry()?;
for (plugin_id, entry) in &registry.plugins {
match repo.check_update(plugin_id, &entry.active_version)? {
Some(version) => println!(
"[ServiceManager] 插件 '{plugin_id}' 发现可用更新: {} -> {version}",
entry.active_version
),
None => println!(
"[ServiceManager] 插件 '{plugin_id}' 已是最新版本 {}",
entry.active_version
),
}
}
Ok(())
}
fn broadcast_plugin_states(&mut self) {
match serde_json::to_string(&self.plugin_states()) {
Ok(payload) => self.broadcast_message(Message::Custom {
kind: "plugin_states".to_string(),
payload,
}),
Err(error) => eprintln!("[ServiceManager] 序列化 plugin_states 失败: {error}"),
}
}
/// 处理发给管理层自身的消息
fn handle_manager_message(&mut self, msg: Message) -> Result<()> {
match msg {
@@ -484,6 +800,79 @@ impl ServiceManager {
println!("[ServiceManager] 插件 '{}' 就绪", id);
self.broadcast_message(Message::PluginReady(id));
}
Message::Custom { kind, payload } => {
let should_broadcast = match kind.as_str() {
"plugin_enable" => {
if let Err(error) = self.set_plugin_enabled(&payload, true) {
eprintln!(
"[ServiceManager] plugin_enable('{}') 失败: {error}",
payload
);
}
true
}
"plugin_disable" => {
if let Err(error) = self.set_plugin_enabled(&payload, false) {
eprintln!(
"[ServiceManager] plugin_disable('{}') 失败: {error}",
payload
);
}
true
}
"plugin_rollback" => {
if let Err(error) = self.rollback_plugin(&payload) {
eprintln!(
"[ServiceManager] plugin_rollback('{}') 失败: {error}",
payload
);
}
true
}
"plugin_switch" => {
match serde_json::from_str::<PluginSwitchCommand>(&payload) {
Ok(command) => {
if let Err(error) =
self.switch_plugin_version(&command.id, &command.version)
{
eprintln!(
"[ServiceManager] plugin_switch('{}', '{}') 失败: {error}",
command.id, command.version
);
}
}
Err(error) => {
eprintln!("[ServiceManager] plugin_switch payload 非法: {error}");
}
}
true
}
"plugin_install" => {
match serde_json::from_str::<PluginInstallCommand>(&payload) {
Ok(command) => {
if let Err(error) = self.install_plugin(command) {
eprintln!("[ServiceManager] plugin_install 失败: {error}");
}
}
Err(error) => {
eprintln!("[ServiceManager] plugin_install payload 非法: {error}");
}
}
true
}
"plugin_check_updates" => {
if let Err(error) = self.check_plugin_updates() {
eprintln!("[ServiceManager] plugin_check_updates 失败: {error}");
}
true
}
_ => false,
};
if should_broadcast {
self.broadcast_plugin_states();
}
}
_ => {}
}
Ok(())
@@ -665,75 +1054,79 @@ impl ServiceManager {
"[ServiceManager] 插件 '{}' 错误次数达到阈值,尝试自动回退到稳定版本",
plugin_id
);
self.rollback_dynamic_plugin(idx, plugin_id);
}
}
}
let rollback_result = {
let Some(version_manager) = self.version_manager.as_ref() else {
eprintln!(
"[ServiceManager] 插件 '{}' 未配置 VersionManager标记为待回退",
plugin_id
fn rollback_dynamic_plugin(&mut self, idx: usize, plugin_id: &str) {
let rollback_result = {
let Some(version_manager) = self.version_manager.as_ref() else {
eprintln!(
"[ServiceManager] 插件 '{}' 未配置 VersionManager标记为待回退",
plugin_id
);
self.plugins[idx].needs_rollback = true;
return;
};
match version_manager.rollback(plugin_id) {
Ok(version) => match version_manager
.loader()
.load_plugin(plugin_id, Some(&version))
{
Ok((plugin, manifest)) => {
Ok((version, Box::new(plugin) as Box<dyn Plugin>, manifest))
}
Err(e) => Err((Some(version), e)),
},
Err(e) => Err((None, e)),
}
};
match rollback_result {
Ok((version, plugin, manifest)) => {
let max_errors = self.plugins[idx].max_errors;
match self.replace_dynamic_plugin_at_index(
idx,
plugin_id,
plugin,
manifest.error_policy,
max_errors,
manifest.required_capabilities,
manifest.capabilities,
manifest.auto_test,
) {
Ok(()) => {
self.plugins[idx].needs_rollback = false;
println!(
"[ServiceManager] 插件 '{}' 已回退并重新加载稳定版本 {}",
plugin_id, version
);
self.plugins[idx].needs_rollback = true;
return;
};
match version_manager.rollback(plugin_id) {
Ok(version) => match version_manager
.loader()
.load_plugin(plugin_id, Some(&version))
{
Ok((plugin, manifest)) => {
Ok((version, Box::new(plugin) as Box<dyn Plugin>, manifest))
}
Err(e) => Err((Some(version), e)),
},
Err(e) => Err((None, e)),
}
};
match rollback_result {
Ok((version, plugin, manifest)) => {
let max_errors = self.plugins[idx].max_errors;
match self.replace_dynamic_plugin_at_index(
idx,
plugin_id,
plugin,
manifest.error_policy,
max_errors,
manifest.required_capabilities,
manifest.capabilities,
manifest.auto_test,
) {
Ok(()) => {
println!(
"[ServiceManager] 插件 '{}' 已回退并重新加载稳定版本 {}",
plugin_id, version
);
}
Err(e) => {
eprintln!(
"[ServiceManager] 插件 '{}' 已切换到稳定版本 {},但热替换失败: {}",
plugin_id, version, e
);
self.plugins[idx].needs_rollback = true;
}
}
}
Err((Some(version), e)) => {
Err(e) => {
eprintln!(
"[ServiceManager] 插件 '{}' 已切换到稳定版本 {},但加载回退版本失败: {}",
"[ServiceManager] 插件 '{}' 已切换到稳定版本 {},但热替换失败: {}",
plugin_id, version, e
);
self.plugins[idx].needs_rollback = true;
}
Err((None, e)) => {
eprintln!(
"[ServiceManager] 插件 '{}' 自动回退失败,标记为待回退: {}",
plugin_id, e
);
self.plugins[idx].needs_rollback = true;
}
}
}
Err((Some(version), e)) => {
eprintln!(
"[ServiceManager] 插件 '{}' 已切换到稳定版本 {},但加载回退版本失败: {}",
plugin_id, version, e
);
self.plugins[idx].needs_rollback = true;
}
Err((None, e)) => {
eprintln!(
"[ServiceManager] 插件 '{}' 自动回退失败,标记为待回退: {}",
plugin_id, e
);
self.plugins[idx].needs_rollback = true;
}
}
}