fix(coding-agent): add settled agent lifecycle event

closes #6363
This commit is contained in:
Mario Zechner
2026-07-09 12:40:52 +02:00
parent 050b8176bf
commit e9fa5a68a1
14 changed files with 272 additions and 32 deletions
+11 -6
View File
@@ -307,7 +307,8 @@ user sends prompt ────────────────────
│ │ │ │
│ └─► turn_end │ │
│ │
─► agent_end │
─► agent_end │
└─► agent_settled (no retry/compaction/follow-up left) │
user sends another prompt ◄────────────────────────────────┘
@@ -546,15 +547,19 @@ The `systemPromptOptions` field gives extensions access to the same structured d
Inside `before_agent_start`, `event.systemPrompt` and `ctx.getSystemPrompt()` both reflect the chained system prompt as of the current handler. Later `before_agent_start` handlers can still modify it again.
#### agent_start / agent_end
#### agent_start / agent_end / agent_settled
Fired once per user prompt.
`agent_start` fires when a low-level agent run begins. `agent_end` fires when that run ends, but Pi may still auto-retry, auto-compact and retry, or continue with queued follow-up messages. Use `agent_settled` for status integrations that need to know Pi will not continue running automatically.
```typescript
pi.on("agent_start", async (_event, ctx) => {});
pi.on("agent_end", async (event, ctx) => {
// event.messages - messages from this prompt
// event.messages - messages from this low-level run
});
pi.on("agent_settled", async (_event, ctx) => {
// ctx.isIdle() is true here unless another extension started a new run.
});
```
@@ -1000,7 +1005,7 @@ pi.on("tool_result", async (event, ctx) => {
### ctx.isIdle() / ctx.abort() / ctx.hasPendingMessages()
Control flow helpers.
Control flow helpers. `ctx.isIdle()` is false while Pi is processing an agent run, automatic retry, auto-compaction retry, or queued continuation.
### ctx.shutdown()
@@ -1082,7 +1087,7 @@ This reports the current base prompt inputs. It does not include per-turn `befor
### ctx.waitForIdle()
Wait for the agent to finish streaming:
Wait for the agent to fully settle, including automatic retries, auto-compaction retries, and queued continuations:
```typescript
pi.registerCommand("my-cmd", {
+13 -3
View File
@@ -808,7 +808,8 @@ Events are streamed to stdout as JSON lines during agent operation. Events do NO
| Event | Description |
|-------|-------------|
| `agent_start` | Agent begins processing |
| `agent_end` | Agent completes (includes all generated messages) |
| `agent_end` | One low-level agent run completes (may still be followed by retry, compaction, or queued continuations) |
| `agent_settled` | Agent run is fully settled; no automatic retry, compaction retry, or queued continuation remains |
| `turn_start` | New turn begins |
| `turn_end` | Turn completes (includes assistant message and tool results) |
| `message_start` | Message begins |
@@ -834,15 +835,24 @@ Emitted when the agent begins processing a prompt.
### agent_end
Emitted when the agent completes. Contains all messages generated during this run.
Emitted when one low-level agent run completes. Contains all messages generated during this run. If `willRetry` is true, an automatic retry will follow.
```json
{
"type": "agent_end",
"messages": [...]
"messages": [...],
"willRetry": false
}
```
### agent_settled
Emitted after the full session-level run settles. At this point Pi will not continue automatically through retry, compaction retry, or queued follow-up messages.
```json
{"type": "agent_settled"}
```
### turn_start / turn_end
A turn consists of one assistant response plus any resulting tool calls and results.