Write plugin versions while loading configs

The staging copy made by Config.set() went through the constructor, so
plugin migrations ran a second time on the live fields. A version-gated
migration was applied twice when setPluginVersions() changed the version.
Staging now copies the fields with update() instead.

A config saved without <plugin>.version got the running version as its
default, so setPluginVersions() saw no change and nothing saved the stamp.
The constructor now writes the registered plugin versions after the
migrations run and marks the config dirty when that changes a loaded value.
Controller.bootstrap() saves the instances that came out dirty, and
setPluginVersions() is gone.

Restructure the SyncHook and plugin config tests by method, and suggest the
controller config version for role migrations in the docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-10-14 16:37:03 +00:00
1 parent aec0fc7a1e
commit e455d17bb7
10 files changed
+188 -119

No files matched your search

+1 -4
View File
@@ -430,9 +430,6 @@ async function initialize(): Promise<InitializeParameters> {
);
controllerConfig.set("controller.version", packageConfig.version); // Allows tracking last loaded version
if (!args.recovery) {
lib.setPluginVersions(controllerConfig, pluginInfos);
}
if (!controllerConfig.get("controller.auth_secret")) {
logger.info("Generating new controller authentication secret");
@@ -483,7 +480,7 @@ async function startup() {
controllerConfig,
Boolean(args.canRestart),
Boolean(args.recovery),
...await Controller.bootstrap(controllerConfig, args.recovery ? [] : pluginInfos)
...await Controller.bootstrap(controllerConfig)
);
// Refuse to start if there are no users loaded
+3 -10
View File
@@ -148,15 +148,7 @@ export default class Controller {
}
});
/**
* Load the controller datastores
*
* @param config - Controller config.
* @param pluginInfos -
* Plugins whose migrations ran, their versions are saved to the
* instance configs.
*/
static async bootstrap(config: lib.ControllerConfig, pluginInfos: lib.PluginNodeEnvInfo[] = []) {
static async bootstrap(config: lib.ControllerConfig) {
let databaseDirectory = config.get("controller.database_directory");
await fs.mkdir(databaseDirectory, { recursive: true });
@@ -183,8 +175,9 @@ export default class Controller {
InstanceRecord.fromJSON.bind(InstanceRecord),
this.migrateInstances, this.finaliseInstances,
).bootstrap());
// Save plugin versions written while loading
for (const instance of instances.values()) {
if (lib.setPluginVersions(instance.config, pluginInfos)) {
if (instance.config.dirty) {
instances.set(instance);
}
}
-3
View File
@@ -188,9 +188,6 @@ async function startHost() {
);
hostConfig.set("host.version", packageConfig.version); // Allows tracking last loaded version
if (!args.recovery) {
lib.setPluginVersions(hostConfig, pluginInfos);
}
if (command === "config") {
await lib.handleConfigCommand(args, hostConfig, hostConfigLock);
+29 -6
View File
@@ -231,6 +231,12 @@ export class Config<
*/
declare static pluginMigrations?: SyncHook<ConfigSchema>;
/**
* Versions of the plugins whose migrations are attached, keyed by
* plugin name. Written to `<plugin>.version` after the migrations run.
*/
static pluginVersions = new Map<string, string>();
/** Defines the default access locations for the config class */
static defaultAccess: ConfigLocation[] = ["controller", "host", "control"];
@@ -241,10 +247,15 @@ export class Config<
/** Set to true during validators to prevent edits */
private _readonly = false; // TODO replace with "using config.asReadonly()"
/** Set to true when a field in the config is changed. */
private dirty = false;
private _dirty = false;
/** Set to true when a 'restart required' field in the config is changed. */
restartRequired = false;
/** True if the config has changes that have not been saved. */
get dirty() {
return this._dirty;
}
/**
* Create a new instance of the given config
*
@@ -282,6 +293,16 @@ export class Config<
}
this.update(migrated, false, location);
}
for (const [pluginName, version] of this.constructor.pluginVersions) {
const name = `${pluginName}.version` as keyof Fields & string;
if (name in this.fields && this.fields[name] !== version) {
this.fields[name] = version as Fields[typeof name];
if (fields) {
this._dirty = true;
}
}
}
}
get filepath() {
@@ -289,7 +310,7 @@ export class Config<
}
set filepath(filepath: string | undefined) {
this.dirty = true;
this._dirty = true;
this._filepath = filepath;
}
@@ -300,7 +321,9 @@ export class Config<
// TODO replace with "using config.startStating()"
get staging(): Config<Fields> {
if (this._staging) { return this._staging.config; }
const config = new this.constructor(this.location, this.fields);
// Copied with update so migrations don't run again on the live fields
const config = new this.constructor(this.location);
config.update(this.fields, false);
const changes = new Set<string>();
config.on("fieldChanged", name => changes.add(name));
this._staging = { config, changes };
@@ -454,7 +477,7 @@ export class Config<
if (def.restartRequired) {
this.restartRequired = true;
}
this.dirty = true;
this._dirty = true;
this.emit("fieldChanged", name, value, prev);
}
}
@@ -476,14 +499,14 @@ export class Config<
* @param force - Set to true to save the file even if no changes have been made.
*/
async save(force = false) {
if (!force && !this.dirty) {
if (!force && !this._dirty) {
return;
}
if (!this.filepath) {
throw new Error("Cannot save config which has no filepath");
}
await safeOutputFile(this.filepath, JSON.stringify(this, null, "\t"), { mode: 0o600 });
this.dirty = false;
this._dirty = false;
}
static jsonSchema = ConfigSchema;
+2 -23
View File
@@ -812,10 +812,10 @@ export function addPluginFieldDefinitions(
Config.fieldDefinitions[`${pluginInfo.name}.load_plugin`] = fieldDef;
// Version of the plugin the config was last loaded with, see setPluginVersions
// Version of the plugin the config was last loaded with, see Config.pluginVersions
Config.fieldDefinitions[`${pluginInfo.name}.version`] = {
type: "string",
initialValue: "version" in pluginInfo ? pluginInfo.version : "",
initialValue: "",
readonly: ["controller", "host"],
hidden: true,
};
@@ -845,24 +845,3 @@ export function addPluginConfigFields(pluginInfos: PluginNodeEnvInfo[] | PluginW
}
}
/**
* Set the version field of each plugin to the version that was loaded
*
* Done after loading a config so the plugin migrations can compare the
* stored version against the one now running.
*
* @param config - Config to update.
* @param pluginInfos - Plugins loaded.
* @returns true if any version changed.
*/
export function setPluginVersions(config: classes.Config<any>, pluginInfos: PluginNodeEnvInfo[]) {
let changed = false;
for (const pluginInfo of pluginInfos) {
const name = `${pluginInfo.name}.version`;
if (config.constructor.fieldDefinitions[name] && config.get(name) !== pluginInfo.version) {
config.set(name, pluginInfo.version);
changed = true;
}
}
return changed;
}
+4 -1
View File
@@ -10,6 +10,7 @@ import * as libErrors from "./errors.js";
import * as libPlugin from "./plugin.js";
import { type Logger, logger } from "./logging.js";
import { loadPluginEntrypoint, loadPluginClass } from "./loadPlugin.js";
import { Config } from "./config/classes.js";
import { ControllerConfig, HostConfig, InstanceConfig } from "./config/definitions.js";
import Role from "./data/Role.js";
@@ -97,7 +98,8 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
*
* Must be called before configs and roles are loaded for the migrations
* to run on them. Loaded for every plugin, including those disabled with
* load_plugin.
* load_plugin. Also registers the plugin versions written to each config
* after the migrations run.
*
* @param pluginInfos - Plugins to load migrations for.
*/
@@ -110,6 +112,7 @@ export async function loadPluginMigrations(pluginInfos: libPlugin.PluginNodeEnvI
};
for (const pluginInfo of pluginInfos) {
Config.pluginVersions.set(pluginInfo.name, pluginInfo.version);
if (!pluginInfo.migrationEntrypoint) {
continue;
}