mirror of
https://github.com/openai/codex.git
synced 2026-12-01 00:20:48 +00:00
putting git_info in sessionconfigureevent
This commit is contained in:
@@ -899,9 +899,11 @@ async fn submission_loop(
|
||||
}
|
||||
}
|
||||
|
||||
// Gather history metadata for SessionConfiguredEvent.
|
||||
let (history_log_id, history_entry_count) =
|
||||
crate::message_history::history_metadata(&config).await;
|
||||
// Gather history metadata for SessionConfiguredEvent and await git info.
|
||||
let git_info_fut = crate::git_info::collect_git_info(&sess.as_ref().unwrap().cwd);
|
||||
let history_fut = crate::message_history::history_metadata(&config);
|
||||
let (git_info, (history_log_id, history_entry_count)) =
|
||||
tokio::join!(git_info_fut, history_fut);
|
||||
|
||||
// ack
|
||||
let events = std::iter::once(Event {
|
||||
@@ -911,6 +913,7 @@ async fn submission_loop(
|
||||
model,
|
||||
history_log_id,
|
||||
history_entry_count,
|
||||
git_info,
|
||||
}),
|
||||
})
|
||||
.chain(mcp_connection_errors.into_iter());
|
||||
|
||||
@@ -6,8 +6,12 @@ use tokio::process::Command;
|
||||
use tokio::time::Duration as TokioDuration;
|
||||
use tokio::time::timeout;
|
||||
|
||||
/// Timeout for git commands to prevent freezing on large repositories
|
||||
const GIT_COMMAND_TIMEOUT: TokioDuration = TokioDuration::from_secs(5);
|
||||
/// Timeout for git commands to prevent freezing on large repositories.
|
||||
///
|
||||
/// Tests that wait for the initial `SessionConfigured` event use a short
|
||||
/// timeout (~1s). Collecting Git info is best-effort and must not block the
|
||||
/// session handshake, so we cap individual Git calls to a small value.
|
||||
const GIT_COMMAND_TIMEOUT: TokioDuration = TokioDuration::from_millis(400);
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct GitInfo {
|
||||
@@ -83,36 +87,6 @@ pub async fn collect_git_info(cwd: &Path) -> Option<GitInfo> {
|
||||
Some(git_info)
|
||||
}
|
||||
|
||||
/// Blocking convenience wrapper around `collect_git_info` suitable for
|
||||
/// synchronous contexts. If called from within an active Tokio runtime,
|
||||
/// spawns a separate thread to avoid blocking the runtime's executor thread.
|
||||
pub fn collect_git_info_blocking(cwd: &Path) -> Option<GitInfo> {
|
||||
// If we're already on a Tokio runtime, avoid `block_on` on the same thread.
|
||||
if tokio::runtime::Handle::try_current().is_ok() {
|
||||
let cwd = cwd.to_path_buf();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
// Use a lightweight current-thread runtime.
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build();
|
||||
let res = match rt {
|
||||
Ok(rt) => rt.block_on(collect_git_info(&cwd)),
|
||||
Err(_) => None,
|
||||
};
|
||||
let _ = tx.send(res);
|
||||
});
|
||||
rx.recv().ok().flatten()
|
||||
} else {
|
||||
// Safe to block here – we're not inside a runtime.
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.ok()?;
|
||||
rt.block_on(collect_git_info(cwd))
|
||||
}
|
||||
}
|
||||
|
||||
/// Run a git command with a timeout to prevent blocking on large repositories
|
||||
async fn run_git_command_with_timeout(args: &[&str], cwd: &Path) -> Option<std::process::Output> {
|
||||
let result = timeout(
|
||||
|
||||
@@ -19,6 +19,7 @@ use uuid::Uuid;
|
||||
|
||||
use crate::config_types::ReasoningEffort as ReasoningEffortConfig;
|
||||
use crate::config_types::ReasoningSummary as ReasoningSummaryConfig;
|
||||
use crate::git_info::GitInfo;
|
||||
use crate::message_history::HistoryEntry;
|
||||
use crate::model_provider_info::ModelProviderInfo;
|
||||
use crate::plan_tool::UpdatePlanArgs;
|
||||
@@ -693,6 +694,10 @@ pub struct SessionConfiguredEvent {
|
||||
|
||||
/// Current number of entries in the history log.
|
||||
pub history_entry_count: usize,
|
||||
|
||||
/// Optional Git metadata for the configured cwd.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub git_info: Option<GitInfo>,
|
||||
}
|
||||
|
||||
/// User's decision in response to an ExecApprovalRequest.
|
||||
@@ -755,6 +760,7 @@ mod tests {
|
||||
model: "codex-mini-latest".to_string(),
|
||||
history_log_id: 0,
|
||||
history_entry_count: 0,
|
||||
git_info: None,
|
||||
}),
|
||||
};
|
||||
let serialized = serde_json::to_string(&event).unwrap();
|
||||
|
||||
@@ -493,6 +493,7 @@ impl EventProcessor for EventProcessorWithHumanOutput {
|
||||
model,
|
||||
history_log_id: _,
|
||||
history_entry_count: _,
|
||||
git_info: _,
|
||||
} = session_configured_event;
|
||||
|
||||
ts_println!(
|
||||
|
||||
@@ -908,6 +908,7 @@ mod tests {
|
||||
model: "codex-mini-latest".into(),
|
||||
history_log_id: 42,
|
||||
history_entry_count: 3,
|
||||
git_info: None,
|
||||
}),
|
||||
};
|
||||
|
||||
|
||||
@@ -244,6 +244,7 @@ mod tests {
|
||||
model: "gpt-4o".to_string(),
|
||||
history_log_id: 1,
|
||||
history_entry_count: 1000,
|
||||
git_info: None,
|
||||
}),
|
||||
};
|
||||
|
||||
@@ -284,6 +285,7 @@ mod tests {
|
||||
model: "gpt-4o".to_string(),
|
||||
history_log_id: 1,
|
||||
history_entry_count: 1000,
|
||||
git_info: None,
|
||||
};
|
||||
let event = Event {
|
||||
id: "1".to_string(),
|
||||
|
||||
@@ -204,6 +204,7 @@ impl HistoryCell {
|
||||
session_id: _,
|
||||
history_log_id: _,
|
||||
history_entry_count: _,
|
||||
git_info,
|
||||
} = event;
|
||||
if is_first_event {
|
||||
let cwd_str = match relativize_to_home(&config.cwd) {
|
||||
@@ -212,9 +213,8 @@ impl HistoryCell {
|
||||
None => config.cwd.display().to_string(),
|
||||
};
|
||||
|
||||
// Determine the current Git branch (if any) for display using
|
||||
// the shared Git info collector in a way that is safe in sync contexts.
|
||||
let branch_suffix = codex_core::git_info::collect_git_info_blocking(&config.cwd)
|
||||
// Use async-collected Git info from the event if available.
|
||||
let branch_suffix = git_info
|
||||
.and_then(|g| g.branch)
|
||||
.map(|b| format!(" ({b})"))
|
||||
.unwrap_or_default();
|
||||
|
||||
Reference in New Issue
Block a user