feat: add stopFailure and postCompact (#2825)

This commit is contained in:
DennisYu07 2026-04-13 12:54:44 +08:00 committed by GitHub
parent 732cee2604
commit dddb56d885
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 1484 additions and 9 deletions

View file

@ -57,9 +57,11 @@ The following table lists all available hook events in Qwen Code:
| `UserPromptSubmit` | Fired when user submits a prompt | Input processing, validation, context injection |
| `SessionStart` | Fired when a new session starts | Initialization, context setup |
| `Stop` | Fired before Qwen concludes its response | Finalization, cleanup |
| `StopFailure` | Fired when turn ends due to API error | Error logging, alerting, rate limit handling |
| `SubagentStart` | Fired when a subagent starts | Subagent initialization |
| `SubagentStop` | Fired when a subagent stops | Subagent finalization |
| `PreCompact` | Fired before conversation compaction | Pre-compaction processing |
| `PostCompact` | Fired after conversation compaction | Summary archiving, usage statistics |
| `SessionEnd` | Fired when a session ends | Cleanup, reporting |
| `PermissionRequest` | Fired when permission dialogs are displayed | Permission automation, policy enforcement |
@ -299,6 +301,60 @@ Event-specific fields are added based on the hook type. Below are the event-spec
}
```
#### StopFailure
**Purpose**: Executed when the turn ends due to an API error (instead of Stop). This is a **fire-and-forget** event - hook output and exit codes are ignored.
**Event-specific fields**:
```json
{
"error": "rate_limit | authentication_failed | billing_error | invalid_request | server_error | max_output_tokens | unknown",
"error_details": "detailed error message (optional)",
"last_assistant_message": "the last message from the assistant before the error (optional)"
}
```
**Matcher**: Matches against the `error` field. For example, `"matcher": "rate_limit"` will only trigger for rate limit errors.
**Output Options**:
- **None** - StopFailure is fire-and-forget. All hook output and exit codes are ignored.
**Exit Code Handling**:
| Exit Code | Behavior |
| --------- | ------------------------- |
| Any | Ignored (fire-and-forget) |
**Example Configuration**:
```json
{
"hooks": {
"StopFailure": [
{
"matcher": "rate_limit",
"hooks": [
{
"type": "command",
"command": "/path/to/rate-limit-alert.sh",
"name": "rate-limit-alerter"
}
]
}
]
}
}
```
**Use Cases**:
- Rate limit monitoring and alerting
- Authentication failure logging
- Billing error notifications
- Error statistics collection
#### SubagentStart
**Purpose**: Executed when a subagent (like the Task tool) is started to set up context or permissions.
@ -387,6 +443,63 @@ Event-specific fields are added based on the hook type. Below are the event-spec
}
```
#### PostCompact
**Purpose**: Executed after conversation compaction completes to archive summaries or track usage.
**Event-specific fields**:
```json
{
"trigger": "manual | auto",
"compact_summary": "the summary generated by the compaction process"
}
```
**Matcher**: Matches against the `trigger` field. For example, `"matcher": "manual"` will only trigger for manual compaction via `/compact` command.
**Output Options**:
- `hookSpecificOutput.additionalContext`: additional context (for logging only)
- Standard hook output fields (for logging only)
**Note**: PostCompact is **not** in the official decision mode supported events list. The `decision` field and other control fields do not produce any control effects - they are only used for logging purposes.
**Exit Code Handling**:
| Exit Code | Behavior |
| --------- | --------------------------------------------------------- |
| 0 | Success - stdout shown to user in verbose mode |
| Other | Non-blocking error - stderr shown to user in verbose mode |
**Example Configuration**:
```json
{
"hooks": {
"PostCompact": [
{
"matcher": "manual",
"hooks": [
{
"type": "command",
"command": "/path/to/save-compact-summary.sh",
"name": "save-summary"
}
]
}
]
}
}
```
**Use Cases**:
- Summary archiving to files or databases
- Usage statistics tracking
- Context change monitoring
- Audit logging for compaction operations
#### Notification
**Purpose**: Executed when notifications are sent to customize or intercept them.