mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-26 16:06:16 +00:00
fix(skills): scope skill-command APIs to respect agent allowlists (#32155)
* refactor(skills): use explicit skill-command scope APIs * test(skills): cover scoped listing and telegram allowlist * fix(skills): add mergeSkillFilters edge-case tests and simplify dead code Cover unrestricted-co-tenant and empty-allowlist merge paths in skill-commands tests. Remove dead ternary in bot-handlers pagination. Add clarifying comments on undefined vs [] filter semantics. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor(skills): collapse scope functions into single listSkillCommandsForAgents Replace listSkillCommandsForAgentIds, listSkillCommandsForAllAgents, and the deprecated listSkillCommandsForAgents with a single function that accepts optional agentIds and falls back to all agents when omitted. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(skills): harden realpathSync race and add missing test coverage - Wrap fs.realpathSync in try-catch to gracefully skip workspaces that disappear between existsSync and realpathSync (TOCTOU race). - Log verbose diagnostics for missing/unresolvable workspace paths. - Add test for overlapping allowlists deduplication on shared workspaces. - Add test for graceful skip of missing workspaces. - Add test for pagination callback without agent suffix (default agent). - Clean up temp directories in skill-commands tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(telegram): warn when nativeSkillsEnabled but no agent route is bound Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: use runtime.log instead of nonexistent runtime.warn Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1142,10 +1142,10 @@ export const registerTelegramHandlers = ({
|
||||
return;
|
||||
}
|
||||
|
||||
const agentId = paginationMatch[2]?.trim() || resolveDefaultAgentId(cfg) || undefined;
|
||||
const agentId = paginationMatch[2]?.trim() || resolveDefaultAgentId(cfg);
|
||||
const skillCommands = listSkillCommandsForAgents({
|
||||
cfg,
|
||||
agentIds: agentId ? [agentId] : undefined,
|
||||
agentIds: [agentId],
|
||||
});
|
||||
const result = buildCommandsMessagePaginated(cfg, skillCommands, {
|
||||
page,
|
||||
|
||||
105
src/telegram/bot-native-commands.skills-allowlist.test.ts
Normal file
105
src/telegram/bot-native-commands.skills-allowlist.test.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { writeSkill } from "../agents/skills.e2e-test-helpers.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import type { TelegramAccountConfig } from "../config/types.js";
|
||||
import { registerTelegramNativeCommands } from "./bot-native-commands.js";
|
||||
import { createNativeCommandTestParams } from "./bot-native-commands.test-helpers.js";
|
||||
|
||||
const pluginCommandMocks = vi.hoisted(() => ({
|
||||
getPluginCommandSpecs: vi.fn(() => []),
|
||||
matchPluginCommand: vi.fn(() => null),
|
||||
executePluginCommand: vi.fn(async () => ({ text: "ok" })),
|
||||
}));
|
||||
const deliveryMocks = vi.hoisted(() => ({
|
||||
deliverReplies: vi.fn(async () => ({ delivered: true })),
|
||||
}));
|
||||
|
||||
vi.mock("../plugins/commands.js", () => ({
|
||||
getPluginCommandSpecs: pluginCommandMocks.getPluginCommandSpecs,
|
||||
matchPluginCommand: pluginCommandMocks.matchPluginCommand,
|
||||
executePluginCommand: pluginCommandMocks.executePluginCommand,
|
||||
}));
|
||||
vi.mock("./bot/delivery.js", () => ({
|
||||
deliverReplies: deliveryMocks.deliverReplies,
|
||||
}));
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
async function makeWorkspace(prefix: string) {
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||
tempDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
describe("registerTelegramNativeCommands skill allowlist integration", () => {
|
||||
afterEach(async () => {
|
||||
pluginCommandMocks.getPluginCommandSpecs.mockClear().mockReturnValue([]);
|
||||
pluginCommandMocks.matchPluginCommand.mockClear().mockReturnValue(null);
|
||||
pluginCommandMocks.executePluginCommand.mockClear().mockResolvedValue({ text: "ok" });
|
||||
deliveryMocks.deliverReplies.mockClear().mockResolvedValue({ delivered: true });
|
||||
await Promise.all(
|
||||
tempDirs
|
||||
.splice(0, tempDirs.length)
|
||||
.map((dir) => fs.rm(dir, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
|
||||
it("registers only allowlisted skills for the bound agent menu", async () => {
|
||||
const workspaceDir = await makeWorkspace("openclaw-telegram-skills-");
|
||||
await writeSkill({
|
||||
dir: path.join(workspaceDir, "skills", "alpha-skill"),
|
||||
name: "alpha-skill",
|
||||
description: "Alpha skill",
|
||||
});
|
||||
await writeSkill({
|
||||
dir: path.join(workspaceDir, "skills", "beta-skill"),
|
||||
name: "beta-skill",
|
||||
description: "Beta skill",
|
||||
});
|
||||
|
||||
const setMyCommands = vi.fn().mockResolvedValue(undefined);
|
||||
const cfg: OpenClawConfig = {
|
||||
agents: {
|
||||
list: [
|
||||
{ id: "alpha", workspace: workspaceDir, skills: ["alpha-skill"] },
|
||||
{ id: "beta", workspace: workspaceDir, skills: ["beta-skill"] },
|
||||
],
|
||||
},
|
||||
bindings: [
|
||||
{
|
||||
agentId: "alpha",
|
||||
match: { channel: "telegram", accountId: "bot-a" },
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
registerTelegramNativeCommands({
|
||||
...createNativeCommandTestParams({
|
||||
bot: {
|
||||
api: {
|
||||
setMyCommands,
|
||||
sendMessage: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
command: vi.fn(),
|
||||
} as unknown as Parameters<typeof registerTelegramNativeCommands>[0]["bot"],
|
||||
cfg,
|
||||
accountId: "bot-a",
|
||||
telegramCfg: {} as TelegramAccountConfig,
|
||||
}),
|
||||
});
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(setMyCommands).toHaveBeenCalled();
|
||||
});
|
||||
const registeredCommands = setMyCommands.mock.calls[0]?.[0] as Array<{
|
||||
command: string;
|
||||
description: string;
|
||||
}>;
|
||||
|
||||
expect(registeredCommands.some((entry) => entry.command === "alpha_skill")).toBe(true);
|
||||
expect(registeredCommands.some((entry) => entry.command === "beta_skill")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -324,10 +324,14 @@ export const registerTelegramNativeCommands = ({
|
||||
nativeEnabled && nativeSkillsEnabled
|
||||
? resolveAgentRoute({ cfg, channel: "telegram", accountId })
|
||||
: null;
|
||||
const boundAgentIds = boundRoute ? [boundRoute.agentId] : null;
|
||||
if (nativeEnabled && nativeSkillsEnabled && !boundRoute) {
|
||||
runtime.log?.(
|
||||
"nativeSkillsEnabled is true but no agent route is bound for this Telegram account; skill commands will not appear in the native menu.",
|
||||
);
|
||||
}
|
||||
const skillCommands =
|
||||
nativeEnabled && nativeSkillsEnabled
|
||||
? listSkillCommandsForAgents(boundAgentIds ? { cfg, agentIds: boundAgentIds } : { cfg })
|
||||
nativeEnabled && nativeSkillsEnabled && boundRoute
|
||||
? listSkillCommandsForAgents({ cfg, agentIds: [boundRoute.agentId] })
|
||||
: [];
|
||||
const nativeCommands = nativeEnabled
|
||||
? listNativeCommandSpecsForConfig(cfg, {
|
||||
|
||||
@@ -294,6 +294,38 @@ describe("createTelegramBot", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to default agent for pagination callbacks without agent suffix", async () => {
|
||||
onSpy.mockClear();
|
||||
listSkillCommandsForAgents.mockClear();
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
const callbackHandler = onSpy.mock.calls.find((call) => call[0] === "callback_query")?.[1] as (
|
||||
ctx: Record<string, unknown>,
|
||||
) => Promise<void>;
|
||||
expect(callbackHandler).toBeDefined();
|
||||
|
||||
await callbackHandler({
|
||||
callbackQuery: {
|
||||
id: "cbq-no-suffix",
|
||||
data: "commands_page_2",
|
||||
from: { id: 9, first_name: "Ada", username: "ada_bot" },
|
||||
message: {
|
||||
chat: { id: 1234, type: "private" },
|
||||
date: 1736380800,
|
||||
message_id: 14,
|
||||
},
|
||||
},
|
||||
me: { username: "openclaw_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(listSkillCommandsForAgents).toHaveBeenCalledWith({
|
||||
cfg: expect.any(Object),
|
||||
agentIds: ["main"],
|
||||
});
|
||||
expect(editMessageTextSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("blocks pagination callbacks when allowlist rejects sender", async () => {
|
||||
onSpy.mockClear();
|
||||
editMessageTextSpy.mockClear();
|
||||
|
||||
Reference in New Issue
Block a user