fix(commands): deduplicate provider slash commands by name

Antigravity ACP emits both its built-in slash commands and discovered skills in one available_commands_update without deduplicating colliding names (such as a local plan skill alongside built-in /plan). Both entries flowed through plugin ACP, plugin-provider, and mergeSlashCommandSources into Autocomplete's FlatList, triggering React's duplicate key error on .$plan.

Enforce unique command names (first-wins) at the plugin ACP, plugin provider, core ACP, and Codex boundaries so every daemon consumer gets a valid catalog, and claim provider command names in mergeSlashCommandSources and inline skill filtering so the app stays safe against older daemons.
This commit is contained in:
Mohamed Boudra
2026-11-20 07:05:22 +01:00
parent 7b9c6a7d4a
commit 91fac29fae
12 changed files with 242 additions and 19 deletions
@@ -28,6 +28,24 @@ describe("plugin client slash commands", () => {
expect(collision).toHaveBeenCalledTimes(3);
});
it("deduplicates duplicate provider command names, keeping the first occurrence", () => {
const result = mergeSlashCommandSources({
builtIn: [],
plugins: [],
provider: [
{ name: "plan", description: "Built-in plan" },
{ name: "plan", description: "Skill plan" },
{ name: "review", description: "Review changes" },
],
onPluginCollision: vi.fn(),
});
expect(result).toEqual([
{ source: "provider", command: { name: "plan", description: "Built-in plan" } },
{ source: "provider", command: { name: "review", description: "Review changes" } },
]);
});
it("passes the trimmed raw remainder as args", () => {
const command = { name: "review" };
expect(
@@ -39,8 +39,11 @@ export function mergeSlashCommandSources<
claimed.set(command.name, "plugin");
commands.push({ source: "plugin", command });
}
const seenProvider = new Set<string>();
for (const command of input.provider) {
if (!claimed.has(command.name)) commands.push({ source: "provider", command });
if (claimed.has(command.name) || seenProvider.has(command.name)) continue;
seenProvider.add(command.name);
commands.push({ source: "provider", command });
}
return commands;
}
@@ -184,4 +184,14 @@ describe("filterInlineSkillCommandEntries", () => {
"taste",
]);
});
it("deduplicates duplicate inline skill names, keeping the first occurrence", () => {
const entries = [
{ source: "provider" as const, command: { name: "plan", kind: "skill", tag: "first" } },
{ source: "provider" as const, command: { name: "plan", kind: "skill", tag: "second" } },
{ source: "provider" as const, command: { name: "taste", kind: "skill", tag: "only" } },
];
expect(filterInlineSkillCommandEntries(entries)).toEqual([entries[0], entries[2]]);
});
});
@@ -91,7 +91,13 @@ export function filterAndRankCommandAutocompleteEntries<TEntry extends CommandAu
export function filterInlineSkillCommandEntries<TEntry extends CommandAutocompleteEntry>(
entries: readonly TEntry[],
): TEntry[] {
return entries.filter((entry) => entry.source === "provider" && entry.command.kind === "skill");
const seen = new Set<string>();
return entries.filter((entry) => {
if (entry.source !== "provider" || entry.command.kind !== "skill") return false;
if (seen.has(entry.command.name)) return false;
seen.add(entry.command.name);
return true;
});
}
const INVALID_SLASH_COMMAND_QUERY_CHARS = /[/\s\n\r\t"']/;
@@ -1215,11 +1215,13 @@ class AcpRuntime {
return;
}
if (update.sessionUpdate === "available_commands_update") {
const commands = update.availableCommands.map((command) => ({
name: command.name,
description: command.description,
argumentHint: command.input?.hint,
}));
const commands = dedupeByName(
update.availableCommands.map((command) => ({
name: command.name,
description: command.description,
argumentHint: command.input?.hint,
})),
);
this.emit({ type: "session.commands", sessionId: this.options.boundarySessionId, commands });
this.receivedInitialCommands = true;
this.commandWaiter?.();
@@ -1332,7 +1334,7 @@ class AcpRuntime {
this.emit({
type: "session.commands",
sessionId: this.options.boundarySessionId,
commands: [...update.commands],
commands: dedupeByName(update.commands),
});
} else if (update.type === "config") {
if (this.configTransaction) this.stagedTransformerConfig = update.config;
@@ -1550,6 +1552,17 @@ function toAcpMcpServers(config: ProviderSessionConfig) {
});
}
function dedupeByName<T extends { name: string }>(items: readonly T[]): T[] {
const seen = new Set<string>();
const unique: T[] = [];
for (const item of items) {
if (seen.has(item.name)) continue;
seen.add(item.name);
unique.push(item);
}
return unique;
}
function toAcpPrompt(
prompt: Extract<ProviderInput, { type: "session.prompt" }>["prompt"],
): ContentBlock[] {
+48
View File
@@ -2452,3 +2452,51 @@ it("rewrites the session-ready, notification and configure-commit config snapsho
await connection.close();
}
});
it("deduplicates available_commands_update entries by name, keeping the first occurrence", async () => {
const sessionId = "connector-session";
const { connector } = connectorHarness({
handleRequest(runtime, request) {
if (request.method !== "session/new") return false;
runtime.respond(request, { sessionId, configOptions: [] });
setTimeout(() => {
runtime.notify("session/update", {
sessionId,
update: {
sessionUpdate: "available_commands_update",
availableCommands: [
{ name: "goal", description: "Run until goal is finished" },
{ name: "plan", description: "Plan carefully before executing a task." },
{ name: "plan", description: "Local plan skill description" },
{ name: "review", description: "Review changes" },
],
},
});
}, 10);
return true;
},
});
const connection = await runAcpProvider({
id: "dup-commands",
label: "Dup Commands",
connector,
acpOptions: { waitForInitialCommands: true },
}).connect({ versions: [1], capabilities: ["prompt.message"] });
const events: ProviderEvent[] = [];
connection.onEvent((event) => events.push(event));
try {
await connection.send(openInput());
const commandsEvent = await waitForEvent(
events,
(event): event is Extract<ProviderEvent, { type: "session.commands" }> =>
event.type === "session.commands",
);
expect(commandsEvent.commands).toEqual([
{ name: "goal", description: "Run until goal is finished" },
{ name: "plan", description: "Plan carefully before executing a task." },
{ name: "review", description: "Review changes" },
]);
} finally {
await connection.close();
}
});
@@ -2814,3 +2814,47 @@ test("resumes a goose session saved by the generic ACP provider", async () => {
await plugins.shutdown();
}
});
test("deduplicates duplicate command names emitted by a plugin provider", async () => {
const harness = createProviderHarness({
async handleInput(input, emit) {
if (input.type !== "session.open") return false;
emit({
type: "session.opened",
requestId: input.requestId,
sessionId: input.sessionId,
capabilities: CAPABILITIES,
restoration: "core",
persistence: { version: 1, data: { token: "root" } },
cwd: input.config.cwd,
});
emit({
type: "session.commands",
sessionId: input.sessionId,
commands: [
{ name: "plan", description: "Built-in plan" },
{ name: "plan", description: "Skill plan" },
{ name: "review", description: "Review code" },
],
});
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
return true;
},
});
const registry = new PluginAgentClientRegistry(createTestLogger());
registry.replace([harness.registration]);
const client = registry.clients()[harness.registration.id]!;
try {
const session = await client.createSession({ provider: harness.registration.id, cwd: "/tmp" });
try {
await expect(session.listCommands?.()).resolves.toEqual([
{ name: "plan", description: "Built-in plan", argumentHint: "", kind: "command" },
{ name: "review", description: "Review code", argumentHint: "", kind: "command" },
]);
} finally {
await session.close();
}
} finally {
await registry.shutdown();
}
});
@@ -897,7 +897,7 @@ class ProviderRuntimeSession {
else this.activeTurnIds.delete(event.turnId);
}
if (event.type === "session.config") this.config = event.config;
if (event.type === "session.commands") this.commands = [...event.commands];
if (event.type === "session.commands") this.commands = dedupeCommandsByName(event.commands);
if (event.type === "session.persistence" && this.restoration === "core") {
this.persistence = event.persistence;
}
@@ -2073,6 +2073,17 @@ function combineSystemPrompts(
return parts.length > 0 ? parts.join("\n\n") : undefined;
}
function dedupeCommandsByName<T extends { name: string }>(commands: readonly T[]): T[] {
const seen = new Set<string>();
const unique: T[] = [];
for (const command of commands) {
if (seen.has(command.name)) continue;
seen.add(command.name);
unique.push(command);
}
return unique;
}
function mapPromptInput(
prompt: AgentPromptInput,
commands: readonly { name: string }[],
@@ -3016,6 +3016,44 @@ describe("ACPAgentSession slash commands", () => {
]);
});
test("deduplicates available_commands_update entries by name, keeping the first occurrence", async () => {
const session = new ACPAgentSession(
{
provider: "claude-acp",
cwd: "/tmp/paseo-acp-test",
},
{
provider: "claude-acp",
logger: createTestLogger(),
defaultCommand: ["claude", "--acp"],
defaultModes: [],
capabilities: {
supportsStreaming: true,
supportsSessionPersistence: true,
supportsDynamicModes: true,
supportsMcpServers: true,
supportsReasoningStream: true,
supportsToolInvocations: true,
},
waitForInitialCommands: false,
},
);
asInternals<ACPSessionInternals>(session).translateSessionUpdate({
sessionUpdate: "available_commands_update",
availableCommands: [
{ name: "plan", description: "Built-in plan" },
{ name: "plan", description: "Skill plan" },
{ name: "review", description: "Review changes" },
],
});
await expect(session.listCommands()).resolves.toEqual([
{ name: "plan", description: "Built-in plan", argumentHint: "", kind: "command" },
{ name: "review", description: "Review changes", argumentHint: "", kind: "command" },
]);
});
test("lists late command batches through the public ACP client", async () => {
const artifacts = path.resolve(".artifacts");
await mkdir(artifacts, { recursive: true });
@@ -2988,7 +2988,7 @@ export class ACPAgentSession implements AgentSession, ACPClient {
if (commands.length > 0) {
this.commandsUpdatedAt = Date.now();
this.cachedCommands = commands;
this.cachedCommands = dedupeSlashCommands(commands);
// A non-empty batch may be followed by more. Let the quiet window decide
// when the inventory is complete rather than settling here; an empty
// batch is the provider saying it has nothing, which must still unblock
@@ -3379,12 +3379,14 @@ export class ACPAgentSession implements AgentSession, ACPClient {
return pendingUserEvents;
case "available_commands_update":
this.commandsUpdatedAt = Date.now();
this.cachedCommands = update.availableCommands.map((command) => ({
name: command.name,
description: command.description,
argumentHint: command.input?.hint ?? "",
kind: "command",
}));
this.cachedCommands = dedupeSlashCommands(
update.availableCommands.map((command) => ({
name: command.name,
description: command.description,
argumentHint: command.input?.hint ?? "",
kind: "command",
})),
);
// An empty batch means the agent has no commands and would otherwise
// wait out the whole quiet window. A non-empty batch may be one of
// several, so completion belongs to the quiet window.
@@ -4482,3 +4484,14 @@ async function terminateChildProcess(
child.stderr.destroy();
}
}
function dedupeSlashCommands(commands: readonly AgentSlashCommand[]): AgentSlashCommand[] {
const seen = new Set<string>();
const unique: AgentSlashCommand[] = [];
for (const command of commands) {
if (seen.has(command.name)) continue;
seen.add(command.name);
unique.push(command);
}
return unique;
}
@@ -3795,6 +3795,21 @@ describe("Codex app-server provider", () => {
);
});
test("deduplicates Codex skills that collide with built-in slash commands", async () => {
const commands = await listCommandsFromFakeCodex([
{
name: "compact",
description: "Custom compact skill.",
path: "/tmp/skills/compact/SKILL.md",
enabled: true,
},
]);
const compactCommands = commands.filter((command) => command.name === "compact");
expect(compactCommands).toHaveLength(1);
expect(compactCommands[0]?.kind).toBe("command");
});
test("maps image prompt blocks to Codex localImage input", async () => {
const input = await codexAppServerTurnInputFromPrompt(
[
@@ -5824,9 +5824,13 @@ export class CodexAppServerAgentSession implements AgentSession {
kind: "command",
});
}
return [...builtin, ...appServerSkills, ...fallbackSkills, ...prompts].sort((a, b) =>
a.name.localeCompare(b.name),
);
const commandsByName = new Map<string, AgentSlashCommand>();
for (const command of [...builtin, ...appServerSkills, ...fallbackSkills, ...prompts]) {
if (!commandsByName.has(command.name)) {
commandsByName.set(command.name, command);
}
}
return Array.from(commandsByName.values()).sort((a, b) => a.name.localeCompare(b.name));
}
tryHandleOutOfBand(