Unify model request hook lifecycles across sampling and compaction (#61562)

## What changed

- Pair `PreModelRequest` and `PostModelRequest` hooks per sampling or compaction attempt, with distinct lifecycle IDs and cancellation reporting.
- Add `thread_id` and `kind` to hook inputs, distinguishing `sampling`, `foreground_compaction`, and `background_compaction`. Include the provider's `response_id` in post-hook inputs when available.
- Wait for synchronous post-hooks before releasing terminal response events, accounting for usage, or installing compaction results. Allow streaming output and tool execution to proceed before the hook finishes, and cancel synchronous post-hooks when their owning request is interrupted.
- Preserve incomplete-response, budget-exceeded, and hook-denied outcomes across sampling retries and compaction model fallback.

## Testing

Add coverage for streaming and tool ordering, interrupted post-hooks, per-attempt metadata, and foreground and background compaction accounting and installation ordering.

GitOrigin-RevId: 3e808c5b3bf2d5a6a326d9f5be5bbd0359b2d16e
This commit is contained in:
tongzhou wang
2026-11-14 19:40:12 +00:00
committed by copyberry
parent 65a5410f72
commit 81b995a6c9
26 changed files with 1268 additions and 441 deletions
@@ -19,6 +19,14 @@
"null"
]
},
"SamplingKind": {
"enum": [
"sampling",
"foreground_compaction",
"background_compaction"
],
"type": "string"
},
"TokenUsage": {
"properties": {
"cache_write_input_tokens": {
@@ -83,6 +91,14 @@
],
"type": "string"
},
"response_id": {
"allOf": [
{
"$ref": "#/definitions/NullableString"
}
],
"description": "The provider's identifier for the completed or incomplete response, if available."
},
"status": {
"$ref": "#/definitions/ModelRequestStatus"
},
@@ -100,6 +116,7 @@
"required": [
"error",
"hook_event_name",
"response_id",
"status",
"token_usage"
],
@@ -116,12 +133,19 @@
"cwd": {
"$ref": "#/definitions/AbsolutePathBuf"
},
"kind": {
"$ref": "#/definitions/SamplingKind"
},
"model": {
"type": "string"
},
"session_id": {
"type": "string"
},
"thread_id": {
"description": "Identifies the issuing thread within its shared session.",
"type": "string"
},
"transcript_path": {
"$ref": "#/definitions/NullableString"
},
@@ -131,8 +155,10 @@
},
"required": [
"cwd",
"kind",
"model",
"session_id",
"thread_id",
"transcript_path",
"turn_id"
],