fix: commit pending plugin install records in config flows

This commit is contained in:
Shakker
2026-04-26 01:53:35 +01:00
parent 87142b5fb1
commit 921ffad7c7
5 changed files with 229 additions and 7 deletions

View File

@@ -113,6 +113,7 @@ Docs: https://docs.openclaw.ai
- Plugins/install: restore the previous plugin index records if a concurrent config write conflict interrupts install, update, or uninstall metadata commits. Thanks @shakkernerd.
- Plugins/update: restore previous plugin index records if core update or channel setup hits a concurrent config write conflict after plugin metadata changes. Thanks @shakkernerd.
- Plugins/onboarding: defer channel/provider plugin install records until the owning config write commits, keeping setup failures from advancing the plugin index ahead of `openclaw.json`. Thanks @shakkernerd.
- Plugins/config: route configure and agent setup writes with pending plugin install records through the plugin index commit helper so provider onboarding metadata is not stripped by plain config writes. Thanks @shakkernerd.
- Sessions: keep embedded runtime context out of the visible user prompt by
sending it as a hidden next-turn custom message, and teach doctor to repair
affected 2026.4.24 transcripts with duplicated prompt-rewrite branches.

View File

@@ -0,0 +1,158 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import type { PluginInstallRecord } from "../config/types.plugins.js";
const mocks = vi.hoisted(() => ({
loadInstalledPluginIndexInstallRecords: vi.fn(),
replaceConfigFile: vi.fn(),
writePersistedInstalledPluginIndexInstallRecords: vi.fn(),
}));
vi.mock("../config/config.js", () => ({
replaceConfigFile: mocks.replaceConfigFile,
}));
vi.mock("../plugins/installed-plugin-index-records.js", async (importOriginal) => {
const actual =
await importOriginal<typeof import("../plugins/installed-plugin-index-records.js")>();
return {
...actual,
loadInstalledPluginIndexInstallRecords: mocks.loadInstalledPluginIndexInstallRecords,
writePersistedInstalledPluginIndexInstallRecords:
mocks.writePersistedInstalledPluginIndexInstallRecords,
};
});
import { commitConfigWithPendingPluginInstalls } from "./plugins-install-record-commit.js";
describe("commitConfigWithPendingPluginInstalls", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.loadInstalledPluginIndexInstallRecords.mockResolvedValue({});
mocks.replaceConfigFile.mockResolvedValue(undefined);
mocks.writePersistedInstalledPluginIndexInstallRecords.mockResolvedValue(undefined);
});
it("moves pending plugin install records into the plugin index before writing stripped config", async () => {
const existingRecords: Record<string, PluginInstallRecord> = {
existing: {
source: "npm",
spec: "existing@1.0.0",
},
};
const pendingRecords: Record<string, PluginInstallRecord> = {
demo: {
source: "npm",
spec: "demo@1.0.0",
},
};
mocks.loadInstalledPluginIndexInstallRecords.mockResolvedValue(existingRecords);
const nextConfig: OpenClawConfig = {
plugins: {
entries: {
demo: { enabled: true },
},
installs: pendingRecords,
},
};
const result = await commitConfigWithPendingPluginInstalls({
nextConfig,
baseHash: "config-1",
});
expect(mocks.writePersistedInstalledPluginIndexInstallRecords).toHaveBeenCalledWith({
...existingRecords,
...pendingRecords,
});
expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
nextConfig: {
plugins: {
entries: {
demo: { enabled: true },
},
},
},
baseHash: "config-1",
writeOptions: {
unsetPaths: [["plugins", "installs"]],
},
});
expect(result).toEqual({
config: {
plugins: {
entries: {
demo: { enabled: true },
},
},
},
installRecords: {
...existingRecords,
...pendingRecords,
},
movedInstallRecords: true,
});
});
it("rolls back plugin index writes when the config write fails", async () => {
const existingRecords: Record<string, PluginInstallRecord> = {
existing: {
source: "npm",
spec: "existing@1.0.0",
},
};
mocks.loadInstalledPluginIndexInstallRecords.mockResolvedValue(existingRecords);
mocks.replaceConfigFile.mockRejectedValue(new Error("config changed"));
await expect(
commitConfigWithPendingPluginInstalls({
nextConfig: {
plugins: {
installs: {
demo: {
source: "npm",
spec: "demo@1.0.0",
},
},
},
},
}),
).rejects.toThrow("config changed");
expect(mocks.writePersistedInstalledPluginIndexInstallRecords).toHaveBeenNthCalledWith(1, {
existing: {
source: "npm",
spec: "existing@1.0.0",
},
demo: {
source: "npm",
spec: "demo@1.0.0",
},
});
expect(mocks.writePersistedInstalledPluginIndexInstallRecords).toHaveBeenNthCalledWith(
2,
existingRecords,
);
});
it("uses a plain config write when no pending plugin install records exist", async () => {
const nextConfig: OpenClawConfig = {
gateway: {
mode: "local",
},
};
const result = await commitConfigWithPendingPluginInstalls({ nextConfig });
expect(mocks.loadInstalledPluginIndexInstallRecords).not.toHaveBeenCalled();
expect(mocks.writePersistedInstalledPluginIndexInstallRecords).not.toHaveBeenCalled();
expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
nextConfig,
});
expect(result).toEqual({
config: nextConfig,
installRecords: {},
movedInstallRecords: false,
});
});
});

View File

@@ -1,17 +1,28 @@
import { replaceConfigFile } from "../config/config.js";
import type { ConfigWriteOptions } from "../config/io.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import type { PluginInstallRecord } from "../config/types.plugins.js";
import {
loadInstalledPluginIndexInstallRecords,
PLUGIN_INSTALLS_CONFIG_PATH,
withoutPluginInstallRecords,
writePersistedInstalledPluginIndexInstallRecords,
} from "../plugins/installed-plugin-index-records.js";
function mergeUnsetPaths(
left?: ConfigWriteOptions["unsetPaths"],
right?: ConfigWriteOptions["unsetPaths"],
): ConfigWriteOptions["unsetPaths"] | undefined {
const merged = [...(left ?? []), ...(right ?? [])];
return merged.length > 0 ? merged : undefined;
}
export async function commitPluginInstallRecordsWithConfig(params: {
previousInstallRecords?: Record<string, PluginInstallRecord>;
nextInstallRecords: Record<string, PluginInstallRecord>;
nextConfig: OpenClawConfig;
baseHash?: string;
writeOptions?: ConfigWriteOptions;
}): Promise<void> {
const previousInstallRecords =
params.previousInstallRecords ?? (await loadInstalledPluginIndexInstallRecords());
@@ -20,7 +31,12 @@ export async function commitPluginInstallRecordsWithConfig(params: {
await replaceConfigFile({
nextConfig: params.nextConfig,
...(params.baseHash !== undefined ? { baseHash: params.baseHash } : {}),
writeOptions: { unsetPaths: [Array.from(PLUGIN_INSTALLS_CONFIG_PATH)] },
writeOptions: {
...params.writeOptions,
unsetPaths: mergeUnsetPaths(params.writeOptions?.unsetPaths, [
Array.from(PLUGIN_INSTALLS_CONFIG_PATH),
]),
},
});
} catch (error) {
try {
@@ -34,3 +50,46 @@ export async function commitPluginInstallRecordsWithConfig(params: {
throw error;
}
}
export async function commitConfigWithPendingPluginInstalls(params: {
nextConfig: OpenClawConfig;
baseHash?: string;
writeOptions?: ConfigWriteOptions;
}): Promise<{
config: OpenClawConfig;
installRecords: Record<string, PluginInstallRecord>;
movedInstallRecords: boolean;
}> {
const pendingInstallRecords = params.nextConfig.plugins?.installs ?? {};
if (Object.keys(pendingInstallRecords).length === 0) {
await replaceConfigFile({
nextConfig: params.nextConfig,
...(params.baseHash !== undefined ? { baseHash: params.baseHash } : {}),
...(params.writeOptions ? { writeOptions: params.writeOptions } : {}),
});
return {
config: params.nextConfig,
installRecords: {},
movedInstallRecords: false,
};
}
const previousInstallRecords = await loadInstalledPluginIndexInstallRecords();
const nextInstallRecords = {
...previousInstallRecords,
...pendingInstallRecords,
};
const strippedConfig = withoutPluginInstallRecords(params.nextConfig);
await commitPluginInstallRecordsWithConfig({
previousInstallRecords,
nextInstallRecords,
nextConfig: strippedConfig,
...(params.baseHash !== undefined ? { baseHash: params.baseHash } : {}),
...(params.writeOptions ? { writeOptions: params.writeOptions } : {}),
});
return {
config: strippedConfig,
installRecords: nextInstallRecords,
movedInstallRecords: true,
};
}

View File

@@ -7,7 +7,7 @@ import {
} from "../agents/agent-scope.js";
import { ensureAuthProfileStore } from "../agents/auth-profiles.js";
import { resolveAuthStorePath } from "../agents/auth-profiles/paths.js";
import { replaceConfigFile } from "../config/config.js";
import { commitConfigWithPendingPluginInstalls } from "../cli/plugins-install-record-commit.js";
import { logConfigUpdated } from "../config/logging.js";
import { DEFAULT_AGENT_ID, normalizeAgentId } from "../routing/session-key.js";
import { type RuntimeEnv, writeRuntimeJson } from "../runtime.js";
@@ -133,7 +133,7 @@ export async function agentsAddCommand(
? applyAgentBindings(nextConfig, bindingParse.bindings)
: { config: nextConfig, added: [], updated: [], skipped: [], conflicts: [] };
await replaceConfigFile({
await commitConfigWithPendingPluginInstalls({
nextConfig: bindingResult.config,
...(baseHash !== undefined ? { baseHash } : {}),
});
@@ -360,10 +360,11 @@ export async function agentsAddCommand(
}
}
await replaceConfigFile({
const committed = await commitConfigWithPendingPluginInstalls({
nextConfig,
...(baseHash !== undefined ? { baseHash } : {}),
});
nextConfig = committed.config;
logConfigUpdated(runtime);
await ensureWorkspaceAndSessions(workspaceDir, runtime, {
skipBootstrap: Boolean(nextConfig.agents?.defaults?.skipBootstrap),

View File

@@ -3,7 +3,8 @@ import nodePath from "node:path";
import { isDeepStrictEqual } from "node:util";
import { describeCodexNativeWebSearch } from "../agents/codex-native-web-search.shared.js";
import { formatCliCommand } from "../cli/command-format.js";
import { readConfigFileSnapshot, replaceConfigFile, resolveGatewayPort } from "../config/config.js";
import { commitConfigWithPendingPluginInstalls } from "../cli/plugins-install-record-commit.js";
import { readConfigFileSnapshot, resolveGatewayPort } from "../config/config.js";
import { logConfigUpdated } from "../config/logging.js";
import { ConfigMutationConflictError } from "../config/mutate.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
@@ -457,10 +458,11 @@ export async function runConfigureWizard(
command: opts.command,
mode,
});
await replaceConfigFile({
const committed = await commitConfigWithPendingPluginInstalls({
nextConfig: remoteConfig,
...(currentBaseHash !== undefined ? { baseHash: currentBaseHash } : {}),
});
remoteConfig = committed.config;
currentBaseHash = undefined;
logConfigUpdated(runtime);
outro("Remote gateway configured.");
@@ -496,10 +498,11 @@ export async function runConfigureWizard(
const maxRetries = 3;
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
await replaceConfigFile({
const committed = await commitConfigWithPendingPluginInstalls({
nextConfig,
...(currentBaseHash !== undefined ? { baseHash: currentBaseHash } : {}),
});
nextConfig = committed.config;
// After successful write, re-read the snapshot to get the new hash
const freshSnapshot = await readConfigFileSnapshot();