Report workspace kind on TUI composer turns (#63545)

## Why

A TUI composer prompt does not supply `workspace_kind`. The app server
can derive it from the thread's current project assignment.

## What changed

Use the staged assignment until the thread is saved, then read the
persisted assignment on each new composer turn. Use `project` or
`projectless` as a fallback for model request metadata and turn analytics.
Explicit client metadata takes precedence, and the inferred value stays
with the originating turn.

## Testing

Cover the model request header and analytics event on the first turn,
on a follow-up, and after removing the project assignment. Unit tests
cover explicit metadata precedence and verify that MCP requests,
extensions, and child agents do not inherit the inferred value.

GitOrigin-RevId: 62b88484dd4028a6b72584999e0fa9b84e6e3b03
This commit is contained in:
marksteinbrick-oai
2026-11-24 05:33:38 +00:00
committed by copyberry
parent a90526e8a7
commit baf9b39b16
7 changed files with 182 additions and 0 deletions
@@ -473,6 +473,24 @@ impl TurnRequestProcessor {
Ok((thread_id, thread))
}
async fn thread_workspace_kind(&self, thread: &CodexThread) -> Option<String> {
let db = thread.state_db()?;
let thread_id = thread.startup_metadata().thread_id;
// Saving a new thread consumes its staged assignment, so read staging first.
let staged_project_id = self
.thread_manager
.thread_store()
.read_pending_thread_metadata(thread_id)
.await
.ok()?
.and_then(|patch| patch.project_id.flatten());
let assigned = db
.thread_has_project(thread_id, staged_project_id.as_deref())
.await
.ok()?;
Some(if assigned { "project" } else { "projectless" }.to_owned())
}
async fn ensure_direct_input_allowed(
&self,
request_id: &ConnectionRequestId,
@@ -647,6 +665,14 @@ impl TurnRequestProcessor {
.inspect_err(|error| {
self.track_error_response(&request_id, error, /*error_type*/ None);
})?;
// TUI composer prompts carry a client message ID. Helper requests omit it.
let inferred_workspace_kind = if app_server_client_name.as_deref() == Some("codex-tui")
&& params.client_user_message_id.is_some()
{
self.thread_workspace_kind(&thread).await
} else {
None
};
Self::set_app_server_client_info(
thread.as_ref(),
app_server_client_name,
@@ -729,6 +755,7 @@ impl TurnRequestProcessor {
TurnInputRequest::new(input)
.with_thread_settings(thread_settings)
.on_start(TurnStartOptions {
inferred_workspace_kind,
turn_trigger: params.turn_trigger,
parent_turn_id: params.parent_turn_id,
root_turn_id: params.root_turn_id,
@@ -3,6 +3,10 @@ use app_test_support::MockResponsesConfig;
use app_test_support::TestAppServer;
use app_test_support::create_fake_parented_rollout_with_source;
use app_test_support::create_fake_rollout;
use codex_app_server_protocol::ClientInfo;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::ProjectCreateParams;
use codex_app_server_protocol::ProjectCreateResponse;
use codex_app_server_protocol::RawResponseCompletedNotification;
use codex_app_server_protocol::ReviewDelivery;
use codex_app_server_protocol::ReviewStartParams;
@@ -13,6 +17,8 @@ use codex_app_server_protocol::ThreadClosedNotification;
use codex_app_server_protocol::ThreadForkParams;
use codex_app_server_protocol::ThreadForkResponse;
use codex_app_server_protocol::ThreadLoadedListResponse;
use codex_app_server_protocol::ThreadMetadataUpdateParams;
use codex_app_server_protocol::ThreadMetadataUpdateResponse;
use codex_app_server_protocol::ThreadResumeParams;
use codex_app_server_protocol::ThreadResumeResponse;
use codex_app_server_protocol::ThreadSource;
@@ -41,11 +47,92 @@ use tokio::time::timeout;
use super::analytics::captured_analytics_events;
use super::analytics::mount_analytics_capture;
use super::analytics::wait_for_analytics_event;
use super::analytics::wait_for_matching_analytics_event;
// Bazel CI can spend tens of seconds starting app-server subprocesses or
// processing turn RPCs under load.
const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);
#[tokio::test]
async fn tui_reports_the_current_project_assignment_on_each_turn() -> Result<()> {
skip_if_no_network!(Ok(()));
let model = responses::start_mock_server().await;
let codex_home = TempDir::new()?;
MockResponsesConfig::new(&model.uri())
.with_root_config(&format!("chatgpt_base_url = \"{}\"", model.uri()))
.write(codex_home.path())?;
mount_analytics_capture(&model, codex_home.path()).await?;
let mut tui = TestAppServer::builder()
.with_codex_home(codex_home.path())
.build()
.await?;
tui.initialize_with_client_info(ClientInfo {
name: "codex-tui".into(),
version: "1".into(),
title: None,
})
.await?;
let ProjectCreateResponse { project } = tui
.request(|request_id| ClientRequest::ProjectCreate {
request_id,
params: ProjectCreateParams {
name: "Work".into(),
roots: vec![],
metadata: None,
idempotency_key: "work".into(),
},
})
.await?;
let ThreadStartResponse { thread, .. } = tui
.start_thread(ThreadStartParams {
project_id: Some(project.id),
..Default::default()
})
.await?;
let check_turn = async |tui: &mut TestAppServer, prompt: &str, expected: &str| -> Result<()> {
let request = responses::mount_sse_once(&model, responses::sse_completed(prompt)).await;
let completed = tui
.start_turn_and_wait_for_completion(TurnStartParams {
thread_id: thread.id.clone(),
client_user_message_id: Some(prompt.into()),
input: vec![V2UserInput::Text {
text: prompt.into(),
text_elements: vec![],
}],
..Default::default()
})
.await?;
let header = request
.single_request()
.header("x-codex-turn-metadata")
.expect("turn metadata");
assert_eq!(parse_json_header(&header)["workspace_kind"], expected);
let event = wait_for_matching_analytics_event(&model, DEFAULT_READ_TIMEOUT, |event| {
event["event_type"] == "codex_turn_event"
&& event["event_params"]["turn_id"] == completed.turn.id
})
.await?;
assert_eq!(event["event_params"]["workspace_kind"], expected);
Ok(())
};
check_turn(&mut tui, "First", "project").await?;
check_turn(&mut tui, "Continue", "project").await?;
let update = tui
.send_thread_metadata_update_request(ThreadMetadataUpdateParams {
thread_id: thread.id.clone(),
project_id: Some(String::new()),
git_info: None,
daybreak_enabled: None,
})
.await?;
let _: ThreadMetadataUpdateResponse = tui.read_response(update).await?;
check_turn(&mut tui, "After move", "projectless").await
}
#[tokio::test]
async fn turn_start_forwards_client_metadata_to_responses_request_v2() -> Result<()> {
skip_if_no_network!(Ok(()));