Skip to content

Commit 14ecfcb

Browse files
fix: stop bundled plugin startup refresh loops
1 parent 52cda53 commit 14ecfcb

8 files changed

Lines changed: 377 additions & 71 deletions
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4+
import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js";
5+
6+
const mocks = vi.hoisted(() => ({
7+
listManagedPluginNpmRoots: vi.fn(),
8+
maybeRepairStaleManagedNpmBundledPlugins: vi.fn(),
9+
repairMissingConfiguredPluginInstalls: vi.fn(),
10+
relinkOpenClawPeerDependenciesInManagedNpmRoot: vi.fn(),
11+
runPluginPayloadSmokeCheck: vi.fn(),
12+
}));
13+
14+
vi.mock("../../commands/doctor/shared/missing-configured-plugin-install.js", () => ({
15+
repairMissingConfiguredPluginInstalls: mocks.repairMissingConfiguredPluginInstalls,
16+
}));
17+
vi.mock("../../commands/doctor-plugin-registry.js", () => ({
18+
maybeRepairStaleManagedNpmBundledPlugins: mocks.maybeRepairStaleManagedNpmBundledPlugins,
19+
}));
20+
vi.mock("../../plugins/plugin-peer-link.js", async (importOriginal) => {
21+
const actual = await importOriginal<typeof import("../../plugins/plugin-peer-link.js")>();
22+
return {
23+
...actual,
24+
relinkOpenClawPeerDependenciesInManagedNpmRoot:
25+
mocks.relinkOpenClawPeerDependenciesInManagedNpmRoot,
26+
};
27+
});
28+
vi.mock("../../plugins/npm-project-roots.js", async (importOriginal) => {
29+
const actual = await importOriginal<typeof import("../../plugins/npm-project-roots.js")>();
30+
return {
31+
...actual,
32+
listManagedPluginNpmRoots: mocks.listManagedPluginNpmRoots,
33+
};
34+
});
35+
vi.mock("./plugin-payload-validation.js", () => ({
36+
runPluginPayloadSmokeCheck: mocks.runPluginPayloadSmokeCheck,
37+
}));
38+
39+
import { resolvePluginNpmGenerationProjectDir } from "../../plugins/install-paths.js";
40+
import {
41+
loadInstalledPluginIndexInstallRecords,
42+
readPersistedInstalledPluginIndexInstallRecords,
43+
writePersistedInstalledPluginIndexInstallRecords,
44+
} from "../../plugins/installed-plugin-index-records.js";
45+
import { VERSION } from "../../version.js";
46+
import { runPostCorePluginConvergence } from "./post-core-plugin-convergence.js";
47+
48+
describe("post-core bundled plugin retirement", () => {
49+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
50+
51+
beforeEach(() => {
52+
vi.clearAllMocks();
53+
mocks.listManagedPluginNpmRoots.mockImplementation((npmRoot: string) =>
54+
Promise.resolve([npmRoot]),
55+
);
56+
mocks.relinkOpenClawPeerDependenciesInManagedNpmRoot.mockResolvedValue({
57+
checked: 0,
58+
attempted: 0,
59+
repaired: 0,
60+
skipped: 0,
61+
});
62+
mocks.runPluginPayloadSmokeCheck.mockResolvedValue({ checked: [], failures: [] });
63+
});
64+
65+
it("retires payload and record state before repair across two starts", async () => {
66+
const stateDir = tempDirs.make("openclaw-post-core-convergence-");
67+
const bundledRoot = tempDirs.make("openclaw-post-core-bundled-");
68+
const cfg = {
69+
update: { channel: "beta" as const },
70+
plugins: { allow: ["codex"], entries: { codex: { enabled: true } } },
71+
};
72+
const env = {
73+
OPENCLAW_STATE_DIR: stateDir,
74+
OPENCLAW_BUNDLED_PLUGINS_DIR: bundledRoot,
75+
OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR: "1",
76+
VITEST: "true",
77+
};
78+
const bundledDir = path.join(bundledRoot, "codex");
79+
fs.mkdirSync(bundledDir, { recursive: true });
80+
fs.writeFileSync(path.join(bundledDir, "index.js"), "export default {};\n", "utf8");
81+
fs.writeFileSync(
82+
path.join(bundledDir, "openclaw.plugin.json"),
83+
JSON.stringify({
84+
id: "codex",
85+
name: "codex",
86+
version: VERSION,
87+
configSchema: { type: "object" },
88+
}),
89+
"utf8",
90+
);
91+
fs.writeFileSync(
92+
path.join(bundledDir, "package.json"),
93+
JSON.stringify({ name: "@openclaw/codex", version: VERSION }),
94+
"utf8",
95+
);
96+
const npmRoot = resolvePluginNpmGenerationProjectDir({
97+
npmDir: path.join(stateDir, "npm"),
98+
packageName: "@openclaw/codex",
99+
generationKey: "@openclaw/[email protected]",
100+
});
101+
const packageDir = path.join(npmRoot, "node_modules", "@openclaw", "codex");
102+
fs.mkdirSync(packageDir, { recursive: true });
103+
fs.writeFileSync(
104+
path.join(npmRoot, "package.json"),
105+
JSON.stringify({ dependencies: { "@openclaw/codex": "2026.7.2-beta.7" } }),
106+
"utf8",
107+
);
108+
fs.writeFileSync(
109+
path.join(packageDir, "package.json"),
110+
JSON.stringify({ name: "@openclaw/codex", version: "2026.7.2-beta.7" }),
111+
"utf8",
112+
);
113+
fs.writeFileSync(
114+
path.join(packageDir, "openclaw.plugin.json"),
115+
JSON.stringify({ id: "codex", name: "codex", configSchema: { type: "object" } }),
116+
"utf8",
117+
);
118+
await writePersistedInstalledPluginIndexInstallRecords(
119+
{
120+
codex: {
121+
source: "npm",
122+
spec: "@openclaw/codex@beta",
123+
installPath: packageDir,
124+
version: "2026.7.2-beta.7",
125+
resolvedName: "@openclaw/codex",
126+
resolvedSpec: "@openclaw/[email protected]",
127+
resolvedVersion: "2026.7.2-beta.7",
128+
},
129+
},
130+
{ config: cfg, env },
131+
);
132+
const actualDoctorRegistry = await vi.importActual<
133+
typeof import("../../commands/doctor-plugin-registry.js")
134+
>("../../commands/doctor-plugin-registry.js");
135+
mocks.maybeRepairStaleManagedNpmBundledPlugins.mockImplementation(
136+
actualDoctorRegistry.maybeRepairStaleManagedNpmBundledPlugins,
137+
);
138+
let installAttempts = 0;
139+
mocks.repairMissingConfiguredPluginInstalls.mockImplementation(async (params) => {
140+
const records =
141+
params.baselineRecords ??
142+
(await loadInstalledPluginIndexInstallRecords({ env: params.env }));
143+
const codexRecord = records.codex;
144+
if (codexRecord) {
145+
installAttempts += 1;
146+
const retryRoot = resolvePluginNpmGenerationProjectDir({
147+
npmDir: path.join(stateDir, "npm"),
148+
packageName: "@openclaw/codex",
149+
generationKey: `@openclaw/codex@retry-${installAttempts}`,
150+
});
151+
const retryPackageDir = path.join(retryRoot, "node_modules", "@openclaw", "codex");
152+
fs.mkdirSync(retryPackageDir, { recursive: true });
153+
const nextRecords = {
154+
...records,
155+
codex: { ...codexRecord, installPath: retryPackageDir },
156+
};
157+
await writePersistedInstalledPluginIndexInstallRecords(nextRecords, {
158+
config: cfg,
159+
env: params.env,
160+
});
161+
return {
162+
changes: ['Refreshed stale configured plugin "codex" from @openclaw/codex@beta.'],
163+
warnings: [],
164+
records: nextRecords,
165+
};
166+
}
167+
if (params.baselineRecords) {
168+
await writePersistedInstalledPluginIndexInstallRecords(records, {
169+
config: cfg,
170+
env: params.env,
171+
});
172+
}
173+
return { changes: [], warnings: [], records };
174+
});
175+
176+
const first = await runPostCorePluginConvergence({ cfg, env });
177+
const projectsAfterFirst = fs.readdirSync(path.join(stateDir, "npm", "projects"));
178+
const second = await runPostCorePluginConvergence({ cfg, env });
179+
180+
expect(fs.existsSync(packageDir)).toBe(false);
181+
expect(installAttempts).toBe(0);
182+
expect(projectsAfterFirst).toHaveLength(1);
183+
expect(fs.readdirSync(path.join(stateDir, "npm", "projects"))).toEqual(projectsAfterFirst);
184+
expect(await readPersistedInstalledPluginIndexInstallRecords({ env })).not.toHaveProperty(
185+
"codex",
186+
);
187+
expect(first.installRecords).not.toHaveProperty("codex");
188+
expect(second.installRecords).not.toHaveProperty("codex");
189+
expect(first.changes).toContain(
190+
'Removed stale managed install record for bundled plugin "codex".',
191+
);
192+
expect(second.changes).toEqual([]);
193+
});
194+
});

‎src/cli/update-cli/post-core-plugin-convergence.test.ts‎

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ vi.mock("./plugin-payload-validation.js", () => ({
4040

4141
import type { OpenClawConfig } from "../../config/types.openclaw.js";
4242
import type { PluginInstallRecord } from "../../config/types.plugins.js";
43-
import { resolvePluginNpmGenerationProjectDir } from "../../plugins/install-paths.js";
4443
import { VERSION } from "../../version.js";
4544
import {
4645
filterRecordsToActive,
@@ -59,7 +58,7 @@ describe("runPostCorePluginConvergence", () => {
5958
mocks.listManagedPluginNpmRoots.mockImplementation((npmRoot: string) =>
6059
Promise.resolve([npmRoot]),
6160
);
62-
mocks.maybeRepairStaleManagedNpmBundledPlugins.mockReturnValue(false);
61+
mocks.maybeRepairStaleManagedNpmBundledPlugins.mockReturnValue(null);
6362
mocks.repairMissingConfiguredPluginInstalls.mockResolvedValue({
6463
changes: [],
6564
warnings: [],
@@ -341,6 +340,15 @@ describe("runPostCorePluginConvergence", () => {
341340
env: {},
342341
baselineInstallRecords: baseline,
343342
});
343+
expect(mocks.maybeRepairStaleManagedNpmBundledPlugins).toHaveBeenCalledWith({
344+
config: cfg,
345+
env: {
346+
OPENCLAW_COMPATIBILITY_HOST_VERSION: VERSION,
347+
OPENCLAW_UPDATE_POST_CORE_CONVERGENCE: "1",
348+
},
349+
installRecords: baseline,
350+
prompter: { shouldRepair: true },
351+
});
344352
expect(mocks.repairMissingConfiguredPluginInstalls).toHaveBeenCalledTimes(1);
345353
expect(mocks.repairMissingConfiguredPluginInstalls).toHaveBeenCalledWith({
346354
cfg,
@@ -406,54 +414,6 @@ describe("runPostCorePluginConvergence", () => {
406414
expect(result.installRecords).toEqual({ brave: baseline.brave });
407415
});
408416

409-
it("retires a stale managed generation when its official plugin is now bundled", async () => {
410-
const stateDir = tempDirs.make("openclaw-post-core-convergence-");
411-
const bundledRoot = tempDirs.make("openclaw-post-core-bundled-");
412-
writeBundledPlugin(bundledRoot, "codex", VERSION);
413-
const npmRoot = resolvePluginNpmGenerationProjectDir({
414-
npmDir: path.join(stateDir, "npm"),
415-
packageName: "@openclaw/codex",
416-
generationKey: "@openclaw/[email protected]",
417-
});
418-
const packageDir = path.join(npmRoot, "node_modules", "@openclaw", "codex");
419-
fs.mkdirSync(packageDir, { recursive: true });
420-
fs.writeFileSync(
421-
path.join(npmRoot, "package.json"),
422-
JSON.stringify({ dependencies: { "@openclaw/codex": "2026.7.2-beta.7" } }),
423-
"utf8",
424-
);
425-
fs.writeFileSync(
426-
path.join(packageDir, "package.json"),
427-
JSON.stringify({ name: "@openclaw/codex", version: "2026.7.2-beta.7" }),
428-
"utf8",
429-
);
430-
fs.writeFileSync(
431-
path.join(packageDir, "openclaw.plugin.json"),
432-
JSON.stringify({ id: "codex", name: "codex", configSchema: { type: "object" } }),
433-
"utf8",
434-
);
435-
const actualDoctorRegistry = await vi.importActual<
436-
typeof import("../../commands/doctor-plugin-registry.js")
437-
>("../../commands/doctor-plugin-registry.js");
438-
mocks.maybeRepairStaleManagedNpmBundledPlugins.mockImplementation(
439-
actualDoctorRegistry.maybeRepairStaleManagedNpmBundledPlugins,
440-
);
441-
442-
await runPostCorePluginConvergence({
443-
cfg: {
444-
plugins: { allow: ["codex"], entries: { codex: { enabled: true } } },
445-
},
446-
env: {
447-
OPENCLAW_STATE_DIR: stateDir,
448-
OPENCLAW_BUNDLED_PLUGINS_DIR: bundledRoot,
449-
OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR: "1",
450-
VITEST: "true",
451-
},
452-
});
453-
454-
expect(fs.existsSync(packageDir)).toBe(false);
455-
});
456-
457417
it("forwards ClawHub risk acknowledgement options to repair", async () => {
458418
const cfg = {
459419
plugins: { entries: { matrix: { enabled: true } } },

‎src/cli/update-cli/post-core-plugin-convergence.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,17 @@ export async function runPostCorePluginConvergence(params: {
173173
// became bundled with the new core must not survive into the next startup's contract graph.
174174
const { maybeRepairStaleManagedNpmBundledPlugins } =
175175
await import("../../commands/doctor-plugin-registry.js");
176-
maybeRepairStaleManagedNpmBundledPlugins({
176+
const staleManagedNpmBundledPluginRepair = maybeRepairStaleManagedNpmBundledPlugins({
177177
config: params.cfg,
178178
env,
179179
prompter: { shouldRepair: true },
180+
...(params.baselineInstallRecords ? { installRecords: params.baselineInstallRecords } : {}),
180181
});
181-
const prunedBaseline = params.baselineInstallRecords
182+
const convergenceBaseline =
183+
staleManagedNpmBundledPluginRepair?.installRecords ?? params.baselineInstallRecords;
184+
const prunedBaseline = convergenceBaseline
182185
? pruneStaleLocalBundledPluginInstallRecords({
183-
installRecords: params.baselineInstallRecords,
186+
installRecords: convergenceBaseline,
184187
env,
185188
})
186189
: null;
@@ -264,6 +267,9 @@ export async function runPostCorePluginConvergence(params: {
264267

265268
return {
266269
changes: [
270+
...(staleManagedNpmBundledPluginRepair?.removedPluginIds.map(
271+
(pluginId) => `Removed stale managed install record for bundled plugin "${pluginId}".`,
272+
) ?? []),
267273
...(prunedBaseline?.stale.map(
268274
(record) => `Removed stale local bundled plugin install record "${record.pluginId}".`,
269275
) ?? []),

0 commit comments

Comments
 (0)