Add an AgentActivate hook with agent-scoped context when switching custom agents
- Dominant language
- TypeScript
- Stars
- 193k
- Forks
- 42.4k
- PR merge metrics
- PR metrics pending
Description
## Feature request
Add a custom-agent lifecycle hook that fires when the active custom agent
changes within an existing chat session, for example `AgentActivate` or
`AgentChanged`.
## Problem
VS Code supports switching custom agents during an existing chat. A common
workflow is:
1. Use an implementation agent for several prompts.
2. Switch to a documentation agent in the same chat.
3. Use that agent for several documentation-specific prompts.
4. Switch to another agent.
An agent-scoped `SessionStart` hook does not run at step 2 because the chat
session has already started. `UserPromptSubmit` can load the documentation
context, but it reruns the hook for every prompt.
There is currently no deterministic lifecycle point meaning "this custom agent
has just become active."
## Proposed behavior
When the selected custom agent changes:
- Fire an `AgentActivate` or `AgentChanged` hook once.
- Provide fields such as:
- `session_id`
- `previous_agent`
- `active_agent`
- `activation_source` (`picker`, `handoff`, or `subagent`)
- `cwd`
- `timestamp`
- Allow the hook to return `additionalContext`.
- Cache and attach that context while the custom agent remains active.
- Remove or deactivate that context when another agent is selected.
- Fire the hook again if the custom agent is later reactivated.
## Example
When I switch from an implementation agent to a documentation agent, the
documentation agent loads project documentation rules and relevant reference
documents once. That context remains effective for the next several
documentation prompts without rerunning a command on every prompt.
When I switch away from the documentation agent, its scoped context no longer
applies.
## Why existing hooks do not cover this
- `SessionStart` runs only at the beginning of a new chat session.
- `UserPromptSubmit` runs on every prompt.
- `SubagentStart` concerns spawned subagents, not selecting another active
custom agent in the chat panel.
- Static custom-agent instructions cannot replace deterministic loading of
dynamic project context.
## Related work
This is similar in lifecycle intent to #303777, which requests an on-load hook
for skills, but this request concerns activation of custom agents within an
existing chat.
Contributor guide
Assessment
This issue has not been assessed yet.