mirror of
https://github.com/openai/codex.git
synced 2026-11-09 20:50:33 +00:00
## Why Hook configurations can mark command handlers as asynchronous, but Codex previously skipped those handlers outside `SessionEnd`. ## What changed - Run asynchronous command hooks in the background with a per-session concurrency limit, while keeping `SessionEnd` hooks synchronous. - Prevent asynchronous hooks from blocking, stopping, rewriting, or otherwise controlling the operation that launched them. - Deliver warnings and additional context at safe turn boundaries: inject results into an active turn after sampling, or buffer them ahead of the next user prompt when the session is idle. - Preserve in-flight hooks across configuration reloads, scope spilled output to the thread, and abort outstanding work during session shutdown. ## Testing Add unit and integration coverage for background scheduling, concurrency, output parsing, active and idle result delivery, configuration reloads, and shutdown cleanup. GitOrigin-RevId: 8094552e4afe7b47b09a61bb575bb20f4e491d8d
136 lines
4.4 KiB
Rust
136 lines
4.4 KiB
Rust
use codex_protocol::ThreadId;
|
|
use codex_protocol::items::HookPromptFragment;
|
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
|
use codex_utils_output_truncation::TruncationPolicy;
|
|
use codex_utils_output_truncation::approx_token_count;
|
|
use codex_utils_output_truncation::formatted_truncate_text;
|
|
use tokio::fs;
|
|
use tracing::warn;
|
|
use uuid::Uuid;
|
|
|
|
const HOOK_OUTPUTS_DIR: &str = "hook_outputs";
|
|
pub(crate) const DEFAULT_HOOK_OUTPUT_TOKEN_LIMIT: usize = 2_500;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub(crate) struct AdditionalContextLimit {
|
|
token_limit: usize,
|
|
}
|
|
|
|
impl AdditionalContextLimit {
|
|
pub(crate) fn from_config(value: Option<usize>) -> Self {
|
|
Self {
|
|
token_limit: value.unwrap_or(DEFAULT_HOOK_OUTPUT_TOKEN_LIMIT),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for AdditionalContextLimit {
|
|
fn default() -> Self {
|
|
Self::from_config(/*value*/ None)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub(crate) struct AdditionalContext {
|
|
pub text: String,
|
|
pub limit: AdditionalContextLimit,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub(crate) struct HookOutputSpiller {
|
|
output_dir: AbsolutePathBuf,
|
|
}
|
|
|
|
impl HookOutputSpiller {
|
|
pub(crate) fn new(thread_id: ThreadId) -> Self {
|
|
Self {
|
|
output_dir: AbsolutePathBuf::resolve_path_against_base(std::env::temp_dir(), "/")
|
|
.join(HOOK_OUTPUTS_DIR)
|
|
.join(thread_id.to_string()),
|
|
}
|
|
}
|
|
|
|
/// Keeps hook text within the model-visible hook-output budget.
|
|
///
|
|
/// Oversized text is written in full under the OS temp directory at
|
|
/// `<temp_dir>/hook_outputs/<thread_id>/`
|
|
/// and replaced with the same head/tail preview style used for other truncated
|
|
/// output, plus a path back to the preserved full text.
|
|
pub(crate) async fn maybe_spill_text(&self, text: String) -> String {
|
|
self.maybe_spill_text_with_limit(text, AdditionalContextLimit::default())
|
|
.await
|
|
}
|
|
|
|
async fn maybe_spill_text_with_limit(
|
|
&self,
|
|
text: String,
|
|
limit: AdditionalContextLimit,
|
|
) -> String {
|
|
let token_limit = limit.token_limit;
|
|
if token_limit == 0 || approx_token_count(&text) <= token_limit {
|
|
return text;
|
|
}
|
|
|
|
let path = self.output_dir.join(format!("{}.txt", Uuid::new_v4()));
|
|
if let Some(parent) = path.parent()
|
|
&& let Err(err) = fs::create_dir_all(parent.as_ref()).await
|
|
{
|
|
warn!(
|
|
"failed to create hook output directory {}: {err}",
|
|
parent.display()
|
|
);
|
|
return formatted_truncate_text(&text, TruncationPolicy::Tokens(token_limit));
|
|
}
|
|
|
|
if let Err(err) = fs::write(path.as_ref(), &text).await {
|
|
warn!("failed to write hook output {}: {err}", path.display());
|
|
return formatted_truncate_text(&text, TruncationPolicy::Tokens(token_limit));
|
|
}
|
|
|
|
spilled_hook_output_preview(&text, &path, token_limit)
|
|
}
|
|
|
|
pub(crate) async fn maybe_spill_additional_contexts(
|
|
&self,
|
|
contexts: Vec<AdditionalContext>,
|
|
) -> Vec<String> {
|
|
let mut spilled = Vec::with_capacity(contexts.len());
|
|
for context in contexts {
|
|
spilled.push(
|
|
self.maybe_spill_text_with_limit(context.text, context.limit)
|
|
.await,
|
|
);
|
|
}
|
|
spilled
|
|
}
|
|
|
|
pub(crate) async fn maybe_spill_prompt_fragments(
|
|
&self,
|
|
fragments: Vec<HookPromptFragment>,
|
|
) -> Vec<HookPromptFragment> {
|
|
let mut spilled = Vec::with_capacity(fragments.len());
|
|
for fragment in fragments {
|
|
spilled.push(HookPromptFragment {
|
|
text: self.maybe_spill_text(fragment.text).await,
|
|
hook_run_id: fragment.hook_run_id,
|
|
});
|
|
}
|
|
spilled
|
|
}
|
|
}
|
|
|
|
/// Builds the model-visible replacement for a spilled hook output.
|
|
///
|
|
/// The path footer is budgeted before truncation so adding the recovery path
|
|
/// does not consume the configured preview budget.
|
|
fn spilled_hook_output_preview(text: &str, path: &AbsolutePathBuf, token_limit: usize) -> String {
|
|
let footer = format!("\n\nFull hook output saved to: {}", path.display());
|
|
let preview_policy =
|
|
TruncationPolicy::Tokens(token_limit.saturating_sub(approx_token_count(&footer)));
|
|
format!("{}{footer}", formatted_truncate_text(text, preview_policy))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "output_spill_tests.rs"]
|
|
mod tests;
|