Rebuild installed modules for the new core during an update

The core changes almost daily and a plugin loads only into the core it was
built for. update::apply now brings every enabled plugin to the new core
before the trial start: a build already in the store is reused, otherwise it
is rebuilt from the sources recorded at its last build (build recipe). A
plugin that cannot be rebuilt is switched off with the reason (and plugins
that depend on it); the core update always goes ahead. A rollback puts the
builds for the restored core back. The update result lists rebuilt and
switched-off modules.

Also: plugin.json records the real compiler instead of a fixed string.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ДмитрийandClaude Opus 5.5 committed 2026-11-05 15:36:16 +05:00
1 parent 76be1d3238
commit da94fb49a4
16 files changed
+424 -33

No files matched your search

+35 -1
View File
@@ -832,6 +832,33 @@ pub fn build_module_from_source(
build_module_with_bundle(module_source_dir, &[], realm_root, custom_staging_dir, progress_callback)
}
/// The sources a plugin was last built from, kept per realm so the manager can rebuild it on its own
/// after a core update.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BuildRecipe {
pub schema: u32,
pub source_dir: PathBuf,
#[serde(default)]
pub bundled: Vec<PathBuf>,
#[serde(default)]
pub source_commit: Option<String>,
}
fn build_recipe_path(realm_root: &Path, module_id: &str) -> PathBuf {
crate::plugins::meta_dir(realm_root).join("build-recipes").join(format!("{module_id}.json"))
}
fn write_build_recipe(realm_root: &Path, module_id: &str, recipe: &BuildRecipe) -> Result<()> {
let path = build_recipe_path(realm_root, module_id);
fs::create_dir_all(path.parent().unwrap())?;
crate::fsx::atomic_write(&path, serde_json::to_string_pretty(recipe)?.as_bytes())?;
Ok(())
}
pub fn read_build_recipe(realm_root: &Path, module_id: &str) -> Option<BuildRecipe> {
serde_json::from_slice(&fs::read(build_recipe_path(realm_root, module_id)).ok()?).ok()
}
/// Builds `module_source_dir` into one plugin DLL, together with `bundled` source trees that must live
/// in the same image (a module that derives from another module's C++ classes, such as Dungeon Clear
/// on SQUID). Each bundled tree's loader is called from the plugin's registration.
@@ -1218,7 +1245,7 @@ if errorlevel 1 exit /b 103
"dll_sha256": dll_sha256,
"abi_fingerprint": host_abi_fingerprint,
"master_loader": canonical_loader,
"compiler": "MSVC 19.51.36247 (x64 /MD)",
"compiler": toolchain.msvc_cl_version.as_deref().map(|v| format!("MSVC {v} (x64 /MD)")),
"built_at_iso": chrono_now_iso(),
"configs": packaged_confs,
"sql_files": packaged_sqls,
@@ -1233,6 +1260,13 @@ if errorlevel 1 exit /b 103
}
});
fs::write(store_dir.join("plugin.json"), serde_json::to_string_pretty(&manifest_content)?)?;
// What to rebuild from when the core changes (crate::rebuild).
write_build_recipe(realm_root, &module_id, &BuildRecipe {
schema: 1,
source_dir: module_source_dir.to_path_buf(),
bundled: bundled.to_vec(),
source_commit: source_commit.clone(),
})?;
let duration = start_time.elapsed().as_secs_f64();
emit_progress("SUCCESS", 100, "Module built, qualified and packaged successfully.", None);
+1
View File
@@ -60,6 +60,7 @@ pub mod race_availability;
pub mod realm_registry;
pub mod realmlist;
pub mod realms;
pub mod rebuild;
pub mod registry;
pub mod release;
pub mod release_history;
+50 -1
View File
@@ -57,6 +57,10 @@ pub struct PluginEntry {
pub deps: Vec<String>,
#[serde(default = "yes")]
pub required: bool,
/// Why the manager switched this plugin off by itself (it could not be rebuilt for the current
/// core). Such a plugin is rebuilt and switched back on after the next core change.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub disabled_reason: Option<String>,
}
/// The root registry document in `.manager/plugins.json`.
@@ -776,6 +780,7 @@ pub fn install(
conf_files: meta.conf_files.clone(),
deps: meta.deps.clone(),
required: meta.required,
disabled_reason: None,
};
reg.plugins.insert(meta.id.clone(), entry);
@@ -830,6 +835,14 @@ pub fn set_enabled(server: &Path, id: &str, enabled: bool) -> Result<()> {
if id == "playerbots" && reg.plugins.get("companions").map(|p| p.enabled).unwrap_or(false) {
return Err(Error::BotModulesConflict);
}
// The core refuses a plugin built for another core: it has to be rebuilt first.
if let (Some(host), Some(built)) = (crate::provenance::realm_host_fingerprint(server), installed_plugin_fingerprint(server, id)) {
if host != built {
return Err(Error::Invalid(format!(
"Plugin '{id}' was built for a previous core; rebuild it before switching it on"
)));
}
}
for dep in &entry.deps {
if !reg.plugins.get(dep).map(|p| p.enabled).unwrap_or(false) {
@@ -849,7 +862,9 @@ pub fn set_enabled(server: &Path, id: &str, enabled: bool) -> Result<()> {
}
tx.set_phase(TransactionPhase::Applying)?;
reg.plugins.get_mut(id).unwrap().enabled = enabled;
let e = reg.plugins.get_mut(id).unwrap();
e.enabled = enabled;
e.disabled_reason = None;
save_registry(server, &reg)?;
check_crash_injection("after_registry_write");
@@ -882,6 +897,39 @@ pub fn set_enabled(server: &Path, id: &str, enabled: bool) -> Result<()> {
Ok(())
}
/// Switches a plugin off because it cannot run on the current core, together with every enabled plugin
/// that depends on it, and records why. The core itself is never held back by a plugin.
pub fn disable_with_reason(server: &Path, id: &str, reason: &str) -> Result<Vec<String>> {
require_server_stopped(server)?;
let mut tx = TxGuard::begin_op(server, PluginOpType::Disable, id)?;
let mut reg = load_registry_direct(server)?;
if !reg.plugins.contains_key(id) {
return Err(Error::Invalid(format!("Plugin '{id}' is not installed")));
}
let mut off = vec![id.to_string()];
let mut i = 0;
while i < off.len() {
let cur = off[i].clone();
for (other_id, other) in &reg.plugins {
if other.enabled && other.deps.contains(&cur) && !off.contains(other_id) {
off.push(other_id.clone());
}
}
i += 1;
}
tx.set_phase(TransactionPhase::Applying)?;
for (n, pid) in off.iter().enumerate() {
let e = reg.plugins.get_mut(pid).unwrap();
e.enabled = false;
e.disabled_reason = Some(if n == 0 { reason.to_string() } else { format!("depends on '{id}', which was switched off: {reason}") });
}
save_registry(server, &reg)?;
regenerate_lockfile(server, &reg)?;
tx.set_phase(TransactionPhase::Validating)?;
tx.commit()?;
Ok(off)
}
/// Update an installed plugin to a new version with 3-way configuration merge.
pub fn update(
server: &Path,
@@ -941,6 +989,7 @@ pub fn update(
conf_files: new_meta.conf_files,
deps: new_meta.deps,
required: new_meta.required,
disabled_reason: None,
};
reg.plugins.insert(new_meta.id, updated_entry);
+37 -28
View File
@@ -146,34 +146,9 @@ pub fn plan_sql_review(module_id: &str, report: &CompatibilityReport) -> Option<
})
}
/// Installs a qualified build package into the realm's Plugin Store and enables it.
/// Enforces Stop-Gate and SQL Review Gate.
pub fn install_qualified_module(
realm_root: &Path,
module_id: &str,
version: &str,
apply_sql: bool,
sql_plan: Option<&SqlReviewPlan>,
) -> Result<PreviewInstallResult> {
// 1. Enforce Stop-Gate
require_server_stopped(realm_root)?;
// 2. Check SQL Review Gate
if let Some(plan) = sql_plan {
if !apply_sql && plan.total_sql_files > 0 {
return Ok(PreviewInstallResult {
success: false,
module_id: module_id.to_string(),
display_name: module_id.to_string(),
enabled: false,
sql_applied: false,
message: "SQL Review required before activating module. User confirmation needed.".into(),
sql_plan: Some(plan.clone()),
});
}
}
// 3. Locate package in plugin store
/// A module the build engine packaged into `.manager/plugin-store/<id>/<version>`: its metadata, DLL and
/// configuration templates, ready for the registry.
pub fn built_package(realm_root: &Path, module_id: &str, version: &str) -> Result<(PluginMetadata, Vec<u8>, Vec<(String, Vec<u8>)>)> {
let store_dir = realm_root.join(".manager").join("plugin-store").join(module_id).join(version);
let dll_name = format!("{}.dll", module_id);
let dll_path = store_dir.join(&dll_name);
@@ -250,6 +225,40 @@ pub fn install_qualified_module(
icon: None,
};
Ok((meta, dll_bytes, dist_confs))
}
/// Installs a qualified build package into the realm's Plugin Store and enables it.
/// Enforces Stop-Gate and SQL Review Gate.
pub fn install_qualified_module(
realm_root: &Path,
module_id: &str,
version: &str,
apply_sql: bool,
sql_plan: Option<&SqlReviewPlan>,
) -> Result<PreviewInstallResult> {
// 1. Enforce Stop-Gate
require_server_stopped(realm_root)?;
// 2. Check SQL Review Gate
if let Some(plan) = sql_plan {
if !apply_sql && plan.total_sql_files > 0 {
return Ok(PreviewInstallResult {
success: false,
module_id: module_id.to_string(),
display_name: module_id.to_string(),
enabled: false,
sql_applied: false,
message: "SQL Review required before activating module. User confirmation needed.".into(),
sql_plan: Some(plan.clone()),
});
}
}
// 3. Locate package in plugin store
let (meta, dll_bytes, dist_confs) = built_package(realm_root, module_id, version)?;
let display_name = meta.name.clone();
let dist_confs_ref: Vec<(String, &[u8])> = dist_confs.iter().map(|(n, b)| (n.clone(), b.as_slice())).collect();
// 4. Execute transactional install into store & enable in plugins.lock
+241
View File
@@ -0,0 +1,241 @@
//! Keeps installed plugins usable across core changes.
//!
//! A plugin loads only into the core whose ABI fingerprint it was built with, and the core changes almost
//! every day. After a core update (or a rollback) every plugin that should run is brought to the current
//! fingerprint: an earlier build already in the store is reused, otherwise it is rebuilt from the sources
//! it was last built from. A plugin that cannot be brought up to date is switched off with the reason; the
//! core update itself always goes ahead.
use std::fs;
use std::path::Path;
use serde::Serialize;
use crate::build_engine;
use crate::error::Result;
use crate::plugins;
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[serde(tag = "result", rename_all = "camelCase")]
pub enum ModuleRebuild {
/// Already built for this core.
UpToDate { id: String },
/// An earlier build for this core was still in the store (a rollback to a core seen before).
Reused { id: String },
Rebuilt { id: String, seconds: u64 },
/// Switched off; `also` lists the plugins that depend on it and were switched off with it.
Disabled { id: String, reason: String, also: Vec<String> },
}
/// Whether a plugin takes part: enabled, or switched off earlier only because a rebuild failed.
fn wanted(entry: &plugins::PluginEntry) -> bool {
entry.installed && (entry.enabled || entry.disabled_reason.is_some())
}
/// Brings every plugin that should run to the realm's current core. Does nothing for a core without
/// plugin support. Never fails because of one plugin: each outcome is reported.
pub fn rebuild_for_current_core(
realm_root: &Path,
progress: &dyn Fn(&str, &str),
) -> Result<Vec<ModuleRebuild>> {
let Some(host) = crate::provenance::realm_host_fingerprint(realm_root) else {
return Ok(Vec::new());
};
let registry = plugins::load_registry(realm_root)?;
let mut out = Vec::new();
for (id, entry) in registry.plugins.iter().filter(|(_, e)| wanted(e)) {
if entry.enabled && plugins::installed_plugin_fingerprint(realm_root, id).as_deref() == Some(host.as_str()) {
out.push(ModuleRebuild::UpToDate { id: id.clone() });
continue;
}
if out.iter().any(|o| matches!(o, ModuleRebuild::Disabled { also, .. } if also.contains(id))) {
continue; // switched off together with a plugin it depends on
}
progress(id, "Building the module for the new core");
let result = bring_up_to_date(realm_root, id, &host, progress).and_then(|done| {
if !plugins::load_registry(realm_root).map_err(|e| e.to_string())?.plugins.get(id).is_some_and(|e| e.enabled) {
plugins::set_enabled(realm_root, id, true).map_err(|e| e.to_string())?;
}
Ok(done)
});
out.push(match result {
Ok(done) => done,
Err(reason) => {
let reason = first_line(&reason);
progress(id, &format!("Switched off: {reason}"));
let off = plugins::disable_with_reason(realm_root, id, &reason)?;
ModuleRebuild::Disabled { id: id.clone(), reason, also: off.into_iter().skip(1).collect() }
}
});
}
Ok(out)
}
fn bring_up_to_date(
realm_root: &Path,
id: &str,
host: &str,
progress: &dyn Fn(&str, &str),
) -> std::result::Result<ModuleRebuild, String> {
if let Some(sha) = stored_build_for(realm_root, id, host) {
reuse_stored(realm_root, id, &sha).map_err(|e| e.to_string())?;
return Ok(ModuleRebuild::Reused { id: id.to_string() });
}
let recipe = build_engine::read_build_recipe(realm_root, id)
.ok_or("the manager does not know which sources this module was built from; install it again")?;
if !recipe.source_dir.is_dir() || recipe.bundled.iter().any(|b| !b.is_dir()) {
return Err("the module's sources are no longer on this PC; install it again".into());
}
let started = std::time::Instant::now();
let built = build_engine::build_module_with_bundle(&recipe.source_dir, &recipe.bundled, realm_root, None, None)
.map_err(|e| e.to_string())?;
if built.module_id != id {
return Err(format!("the sources now build module '{}', not '{id}'", built.module_id));
}
progress(id, "Installing the rebuilt module");
let (meta, dll, confs) = crate::preview::built_package(realm_root, id, &built.version).map_err(|e| e.to_string())?;
let confs: Vec<(String, &[u8])> = confs.iter().map(|(n, b)| (n.clone(), b.as_slice())).collect();
plugins::update(realm_root, meta, &dll, None, &confs).map_err(|e| e.to_string())?;
if plugins::installed_plugin_fingerprint(realm_root, id).as_deref() != Some(host) {
return Err("the rebuilt module does not match the installed core".into());
}
Ok(ModuleRebuild::Rebuilt { id: id.to_string(), seconds: started.elapsed().as_secs() })
}
/// The store keeps every build by content hash: find one made for `host`.
fn stored_build_for(realm_root: &Path, id: &str, host: &str) -> Option<String> {
let dir = plugins::store_dir(realm_root).join(id);
fs::read_dir(dir).ok()?.flatten().find_map(|e| {
let dll = e.path().join(format!("{id}.dll"));
(crate::provenance::file_abi_fingerprint(&dll).as_deref() == Some(host))
.then(|| e.file_name().to_string_lossy().into_owned())
})
}
fn reuse_stored(realm_root: &Path, id: &str, sha: &str) -> Result<()> {
let dir = plugins::store_dir(realm_root).join(id).join(sha);
let meta = plugins::load_store_metadata(realm_root, id, sha)?;
let dll = fs::read(dir.join(format!("{id}.dll")))?;
let mut confs = Vec::new();
for name in &meta.conf_files {
if let Ok(bytes) = fs::read(dir.join(name)) {
confs.push((name.clone(), bytes));
}
}
let confs: Vec<(String, &[u8])> = confs.iter().map(|(n, b)| (n.clone(), b.as_slice())).collect();
plugins::update(realm_root, meta, &dll, None, &confs)
}
fn first_line(s: &str) -> String {
let line = s.lines().find(|l| !l.trim().is_empty()).unwrap_or(s).trim();
if line.chars().count() > 300 { line.chars().take(300).collect::<String>() + "…" } else { line.to_string() }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::plugins::{PluginMetadata, PluginsRegistry};
use std::collections::BTreeMap;
/// The smallest PE image with a ".coapl" section, as plugins and plugin hosts carry it.
fn image(id: &str, fp: &str) -> Vec<u8> {
let text = format!("COAPLUGIN1|{id}|{fp}\0");
let mut b = vec![0u8; 0x200];
b[0] = b'M';
b[1] = b'Z';
b[0x3C..0x40].copy_from_slice(&0x40u32.to_le_bytes());
b[0x40..0x44].copy_from_slice(b"PE\0\0");
b[0x46..0x48].copy_from_slice(&1u16.to_le_bytes()); // one section, no optional header
let sec = 0x40 + 24;
b[sec..sec + 6].copy_from_slice(b".coapl");
let len = text.len() as u32;
b[sec + 8..sec + 12].copy_from_slice(&len.to_le_bytes());
b[sec + 16..sec + 20].copy_from_slice(&len.to_le_bytes());
b[sec + 20..sec + 24].copy_from_slice(&0x100u32.to_le_bytes());
b[0x100..0x100 + text.len()].copy_from_slice(text.as_bytes());
b
}
fn meta(id: &str, dll: &[u8]) -> PluginMetadata {
PluginMetadata {
id: id.into(),
name: id.into(),
version: "1.0.0".into(),
description: BTreeMap::new(),
sha256: plugins::sha256_digest(dll),
required: false,
deps: vec![],
conf_files: vec![],
enable_key: None,
status: None,
icon: None,
}
}
fn registry(server: &Path) -> PluginsRegistry {
plugins::load_registry(server).unwrap()
}
#[test]
fn plugins_follow_the_core_or_are_switched_off_with_a_reason() {
let old_fp = "a".repeat(64);
let new_fp = "b".repeat(64);
let dir = tempfile::tempdir().unwrap();
let server = dir.path();
fs::create_dir_all(server.join("Core")).unwrap();
fs::write(server.join("Core/worldserver.exe"), image("worldserver", &old_fp)).unwrap();
// "kept" was built for both cores (the store has both builds); "lost" only for the old one and
// the manager has no recipe to rebuild it from.
let kept_new = image("kept", &new_fp);
let kept_old = image("kept", &old_fp);
plugins::install(server, meta("kept", &kept_new), &kept_new, None, &[], true).unwrap();
plugins::update(server, meta("kept", &kept_old), &kept_old, None, &[]).unwrap();
let lost = image("lost", &old_fp);
plugins::install(server, meta("lost", &lost), &lost, None, &[], true).unwrap();
// A plugin the owner switched off stays off and is not rebuilt.
let idle = image("idle", &old_fp);
plugins::install(server, meta("idle", &idle), &idle, None, &[], false).unwrap();
// Same core: nothing to do.
let same = rebuild_for_current_core(server, &|_, _| {}).unwrap();
assert_eq!(same, vec![
ModuleRebuild::UpToDate { id: "kept".into() },
ModuleRebuild::UpToDate { id: "lost".into() },
]);
// New core.
fs::write(server.join("Core/worldserver.exe"), image("worldserver", &new_fp)).unwrap();
let out = rebuild_for_current_core(server, &|_, _| {}).unwrap();
assert_eq!(out[0], ModuleRebuild::Reused { id: "kept".into() });
assert!(matches!(&out[1], ModuleRebuild::Disabled { id, reason, .. } if id == "lost" && reason.contains("install it again")));
assert_eq!(out.len(), 2);
let reg = registry(server);
assert!(reg.plugins["kept"].enabled);
assert_eq!(plugins::installed_plugin_fingerprint(server, "kept").as_deref(), Some(new_fp.as_str()));
assert!(!reg.plugins["lost"].enabled);
assert!(reg.plugins["lost"].disabled_reason.is_some());
assert!(!reg.plugins["idle"].enabled && reg.plugins["idle"].disabled_reason.is_none());
let lock = fs::read_to_string(plugins::lockfile_path(server)).unwrap();
assert!(lock.contains("kept") && !lock.contains("\"lost\""), "{lock}");
// Back to the old core (a rollback): the old builds are put back, "lost" is switched on again.
fs::write(server.join("Core/worldserver.exe"), image("worldserver", &old_fp)).unwrap();
let back = rebuild_for_current_core(server, &|_, _| {}).unwrap();
assert_eq!(back, vec![
ModuleRebuild::Reused { id: "kept".into() },
ModuleRebuild::Reused { id: "lost".into() },
]);
let reg = registry(server);
assert!(reg.plugins["kept"].enabled && reg.plugins["lost"].enabled);
assert!(reg.plugins["lost"].disabled_reason.is_none());
}
#[test]
fn a_monolithic_core_needs_no_rebuild() {
let dir = tempfile::tempdir().unwrap();
fs::create_dir_all(dir.path().join("Core")).unwrap();
fs::write(dir.path().join("Core/worldserver.exe"), b"MZ no plugin host").unwrap();
assert!(rebuild_for_current_core(dir.path(), &|_, _| {}).unwrap().is_empty());
}
}
+27
View File
@@ -116,6 +116,11 @@ pub trait Env {
fn migrate(&self, manifest: &Manifest, staged_migrations: &Path) -> Result<ApplyReport>;
/// Start the server and confirm it becomes healthy.
fn validate(&self) -> Result<()>;
/// Bring installed plugins to the new core before it is started. Never fails the update: a plugin
/// that cannot be rebuilt is switched off and reported.
fn rebuild_plugins(&self, _report: &dyn Fn(&str, &str)) -> Vec<crate::rebuild::ModuleRebuild> {
Vec::new()
}
}
fn updates_dir(meta: &Path) -> PathBuf {
@@ -692,6 +697,9 @@ pub struct Outcome {
pub txn: Txn,
pub preexisting_character_count: usize,
pub migrations: Option<ApplyReport>,
/// What happened to each installed plugin on the new core (rebuilt, reused or switched off).
#[serde(default)]
pub modules: Vec<crate::rebuild::ModuleRebuild>,
}
fn step(report: &dyn Fn(&str, u8), name: &str, pct: u8) {
@@ -916,6 +924,9 @@ pub fn apply(p: &Params, report: &dyn Fn(&str, u8)) -> Result<Outcome> {
}
}
step(report, "Building modules for the new core", 80);
let modules = p.env.rebuild_plugins(&|id, what| report(&format!("{id}: {what}"), 80));
step(report, "Starting the updated server", 85);
// Core startup can apply SQL and write character data even when the package has no explicit migrations.
txn.databases_started = true;
@@ -938,6 +949,7 @@ pub fn apply(p: &Params, report: &dyn Fn(&str, u8)) -> Result<Outcome> {
txn,
preexisting_character_count,
migrations: migrated,
modules,
});
}
@@ -949,6 +961,7 @@ pub fn apply(p: &Params, report: &dyn Fn(&str, u8)) -> Result<Outcome> {
txn,
preexisting_character_count,
migrations: migrated,
modules,
})
}
@@ -1198,6 +1211,9 @@ fn restore_transaction(
if saved_meta.exists() {
fsx::atomic_write(&meta.join("install.json"), &fs::read(saved_meta)?)?;
}
// Plugins may already have been rebuilt for the core that was just taken back out; the builds for
// the restored core are still in the plugin store and are put back (or rebuilt).
let _ = env.rebuild_plugins(&|_, _| {});
Ok(())
}
@@ -1703,6 +1719,17 @@ impl RepackEnv<'_> {
}
impl Env for RepackEnv<'_> {
fn rebuild_plugins(&self, report: &dyn Fn(&str, &str)) -> Vec<crate::rebuild::ModuleRebuild> {
match crate::rebuild::rebuild_for_current_core(self.root, report) {
Ok(done) => done,
Err(e) => {
tracing::warn!(error = %e, "plugins could not be brought to the new core");
report("modules", &format!("not rebuilt: {e}"));
Vec::new()
}
}
}
fn recover_validation(&self) -> Result<()> {
crate::update_isolation::restore(self.root, self.meta_dir)
}