google-gemini / google-gemini/gemini-cli

bug: hooks.AfterAgent is never executed in settings.json configuration

Open
#27,712 2 comments 1 reaction 0 assignees View on GitHub
area/agent kind/bug priority/p2 status/bot-triaged
Dominant language
TypeScript
Stars
107k
Forks
14.6k
Avg merge
2d 3h
Merged PRs (30d)
45

Description

### What happened?

# Bug Report: `AfterAgent` Hook is Never Triggered by the CLI

## Description
The `@google/gemini-cli` configuration specification (`settings.json`) supports registering lifecycle hooks under the `"hooks"` key, including `BeforeAgent`, `AfterTool`, and `AfterAgent`. While `BeforeAgent` and `AfterTool` execute successfully during their respective lifecycle phases, scripts registered under `AfterAgent` are never executed at the end of the agent's turn.

---

## Environment
* **CLI Version:** `@google/gemini-cli` version `0.45.0` (and related versions)
* **OS:** Windows / Linux / macOS
* **Shell:** PowerShell / bash

---

## Steps to Reproduce
1. In the `.gemini/settings.json` configuration file, register a dummy hook script under `AfterAgent`:
```json
{
"hooks": {
"AfterAgent": [
{
"matcher": "*",
"hooks": [
{
"name": "test-after-agent",
"type": "command",
"command": "node -e \"const fs = require('fs'); fs.writeFileSync('after-agent-triggered.txt', 'Triggered!');\"",
"timeout": 5000
}
]
}
]
}
}
```
2. Start a conversation using the Gemini CLI.
3. Send any prompt and wait for the model to output its complete response (`PLANNER_RESPONSE`).
4. Check if the output file `after-agent-triggered.txt` was created in the workspace.

### Expected Behavior
The CLI should execute the registered command after the model outputs its final response, creating the `after-agent-triggered.txt` file in the workspace directory.

### Actual Behavior
The script is never executed, and no file is created. The execution lifecycle finishes without invoking the `AfterAgent` hooks.

---

## Suggested Fix
In the CLI's internal execution pipeline:
1. Locate the stage where the agent finishes generating its final response (`PLANNER_RESPONSE`).
2. Trigger the registered hooks in `hooks.AfterAgent` (if present in `settings.json`), passing the final agent text response or full payload to the subprocess's `stdin` (analogous to how `BeforeAgent` passes data).

### What did you expect to happen?

The CLI should execute the registered command or script specified under `hooks.AfterAgent` in settings.json immediately after the model outputs its
final response (PLANNER_RESPONSE), passing the response payload to the subprocess via stdin (analogous to how BeforeAgent behaves).

### Client information

CLI Version: @google/gemini-cli@0.45.0
Platform: Windows (PowerShell)

### Login information

Google Account

### Anything else we need to know?

This missing hook execution severely impacts integrations and educational frameworks that rely on real-time feedback loop synchronization (such as syncing agent actions and session logs back to a local web dashboard right after a turn finishes).

As a temporary workaround, we had to implement sync logic inside the `BeforeAgent` hook of the *next* user turn to retroactively parse the model's last response from the local chat transcript logs. Triggering `AfterAgent` natively at the end of the turn would eliminate this overhead, latency, and extra file-parsing logic.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.