Keep StopFailure tests in hooks suite

This commit is contained in:
Abhinav Vedmala
2026-06-16 12:09:07 -07:00
parent 97c4989046
commit ae3734b6c9
3 changed files with 175 additions and 185 deletions
+175
View File
@@ -25,6 +25,7 @@ use codex_utils_absolute_path::AbsolutePathBuf;
use core_test_support::hooks::trust_discovered_hooks;
use core_test_support::hooks::trust_hooks;
use core_test_support::managed_network_requirements_loader;
use core_test_support::responses::ResponsesRequest;
use core_test_support::responses::ev_apply_patch_custom_tool_call;
use core_test_support::responses::ev_assistant_message;
use core_test_support::responses::ev_completed;
@@ -37,6 +38,7 @@ use core_test_support::responses::ev_response_created;
use core_test_support::responses::mount_sse_once;
use core_test_support::responses::mount_sse_sequence;
use core_test_support::responses::sse;
use core_test_support::responses::sse_failed;
use core_test_support::responses::start_mock_server;
use core_test_support::skip_if_no_network;
use core_test_support::skip_if_windows;
@@ -58,6 +60,13 @@ const SECOND_CONTINUATION_PROMPT: &str = "Now tighten it to just: meow.";
const BLOCKED_PROMPT_CONTEXT: &str = "Remember the blocked lighthouse note.";
const PERMISSION_REQUEST_HOOK_MATCHER: &str = "^Bash$";
const PERMISSION_REQUEST_ALLOW_REASON: &str = "should not be used for allow";
const STOP_FAILURE_INITIAL_MODEL: &str = "gpt-5.2";
const STOP_FAILURE_FALLBACK_MODEL: &str = "gpt-5.4";
struct StopFailureRun {
requests: Vec<ResponsesRequest>,
hook_inputs: Vec<Value>,
}
fn restrictive_workspace_write_profile() -> PermissionProfile {
PermissionProfile::workspace_write_with(
@@ -168,6 +177,94 @@ else:
Ok(())
}
fn write_stop_failure_hook(home: &Path, recovery: Value) -> Result<()> {
let script_path = home.join("stop_failure_hook.py");
let log_path = home.join("stop_failure_hook_log.jsonl");
let output_json = serde_json::json!({
"hookSpecificOutput": {
"hookEventName": "StopFailure",
"recovery": recovery,
}
})
.to_string();
let script = format!(
r#"import json
from pathlib import Path
import sys
log_path = Path(r"{log_path}")
payload = json.load(sys.stdin)
with log_path.open("a", encoding="utf-8") as handle:
handle.write(json.dumps(payload) + "\n")
print({output_json:?})
"#,
log_path = log_path.display(),
);
let hooks = serde_json::json!({
"hooks": {
"StopFailure": [{
"matcher": "overloaded",
"hooks": [{
"type": "command",
"command": format!("python3 {}", script_path.display()),
}]
}]
}
});
fs::write(&script_path, script).context("write StopFailure hook script")?;
fs::write(home.join("hooks.json"), hooks.to_string()).context("write hooks.json")?;
Ok(())
}
fn read_stop_failure_hook_inputs(home: &Path) -> Result<Vec<Value>> {
fs::read_to_string(home.join("stop_failure_hook_log.jsonl"))?
.lines()
.filter(|line| !line.trim().is_empty())
.map(|line| serde_json::from_str(line).context("parse StopFailure hook input"))
.collect()
}
fn successful_stop_failure_response() -> String {
sse(vec![
ev_response_created("resp-2"),
ev_assistant_message("msg-2", "recovered"),
ev_completed("resp-2"),
])
}
async fn run_stop_failure_scenario(
recovery: Value,
second_response: String,
) -> Result<StopFailureRun> {
let server = start_mock_server().await;
let responses = mount_sse_sequence(
&server,
vec![
sse_failed("resp-1", "server_is_overloaded", "capacity"),
second_response,
],
)
.await;
let mut builder = test_codex()
.with_model(STOP_FAILURE_INITIAL_MODEL)
.with_pre_build_hook(move |home| {
write_stop_failure_hook(home, recovery)
.expect("failed to write StopFailure hook fixture");
})
.with_config(trust_discovered_hooks);
let test = builder.build(&server).await?;
test.submit_turn("recover from overload").await?;
Ok(StopFailureRun {
requests: responses.requests(),
hook_inputs: read_stop_failure_hook_inputs(test.codex_home_path())?,
})
}
fn write_parallel_stop_hooks(home: &Path, prompts: &[&str]) -> Result<()> {
let hook_entries = prompts
.iter()
@@ -1065,6 +1162,84 @@ fn request_message_input_texts(body: &[u8], role: &str) -> Vec<String> {
.collect()
}
#[tokio::test]
async fn stop_failure_hook_retries_with_an_explicit_model() -> Result<()> {
skip_if_no_network!(Ok(()));
let run = run_stop_failure_scenario(
serde_json::json!({
"action": "retry",
"model": { "selector": "id", "id": STOP_FAILURE_FALLBACK_MODEL },
}),
successful_stop_failure_response(),
)
.await?;
assert_eq!(run.requests.len(), 2);
assert_eq!(
run.requests[0].body_json()["model"],
STOP_FAILURE_INITIAL_MODEL
);
assert_eq!(
run.requests[1].body_json()["model"],
STOP_FAILURE_FALLBACK_MODEL
);
assert!(run.requests[1].body_contains_text("<model_switch>"));
assert_eq!(run.hook_inputs.len(), 1);
let input = &run.hook_inputs[0];
assert_eq!(input["hook_event_name"], "StopFailure");
assert_eq!(input["error"], "overloaded");
assert_eq!(input["model"], STOP_FAILURE_INITIAL_MODEL);
assert_eq!(input["last_assistant_message"], Value::Null);
assert!(
input["error_details"]
.as_str()
.is_some_and(|details| details.contains("capacity"))
);
Ok(())
}
#[tokio::test]
async fn stop_failure_hook_retries_with_the_catalog_default() -> Result<()> {
skip_if_no_network!(Ok(()));
let run = run_stop_failure_scenario(
serde_json::json!({
"action": "retry",
"model": { "selector": "catalog_default" },
}),
successful_stop_failure_response(),
)
.await?;
let default_model = codex_core::test_support::all_model_presets()
.iter()
.find(|preset| preset.is_default)
.expect("bundled models should include a default")
.model
.clone();
assert_eq!(run.requests[1].body_json()["model"], default_model);
Ok(())
}
#[tokio::test]
async fn stop_failure_hook_runs_at_most_once_per_turn() -> Result<()> {
skip_if_no_network!(Ok(()));
let run = run_stop_failure_scenario(
serde_json::json!({
"action": "retry",
"model": { "selector": "id", "id": STOP_FAILURE_FALLBACK_MODEL },
}),
sse_failed("resp-2", "server_is_overloaded", "recovery failure"),
)
.await?;
assert_eq!(run.requests.len(), 2);
assert_eq!(run.hook_inputs.len(), 1);
Ok(())
}
#[tokio::test]
async fn stop_hook_can_block_multiple_times_in_same_turn() -> Result<()> {
skip_if_no_network!(Ok(()));
-2
View File
@@ -109,8 +109,6 @@ mod skill_approval;
mod skills;
mod spawn_agent_description;
mod sqlite_state;
#[cfg(not(target_os = "windows"))]
mod stop_failure_hooks;
mod stream_error_allows_next_turn;
mod stream_no_completed;
mod subagent_notifications;
@@ -1,183 +0,0 @@
use std::fs;
use std::path::Path;
use anyhow::Context;
use anyhow::Result;
use core_test_support::hooks::trust_discovered_hooks;
use core_test_support::responses::ResponsesRequest;
use core_test_support::responses::ev_assistant_message;
use core_test_support::responses::ev_completed;
use core_test_support::responses::ev_response_created;
use core_test_support::responses::mount_sse_sequence;
use core_test_support::responses::sse;
use core_test_support::responses::sse_failed;
use core_test_support::responses::start_mock_server;
use core_test_support::skip_if_no_network;
use core_test_support::test_codex::test_codex;
use pretty_assertions::assert_eq;
use serde_json::Value;
const INITIAL_MODEL: &str = "gpt-5.2";
const FALLBACK_MODEL: &str = "gpt-5.4";
struct StopFailureRun {
requests: Vec<ResponsesRequest>,
hook_inputs: Vec<Value>,
}
fn write_stop_failure_hook(home: &Path, recovery: Value) -> Result<()> {
let script_path = home.join("stop_failure_hook.py");
let log_path = home.join("stop_failure_hook_log.jsonl");
let output_json = serde_json::json!({
"hookSpecificOutput": {
"hookEventName": "StopFailure",
"recovery": recovery,
}
})
.to_string();
let script = format!(
r#"import json
from pathlib import Path
import sys
log_path = Path(r"{log_path}")
payload = json.load(sys.stdin)
with log_path.open("a", encoding="utf-8") as handle:
handle.write(json.dumps(payload) + "\n")
print({output_json:?})
"#,
log_path = log_path.display(),
);
let hooks = serde_json::json!({
"hooks": {
"StopFailure": [{
"matcher": "overloaded",
"hooks": [{
"type": "command",
"command": format!("python3 {}", script_path.display()),
}]
}]
}
});
fs::write(&script_path, script).context("write StopFailure hook script")?;
fs::write(home.join("hooks.json"), hooks.to_string()).context("write hooks.json")?;
Ok(())
}
fn read_hook_inputs(home: &Path) -> Result<Vec<Value>> {
fs::read_to_string(home.join("stop_failure_hook_log.jsonl"))?
.lines()
.filter(|line| !line.trim().is_empty())
.map(|line| serde_json::from_str(line).context("parse StopFailure hook input"))
.collect()
}
fn successful_response() -> String {
sse(vec![
ev_response_created("resp-2"),
ev_assistant_message("msg-2", "recovered"),
ev_completed("resp-2"),
])
}
async fn run_scenario(recovery: Value, second_response: String) -> Result<StopFailureRun> {
let server = start_mock_server().await;
let responses = mount_sse_sequence(
&server,
vec![
sse_failed("resp-1", "server_is_overloaded", "capacity"),
second_response,
],
)
.await;
let mut builder = test_codex()
.with_model(INITIAL_MODEL)
.with_pre_build_hook(move |home| {
write_stop_failure_hook(home, recovery)
.expect("failed to write StopFailure hook fixture");
})
.with_config(trust_discovered_hooks);
let test = builder.build(&server).await?;
test.submit_turn("recover from overload").await?;
Ok(StopFailureRun {
requests: responses.requests(),
hook_inputs: read_hook_inputs(test.codex_home_path())?,
})
}
#[tokio::test]
async fn retries_with_an_explicit_model() -> Result<()> {
skip_if_no_network!(Ok(()));
let run = run_scenario(
serde_json::json!({
"action": "retry",
"model": { "selector": "id", "id": FALLBACK_MODEL },
}),
successful_response(),
)
.await?;
assert_eq!(run.requests.len(), 2);
assert_eq!(run.requests[0].body_json()["model"], INITIAL_MODEL);
assert_eq!(run.requests[1].body_json()["model"], FALLBACK_MODEL);
assert!(run.requests[1].body_contains_text("<model_switch>"));
assert_eq!(run.hook_inputs.len(), 1);
let input = &run.hook_inputs[0];
assert_eq!(input["hook_event_name"], "StopFailure");
assert_eq!(input["error"], "overloaded");
assert_eq!(input["model"], INITIAL_MODEL);
assert_eq!(input["last_assistant_message"], Value::Null);
assert!(
input["error_details"]
.as_str()
.is_some_and(|details| details.contains("capacity"))
);
Ok(())
}
#[tokio::test]
async fn retries_with_the_catalog_default() -> Result<()> {
skip_if_no_network!(Ok(()));
let run = run_scenario(
serde_json::json!({
"action": "retry",
"model": { "selector": "catalog_default" },
}),
successful_response(),
)
.await?;
let default_model = codex_core::test_support::all_model_presets()
.iter()
.find(|preset| preset.is_default)
.expect("bundled models should include a default")
.model
.clone();
assert_eq!(run.requests[1].body_json()["model"], default_model);
Ok(())
}
#[tokio::test]
async fn runs_at_most_once_per_turn() -> Result<()> {
skip_if_no_network!(Ok(()));
let run = run_scenario(
serde_json::json!({
"action": "retry",
"model": { "selector": "id", "id": FALLBACK_MODEL },
}),
sse_failed("resp-2", "server_is_overloaded", "recovery failure"),
)
.await?;
assert_eq!(run.requests.len(), 2);
assert_eq!(run.hook_inputs.len(), 1);
Ok(())
}