Respect paginated history when stopping background tasks (#40787)

## Why

Stopping a task from the agents overview may need to look up its active turn. Paginated threads should use the turn history endpoint instead of requesting embedded full history.

## What changed

- Read the thread without turns first to determine its history mode.
- Fetch the first turn page for paginated threads when the app server supports it.
- Fall back to embedded turn history for legacy threads and legacy server negotiation.

## Testing

Added coverage for paginated and legacy threads, including fallback after legacy capability negotiation.

GitOrigin-RevId: 44d506b19e32c6b3c27fac11fb748c23b8e47b5e
This commit is contained in:
Owen Lin authored and copyberry committed 2026-08-26 04:23:06 +00:00
1 parent 98ee29c7cd
commit 37f4bb94c9
3 files changed
+118 -8

No files matched your search

@@ -2265,6 +2265,92 @@ async fn paginated_workflows_never_request_full_thread_history() -> Result<()> {
Ok(())
}
#[tokio::test]
async fn agents_overview_stop_uses_history_mode_for_turn_lookup() -> Result<()> {
let (mut app, _codex_home) = make_history_test_app().await?;
let paginated_thread_id = create_history_rollout(
&app.config,
ThreadHistoryMode::Paginated,
"paginated background task",
)?;
let cases = [
(paginated_thread_id, vec![false], 1),
(
create_history_rollout(
&app.config,
ThreadHistoryMode::Legacy,
"legacy background task",
)?,
vec![false, true],
0,
),
];
let (mut app_server, requests, proxy) = start_recording_app_server_with_history(
&app.config,
HistoryCapabilities::Current,
/*blocked_thread_list*/ None,
/*failed_thread_name*/ None,
crate::app_server_session::ThreadParamsMode::Embedded,
)
.await?;
for (thread_id, expected_include_turns, expected_turn_page_count) in cases {
let previous_reads = recorded_params(&requests, "thread/read");
let previous_turn_page_count = recorded_params(&requests, "thread/turns/list").len();
app.stop_agents_overview_thread(&mut app_server, thread_id)
.await;
let reads = recorded_params(&requests, "thread/read");
let include_turns = reads[previous_reads.len()..]
.iter()
.map(|params| params["includeTurns"].as_bool().unwrap_or(false))
.collect::<Vec<_>>();
assert_eq!(include_turns, expected_include_turns);
assert_eq!(
recorded_params(&requests, "thread/turns/list").len() - previous_turn_page_count,
expected_turn_page_count
);
}
app_server.shutdown().await?;
proxy.await??;
Ok(())
}
#[tokio::test]
async fn agents_overview_stop_uses_full_history_after_legacy_negotiation() -> Result<()> {
let (mut app, _codex_home) = make_history_test_app().await?;
let thread_id = create_history_rollout(
&app.config,
ThreadHistoryMode::Paginated,
"paginated background task",
)?;
let (mut app_server, requests, proxy) = start_recording_app_server_with_history(
&app.config,
HistoryCapabilities::LegacyOnly,
/*blocked_thread_list*/ None,
/*failed_thread_name*/ None,
crate::app_server_session::ThreadParamsMode::Embedded,
)
.await?;
app_server.start_thread(&app.config).await?;
app.stop_agents_overview_thread(&mut app_server, thread_id)
.await;
let include_turns = recorded_params(&requests, "thread/read")
.into_iter()
.map(|params| params["includeTurns"].as_bool().unwrap_or(false))
.collect::<Vec<_>>();
assert_eq!(include_turns, vec![false, true]);
assert!(recorded_params(&requests, "thread/turns/list").is_empty());
app_server.shutdown().await?;
proxy.await??;
Ok(())
}
#[tokio::test]
async fn cold_paginated_subagent_transcript_excludes_inherited_parent_history() -> Result<()> {
let (app, codex_home) = make_history_test_app().await?;