Skip to content

Add preAgentStop hook to allow validation before the agent finishes #2253

@DanteDeRuwe

Description

@DanteDeRuwe

Describe the feature or problem you'd like to solve

There is currently no way to deterministically guard the agent's work before it returns control to the user, while still being able to act on the result of the hook.

Proposed solution

Introduce a preAgentStop hook that fires when the agent is about to stop responding:

  • Exit code 0: the agent stops as normal.
  • Non-zero exit code: the agent receives the hook's stdout/stderr as context and continues working to address the issues. The hook re-triggers when the agent tries to stop again.
  • A configurable maxRetries field (e.g., 3) prevents infinite loops.

This benefits Copilot CLI users by enabling deterministic, enforceable guardrails and quality gates without relying on the model to remember instructions (which is not deterministic), while also being able to make changes on the outcome of those checks

Example prompts or workflows

  1. Linting and formatting enforcement
  2. Type-checks and build checks
  3. Running unit tests
    ...

Additional context

  • Example hooks.json configuration:
{
  "version": 1,
  "hooks": {
    "preAgentStop": [
      {
        "type": "command",
        "bash": "npx eslint",
        "powershell": "npx eslint",
        "cwd": ".",
        "timeoutSec": 120,
        "maxRetries": 3
      }
    ]
  }
}
  • Also add preSubAgentStop
    Analogous to agentStop and subAgentStop

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions