diff --git a/src/commands/agents.bind.commands.test.ts b/src/commands/agents.bind.commands.test.ts index 456f9408138..fd377c8e8f3 100644 --- a/src/commands/agents.bind.commands.test.ts +++ b/src/commands/agents.bind.commands.test.ts @@ -95,4 +95,47 @@ describe("agents bind/unbind commands", () => { expect(runtime.error).toHaveBeenCalledWith("Bindings are owned by another agent:"); expect(runtime.exit).toHaveBeenCalledWith(1); }); + + it("keeps role-based bindings when removing channel-level discord binding", async () => { + readConfigFileSnapshotMock.mockResolvedValue({ + ...baseConfigSnapshot, + config: { + bindings: [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + roles: ["111", "222"], + }, + }, + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + }, + }, + ], + }, + }); + + await agentsUnbindCommand({ bind: ["discord:guild-a"] }, runtime); + + expect(writeConfigFileMock).toHaveBeenCalledWith( + expect.objectContaining({ + bindings: [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + roles: ["111", "222"], + }, + }, + ], + }), + ); + expect(runtime.exit).not.toHaveBeenCalled(); + }); }); diff --git a/src/commands/agents.bindings.ts b/src/commands/agents.bindings.ts index b5edce30ecd..2df9d35a436 100644 --- a/src/commands/agents.bindings.ts +++ b/src/commands/agents.bindings.ts @@ -8,6 +8,16 @@ import type { ChannelChoice } from "./onboard-types.js"; function bindingMatchKey(match: AgentBinding["match"]) { const accountId = match.accountId?.trim() || DEFAULT_ACCOUNT_ID; + const roles = Array.isArray(match.roles) + ? Array.from( + new Set( + match.roles + .map((role) => role.trim()) + .filter(Boolean) + .toSorted(), + ), + ) + : []; return [ match.channel, accountId, @@ -15,6 +25,7 @@ function bindingMatchKey(match: AgentBinding["match"]) { match.peer?.id ?? "", match.guildId ?? "", match.teamId ?? "", + roles.join(","), ].join("|"); } diff --git a/src/commands/agents.test.ts b/src/commands/agents.test.ts index 1becb77548f..48a1e23a3ee 100644 --- a/src/commands/agents.test.ts +++ b/src/commands/agents.test.ts @@ -8,6 +8,7 @@ import { applyAgentConfig, buildAgentSummaries, pruneAgentConfig, + removeAgentBindings, } from "./agents.js"; describe("agents helpers", () => { @@ -111,6 +112,86 @@ describe("agents helpers", () => { expect(result.config.bindings).toHaveLength(2); }); + it("applyAgentBindings treats role-based bindings as distinct routes", () => { + const cfg: OpenClawConfig = { + bindings: [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + roles: ["111", "222"], + }, + }, + ], + }; + + const result = applyAgentBindings(cfg, [ + { + agentId: "work", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + }, + }, + ]); + + expect(result.added).toHaveLength(1); + expect(result.conflicts).toHaveLength(0); + expect(result.config.bindings).toHaveLength(2); + }); + + it("removeAgentBindings does not remove role-based bindings when removing channel-level routes", () => { + const cfg: OpenClawConfig = { + bindings: [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + roles: ["111", "222"], + }, + }, + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + }, + }, + ], + }; + + const result = removeAgentBindings(cfg, [ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + }, + }, + ]); + + expect(result.removed).toHaveLength(1); + expect(result.conflicts).toHaveLength(0); + expect(result.config.bindings).toEqual([ + { + agentId: "main", + match: { + channel: "discord", + accountId: "guild-a", + guildId: "123", + roles: ["111", "222"], + }, + }, + ]); + }); + it("pruneAgentConfig removes agent, bindings, and allowlist entries", () => { const cfg: OpenClawConfig = { agents: {