Description
When a user does not have access to all configured Agents (i.e., some Agents are set to "Allow for selected users" or a user is excluded by "Block selected users"), the Agent Pane does not have an icon and the Agents menu within it is completely empty even for Agents the user does have access to. Behind the scene, you will get HTTP Error 500 for the Mattermost Plugin [AI] Agents and if you enable Debug Logging you will get the error runtime error: index out of range [N] with length N.
The root cause is in api/api.go: the loop iterating over all bots uses the index variable i (from the full allBots slice) to swap the default bot to position 0. However, bots the user cannot access are skipped via continue, so the index i no longer corresponds to a valid position in the filtered bots slice. This causes a runtime panic: index out of range.
Expected behavior: Users with access to a subset of Agents should see only those Agents in the Agent Pane, with the correct icon and a working Agents menu.
Patch: Change the loop variable from for i, bot to for _, bot, and fix the swap to use bots[len(bots)-1] instead of bots[i]:
--- a/api/api.go
+++ b/api/api.go
@@ -311,7 +311,7 @@
// Put the default bot first.
bots := make([]AIBotInfo, 0, len(allBots))
defaultBotName := a.config.GetDefaultBotName()
- for i, bot := range allBots {
+ for _, bot := range allBots {
// Don't return bots the user is excluded from using.
if a.bots.CheckUsageRestrictionsForUser(bot, userID) != nil {
continue
@@ -338,7 +338,7 @@
UserIDs: bot.GetConfig().UserIDs,
})
if bot.GetMMBot().Username == defaultBotName {
- bots[0], bots[i] = bots[i], bots[0]
+ bots[0], bots[len(bots)-1] = bots[len(bots)-1], bots[0]
}
}
Steps to reproduce
- Create a new Agent and immediately deny yourself access to it via the "Block selected users" restriction.
- Reload Mattermost β the Agent Pane icon will be broken (missing due to 500 error and plugin crash) and the Agent menu within Agent Pane will be empty.
Alternative reproduction:
- Configure multiple Agents in the Mattermost Agents plugin, with one or more set to "Allow for selected users".
- Create UserA with access to all Agents and confirm the Agent Pane works correctly.
- Create UserB with access to only some Agents (deny access to at least one Agent by using "Allow for selected users" or "Block selected users").
- Log in as UserB.
- Observe that the Agent Pane icon is missing and the Agents menu is empty, even for Agents UserB has access to.
Description
When a user does not have access to all configured Agents (i.e., some Agents are set to "Allow for selected users" or a user is excluded by "Block selected users"), the Agent Pane does not have an icon and the Agents menu within it is completely empty even for Agents the user does have access to. Behind the scene, you will get HTTP Error 500 for the Mattermost Plugin [AI] Agents and if you enable Debug Logging you will get the error runtime error: index out of range [N] with length N.
The root cause is in
api/api.go: the loop iterating over all bots uses the index variablei(from the fullallBotsslice) to swap the default bot to position 0. However, bots the user cannot access are skipped viacontinue, so the indexino longer corresponds to a valid position in the filteredbotsslice. This causes a runtime panic: index out of range.Expected behavior: Users with access to a subset of Agents should see only those Agents in the Agent Pane, with the correct icon and a working Agents menu.
Patch: Change the loop variable from
for i, bottofor _, bot, and fix the swap to usebots[len(bots)-1]instead ofbots[i]:Steps to reproduce
Alternative reproduction: