mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Preserve the thread's selected workspace when the TUI starts a turn (#65084)
## Why Another client can change a thread's workspace after startup. When the TUI sends its local working directory with the next prompt, it can reset the thread's selected environments before the TUI receives the update. ## What changed Omit `cwd` and `runtimeWorkspaceRoots` from TUI `turn/start` requests for both regular prompts and misalignment continuations so the turn uses the thread's current workspace. ## Testing Add a regression test that changes the thread's workspace before the TUI receives the update and checks that the first prompt sent to the model includes the newly selected working directory. GitOrigin-RevId: b5cc23bce787f91058ede6aa7ee825e0e7a9518c
This commit is contained in:
@@ -165,11 +165,6 @@ impl App {
|
||||
);
|
||||
return;
|
||||
};
|
||||
params.cwd = Some(cwd);
|
||||
params.runtime_workspace_roots = app_server
|
||||
.thread_params_mode()
|
||||
.local_workspace_roots(config.permissions.user_visible_workspace_roots())
|
||||
.map(<[_]>::to_vec);
|
||||
params.approval_policy = Some(config.permissions.approval_policy.value().into());
|
||||
params.sandbox_policy = sandbox_policy;
|
||||
params.permissions = permissions;
|
||||
|
||||
@@ -249,9 +249,6 @@ async fn misalignment_continuation_requires_current_review_and_submits_once() ->
|
||||
params,
|
||||
codex_app_server_protocol::TurnStartParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
cwd: (!server_owned).then(|| config.cwd.to_path_buf()),
|
||||
runtime_workspace_roots: (!server_owned)
|
||||
.then(|| config.permissions.user_visible_workspace_roots().to_vec()),
|
||||
approval_policy: (!server_owned).then_some(AskForApproval::OnRequest),
|
||||
approvals_reviewer: None,
|
||||
sandbox_policy: (!server_owned && !preserve).then(|| config
|
||||
|
||||
@@ -617,6 +617,8 @@ requires_openai_auth = false
|
||||
.await?;
|
||||
let turn = &recorded_params(&requests, "turn/start")[0];
|
||||
assert!(turn["approvalsReviewer"].is_null());
|
||||
assert!(turn["runtimeWorkspaceRoots"].is_null());
|
||||
assert!(turn["cwd"].is_null());
|
||||
assert_eq!(
|
||||
turn["approvalPolicy"],
|
||||
serde_json::json!(expected_turn_policy)
|
||||
@@ -628,6 +630,56 @@ requires_openai_auth = false
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn first_prompt_uses_the_workspace_selected_after_startup() -> Result<()> {
|
||||
use codex_app_server_protocol::ThreadSettingsUpdateParams;
|
||||
use codex_app_server_protocol::TurnEnvironmentParams;
|
||||
use core_test_support::responses;
|
||||
|
||||
let (mut app, _events, mut ops) = make_test_app_with_channels().await;
|
||||
let model = responses::start_mock_server().await;
|
||||
let sent = responses::mount_sse_once(&model, responses::sse_completed("ok")).await;
|
||||
app_test_support::MockResponsesConfig::new(&model.uri()).write(&app.config.codex_home)?;
|
||||
app.config = app
|
||||
.rebuild_config_for_cwd(app.config.cwd.to_path_buf())
|
||||
.await?;
|
||||
let mut server = crate::start_embedded_app_server_for_picker(&app.config).await?;
|
||||
let started = server.start_thread(&app.config).await?;
|
||||
let thread_id = started.session.thread_id;
|
||||
app.pending_startup_thread_start = true;
|
||||
app.handle_startup_thread_started(&mut server, Ok(started))
|
||||
.await?;
|
||||
|
||||
// Another client changes the environment before the TUI receives the update.
|
||||
let workspace = tempdir()?;
|
||||
let selected = dunce::canonicalize(workspace.path())?;
|
||||
assert!(
|
||||
server
|
||||
.thread_settings_update(ThreadSettingsUpdateParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
environments: Some(vec![TurnEnvironmentParams {
|
||||
environment_id: codex_exec_server::LOCAL_ENVIRONMENT_ID.to_string(),
|
||||
cwd: selected.clone().abs().into(),
|
||||
runtime_workspace_roots: None,
|
||||
}]),
|
||||
..Default::default()
|
||||
})
|
||||
.await?
|
||||
);
|
||||
app.chat_widget.insert_str("first prompt");
|
||||
app.chat_widget.handle_key_event(KeyCode::Enter.into());
|
||||
app.submit_thread_op(&mut server, thread_id, next_user_turn_op(&mut ops))
|
||||
.await?;
|
||||
super::super::safety_buffering::wait_for_turn_completed(&mut app, &mut server, thread_id).await;
|
||||
// Match the unique leaf so XML escaping in the temporary directory prefix is irrelevant.
|
||||
let leaf = selected.file_name().unwrap().to_string_lossy();
|
||||
let cwd = format!("{leaf}</cwd>");
|
||||
let input = sent.single_request().message_input_texts("user");
|
||||
assert!(input.iter().any(|message| message.contains(&cwd)));
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fresh_startup_reads_destination_and_cleared_model_uses_catalog() -> Result<()> {
|
||||
for (remote, override_cwd) in [(false, false), (true, false), (true, true)] {
|
||||
|
||||
@@ -192,11 +192,7 @@ pub(super) async fn try_submit_user_turn(
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn turn_start_settings<'a>(
|
||||
app: &'a App,
|
||||
thread_id: ThreadId,
|
||||
op: &AppCommand,
|
||||
) -> TurnStartSettings<'a> {
|
||||
fn turn_start_settings(app: &App, thread_id: ThreadId, op: &AppCommand) -> TurnStartSettings {
|
||||
let AppCommand::UserTurn {
|
||||
approval_policy,
|
||||
approvals_reviewer,
|
||||
@@ -247,6 +243,5 @@ fn turn_start_settings<'a>(
|
||||
approval_policy: turn_approval_policy,
|
||||
approvals_reviewer: turn_approvals_reviewer,
|
||||
permissions: permissions_override,
|
||||
workspace_roots: config.permissions.user_visible_workspace_roots(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,17 +536,9 @@ impl ThreadParamsMode {
|
||||
}
|
||||
|
||||
fn workspace_roots_from_config(self, config: &Config) -> Option<Vec<LegacyAppPathString>> {
|
||||
self.local_workspace_roots(&config.workspace_roots)
|
||||
.map(LegacyAppPathString::from_abs_paths)
|
||||
}
|
||||
|
||||
pub(crate) fn local_workspace_roots(
|
||||
self,
|
||||
roots: &[AbsolutePathBuf],
|
||||
) -> Option<&[AbsolutePathBuf]> {
|
||||
// A remote server owns its workspace roots. A client config can only hold local paths.
|
||||
match self {
|
||||
Self::Embedded => Some(roots),
|
||||
Self::Embedded => Some(LegacyAppPathString::from_abs_paths(&config.workspace_roots)),
|
||||
Self::Remote | Self::Cloud => None,
|
||||
}
|
||||
}
|
||||
@@ -2110,7 +2102,6 @@ impl AppServerSession {
|
||||
approval_policy: Option<AskForApproval>,
|
||||
approvals_reviewer: Option<codex_app_server_protocol::ApprovalsReviewer>,
|
||||
permissions_override: TurnPermissionsOverride,
|
||||
workspace_roots: &[AbsolutePathBuf],
|
||||
model: String,
|
||||
effort: Option<codex_protocol::openai_models::ReasoningEffort>,
|
||||
summary: Option<codex_protocol::config_types::ReasoningSummary>,
|
||||
@@ -2132,11 +2123,7 @@ impl AppServerSession {
|
||||
if self.thread_params_mode.settings_owner() == ThreadSettingsOwner::Client {
|
||||
let (sandbox_policy, permissions) =
|
||||
turn_permissions_overrides(permissions_override, cwd.as_path())?;
|
||||
params.cwd = Some(cwd);
|
||||
params.runtime_workspace_roots = self
|
||||
.thread_params_mode
|
||||
.local_workspace_roots(workspace_roots)
|
||||
.map(<[_]>::to_vec);
|
||||
// Even an unchanged cwd would reset the thread's selected environments.
|
||||
params.approval_policy = approval_policy;
|
||||
params.approvals_reviewer = approvals_reviewer;
|
||||
params.sandbox_policy = sandbox_policy;
|
||||
@@ -4152,7 +4139,6 @@ mod tests {
|
||||
text_elements: Vec::new(),
|
||||
}];
|
||||
let output_schema = serde_json::json!({"type": "string"});
|
||||
let workspace_roots = vec![test_path_buf("/local/workspace").abs()];
|
||||
let (turn, sent_params) = tokio::join!(
|
||||
app_server.turn_start(
|
||||
thread_id,
|
||||
@@ -4165,7 +4151,6 @@ mod tests {
|
||||
TurnPermissionsOverride::ActiveProfile(ActivePermissionProfile::new(
|
||||
"local-permission-profile".to_string(),
|
||||
)),
|
||||
&workspace_roots,
|
||||
"local-model".to_string(),
|
||||
Some(ReasoningEffort::High),
|
||||
Some(ReasoningSummary::Detailed),
|
||||
|
||||
@@ -139,7 +139,6 @@ stream_max_retries = 0
|
||||
/*approval_policy*/ None,
|
||||
/*approvals_reviewer*/ None,
|
||||
TurnPermissionsOverride::Preserve,
|
||||
&config.workspace_roots,
|
||||
started.session.model.clone(),
|
||||
/*effort*/ None,
|
||||
Some(
|
||||
@@ -313,7 +312,6 @@ supports_websockets = true
|
||||
/*approval_policy*/ None,
|
||||
/*approvals_reviewer*/ None,
|
||||
TurnPermissionsOverride::Preserve,
|
||||
&config.workspace_roots,
|
||||
started.session.model,
|
||||
/*effort*/ None,
|
||||
reasoning_summary_from_config(&config),
|
||||
|
||||
@@ -20,7 +20,6 @@ use codex_protocol::config_types::SERVICE_TIER_DEFAULT_REQUEST_VALUE;
|
||||
use codex_protocol::openai_models::ModelPreset;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use codex_protocol::turn_input::CyberAccessProgram;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use color_eyre::eyre::Report;
|
||||
|
||||
use crate::app_command::AppCommand;
|
||||
@@ -37,11 +36,10 @@ use super::UnsupportedLegacyPermissionProfile;
|
||||
// Both conventional transports return this before the request reaches its handler.
|
||||
const JSONRPC_SERVER_OVERLOADED: i64 = -32001;
|
||||
|
||||
pub(crate) struct TurnStartSettings<'a> {
|
||||
pub(crate) struct TurnStartSettings {
|
||||
pub(crate) approval_policy: Option<AskForApproval>,
|
||||
pub(crate) approvals_reviewer: Option<ApprovalsReviewer>,
|
||||
pub(crate) permissions: TurnPermissionsOverride,
|
||||
pub(crate) workspace_roots: &'a [AbsolutePathBuf],
|
||||
}
|
||||
|
||||
pub(crate) enum UserMessageAdmission {
|
||||
@@ -194,14 +192,14 @@ impl AppServerSession {
|
||||
Some(selected)
|
||||
}
|
||||
|
||||
pub(crate) async fn submit_user_message<'a>(
|
||||
pub(crate) async fn submit_user_message(
|
||||
&mut self,
|
||||
thread_id: ThreadId,
|
||||
op: &AppCommand,
|
||||
intent: CloudSubmissionIntent,
|
||||
active_turn: &mut Option<String>,
|
||||
preflight: impl FnOnce(&str) -> Result<Option<CyberAccessProgram>, String>,
|
||||
start_settings: impl FnOnce() -> TurnStartSettings<'a>,
|
||||
start_settings: impl FnOnce() -> TurnStartSettings,
|
||||
) -> Result<(UserMessageAdmission, Option<CloudTurnSettings>), UserMessageSubmissionFailure>
|
||||
{
|
||||
let AppCommand::UserTurn {
|
||||
@@ -342,7 +340,6 @@ impl AppServerSession {
|
||||
settings.approval_policy,
|
||||
settings.approvals_reviewer,
|
||||
settings.permissions,
|
||||
settings.workspace_roots,
|
||||
model.to_string(),
|
||||
effort.clone(),
|
||||
*summary,
|
||||
|
||||
@@ -158,7 +158,6 @@ stream_max_retries = 0
|
||||
/*approval_policy*/ None,
|
||||
/*approvals_reviewer*/ None,
|
||||
TurnPermissionsOverride::Preserve,
|
||||
/*workspace_roots*/ &[],
|
||||
"gpt-5.2".to_string(),
|
||||
/*effort*/ None,
|
||||
/*summary*/ None,
|
||||
|
||||
Reference in New Issue
Block a user