Persist response token usage in rollout history (#41912)

## Why

Resumed threads need to continue per-turn and per-thread token totals without scanning arbitrarily far beyond the latest compaction checkpoint.

## What changed

- Add durable `TokenUsageRecord` rollout items with response, turn, thread, session, and root-turn attribution.
- Restore accumulated usage on resume and snapshot the latest record plus the compaction response ID in compaction checkpoints.
- Preserve root-turn lineage in persisted turn context, while ensuring forked child threads start with their own usage totals.

## Testing

- Cover usage accumulation across multiple responses and resumed turns.
- Cover local and remote compaction checkpoints, invalid remote compaction output, rollout reconstruction, and fork isolation.

GitOrigin-RevId: ef9e0c4a9102a08a2c382be4cdac68c84353c90a
This commit is contained in:
Owen Lin authored and copyberry committed 2026-08-31 21:15:38 +00:00
1 parent a7913390f7
commit 5f79a92e39
67 files changed
+932 -162

No files matched your search

+58 -5
View File
@@ -142,8 +142,8 @@ fn response_item_envelope_ignores_unknown_harness_metadata_fields() -> Result<()
}
#[test]
/// Keeps legacy compacted replacement histories readable and shape-compatible.
fn response_item_replacement_history_preserves_shape() -> Result<()> {
/// Keeps legacy compacted replacement histories readable while adding nullable checkpoints.
fn response_item_replacement_history_accepts_legacy_shape() -> Result<()> {
let legacy_item = json!({
"message": "summary",
"replacement_history": [{
@@ -166,7 +166,10 @@ fn response_item_replacement_history_preserves_shape() -> Result<()> {
ResponseItem::Message { .. }
));
assert_eq!(replacement_history[0].metadata, None);
assert_eq!(serde_json::to_value(item)?, legacy_item);
let mut expected_item = legacy_item;
expected_item["compaction_response_id"] = json!(null);
expected_item["latest_token_usage_record"] = json!(null);
assert_eq!(serde_json::to_value(item)?, expected_item);
Ok(())
}
@@ -196,6 +199,8 @@ fn compacted_replacement_history_stores_metadata_in_an_aligned_sidecar() -> Resu
first_window_id: None,
previous_window_id: None,
window_id: None,
compaction_response_id: None,
latest_token_usage_record: None,
};
let serialized = serde_json::to_value(item)?;
@@ -208,6 +213,8 @@ fn compacted_replacement_history_stores_metadata_in_an_aligned_sidecar() -> Resu
{ "client_authored": true },
{ "client_authored": false },
],
"compaction_response_id": null,
"latest_token_usage_record": null,
})
);
@@ -300,6 +307,8 @@ fn compacted_metadata_remains_compatible_with_legacy_response_item_readers() ->
first_window_id: None,
previous_window_id: None,
window_id: None,
compaction_response_id: None,
latest_token_usage_record: None,
}))?;
let LegacyRolloutItem::Compacted(legacy) =
@@ -343,7 +352,11 @@ fn rollout_item_variants_preserve_existing_payload_shapes() -> Result<()> {
}),
json!({
"type": "compacted",
"payload": { "message": "summary" },
"payload": {
"message": "summary",
"compaction_response_id": null,
"latest_token_usage_record": null,
},
}),
json!({
"type": "turn_context",
@@ -355,6 +368,40 @@ fn rollout_item_variants_preserve_existing_payload_shapes() -> Result<()> {
"summary": "auto",
},
}),
json!({
"type": "token_usage_record",
"payload": {
"thread_id": "0195cda5-433d-7f9a-9d7b-a9f15b60c2e2",
"turn_id": "turn-1",
"session_id": "0195cda5-433d-7f9a-9d7b-a9f15b60c2e2",
"root_turn_id": "turn-1",
"response_id": "response-1",
"usage": {
"input_tokens": 10,
"cached_input_tokens": 2,
"cache_write_input_tokens": 0,
"output_tokens": 3,
"reasoning_output_tokens": 1,
"total_tokens": 13,
},
"turn_token_usage": {
"input_tokens": 10,
"cached_input_tokens": 2,
"cache_write_input_tokens": 0,
"output_tokens": 3,
"reasoning_output_tokens": 1,
"total_tokens": 13,
},
"thread_token_usage": {
"input_tokens": 10,
"cached_input_tokens": 2,
"cache_write_input_tokens": 0,
"output_tokens": 3,
"reasoning_output_tokens": 1,
"total_tokens": 13,
},
},
}),
json!({
"type": "world_state",
"payload": { "full": true, "state": { "cwd": "/tmp" } },
@@ -396,7 +443,7 @@ fn rollout_item_variants_preserve_existing_payload_shapes() -> Result<()> {
fn rollout_item_schema_matches_tagged_payload_and_sibling_metadata() -> Result<()> {
let schema = serde_json::to_value(schemars::schema_for!(RolloutItem))?;
let variants = schema["oneOf"].as_array().expect("rollout variants");
assert_eq!(variants.len(), 10);
assert_eq!(variants.len(), 11);
for variant in variants {
let required = variant["required"].as_array().expect("required fields");
@@ -452,6 +499,8 @@ fn compacted_item_serializes_window_number_and_id() -> Result<()> {
first_window_id: Some("019b3f6e-0000-7000-8000-000000000001".to_string()),
previous_window_id: Some("019b3f6e-0000-7000-8000-000000000002".to_string()),
window_id: Some("019b3f6e-7a10-7cc3-8b6e-1d09e2f7a001".to_string()),
compaction_response_id: None,
latest_token_usage_record: None,
};
assert_eq!(
@@ -462,6 +511,8 @@ fn compacted_item_serializes_window_number_and_id() -> Result<()> {
"first_window_id": "019b3f6e-0000-7000-8000-000000000001",
"previous_window_id": "019b3f6e-0000-7000-8000-000000000002",
"window_id": "019b3f6e-7a10-7cc3-8b6e-1d09e2f7a001",
"compaction_response_id": null,
"latest_token_usage_record": null,
})
);
Ok(())
@@ -485,6 +536,8 @@ fn compacted_item_migrates_legacy_numeric_window_id() -> Result<()> {
first_window_id: None,
previous_window_id: None,
window_id: None,
compaction_response_id: None,
latest_token_usage_record: None,
}
);
Ok(())