mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Enable automatic titles for Cloud conversations (#61600)
## Why Cloud conversations previously skipped TUI title generation, leaving them without automatically saved names. ## What changed - Generate structured titles using the selected Cloud account and its backend routing, without agent tools or a temporary conversation. - Read and save names outside the conversation command queue without replacing the active attachment. Preserve an existing name if it no longer matches the expected value. - Fall back to the first-message title when generation is unavailable, and allow up to three seconds for pending saves during shutdown. - Report a retryable error when a manual rename encounters an automatic save in progress, keeping the UI responsive. ## Testing Add coverage for selected-account title generation, saved-name reads without attachment, first-message fallback persistence, and manual rename retries after automatic saves. GitOrigin-RevId: 48030cc0b75e790a2004af018f54e5751665b760
This commit is contained in:
committed by
copyberry
parent
8e30237f64
commit
b016cd1847
Generated
+3
@@ -2221,10 +2221,12 @@ dependencies = [
|
||||
name = "codex-app-server-client"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"codex-api",
|
||||
"codex-app-server",
|
||||
"codex-app-server-protocol",
|
||||
"codex-app-server-transport",
|
||||
"codex-arg0",
|
||||
"codex-backend-client",
|
||||
"codex-cloud-client",
|
||||
"codex-config",
|
||||
"codex-core",
|
||||
@@ -2232,6 +2234,7 @@ dependencies = [
|
||||
"codex-feedback",
|
||||
"codex-http-client",
|
||||
"codex-login",
|
||||
"codex-model-provider",
|
||||
"codex-protocol",
|
||||
"codex-uds",
|
||||
"codex-utils-absolute-path",
|
||||
|
||||
@@ -13,10 +13,12 @@ doctest = false
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
codex-api = { workspace = true }
|
||||
codex-app-server = { workspace = true }
|
||||
codex-app-server-protocol = { workspace = true }
|
||||
codex-app-server-transport = { workspace = true }
|
||||
codex-arg0 = { workspace = true }
|
||||
codex-backend-client = { workspace = true }
|
||||
codex-cloud-client = { path = "../cloud-client" }
|
||||
codex-config = { workspace = true }
|
||||
codex-login = { workspace = true }
|
||||
@@ -24,6 +26,7 @@ codex-core = { workspace = true }
|
||||
codex-exec-server = { workspace = true }
|
||||
codex-feedback = { workspace = true }
|
||||
codex-http-client = { workspace = true }
|
||||
codex-model-provider = { workspace = true }
|
||||
codex-protocol = { workspace = true }
|
||||
codex-uds = { workspace = true }
|
||||
codex-utils-absolute-path = { workspace = true }
|
||||
|
||||
@@ -4,12 +4,14 @@
|
||||
use crate::AppServerEvent;
|
||||
use crate::RequestResult;
|
||||
use crate::TypedRequestError;
|
||||
use crate::cloud_backend::CloudTitleClient;
|
||||
use codex_app_server_protocol::ClientNotification;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
use codex_app_server_protocol::JSONRPCErrorError;
|
||||
use codex_app_server_protocol::ModelListParams;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::Result as JsonRpcResult;
|
||||
use codex_app_server_protocol::ThreadSetNameParams;
|
||||
use codex_app_server_protocol::Turn;
|
||||
use codex_app_server_protocol::TurnStartParams;
|
||||
use std::collections::HashMap;
|
||||
@@ -40,6 +42,8 @@ pub enum CloudOperation {
|
||||
project: codex_cloud_client::ProjectEnvironment,
|
||||
},
|
||||
SubmitUserMessage(Box<TurnStartParams>),
|
||||
/// Apply the result of a name write that already succeeded outside the command queue.
|
||||
ThreadNameSaved(ThreadSetNameParams),
|
||||
Notify(ClientNotification),
|
||||
Resolve {
|
||||
request_id: RequestId,
|
||||
@@ -81,7 +85,8 @@ pub(super) enum IdleReconnect {
|
||||
pub struct CloudRequestHandle {
|
||||
pub(super) reconnect: tokio::sync::watch::Sender<IdleReconnect>,
|
||||
commands: mpsc::Sender<CloudCommand>,
|
||||
catalog: Option<Arc<codex_cloud_client::Client>>,
|
||||
native: Option<Arc<codex_cloud_client::Client>>,
|
||||
title_client: Option<Arc<CloudTitleClient>>,
|
||||
}
|
||||
|
||||
/// Tracks only losses queued for delivery. The worker remains the sole owner
|
||||
@@ -147,12 +152,13 @@ impl CloudClient {
|
||||
mpsc::Receiver<CloudCommand>,
|
||||
mpsc::UnboundedSender<AppServerEvent>,
|
||||
) {
|
||||
Self::channel_with_catalog(capacity, /*catalog*/ None)
|
||||
Self::channel_with_native(capacity, /*native*/ None, /*title_client*/ None)
|
||||
}
|
||||
|
||||
pub(super) fn channel_with_catalog(
|
||||
pub(super) fn channel_with_native(
|
||||
capacity: usize,
|
||||
catalog: Option<codex_cloud_client::Client>,
|
||||
native: Option<codex_cloud_client::Client>,
|
||||
title_client: Option<CloudTitleClient>,
|
||||
) -> (
|
||||
Self,
|
||||
mpsc::Receiver<CloudCommand>,
|
||||
@@ -166,7 +172,8 @@ impl CloudClient {
|
||||
handle: CloudRequestHandle {
|
||||
commands,
|
||||
reconnect: Default::default(),
|
||||
catalog: catalog.map(Arc::new),
|
||||
native: native.map(Arc::new),
|
||||
title_client: title_client.map(Arc::new),
|
||||
},
|
||||
events: event_receiver,
|
||||
},
|
||||
@@ -237,6 +244,47 @@ impl CloudClient {
|
||||
}
|
||||
|
||||
impl CloudRequestHandle {
|
||||
/// Generate metadata with Cloud's own account, outside its conversation command queue.
|
||||
pub async fn generate_thread_title(
|
||||
&self,
|
||||
model: &str,
|
||||
effort: Option<codex_protocol::openai_models::ReasoningEffort>,
|
||||
prompt: String,
|
||||
schema: serde_json::Value,
|
||||
) -> color_eyre::Result<String> {
|
||||
let client = self
|
||||
.title_client
|
||||
.as_ref()
|
||||
.ok_or_else(|| io::Error::other("Cloud title generation is unavailable"))?;
|
||||
client.generate(model, effort, prompt, schema).await
|
||||
}
|
||||
|
||||
/// Read the saved title even when another Cloud conversation owns the attachment.
|
||||
pub async fn read_native_thread_name(
|
||||
&self,
|
||||
thread_id: &str,
|
||||
) -> Option<Result<Option<String>, codex_cloud_client::Error>> {
|
||||
Some(self.native.as_ref()?.read_thread_name(thread_id).await)
|
||||
}
|
||||
|
||||
/// Save an automatic name without occupying the conversation command queue.
|
||||
pub async fn save_native_thread_name(
|
||||
&self,
|
||||
params: ThreadSetNameParams,
|
||||
) -> Option<color_eyre::Result<()>> {
|
||||
let client = self.native.as_ref()?;
|
||||
Some(
|
||||
async {
|
||||
client
|
||||
.set_thread_name(¶ms.thread_id, ¶ms.name)
|
||||
.await?;
|
||||
self.unit(CloudOperation::ThreadNameSaved(params)).await?;
|
||||
Ok(())
|
||||
}
|
||||
.await,
|
||||
)
|
||||
}
|
||||
|
||||
/// Accept a quiet Attach only for the conversation displayed by the caller.
|
||||
pub fn reconnect_idle_thread(&self, thread_id: &str) -> bool {
|
||||
self.reconnect.send_if_modified(|state| {
|
||||
@@ -252,7 +300,7 @@ impl CloudRequestHandle {
|
||||
|
||||
/// Whether a completed model read still belongs to the bound Cloud account.
|
||||
pub fn model_catalog_is_current(&self) -> bool {
|
||||
self.catalog
|
||||
self.native
|
||||
.as_ref()
|
||||
.is_none_or(|catalog| catalog.model_catalog_is_current())
|
||||
}
|
||||
@@ -338,7 +386,8 @@ impl CloudRequestHandle {
|
||||
Self {
|
||||
commands,
|
||||
reconnect: self.reconnect.clone(),
|
||||
catalog: None,
|
||||
native: None,
|
||||
title_client: None,
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -375,8 +424,7 @@ impl CloudRequestHandle {
|
||||
|
||||
pub(crate) async fn request(&self, request: ClientRequest) -> io::Result<RequestResult> {
|
||||
// Catalog reads must not occupy the serialized send and attachment lane.
|
||||
if let (Some(catalog), ClientRequest::ModelList { params, .. }) = (&self.catalog, &request)
|
||||
{
|
||||
if let (Some(catalog), ClientRequest::ModelList { params, .. }) = (&self.native, &request) {
|
||||
// Keep the catalog HTTP state out of the shared request future.
|
||||
return Ok(Box::pin(Self::model_list(catalog, params)).await);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Opens the internal Cloud preview with the selected ChatGPT credentials and local transport policy.
|
||||
|
||||
use super::CloudTitleClient;
|
||||
use super::start_worker;
|
||||
use crate::AppServerClient;
|
||||
use crate::legacy_core::config::Config;
|
||||
@@ -39,6 +40,13 @@ pub async fn connect(config: &Config) -> Result<AppServerClient> {
|
||||
let permit = factory.network_policy().acquire(&origin)?;
|
||||
let credentials = cloud::Credentials::new(&token, &account_id)?;
|
||||
let client = cloud::Client::new(&factory, ORIGIN, credentials)?;
|
||||
let title_client = CloudTitleClient::new(
|
||||
factory,
|
||||
&auth,
|
||||
account_id.clone(),
|
||||
config.chatgpt_backend_url(),
|
||||
)
|
||||
.ok();
|
||||
Ok(start_worker(
|
||||
client,
|
||||
super::idle::Login {
|
||||
@@ -58,5 +66,6 @@ pub async fn connect(config: &Config) -> Result<AppServerClient> {
|
||||
requires_openai_auth: true,
|
||||
workspace_routing: None,
|
||||
},
|
||||
title_client,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ mod auth;
|
||||
mod events;
|
||||
mod idle;
|
||||
mod requests;
|
||||
mod thread_title;
|
||||
|
||||
use crate::AppServerClient;
|
||||
use crate::AppServerEvent;
|
||||
@@ -27,6 +28,7 @@ use tokio::sync::mpsc;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
pub use auth::connect;
|
||||
pub(super) use thread_title::CloudTitleClient;
|
||||
|
||||
type ReplyStream = BoxStream<'static, Result<cloud::Event, cloud::Error>>;
|
||||
type OpenReplyStream = (ReplyStream, Option<cloud::Event>);
|
||||
@@ -291,9 +293,10 @@ fn start_worker(
|
||||
client: cloud::Client,
|
||||
login: idle::Login,
|
||||
account: protocol::GetAccountResponse,
|
||||
title_client: Option<CloudTitleClient>,
|
||||
) -> AppServerClient {
|
||||
let (app_client, commands, output) =
|
||||
CloudClient::channel_with_catalog(/*capacity*/ 32, Some(client.clone()));
|
||||
CloudClient::channel_with_native(/*capacity*/ 32, Some(client.clone()), title_client);
|
||||
let client = Arc::new(client);
|
||||
let mut requests = requests::Requests {
|
||||
client: Arc::clone(&client),
|
||||
@@ -386,6 +389,11 @@ async fn run_worker_with_prepared(
|
||||
let _ = command.response.send(Ok(Ok(Value::Null)));
|
||||
return;
|
||||
}
|
||||
CloudOperation::ThreadNameSaved(params) => {
|
||||
requests.thread_name_saved(params, output);
|
||||
let _ = command.response.send(Ok(Ok(Value::Null)));
|
||||
continue;
|
||||
}
|
||||
CloudOperation::Request(request) => {
|
||||
Ok((*request, cloud::SubmissionMode::StartIfIdle, None))
|
||||
}
|
||||
|
||||
@@ -33,6 +33,27 @@ pub(super) enum Response {
|
||||
}
|
||||
|
||||
impl Requests {
|
||||
pub(super) fn thread_name_saved(
|
||||
&mut self,
|
||||
params: protocol::ThreadSetNameParams,
|
||||
output: &mpsc::UnboundedSender<crate::AppServerEvent>,
|
||||
) {
|
||||
if let Some(loaded) = &mut self.loaded
|
||||
&& loaded.thread.id == params.thread_id
|
||||
{
|
||||
loaded.thread.name = Some(params.name.clone());
|
||||
}
|
||||
send_notification(
|
||||
output,
|
||||
protocol::ServerNotification::ThreadNameUpdated(
|
||||
protocol::ThreadNameUpdatedNotification {
|
||||
thread_id: params.thread_id,
|
||||
thread_name: Some(params.name),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn read_thread(
|
||||
&self,
|
||||
params: &protocol::ThreadReadParams,
|
||||
@@ -72,23 +93,12 @@ impl Requests {
|
||||
}
|
||||
serde_json::to_value(page)?
|
||||
}
|
||||
Request::ThreadSetName { params, .. } => {
|
||||
let name = params.name.trim();
|
||||
self.client.set_thread_name(¶ms.thread_id, name).await?;
|
||||
if let Some(loaded) = &mut self.loaded
|
||||
&& loaded.thread.id == params.thread_id
|
||||
{
|
||||
loaded.thread.name = Some(name.to_owned());
|
||||
}
|
||||
send_notification(
|
||||
output,
|
||||
protocol::ServerNotification::ThreadNameUpdated(
|
||||
protocol::ThreadNameUpdatedNotification {
|
||||
thread_id: params.thread_id,
|
||||
thread_name: Some(name.to_owned()),
|
||||
},
|
||||
),
|
||||
);
|
||||
Request::ThreadSetName { mut params, .. } => {
|
||||
params.name = params.name.trim().to_owned();
|
||||
self.client
|
||||
.set_thread_name(¶ms.thread_id, ¶ms.name)
|
||||
.await?;
|
||||
self.thread_name_saved(params, output);
|
||||
serde_json::to_value(protocol::ThreadSetNameResponse {})?
|
||||
}
|
||||
Request::ThreadArchive { params, .. } => {
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
//! Generates Cloud titles with the Cloud account's routing and no agent tools.
|
||||
|
||||
use codex_api::Compression;
|
||||
use codex_api::ReqwestTransport;
|
||||
use codex_api::ResponseEvent;
|
||||
use codex_api::ResponsesClient;
|
||||
use codex_api::SharedAuthProvider;
|
||||
use codex_api::create_text_param_for_request;
|
||||
use codex_http_client::ClientRouteClass;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_http_client::RouteAwareClientPool;
|
||||
use codex_http_client::is_allowed_chatgpt_host;
|
||||
use codex_login::CodexAuth;
|
||||
use codex_login::default_client::add_originator_header;
|
||||
use codex_login::default_client::get_codex_user_agent;
|
||||
use codex_login::default_client::originator;
|
||||
use codex_model_provider::ACCOUNT_ROUTING_HEADER;
|
||||
use codex_model_provider::ChatgptBaseUrl;
|
||||
use codex_model_provider::auth_provider_from_auth;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::MessagePhase;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use color_eyre::eyre::Result;
|
||||
use color_eyre::eyre::bail;
|
||||
use color_eyre::eyre::ensure;
|
||||
use color_eyre::eyre::eyre;
|
||||
use futures::StreamExt;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
use std::time::Duration;
|
||||
use url::Url;
|
||||
|
||||
pub(crate) struct CloudTitleClient {
|
||||
factory: HttpClientFactory,
|
||||
auth: SharedAuthProvider,
|
||||
account_id: String,
|
||||
bootstrap: Url,
|
||||
require_origin_match: bool,
|
||||
}
|
||||
|
||||
impl CloudTitleClient {
|
||||
pub(super) fn new(
|
||||
factory: HttpClientFactory,
|
||||
auth: &CodexAuth,
|
||||
account_id: String,
|
||||
chatgpt_base_url: ChatgptBaseUrl,
|
||||
) -> Result<Self> {
|
||||
let require_origin_match = matches!(chatgpt_base_url, ChatgptBaseUrl::Configured(_));
|
||||
let bootstrap = Url::parse(chatgpt_base_url.resolve(auth))?;
|
||||
ensure!(
|
||||
bootstrap.scheme() == "https"
|
||||
&& bootstrap.host_str().is_some_and(is_allowed_chatgpt_host)
|
||||
&& bootstrap.username().is_empty()
|
||||
&& bootstrap.password().is_none()
|
||||
&& bootstrap.query().is_none()
|
||||
&& bootstrap.fragment().is_none(),
|
||||
"invalid Cloud title routing bootstrap"
|
||||
);
|
||||
Ok(Self {
|
||||
factory,
|
||||
auth: auth_provider_from_auth(auth),
|
||||
account_id,
|
||||
bootstrap,
|
||||
require_origin_match,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn generate(
|
||||
&self,
|
||||
model: &str,
|
||||
effort: Option<ReasoningEffort>,
|
||||
prompt: String,
|
||||
schema: Value,
|
||||
) -> Result<String> {
|
||||
tokio::time::timeout(Duration::from_secs(/*secs*/ 30), async {
|
||||
let accounts = codex_backend_client::Client::new_without_redirects(
|
||||
self.bootstrap.as_str(),
|
||||
self.factory.clone(),
|
||||
)
|
||||
.with_auth_provider(self.auth.clone())
|
||||
.map_err(|_| eyre!("could not configure Cloud title authentication"))?
|
||||
.with_user_agent(get_codex_user_agent())
|
||||
.get_accounts_check()
|
||||
.await
|
||||
.map_err(|_| eyre!("Cloud title routing is unavailable"))?;
|
||||
let mut selected_accounts = accounts
|
||||
.accounts
|
||||
.into_iter()
|
||||
.filter(|account| account.id == self.account_id);
|
||||
let account = selected_accounts
|
||||
.next()
|
||||
.filter(|account| {
|
||||
!account.is_deactivated
|
||||
&& !account.is_fedramp_compliant_workspace
|
||||
&& selected_accounts.next().is_none()
|
||||
})
|
||||
.ok_or_else(|| eyre!("Cloud title account is unavailable"))?;
|
||||
let origin = match account.workspace_backend_origin.as_deref() {
|
||||
Some("NO_CONSTRAINT") => self.bootstrap.origin(),
|
||||
Some(value) => {
|
||||
let origin = Url::parse(value)?;
|
||||
ensure!(
|
||||
origin.scheme() == "https"
|
||||
&& origin.host_str().is_some_and(is_allowed_chatgpt_host)
|
||||
&& origin.username().is_empty()
|
||||
&& origin.password().is_none()
|
||||
&& origin.path() == "/"
|
||||
&& origin.query().is_none()
|
||||
&& origin.fragment().is_none()
|
||||
&& (!self.require_origin_match
|
||||
|| self.bootstrap.origin() == origin.origin()),
|
||||
"invalid Cloud title backend origin"
|
||||
);
|
||||
origin.origin()
|
||||
}
|
||||
None => return Err(eyre!("Cloud title backend origin is unavailable")),
|
||||
};
|
||||
let path = Url::parse(codex_model_provider::CHATGPT_CODEX_BASE_URL)?;
|
||||
let base_url = format!("{}{}", origin.ascii_serialization(), path.path());
|
||||
let routing_header = match account.account_routing_override.as_deref() {
|
||||
Some("NO_CONSTRAINT") => None,
|
||||
Some("us") => Some("us"),
|
||||
Some("us_cr") => Some("us_cr"),
|
||||
_ => return Err(eyre!("invalid Cloud title routing override")),
|
||||
};
|
||||
let mut provider = codex_api::Provider {
|
||||
name: "OpenAI".to_string(),
|
||||
base_url,
|
||||
query_params: None,
|
||||
headers: Default::default(),
|
||||
retry: codex_api::RetryConfig {
|
||||
max_attempts: 0,
|
||||
base_delay: Duration::ZERO,
|
||||
retry_429: false,
|
||||
retry_5xx: false,
|
||||
retry_transport: false,
|
||||
},
|
||||
stream_idle_timeout: Duration::from_secs(/*secs*/ 30),
|
||||
};
|
||||
if let Some(value) = routing_header {
|
||||
provider.headers.insert(ACCOUNT_ROUTING_HEADER, value.parse()?);
|
||||
}
|
||||
provider.headers.insert("user-agent", get_codex_user_agent().parse()?);
|
||||
add_originator_header(&mut provider.headers, &originator().value);
|
||||
let client = RouteAwareClientPool::with_chatgpt_cloudflare_cookies_without_redirects_or_request_logging(
|
||||
self.factory.clone(),
|
||||
ClientRouteClass::Api,
|
||||
).into_client();
|
||||
let client =
|
||||
ResponsesClient::new(ReqwestTransport::from_http_client(client), provider, self.auth.clone());
|
||||
let mut body = json!({
|
||||
"model": model,
|
||||
"instructions": "Generate a thread title from the user prompt.",
|
||||
"input": [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": prompt}]}],
|
||||
"store": false,
|
||||
"stream": true,
|
||||
"tools": [],
|
||||
"tool_choice": "none",
|
||||
"parallel_tool_calls": false,
|
||||
"include": [],
|
||||
"client_metadata": {"x-codex-turn-metadata": r#"{"thread_source":"thread_title"}"#},
|
||||
"text": create_text_param_for_request(/*verbosity*/ None, &Some(schema), /*output_schema_strict*/ true),
|
||||
});
|
||||
if let Some(effort) = effort {
|
||||
body["reasoning"] = json!({"effort": effort});
|
||||
}
|
||||
let mut stream = client
|
||||
.stream(body, Default::default(), Compression::None, /*turn_state*/ None)
|
||||
.await
|
||||
.map_err(|_| eyre!("Cloud title request failed"))?;
|
||||
let mut title = None;
|
||||
while let Some(event) = stream.next().await {
|
||||
match event.map_err(|_| eyre!("Cloud title stream failed"))? {
|
||||
ResponseEvent::OutputItemDone(ResponseItem::Message {
|
||||
role,
|
||||
content,
|
||||
phase,
|
||||
..
|
||||
}) if role == "assistant" && phase != Some(MessagePhase::Commentary) => {
|
||||
let mut text = String::new();
|
||||
for item in content {
|
||||
if let ContentItem::OutputText { text: part, .. } = item {
|
||||
ensure!(text.len() + part.len() <= 8 * 1024, "Cloud title is too large");
|
||||
text.push_str(&part);
|
||||
}
|
||||
}
|
||||
title = Some(text);
|
||||
}
|
||||
ResponseEvent::Completed { .. } => {
|
||||
return title.ok_or_else(|| eyre!("Cloud title response is empty"));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
bail!("Cloud title stream ended before completion");
|
||||
})
|
||||
.await?
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "thread_title_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,102 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use super::CloudTitleClient;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_http_client::OutboundProxyPolicy;
|
||||
use codex_model_provider::BearerAuthProvider;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
use wiremock::Mock;
|
||||
use wiremock::MockServer;
|
||||
use wiremock::ResponseTemplate;
|
||||
use wiremock::matchers::body_json;
|
||||
use wiremock::matchers::header;
|
||||
use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
|
||||
#[tokio::test]
|
||||
async fn generates_a_structured_title_using_the_selected_cloud_account() -> color_eyre::Result<()> {
|
||||
let server = MockServer::start().await;
|
||||
// Inject an already validated bootstrap so the test can use a loopback server.
|
||||
let client = CloudTitleClient {
|
||||
factory: HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||
auth: Arc::new(BearerAuthProvider::for_test(
|
||||
Some("token"),
|
||||
Some("cloud-account"),
|
||||
)),
|
||||
account_id: "cloud-account".to_owned(),
|
||||
bootstrap: format!("{}/backend-api", server.uri()).parse()?,
|
||||
require_origin_match: false,
|
||||
};
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/backend-api/wham/accounts/check"))
|
||||
.and(header("authorization", "Bearer token"))
|
||||
.and(header("chatgpt-account-id", "cloud-account"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(json!({"accounts": [
|
||||
{"id": "other-account", "workspace_backend_origin": "https://other.chatgpt.com", "account_routing_override": "us"},
|
||||
{"id": "cloud-account", "workspace_backend_origin": "NO_CONSTRAINT", "account_routing_override": "us_cr"}
|
||||
]})))
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let schema = json!({
|
||||
"type": "object",
|
||||
"properties": {"title": {"type": "string"}},
|
||||
"required": ["title"],
|
||||
"additionalProperties": false,
|
||||
});
|
||||
let title = r#"{"title":"Login timeout"}"#;
|
||||
let sse = [
|
||||
json!({"type": "response.output_item.done", "item": {
|
||||
"type": "message", "role": "assistant", "phase": "commentary",
|
||||
"content": [{"type": "output_text", "text": "Considering a title"}]
|
||||
}}),
|
||||
json!({"type": "response.output_item.done", "item": {
|
||||
"type": "message", "role": "assistant", "phase": "final_answer",
|
||||
"content": [{"type": "output_text", "text": title}]
|
||||
}}),
|
||||
json!({"type": "response.completed", "response": {"id": "response-1"}}),
|
||||
]
|
||||
.into_iter()
|
||||
.map(|event| format!("data: {event}\n\n"))
|
||||
.collect::<String>();
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/backend-api/codex/responses"))
|
||||
.and(header("authorization", "Bearer token"))
|
||||
.and(header("chatgpt-account-id", "cloud-account"))
|
||||
.and(header("x-openai-account-routing-override", "us_cr"))
|
||||
.and(body_json(json!({
|
||||
"model": "title-model",
|
||||
"instructions": "Generate a thread title from the user prompt.",
|
||||
"input": [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "Fix the login timeout"}]}],
|
||||
"store": false,
|
||||
"stream": true,
|
||||
"tools": [],
|
||||
"tool_choice": "none",
|
||||
"parallel_tool_calls": false,
|
||||
"include": [],
|
||||
"client_metadata": {"x-codex-turn-metadata": r#"{"thread_source":"thread_title"}"#},
|
||||
"text": {"format": {"type": "json_schema", "strict": true, "schema": schema, "name": "codex_output_schema"}},
|
||||
"reasoning": {"effort": "low"},
|
||||
})))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_raw(sse, "text/event-stream"))
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
client
|
||||
.generate(
|
||||
"title-model",
|
||||
Some(ReasoningEffort::Low),
|
||||
"Fix the login timeout".to_owned(),
|
||||
schema,
|
||||
)
|
||||
.await?,
|
||||
title
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -153,8 +153,11 @@ async fn native_model_list_bypasses_the_cloud_command_queue() -> color_eyre::Res
|
||||
.await;
|
||||
|
||||
// Leave the command receiver idle. The catalog must finish without its worker.
|
||||
let (client, mut commands, _events) =
|
||||
CloudClient::channel_with_catalog(/*capacity*/ 1, Some(native));
|
||||
let (client, mut commands, _events) = CloudClient::channel_with_native(
|
||||
/*capacity*/ 1,
|
||||
Some(native),
|
||||
/*title_client*/ None,
|
||||
);
|
||||
let handle = AppServerClient::Cloud(client).request_handle();
|
||||
let result = tokio::time::timeout(
|
||||
std::time::Duration::from_secs(/*secs*/ 5),
|
||||
|
||||
@@ -159,6 +159,25 @@ async fn rename_sends_the_thread_and_title_and_requires_acknowledgment() {
|
||||
assert!(fixture.requests.try_recv().is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reads_a_saved_name_without_attaching_to_the_thread() {
|
||||
let mut fixture = fixture_responses(vec![
|
||||
response(
|
||||
vec![frame(b"\x0a\x09\x72\x01t\x8a\x01\x03New")],
|
||||
tonic::Status::ok(""),
|
||||
),
|
||||
response(vec![frame(b"\x0a\x03\x72\x01u")], tonic::Status::ok("")),
|
||||
])
|
||||
.await;
|
||||
assert_eq!(
|
||||
fixture.client.read_thread_name("t").await.unwrap(),
|
||||
Some("New".to_string())
|
||||
);
|
||||
assert_request(&mut fixture, "ReadThread", b"\x0a\x01t").await;
|
||||
assert_eq!(fixture.client.read_thread_name("u").await.unwrap(), None);
|
||||
assert_request(&mut fixture, "ReadThread", b"\x0a\x01u").await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn activation_preserves_native_settings_and_thread_metadata() {
|
||||
// Native Thread fields encoded independently of the client's wire types.
|
||||
|
||||
@@ -298,6 +298,24 @@ impl Client {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reads the saved name without opening or replacing an attachment.
|
||||
pub async fn read_thread_name(&self, thread_id: &str) -> Result<Option<String>, Error> {
|
||||
let payload = self
|
||||
.raw_unary(
|
||||
wire::ThreadIdRequest {
|
||||
thread_id: thread_id.to_owned(),
|
||||
},
|
||||
"/oaiproto.codex_app_server_backend.ThreadService/ReadThread",
|
||||
)
|
||||
.await?;
|
||||
let thread = wire::ReadThreadResponse::decode(payload)
|
||||
.map_err(|_| Error::InvalidResponse)?
|
||||
.thread
|
||||
.filter(|thread| thread.id.as_deref() == Some(thread_id))
|
||||
.ok_or(Error::InvalidResponse)?;
|
||||
Ok(thread.name)
|
||||
}
|
||||
|
||||
/// Saves a conversation name without opening an attachment.
|
||||
pub async fn set_thread_name(&self, thread_id: &str, name: &str) -> Result<(), MutationError> {
|
||||
if thread_id.trim().is_empty() || name.trim().is_empty() {
|
||||
|
||||
@@ -24,6 +24,27 @@ pub(crate) struct SetThreadNameRequest {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, prost::Message)]
|
||||
pub(crate) struct ReadThreadResponse {
|
||||
#[prost(message, optional, tag = "1")]
|
||||
pub thread: Option<ThreadName>,
|
||||
}
|
||||
|
||||
#[derive(Clone, prost::Message)]
|
||||
#[prost(skip_debug)]
|
||||
pub(crate) struct ThreadName {
|
||||
#[prost(string, optional, tag = "14")]
|
||||
pub id: Option<String>,
|
||||
#[prost(string, optional, tag = "17")]
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ThreadName {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("ThreadName([redacted])")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, prost::Message)]
|
||||
pub(crate) struct UnarchiveResponse {
|
||||
#[prost(bytes = "bytes", optional, tag = "1")]
|
||||
|
||||
@@ -669,6 +669,8 @@ pub(crate) struct App {
|
||||
),
|
||||
>,
|
||||
/// Track title generation across thread switches and deduplicate automatic requests.
|
||||
/// Each pending Cloud title holds a receiver until its name has been saved.
|
||||
cloud_title_shutdown: tokio::sync::watch::Sender<()>,
|
||||
pending_thread_titles: HashMap<(ThreadId, ThreadTitleDestination), CancellationToken>,
|
||||
thread_event_listener_tasks: HashMap<ThreadId, JoinHandle<()>>,
|
||||
agent_navigation: AgentNavigationState,
|
||||
|
||||
@@ -3431,8 +3431,9 @@ impl App {
|
||||
destination,
|
||||
result,
|
||||
} => {
|
||||
self.temporary_structured_requests
|
||||
.remove(&temporary_thread_id);
|
||||
if let Some(temporary_thread_id) = temporary_thread_id {
|
||||
self.temporary_structured_requests.remove(&temporary_thread_id);
|
||||
}
|
||||
|
||||
if cancellation.is_cancelled() {
|
||||
return Ok(AppRunControl::Continue);
|
||||
|
||||
@@ -1067,6 +1067,7 @@ See the Codex keymap documentation for supported actions and examples."
|
||||
background_voice: None,
|
||||
background_voice_error: None,
|
||||
temporary_structured_requests: HashMap::new(),
|
||||
cloud_title_shutdown: Default::default(),
|
||||
pending_thread_titles: HashMap::new(),
|
||||
thread_event_listener_tasks: HashMap::new(),
|
||||
agent_navigation: AgentNavigationState::default(),
|
||||
@@ -1805,6 +1806,18 @@ See the Codex keymap documentation for supported actions and examples."
|
||||
};
|
||||
// Let the pending terminal poll unwind while the app server shuts down.
|
||||
tui.pause_events();
|
||||
// Skip pending inference on exit. Give the fallback writes a bounded chance
|
||||
// to finish before closing the connections they use.
|
||||
if app.cloud_title_shutdown.send(()).is_ok()
|
||||
&& tokio::time::timeout(Duration::from_secs(3), app.cloud_title_shutdown.closed())
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
tracing::debug!("timed out saving pending Cloud titles during shutdown");
|
||||
}
|
||||
for cancellation in app.pending_thread_titles.values() {
|
||||
cancellation.cancel();
|
||||
}
|
||||
app.chat_widget.mention_usage_mut().flush().await;
|
||||
if let Err(err) = backends.shutdown().await {
|
||||
tracing::warn!(error = %err, "failed to shut down embedded app server");
|
||||
|
||||
@@ -124,6 +124,7 @@ pub(crate) async fn make_test_app() -> Box<App> {
|
||||
background_voice: None,
|
||||
background_voice_error: None,
|
||||
temporary_structured_requests: HashMap::new(),
|
||||
cloud_title_shutdown: Default::default(),
|
||||
pending_thread_titles: HashMap::new(),
|
||||
thread_event_listener_tasks: HashMap::new(),
|
||||
agent_navigation: AgentNavigationState::default(),
|
||||
|
||||
@@ -9173,6 +9173,7 @@ async fn make_test_app() -> Box<App> {
|
||||
background_voice: None,
|
||||
background_voice_error: None,
|
||||
temporary_structured_requests: HashMap::new(),
|
||||
cloud_title_shutdown: Default::default(),
|
||||
pending_thread_titles: HashMap::new(),
|
||||
thread_event_listener_tasks: HashMap::new(),
|
||||
agent_navigation: AgentNavigationState::default(),
|
||||
@@ -9315,6 +9316,7 @@ pub(super) async fn make_test_app_with_channels() -> (
|
||||
background_voice: None,
|
||||
background_voice_error: None,
|
||||
temporary_structured_requests: HashMap::new(),
|
||||
cloud_title_shutdown: Default::default(),
|
||||
pending_thread_titles: HashMap::new(),
|
||||
thread_event_listener_tasks: HashMap::new(),
|
||||
agent_navigation: AgentNavigationState::default(),
|
||||
|
||||
@@ -877,7 +877,11 @@ impl App {
|
||||
}
|
||||
AppCommand::SetThreadName { name } => {
|
||||
let name = name.to_string();
|
||||
app_server.thread_set_name(thread_id, name.clone()).await?;
|
||||
if let Err(error) = app_server.thread_set_name(thread_id, name.clone()).await {
|
||||
self.chat_widget
|
||||
.add_error_message(format!("Failed to rename thread: {error}"));
|
||||
return Ok(true);
|
||||
}
|
||||
self.chat_widget.display_thread_name(thread_id, name);
|
||||
self.cancel_thread_title_generation(thread_id);
|
||||
Ok(true)
|
||||
|
||||
@@ -9,12 +9,12 @@ use super::thread_events::ThreadBufferedEvent;
|
||||
use crate::app_event::AppEvent;
|
||||
use crate::app_event::ThreadTitleDestination;
|
||||
use crate::app_server_session::AppServerSession;
|
||||
use crate::app_server_session::ThreadSettingsOwner;
|
||||
use crate::temporary_structured_request::TemporaryStructuredThreadOptions;
|
||||
use crate::temporary_structured_request::TemporaryThreadKind;
|
||||
use crate::temporary_structured_request::run_temporary_structured_turn;
|
||||
use crate::temporary_structured_request::start_temporary_thread;
|
||||
use crate::temporary_structured_request::unsubscribe_temporary_thread;
|
||||
use codex_app_server_client::AppServerRequestHandle;
|
||||
use codex_app_server_protocol::ServerNotification;
|
||||
use codex_app_server_protocol::ThreadItemPayload;
|
||||
use codex_app_server_protocol::Turn;
|
||||
@@ -63,20 +63,21 @@ impl App {
|
||||
});
|
||||
}
|
||||
|
||||
/// Start a hidden title-generation thread without blocking the UI loop.
|
||||
/// Generate a title without blocking the UI loop.
|
||||
pub(super) fn generate_thread_title(
|
||||
&mut self,
|
||||
app_server: &AppServerSession,
|
||||
app_server: &mut AppServerSession,
|
||||
thread_id: ThreadId,
|
||||
destination: ThreadTitleDestination,
|
||||
source_text: &str,
|
||||
expected_name: Option<String>,
|
||||
) {
|
||||
if app_server.thread_params_mode().settings_owner() == ThreadSettingsOwner::Server {
|
||||
if let ThreadTitleDestination::RenameSuggestion { request_id } = destination {
|
||||
self.chat_widget
|
||||
.apply_thread_name_suggestion(thread_id, request_id, /*suggestion*/ None);
|
||||
}
|
||||
let request_handle = app_server.request_handle();
|
||||
if let AppServerRequestHandle::Cloud(_) = &request_handle
|
||||
&& let ThreadTitleDestination::RenameSuggestion { request_id } = destination
|
||||
{
|
||||
self.chat_widget
|
||||
.apply_thread_name_suggestion(thread_id, request_id, /*suggestion*/ None);
|
||||
return;
|
||||
}
|
||||
let std::collections::hash_map::Entry::Vacant(entry) =
|
||||
@@ -100,8 +101,8 @@ impl App {
|
||||
(recent_conversation_thread_title_prompt(source_text), None)
|
||||
}
|
||||
};
|
||||
let request_handle = app_server.request_handle();
|
||||
let model = if self.chat_widget.config_ref().model_provider_id == "openai"
|
||||
let model = if (matches!(&request_handle, AppServerRequestHandle::Cloud(_))
|
||||
|| self.chat_widget.config_ref().model_provider_id == "openai")
|
||||
&& self.chat_widget.has_chatgpt_account()
|
||||
&& self
|
||||
.chat_widget
|
||||
@@ -114,6 +115,35 @@ impl App {
|
||||
self.chat_widget.current_model().to_string()
|
||||
};
|
||||
let effort = (model == THREAD_TITLE_MODEL).then_some(ReasoningEffort::Low);
|
||||
let event_sender = self.app_event_tx.clone();
|
||||
if let AppServerRequestHandle::Cloud(handle) = request_handle {
|
||||
let writer = app_server.automatic_thread_name_writer(thread_id, expected_name);
|
||||
let mut shutdown = self.cloud_title_shutdown.subscribe();
|
||||
tokio::spawn(async move {
|
||||
let result = tokio::select! {
|
||||
biased;
|
||||
_ = cancellation.cancelled() => return,
|
||||
_ = shutdown.changed() => Err(color_eyre::eyre::eyre!("shutting down")),
|
||||
result = handle.generate_thread_title(&model, effort, prompt, thread_title_output_schema()) => result,
|
||||
};
|
||||
if let Some(title) = result
|
||||
.as_deref()
|
||||
.ok()
|
||||
.and_then(parse_thread_title)
|
||||
.or(fallback_title)
|
||||
{
|
||||
writer.save(title, &cancellation).await;
|
||||
}
|
||||
event_sender.send(AppEvent::GeneratedThreadTitle {
|
||||
cancellation,
|
||||
thread_id,
|
||||
temporary_thread_id: None,
|
||||
destination,
|
||||
result: result.map_err(|error| error.to_string()),
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
let config = self.chat_widget.config_ref();
|
||||
let options = TemporaryStructuredThreadOptions {
|
||||
kind: TemporaryThreadKind::Title,
|
||||
@@ -126,7 +156,6 @@ impl App {
|
||||
.map(|profile| profile.id),
|
||||
};
|
||||
|
||||
let event_sender = self.app_event_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
let result = start_temporary_thread(&request_handle, options)
|
||||
.await
|
||||
@@ -223,7 +252,7 @@ impl App {
|
||||
event_sender.send(AppEvent::GeneratedThreadTitle {
|
||||
cancellation,
|
||||
thread_id,
|
||||
temporary_thread_id,
|
||||
temporary_thread_id: Some(temporary_thread_id),
|
||||
destination,
|
||||
result,
|
||||
});
|
||||
@@ -233,7 +262,7 @@ impl App {
|
||||
/// Suggest a title from stored and live conversation items without persisting it.
|
||||
pub(super) async fn suggest_thread_name(
|
||||
&mut self,
|
||||
app_server: &AppServerSession,
|
||||
app_server: &mut AppServerSession,
|
||||
thread_id: ThreadId,
|
||||
request_id: uuid::Uuid,
|
||||
) {
|
||||
|
||||
@@ -93,27 +93,89 @@ fn bounds_the_entire_title_prompt_for_dense_unicode() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cloud_conversations_skip_tui_generated_titles() {
|
||||
async fn cloud_conversations_save_the_first_message_if_generation_is_unavailable()
|
||||
-> color_eyre::Result<()> {
|
||||
use codex_app_server_client::CloudOperation;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
|
||||
let mut app = make_test_app().await;
|
||||
let thread_id = ThreadId::new();
|
||||
let (client, mut commands, _server_events) =
|
||||
codex_app_server_client::CloudClient::channel(/*capacity*/ 1);
|
||||
let app_server = crate::app_server_session::AppServerSession::new(
|
||||
let mut app_server = crate::app_server_session::AppServerSession::new(
|
||||
crate::backend_id::BackendId::new(),
|
||||
codex_app_server_client::AppServerClient::Cloud(client),
|
||||
crate::app_server_session::ThreadParamsMode::Cloud,
|
||||
);
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let (tx, mut events) = unbounded_channel();
|
||||
app.app_event_tx = AppEventSender::new(tx);
|
||||
app.active_thread_id = Some(thread_id);
|
||||
app.chat_widget
|
||||
.handle_thread_session(crate::app::tests::test_thread_session(
|
||||
thread_id,
|
||||
app.config.cwd.to_path_buf(),
|
||||
));
|
||||
app.chat_widget
|
||||
.setup_terminal_title(vec![crate::bottom_pane::TerminalTitleItem::ThreadName]);
|
||||
|
||||
app.generate_thread_title(
|
||||
&app_server,
|
||||
thread_id,
|
||||
ThreadTitleDestination::Automatic,
|
||||
"Fix the login timeout",
|
||||
/*expected_name*/ None,
|
||||
app.handle_active_thread_event(
|
||||
&mut tui,
|
||||
&mut app_server,
|
||||
ThreadBufferedEvent::Notification(Box::new(ServerNotification::ItemCompleted(
|
||||
ItemCompletedNotification::new(
|
||||
thread_id.to_string(),
|
||||
"first-turn".to_string(),
|
||||
title_user_message("first-message", "Fix the **login** timeout"),
|
||||
/*started_at_ms*/ None,
|
||||
/*completed_at_ms*/ 0,
|
||||
),
|
||||
))),
|
||||
)
|
||||
.await?;
|
||||
insta::assert_snapshot!(
|
||||
app.chat_widget.last_terminal_title.as_deref().unwrap_or_default(),
|
||||
@"Fix the login timeout"
|
||||
);
|
||||
let read = commands.recv().await.expect("read the saved Cloud title");
|
||||
assert!(matches!(
|
||||
read.operation,
|
||||
CloudOperation::Request(request)
|
||||
if matches!(request.as_ref(), ClientRequest::ThreadRead { params, .. }
|
||||
if params.thread_id == thread_id.to_string() && !params.include_turns)
|
||||
));
|
||||
let mut thread = crate::app::agents_overview::tests::overview_thread(
|
||||
thread_id,
|
||||
/*parent_thread_id*/ None,
|
||||
"",
|
||||
codex_app_server_protocol::ThreadStatus::Idle,
|
||||
);
|
||||
thread.name = None;
|
||||
read.response
|
||||
.send(Ok(Ok(serde_json::json!({"thread": thread}))))
|
||||
.unwrap();
|
||||
let rename = commands.recv().await.expect("save the Cloud title");
|
||||
assert!(matches!(
|
||||
rename.operation,
|
||||
CloudOperation::Request(request)
|
||||
if matches!(request.as_ref(), ClientRequest::ThreadSetName { params, .. }
|
||||
if params.thread_id == thread_id.to_string() && params.name == "Fix the login timeout")
|
||||
));
|
||||
app.cloud_title_shutdown.send(()).unwrap();
|
||||
assert!(app.cloud_title_shutdown.closed().now_or_never().is_none());
|
||||
rename.response.send(Ok(Ok(serde_json::json!({})))).unwrap();
|
||||
app.cloud_title_shutdown.closed().await;
|
||||
loop {
|
||||
let event = events.recv().await.expect("title generation completes");
|
||||
if matches!(&event, AppEvent::GeneratedThreadTitle { .. }) {
|
||||
app.handle_event(&mut tui, &mut app_server, event).await?;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert!(app.pending_thread_titles.is_empty());
|
||||
assert!(commands.try_recv().is_err());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -178,7 +240,7 @@ async fn automatic_thread_title_respects_origin_metadata_after_switching() -> co
|
||||
AppEvent::GeneratedThreadTitle {
|
||||
cancellation: CancellationToken::new(),
|
||||
thread_id,
|
||||
temporary_thread_id: codex_protocol::ThreadId::new(),
|
||||
temporary_thread_id: Some(ThreadId::new()),
|
||||
destination: ThreadTitleDestination::Automatic,
|
||||
result: Ok(r#"{"title":"Generated title"}"#.to_string()),
|
||||
},
|
||||
@@ -581,7 +643,7 @@ async fn canceled_thread_title_ignores_late_start_and_completion() -> color_eyre
|
||||
AppEvent::GeneratedThreadTitle {
|
||||
cancellation,
|
||||
thread_id,
|
||||
temporary_thread_id,
|
||||
temporary_thread_id: Some(temporary_thread_id),
|
||||
destination,
|
||||
result: Ok(r#"{"title":"Late automatic title"}"#.to_string()),
|
||||
},
|
||||
@@ -604,7 +666,8 @@ async fn canceled_thread_title_ignores_late_start_and_completion() -> color_eyre
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn manual_rename_waits_for_an_in_flight_automatic_write() -> color_eyre::Result<()> {
|
||||
async fn cloud_manual_rename_can_retry_after_an_in_flight_automatic_write() -> color_eyre::Result<()>
|
||||
{
|
||||
let thread_id = ThreadId::new();
|
||||
let (client, mut requests, _events) =
|
||||
codex_app_server_client::CloudClient::channel(/*capacity*/ 1);
|
||||
@@ -632,8 +695,15 @@ async fn manual_rename_waits_for_an_in_flight_automatic_write() -> color_eyre::R
|
||||
assert!(automatic.as_mut().now_or_never().is_none());
|
||||
let write = requests.try_recv()?;
|
||||
|
||||
let mut manual = std::pin::pin!(session.thread_set_name(thread_id, "Manual".into()));
|
||||
assert!(manual.as_mut().now_or_never().is_none());
|
||||
let error = session
|
||||
.thread_set_name(thread_id, "Manual".into())
|
||||
.now_or_never()
|
||||
.expect("a pending automatic Cloud write must not block the UI")
|
||||
.expect_err("manual rename must report that the write is still in flight");
|
||||
assert_eq!(
|
||||
error.to_string(),
|
||||
"An automatic title is still being saved. Try renaming the thread again."
|
||||
);
|
||||
assert!(
|
||||
requests.try_recv().is_err(),
|
||||
"manual rename raced the automatic write"
|
||||
@@ -648,6 +718,8 @@ async fn manual_rename_waits_for_an_in_flight_automatic_write() -> color_eyre::R
|
||||
automatic
|
||||
.now_or_never()
|
||||
.expect("automatic write acknowledged");
|
||||
|
||||
let mut manual = std::pin::pin!(session.thread_set_name(thread_id, "Manual".into()));
|
||||
assert!(manual.as_mut().now_or_never().is_none());
|
||||
acknowledge(requests.try_recv()?);
|
||||
manual.await?;
|
||||
@@ -955,7 +1027,7 @@ async fn failed_thread_title_requests_are_removed() -> color_eyre::Result<()> {
|
||||
AppEvent::GeneratedThreadTitle {
|
||||
cancellation: CancellationToken::new(),
|
||||
thread_id,
|
||||
temporary_thread_id: codex_protocol::ThreadId::new(),
|
||||
temporary_thread_id: Some(ThreadId::new()),
|
||||
destination: ThreadTitleDestination::Automatic,
|
||||
result: Err("generation failed".to_string()),
|
||||
},
|
||||
|
||||
@@ -449,11 +449,11 @@ pub(crate) enum AppEvent {
|
||||
effort: Option<ReasoningEffort>,
|
||||
result: Result<String, String>,
|
||||
},
|
||||
/// Route a hidden title request to its automatic rename or editable prompt.
|
||||
/// Finish title generation and deliver an editable suggestion when requested.
|
||||
GeneratedThreadTitle {
|
||||
cancellation: CancellationToken,
|
||||
thread_id: ThreadId,
|
||||
temporary_thread_id: ThreadId,
|
||||
temporary_thread_id: Option<ThreadId>,
|
||||
destination: ThreadTitleDestination,
|
||||
result: Result<String, String>,
|
||||
},
|
||||
|
||||
@@ -439,21 +439,45 @@ pub(crate) struct AutomaticThreadName {
|
||||
|
||||
impl AutomaticThreadName {
|
||||
pub(crate) async fn save(mut self, title: String, cancellation: &CancellationToken) {
|
||||
let read = self
|
||||
.session
|
||||
.thread_read(self.thread_id, /*include_turns*/ false);
|
||||
let Some(Ok(thread)) = cancellation.run_until_cancelled(read).await else {
|
||||
let read = async {
|
||||
if let AppServerRequestHandle::Cloud(handle) = &self.session.request_handle
|
||||
&& let Some(result) = handle
|
||||
.read_native_thread_name(&self.thread_id.to_string())
|
||||
.await
|
||||
{
|
||||
return Ok(result?);
|
||||
}
|
||||
let thread = self
|
||||
.session
|
||||
.thread_read(self.thread_id, /*include_turns*/ false)
|
||||
.await?;
|
||||
Ok::<_, color_eyre::Report>(thread.name)
|
||||
};
|
||||
let Some(Ok(saved_name)) = cancellation.run_until_cancelled(read).await else {
|
||||
return;
|
||||
};
|
||||
if thread.name != self.expected_name {
|
||||
if saved_name != self.expected_name {
|
||||
return;
|
||||
}
|
||||
let Ok(_permit) = self.gate.acquire().await else {
|
||||
return;
|
||||
};
|
||||
if !cancellation.is_cancelled()
|
||||
&& let Err(error) = self.session.thread_set_name(self.thread_id, title).await
|
||||
if cancellation.is_cancelled() {
|
||||
return;
|
||||
}
|
||||
let result = if let AppServerRequestHandle::Cloud(handle) = &self.session.request_handle
|
||||
&& let Some(result) = handle
|
||||
.save_native_thread_name(ThreadSetNameParams {
|
||||
thread_id: self.thread_id.to_string(),
|
||||
name: title.clone(),
|
||||
})
|
||||
.await
|
||||
{
|
||||
result
|
||||
} else {
|
||||
self.session.thread_set_name(self.thread_id, title).await
|
||||
};
|
||||
if let Err(error) = result {
|
||||
tracing::debug!(%error, "failed to apply generated thread title");
|
||||
}
|
||||
}
|
||||
@@ -1989,6 +2013,12 @@ impl AppServerSession {
|
||||
) -> Result<()> {
|
||||
let gate = self.automatic_name_writes.get(&thread_id).cloned();
|
||||
let _permit = match &gate {
|
||||
// The UI awaits manual renames. Never hold it up behind a native Cloud RPC.
|
||||
Some(gate) if matches!(&self.request_handle, AppServerRequestHandle::Cloud(_)) => {
|
||||
Some(gate.try_acquire().wrap_err(
|
||||
"An automatic title is still being saved. Try renaming the thread again.",
|
||||
)?)
|
||||
}
|
||||
Some(gate) => Some(gate.acquire().await?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user