Skip to content

Commit 2056b13

Browse files
committed
fix(plugins): preserve registry epoch on activation rollback
1 parent 33e556f commit 2056b13

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎src/plugins/loader-shared.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import type { PluginRecord, PluginRegistry } from "./registry.js";
3939
import {
4040
captureActivePluginRegistrySnapshot,
4141
commitStagedPluginRegistry,
42-
restoreActivePluginRegistrySnapshot,
42+
rollbackStagedPluginRegistry,
4343
stageActivePluginRegistry,
4444
} from "./runtime.js";
4545
import { validateJsonSchemaValue } from "./schema-validator.js";
@@ -374,7 +374,7 @@ export function activatePluginRegistry(
374374
activateContextEngineRegistrations(registry);
375375
commitStagedPluginRegistry(activeSnapshot.activeRegistry, registry);
376376
} catch (error) {
377-
restoreActivePluginRegistrySnapshot(activeSnapshot);
377+
rollbackStagedPluginRegistry(activeSnapshot);
378378
if (previousHookRegistry) {
379379
initializeGlobalHookRunner(previousHookRegistry);
380380
} else {

‎src/plugins/loader.registration.test-utils.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ import {
6363
registerMemoryPromptSupplement,
6464
resolveMemoryFlushPlan,
6565
} from "./memory-state.test-fixtures.js";
66-
import { isPluginRegistryRetired } from "./registry-lifecycle.js";
66+
import {
67+
activatePluginRecordLifecycleEpoch,
68+
isPluginRecordLifecycleEpochActive,
69+
isPluginRegistryRetired,
70+
} from "./registry-lifecycle.js";
6771
import { createEmptyPluginRegistry } from "./registry.js";
6872
import {
6973
getActivePluginRegistry,
@@ -393,6 +397,10 @@ describe("loadOpenClawPlugins", () => {
393397
const priorKey = getActivePluginRegistryKey();
394398
const priorMode = getActivePluginRuntimeSubagentMode();
395399
const priorWorkspaceDir = getActivePluginRegistryWorkspaceDir();
400+
const priorRecord = priorRegistry.plugins.find((entry) => entry.id === "activation-prior");
401+
expect(priorRecord).toBeDefined();
402+
const priorEpoch = activatePluginRecordLifecycleEpoch(priorRegistry, priorRecord!);
403+
expect(priorEpoch).toBeDefined();
396404

397405
const replacement = writePlugin({
398406
id: "activation-replacement",
@@ -437,10 +445,14 @@ describe("loadOpenClawPlugins", () => {
437445
expect(getActivePluginRuntimeSubagentMode()).toBe(priorMode);
438446
expect(getActivePluginRegistryWorkspaceDir()).toBe(priorWorkspaceDir);
439447
expect(getPluginCommandSpecs().map((command) => command.name)).toEqual(["prior"]);
448+
expect(isPluginRecordLifecycleEpochActive(priorRegistry, priorRecord!, priorEpoch!)).toBe(true);
440449

441450
const activated = loadOpenClawPlugins(replacementOptions);
442451
expect(activated.commands.map((entry) => entry.command.name)).toEqual(["replacement"]);
443452
expect(getPluginCommandSpecs().map((command) => command.name)).toEqual(["replacement"]);
453+
expect(isPluginRecordLifecycleEpochActive(priorRegistry, priorRecord!, priorEpoch!)).toBe(
454+
false,
455+
);
444456
});
445457

446458
it("fails plugin registration when a hook is missing its required name", () => {

‎src/plugins/runtime.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,34 @@ export function restoreActivePluginRegistrySnapshot(
200200
});
201201
}
202202

203+
/** Rolls back a staged registry without reactivating the prior committed generation. */
204+
export function rollbackStagedPluginRegistry(
205+
snapshot: ReturnType<typeof captureActivePluginRegistrySnapshot>,
206+
): void {
207+
installActivePluginRegistry({
208+
registry: snapshot.activeRegistry,
209+
key: snapshot.key,
210+
runtimeSubagentMode: snapshot.runtimeSubagentMode,
211+
workspaceDir: snapshot.workspaceDir,
212+
// Staging never retired the prior registry. Reactivating it here would mint a
213+
// new epoch and revoke closures that remained authoritative through rollback.
214+
activateRegistry: false,
215+
});
216+
}
217+
203218
function installActivePluginRegistry(params: {
204219
registry: PluginRegistry | null;
205220
key: string | null;
206221
runtimeSubagentMode: RegistryState["runtimeSubagentMode"];
207222
workspaceDir: string | null;
208223
retirePrevious?: boolean;
224+
activateRegistry?: boolean;
209225
}): void {
210226
const previousRegistry = asPluginRegistry(state.activeRegistry);
211227
state.activeRegistry = params.registry;
212-
markPluginRegistryActive(params.registry);
228+
if (params.activateRegistry !== false) {
229+
markPluginRegistryActive(params.registry);
230+
}
213231
state.activeVersion += 1;
214232
if (params.registry) {
215233
settlePreparedMessageToolCatalog(params.registry, state.activeVersion);

0 commit comments

Comments
 (0)