Skip to content

Commit aa9a485

Browse files
steipetecxbAsDev
andauthored
refactor(plugins): log rejected bundle commands
Co-authored-by: 陈宪彪0668000387 <[email protected]>
1 parent ffa5bd0 commit aa9a485

2 files changed

Lines changed: 24 additions & 32 deletions

File tree

src/plugins/bundle-commands.test.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import type { PluginManifestRecord } from "./manifest-registry.js";
88

99
const mocks = vi.hoisted(() => ({
1010
plugins: [] as PluginManifestRecord[],
11+
warn: vi.fn(),
12+
}));
13+
14+
vi.mock("../logging/subsystem.js", () => ({
15+
createSubsystemLogger: () => ({ warn: mocks.warn }),
1116
}));
1217

1318
vi.mock("./manifest-registry.js", () => ({
@@ -36,6 +41,7 @@ const tempDirs: string[] = [];
3641

3742
afterEach(async () => {
3843
mocks.plugins = [];
44+
mocks.warn.mockReset();
3945
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
4046
});
4147

@@ -225,11 +231,10 @@ describe("loadEnabledClaudeBundleCommands", () => {
225231
);
226232
});
227233

228-
it("skips oversized bundle command markdown files exceeding 1 MB and continues loading siblings", async () => {
234+
it("warns and skips oversized bundle commands without dropping siblings", async () => {
229235
const homeDir = await createTempDir("openclaw-bundle-commands-oversized-");
230236
const workspaceDir = await createTempDir("openclaw-bundle-commands-oversized-ws-");
231237

232-
// Write a normal command file alongside an oversized one
233238
await writeClaudeBundleCommandFixture({
234239
homeDir,
235240
pluginId: "oversized-test",
@@ -246,38 +251,25 @@ describe("loadEnabledClaudeBundleCommands", () => {
246251
],
247252
});
248253

249-
// Create the oversized command file (> 1 MB) alongside the normal one
250254
const pluginRoot = resolveBundlePluginRoot(homeDir, "oversized-test");
251255
const oversizedFilePath = path.join(pluginRoot, "commands", "oversized.md");
252256
await fs.mkdir(path.dirname(oversizedFilePath), { recursive: true });
253257
const oversizedContent = Buffer.alloc(1 * 1024 * 1024 + 1, "x");
254258
await fs.writeFile(oversizedFilePath, oversizedContent);
255259

256-
// Suppress console.warn for this test
257-
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
258-
259-
try {
260-
const commands = loadEnabledClaudeBundleCommands({
261-
workspaceDir,
262-
cfg: {
263-
plugins: {
264-
entries: { "oversized-test": { enabled: true } },
265-
},
260+
const commands = loadEnabledClaudeBundleCommands({
261+
workspaceDir,
262+
cfg: {
263+
plugins: {
264+
entries: { "oversized-test": { enabled: true } },
266265
},
267-
});
268-
269-
// Normal command still loads
270-
const names = commands.map((entry) => entry.rawName);
271-
expect(names).toContain("normal");
272-
expect(names).not.toContain("oversized");
266+
},
267+
});
273268

274-
// console.warn was called with the oversized file path
275-
expect(warnSpy).toHaveBeenCalledWith(
276-
expect.stringContaining("[bundle-commands]"),
277-
expect.any(String),
278-
);
279-
} finally {
280-
warnSpy.mockRestore();
281-
}
269+
expect(commands.map((entry) => entry.rawName)).toEqual(["normal"]);
270+
expect(mocks.warn).toHaveBeenCalledOnce();
271+
const warning = String(mocks.warn.mock.calls[0]?.[0]);
272+
expect(warning).toContain(oversizedFilePath);
273+
expect(warning).toContain("1048576");
282274
});
283275
});

src/plugins/bundle-commands.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import {
1010
stripFrontmatterBlock,
1111
} from "../../packages/markdown-core/src/frontmatter.js";
1212
import type { OpenClawConfig } from "../config/types.openclaw.js";
13+
import { formatErrorMessage } from "../infra/errors.js";
1314
import { readRootJsonObjectSync } from "../infra/json-files.js";
1415
import { readRegularFileSync } from "../infra/regular-file.js";
16+
import { createSubsystemLogger } from "../logging/subsystem.js";
1517
import { isPathInsideWithRealpath } from "../security/scan-paths.js";
1618
import { parseFrontmatterBool } from "../shared/frontmatter.js";
1719
import {
@@ -35,6 +37,7 @@ type ClaudeBundleCommandSpec = {
3537
};
3638

3739
const BUNDLE_COMMAND_MAX_BYTES = 1 * 1024 * 1024;
40+
const log = createSubsystemLogger("plugins/bundle-commands");
3841

3942
function readClaudeBundleManifest(rootDir: string): Record<string, unknown> {
4043
const result = readRootJsonObjectSync({
@@ -110,11 +113,8 @@ function loadBundleCommandsFromRoot(params: {
110113
"utf-8",
111114
);
112115
} catch (error) {
113-
// Log oversized or unreadable command files so upgrades do not
114-
// silently remove installed commands; still skip and continue.
115-
console.warn(
116-
`[bundle-commands] skipping unreadable command file: ${filePath}`,
117-
error instanceof Error ? error.message : String(error),
116+
log.warn(
117+
`skipping unreadable bundle command file ${filePath}: ${formatErrorMessage(error)}`,
118118
);
119119
continue;
120120
}

0 commit comments

Comments
 (0)