[Bug]Custom agent appears duplicated in selection list and fails to load after restart
- Lenguaje dominante
- Shell
- Estrellas
- 11.2k
- Forks
- 1.9k
- Merge medio
- 14 h 16 min
- PR fusionados (30 d)
- 6
Descripción
### Describe the bug
## Summary
When a custom agent's **filename** differs from its **frontmatter `name` field**, two issues occur:
1. The agent appears **duplicated** in the `/agent` selection menu (one entry from the filename, one from the `name` field).
2. After session restart, the CLI fails to find the previously selected agent with error: `Custom agent "xxx" not found`.
## Environment
- **GitHub Copilot CLI version**: v1.0.16-1
- **Model**: Claude Opus 4.6 (6x) (high)
- **OS**: Ubuntu 24.04.4 LTS (WSL2) on Windows
- **Kernel**: Linux 6.6.87.2-microsoft-standard-WSL2 aarch64
- **Node.js**: v24.14.0
- **Agent directory**: `~/.copilot/agents/`
## Agent Configuration
Two custom agent files exist in `~/.copilot/agents/`:
| File | Frontmatter `name` |
| ------------------ | ----------------------------- |
| `azure-expert.md` | `azure-solution-expert` |
| `azure-aws-doc.md` | `azure-vs-aws-compare-expert` |
Note: the **filename** and the **`name` field in frontmatter** are intentionally different.
## Steps to Reproduce
### Bug 1: Duplicate agent entry
1. Create a custom agent file at `~/.copilot/agents/azure-expert.md` with frontmatter `name: azure-solution-expert`
2. Start `ghcp`
3. Run `/agent` to open the agent selection menu
**Expected**: The agent should appear **once** in the list.
**Actual**: The agent appears **twice**:
```
Select Agent:
1. Default (deselect current agent)
> 2. azure-solution-expert (current)
3. azure-solution-expert · user <-- duplicate
4. azure-vs-aws-compare-expert · user
```
### Bug 2: Agent not found after restart
1. Select the custom agent `azure-solution-expert` via `/agent`
2. Restart the CLI session (close and reopen, or the session restarts automatically)
**Expected**: The agent should be loaded successfully on restart.
**Actual**: Error message on startup:
```
✗ Custom agent "azure-solution-expert" not found. Available agents: azure-aws-doc, azure-expert
● Environment loaded: 5 MCP servers, 1 skill, 2 agents
● Selected custom agent: azure-solution-expert
```
The "Available agents" list uses **filenames** (`azure-aws-doc`, `azure-expert`), but the persisted selection uses the **frontmatter `name`** (`azure-solution-expert`). The lookup fails because these don't match.
## Root Cause Analysis
The CLI appears to use **two different identifiers** for agents inconsistently:
- **Agent selection/persistence**: Uses the `name` field from the agent file's YAML frontmatter
- **Agent lookup on restart**: Uses the filename (without `.md` extension)
When filename ≠ frontmatter name, the agent:
- Gets registered under both identifiers → **duplicate in list**
- Gets persisted by frontmatter name but looked up by filename on restart → **not found error**
## Expected Behavior
1. The agent should appear **exactly once** in the selection list regardless of whether the filename matches the frontmatter `name` field.
2. The CLI should use a **consistent identifier** (either filename or frontmatter `name`, not both) for selection, persistence, and lookup.
3. If the frontmatter `name` takes priority over the filename, the persisted selection should resolve correctly on restart.
## Screenshot

## Workaround
Rename the agent file to match the frontmatter `name` field:
```bash
mv ~/.copilot/agents/azure-expert.md ~/.copilot/agents/azure-solution-expert.md
mv ~/.copilot/agents/azure-aws-doc.md ~/.copilot/agents/azure-vs-aws-compare-expert.md
```
## Labels
`bug`, `custom-agents`, `cli`
### Affected version
_No response_
### Steps to reproduce the behavior
_No response_
### Expected behavior
_No response_
### Additional context
_No response_
Guía de contribución
Línea de trabajo
Empieza reproduciendo el problema con un agente personalizado cuyo nombre de archivo difiera del nombre de su frontmatter; después, sigue el descubrimiento de agentes personalizados, la persistencia de la selección y la búsqueda tras el reinicio. Se considera terminado cuando el agente aparece una sola vez en /agent y el mismo agente seleccionado se carga correctamente después de reiniciar el CLI, sin ningún error de not-found.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- shell
- Área
- cli
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 55/100