Preserve item timestamps in thread history projections (#35689)

## What changed

- Add optional start and completion timestamps to `ThreadHistoryItemChange`.
- Populate them from canonical `ItemCompleted` records during stateless thread history projection.
- Treat a zero completion timestamp as absent, and leave timestamps unset for legacy builder events that do not provide them.

## Testing

- Cover projected item timestamps, including missing start times and zero completion times.

GitOrigin-RevId: 2187530f489ad3d86b6ab65faaaab1eab5357f1b
This commit is contained in:
Owen Lin authored and copyberry committed 2026-07-28 00:14:22 +00:00
1 parent 8c72d61727
commit 8d6a91b1ff
4 files changed
+61 -4

No files matched your search

@@ -130,6 +130,8 @@ fn projects_completed_canonical_turn_items() {
vec![ThreadHistoryItemChange {
turn_id: "turn-1".to_string(),
item: ThreadItem::from(user_item),
started_at_ms: Some(100),
completed_at_ms: Some(123),
}]
);
assert_eq!(
@@ -137,10 +139,48 @@ fn projects_completed_canonical_turn_items() {
vec![ThreadHistoryItemChange {
turn_id: "turn-1".to_string(),
item: ThreadItem::from(agent_item),
started_at_ms: Some(100),
completed_at_ms: Some(123),
}]
);
}
#[test]
fn projects_optional_completed_item_lifecycle_timestamps() {
let item = TurnItem::UserMessage(UserMessageItem {
id: "user-1".to_string(),
client_id: None,
content: Vec::new(),
});
for (started_at_ms, completed_at_ms, expected_completed_at_ms) in
[(None, 123, Some(123)), (Some(100), 0, None)]
{
let changes = project(RolloutItem::EventMsg(EventMsg::ItemCompleted(
ItemCompletedEvent {
thread_id: ThreadId::default(),
turn_id: "turn-1".to_string(),
item: item.clone(),
started_at_ms,
completed_at_ms,
},
)));
assert_eq!(
changes,
ThreadHistoryChangeSet {
changed_items: vec![ThreadHistoryItemChange {
turn_id: "turn-1".to_string(),
item: ThreadItem::from(item.clone()),
started_at_ms,
completed_at_ms: expected_completed_at_ms,
}],
..Default::default()
}
);
}
}
#[test]
fn ignores_legacy_abort_without_turn_id_and_context_only_records() {
let aborted = project(RolloutItem::EventMsg(EventMsg::TurnAborted(
@@ -206,7 +246,7 @@ fn item_completed(thread_id: ThreadId, turn_id: &str, item: TurnItem) -> Rollout
thread_id,
turn_id: turn_id.to_string(),
item,
started_at_ms: Some(0),
started_at_ms: Some(100),
completed_at_ms: 123,
}))
}