Skip to content

Commit 545af52

Browse files
committed
core: preserve reasoning accounting through pre-turn compaction
1 parent c4318c3 commit 545af52

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎codex-rs/core/src/session/turn.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ pub(crate) async fn run_turn(
163163
error!("Failed to run pre-sampling compact");
164164
return Ok(None);
165165
}
166+
// Pre-turn accounting uses the previous response's reasoning contract.
167+
// Clear it only after compaction, before the new response can update it.
168+
sess.set_server_reasoning_included(/*included*/ false).await;
166169

167170
// run_turn owns the step used to seed context and make the first sampling request.
168171
let first_step_context = sess.capture_step_context(Arc::clone(&turn_context)).await;

‎codex-rs/core/src/tasks/regular.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ impl SessionTask for RegularTask {
5555
collaboration_mode_kind: ctx.collaboration_mode.mode,
5656
});
5757
sess.send_event(ctx.as_ref(), event).await;
58-
sess.set_server_reasoning_included(/*included*/ false).await;
5958
sess.consume_startup_prewarm_for_regular_turn(&cancellation_token)
6059
.await
6160
}

‎codex-rs/core/tests/suite/compact.rs‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4529,6 +4529,85 @@ async fn auto_compact_counts_encrypted_reasoning_before_last_user() {
45294529
);
45304530
}
45314531

4532+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
4533+
async fn auto_compact_preserves_reasoning_header_for_next_pre_turn_check() {
4534+
skip_if_no_network!();
4535+
4536+
let server = start_mock_server().await;
4537+
4538+
let pre_last_reasoning_content = "a".repeat(2_400);
4539+
let post_last_reasoning_content = "b".repeat(4_000);
4540+
4541+
let first_turn = sse(vec![
4542+
ev_reasoning_item("pre-reasoning", &["pre"], &[&pre_last_reasoning_content]),
4543+
ev_completed_with_tokens("r1", /*total_tokens*/ 10),
4544+
]);
4545+
let second_turn = sse(vec![
4546+
ev_reasoning_item("post-reasoning", &["post"], &[&post_last_reasoning_content]),
4547+
ev_completed_with_tokens("r2", /*total_tokens*/ 80),
4548+
]);
4549+
let third_turn = sse(vec![
4550+
ev_assistant_message("m4", FINAL_REPLY),
4551+
ev_completed_with_tokens("r4", /*total_tokens*/ 1),
4552+
]);
4553+
4554+
let responses = vec![
4555+
sse_response(first_turn).insert_header("X-Reasoning-Included", "true"),
4556+
sse_response(second_turn).insert_header("X-Reasoning-Included", "true"),
4557+
sse_response(third_turn).insert_header("X-Reasoning-Included", "true"),
4558+
];
4559+
mount_response_sequence(&server, responses).await;
4560+
4561+
let compact_mock = mount_compact_json_once(
4562+
&server,
4563+
serde_json::json!({
4564+
"output": [{
4565+
"type": "compaction",
4566+
"encrypted_content": "UNEXPECTED_COMPACTION",
4567+
}],
4568+
}),
4569+
)
4570+
.await;
4571+
4572+
let codex = test_codex()
4573+
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
4574+
.with_config(|config| {
4575+
set_test_compact_prompt(config);
4576+
config.model_auto_compact_token_limit = Some(300);
4577+
let _ = config.features.disable(Feature::RemoteCompactionV2);
4578+
})
4579+
.build(&server)
4580+
.await
4581+
.expect("build codex")
4582+
.codex;
4583+
4584+
for user in [
4585+
"SERVER_INCLUDED_FIRST",
4586+
"SERVER_INCLUDED_SECOND",
4587+
"SERVER_INCLUDED_THIRD",
4588+
] {
4589+
codex
4590+
.submit(Op::UserInput {
4591+
items: vec![UserInput::Text {
4592+
text: user.into(),
4593+
text_elements: Vec::new(),
4594+
}],
4595+
final_output_json_schema: None,
4596+
responsesapi_client_metadata: None,
4597+
additional_context: Default::default(),
4598+
thread_settings: Default::default(),
4599+
})
4600+
.await
4601+
.unwrap();
4602+
wait_for_event(&codex, |ev| matches!(ev, EventMsg::TurnComplete(_))).await;
4603+
}
4604+
4605+
assert!(
4606+
compact_mock.requests().is_empty(),
4607+
"pre-turn accounting should retain the previous response's reasoning-included signal"
4608+
);
4609+
}
4610+
45324611
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
45334612
async fn auto_compact_runs_when_reasoning_header_clears_between_turns() {
45344613
skip_if_no_network!();

0 commit comments

Comments
 (0)