Skip to content

Commit 0da44b4

Browse files
committed
core: restore reasoning accounting across resume
1 parent 545af52 commit 0da44b4

18 files changed

Lines changed: 138 additions & 6 deletions

File tree

‎codex-rs/app-server-protocol/src/protocol/thread_history.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ impl ThreadHistoryBuilder {
393393
RolloutItem::ResponseItem(item) => self.handle_response_item(item),
394394
RolloutItem::InterAgentCommunication(_)
395395
| RolloutItem::InterAgentCommunicationMetadata { .. }
396+
| RolloutItem::ServerReasoningIncluded(_)
396397
| RolloutItem::TurnContext(_)
397398
| RolloutItem::WorldState(_)
398399
| RolloutItem::SessionMeta(_) => {}

‎codex-rs/core/src/agent/control/spawn.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ fn keep_forked_rollout_item(item: &RolloutItem, preserve_reference_context_item:
7474
// from the parent's durable baseline. Truncated forks drop part of that prompt,
7575
// so they must rebuild context on their first child turn.
7676
RolloutItem::TurnContext(_) | RolloutItem::WorldState(_) => preserve_reference_context_item,
77-
RolloutItem::Compacted(_) | RolloutItem::EventMsg(_) | RolloutItem::SessionMeta(_) => true,
77+
RolloutItem::ServerReasoningIncluded(_)
78+
| RolloutItem::Compacted(_)
79+
| RolloutItem::EventMsg(_)
80+
| RolloutItem::SessionMeta(_) => true,
7881
}
7982
}
8083

‎codex-rs/core/src/agent/control_tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ async fn persisted_originator(thread: &CodexThread) -> String {
174174
RolloutItem::ResponseItem(_)
175175
| RolloutItem::InterAgentCommunication(_)
176176
| RolloutItem::InterAgentCommunicationMetadata { .. }
177+
| RolloutItem::ServerReasoningIncluded(_)
177178
| RolloutItem::EventMsg(_)
178179
| RolloutItem::Compacted(_)
179180
| RolloutItem::WorldState(_)

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,8 @@ impl Session {
13701370
let mut state = self.state.lock().await;
13711371
state.set_token_info(Some(info));
13721372
}
1373+
self.restore_server_reasoning_included_from_rollout(&rollout_items)
1374+
.await;
13731375

13741376
// Defer seeding the session's initial context until the first turn starts so
13751377
// turn/start overrides can be merged before we write to the rollout.
@@ -1395,6 +1397,8 @@ impl Session {
13951397
let mut state = self.state.lock().await;
13961398
state.set_token_info(Some(info));
13971399
}
1400+
self.restore_server_reasoning_included_from_rollout(&rollout_items)
1401+
.await;
13981402

13991403
// If persisting, persist all rollout items as-is (the store filters).
14001404
if !rollout_items.is_empty() {
@@ -1500,6 +1504,16 @@ impl Session {
15001504
})
15011505
}
15021506

1507+
async fn restore_server_reasoning_included_from_rollout(&self, rollout_items: &[RolloutItem]) {
1508+
let included = rollout_items.iter().rev().find_map(|item| match item {
1509+
RolloutItem::ServerReasoningIncluded(included) => Some(*included),
1510+
_ => None,
1511+
});
1512+
if let Some(included) = included {
1513+
self.set_server_reasoning_included(included).await;
1514+
}
1515+
}
1516+
15031517
async fn previous_turn_settings(&self) -> Option<PreviousTurnSettings> {
15041518
let state = self.state.lock().await;
15051519
state.previous_turn_settings()
@@ -3794,12 +3808,17 @@ impl Session {
37943808
}
37953809

37963810
pub(crate) async fn send_token_count_event(&self, turn_context: &TurnContext) {
3797-
let (info, rate_limits) = {
3811+
let (info, rate_limits, server_reasoning_included) = {
37983812
let state = self.state.lock().await;
3799-
state.token_info_and_rate_limits()
3813+
let (info, rate_limits) = state.token_info_and_rate_limits();
3814+
(info, rate_limits, state.server_reasoning_included())
38003815
};
38013816
let event = EventMsg::TokenCount(TokenCountEvent { info, rate_limits });
38023817
self.send_event(turn_context, event).await;
3818+
self.persist_rollout_items(&[RolloutItem::ServerReasoningIncluded(
3819+
server_reasoning_included,
3820+
)])
3821+
.await;
38033822
}
38043823

38053824
pub(crate) async fn set_total_tokens_full(&self, turn_context: &TurnContext) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ impl Session {
280280
}
281281
RolloutItem::EventMsg(_)
282282
| RolloutItem::SessionMeta(_)
283+
| RolloutItem::ServerReasoningIncluded(_)
283284
| RolloutItem::InterAgentCommunicationMetadata { .. } => {}
284285
}
285286

@@ -368,6 +369,7 @@ impl Session {
368369
RolloutItem::EventMsg(_)
369370
| RolloutItem::TurnContext(_)
370371
| RolloutItem::WorldState(_)
372+
| RolloutItem::ServerReasoningIncluded(_)
371373
| RolloutItem::SessionMeta(_) => {}
372374
}
373375
}
@@ -414,6 +416,7 @@ impl Session {
414416
| RolloutItem::ResponseItem(_)
415417
| RolloutItem::InterAgentCommunication(_)
416418
| RolloutItem::InterAgentCommunicationMetadata { .. }
419+
| RolloutItem::ServerReasoningIncluded(_)
417420
| RolloutItem::TurnContext(_)
418421
| RolloutItem::EventMsg(_) => {
419422
unreachable!("only world-state replay items are collected")

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,6 +2826,7 @@ async fn start_new_context_window_assigns_and_persists_item_ids() {
28262826
| RolloutItem::ResponseItem(_)
28272827
| RolloutItem::InterAgentCommunication(_)
28282828
| RolloutItem::InterAgentCommunicationMetadata { .. }
2829+
| RolloutItem::ServerReasoningIncluded(_)
28292830
| RolloutItem::TurnContext(_)
28302831
| RolloutItem::WorldState(_)
28312832
| RolloutItem::EventMsg(_) => None,
@@ -2884,6 +2885,7 @@ async fn record_initial_history_assigns_and_persists_id_for_forked_response_item
28842885
RolloutItem::SessionMeta(_)
28852886
| RolloutItem::InterAgentCommunication(_)
28862887
| RolloutItem::InterAgentCommunicationMetadata { .. }
2888+
| RolloutItem::ServerReasoningIncluded(_)
28872889
| RolloutItem::Compacted(_)
28882890
| RolloutItem::TurnContext(_)
28892891
| RolloutItem::WorldState(_)

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4608,6 +4608,92 @@ async fn auto_compact_preserves_reasoning_header_for_next_pre_turn_check() {
46084608
);
46094609
}
46104610

4611+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
4612+
async fn auto_compact_restores_reasoning_header_after_resume() {
4613+
skip_if_no_network!();
4614+
4615+
let server = start_mock_server().await;
4616+
4617+
let pre_last_reasoning_content = "a".repeat(2_400);
4618+
let post_last_reasoning_content = "b".repeat(4_000);
4619+
4620+
let first_turn = sse(vec![
4621+
ev_reasoning_item("pre-reasoning", &["pre"], &[&pre_last_reasoning_content]),
4622+
ev_completed_with_tokens("r1", /*total_tokens*/ 10),
4623+
]);
4624+
let second_turn = sse(vec![
4625+
ev_reasoning_item("post-reasoning", &["post"], &[&post_last_reasoning_content]),
4626+
ev_completed_with_tokens("r2", /*total_tokens*/ 80),
4627+
]);
4628+
let third_turn = sse(vec![
4629+
ev_assistant_message("m4", FINAL_REPLY),
4630+
ev_completed_with_tokens("r4", /*total_tokens*/ 1),
4631+
]);
4632+
4633+
let responses = vec![
4634+
sse_response(first_turn).insert_header("X-Reasoning-Included", "true"),
4635+
sse_response(second_turn).insert_header("X-Reasoning-Included", "true"),
4636+
sse_response(third_turn).insert_header("X-Reasoning-Included", "true"),
4637+
];
4638+
mount_response_sequence(&server, responses).await;
4639+
4640+
let compact_mock = mount_compact_json_once(
4641+
&server,
4642+
serde_json::json!({
4643+
"output": [{
4644+
"type": "compaction",
4645+
"encrypted_content": "UNEXPECTED_COMPACTION",
4646+
}],
4647+
}),
4648+
)
4649+
.await;
4650+
4651+
let configure = |config: &mut Config| {
4652+
set_test_compact_prompt(config);
4653+
config.model_auto_compact_token_limit = Some(300);
4654+
let _ = config.features.disable(Feature::RemoteCompactionV2);
4655+
};
4656+
let mut builder = test_codex()
4657+
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
4658+
.with_config(configure);
4659+
let initial = builder.build(&server).await.expect("build codex");
4660+
let home = Arc::clone(&initial.home);
4661+
let rollout_path = initial
4662+
.session_configured
4663+
.rollout_path
4664+
.clone()
4665+
.expect("rollout path");
4666+
4667+
for user in ["SERVER_INCLUDED_FIRST", "SERVER_INCLUDED_SECOND"] {
4668+
initial
4669+
.submit_turn(user)
4670+
.await
4671+
.expect("submit initial turn");
4672+
}
4673+
initial
4674+
.codex
4675+
.shutdown_and_wait()
4676+
.await
4677+
.expect("shutdown initial thread");
4678+
4679+
let mut resume_builder = test_codex()
4680+
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
4681+
.with_config(configure);
4682+
let resumed = resume_builder
4683+
.resume(&server, home, rollout_path)
4684+
.await
4685+
.expect("resume codex");
4686+
resumed
4687+
.submit_turn("SERVER_INCLUDED_THIRD")
4688+
.await
4689+
.expect("submit resumed turn");
4690+
4691+
assert!(
4692+
compact_mock.requests().is_empty(),
4693+
"resume should restore the previous response's reasoning-included signal"
4694+
);
4695+
}
4696+
46114697
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
46124698
async fn auto_compact_runs_when_reasoning_header_clears_between_turns() {
46134699
skip_if_no_network!();

‎codex-rs/memories/write/src/phase1.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ mod job {
413413
}
414414
RolloutItem::SessionMeta(_)
415415
| RolloutItem::InterAgentCommunicationMetadata { .. }
416+
| RolloutItem::ServerReasoningIncluded(_)
416417
| RolloutItem::Compacted(_)
417418
| RolloutItem::TurnContext(_)
418419
| RolloutItem::WorldState(_)

‎codex-rs/protocol/src/protocol.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,6 +2649,7 @@ impl InitialHistory {
26492649
| RolloutItem::ResponseItem(_)
26502650
| RolloutItem::InterAgentCommunication(_)
26512651
| RolloutItem::InterAgentCommunicationMetadata { .. }
2652+
| RolloutItem::ServerReasoningIncluded(_)
26522653
| RolloutItem::Compacted(_)
26532654
| RolloutItem::WorldState(_)
26542655
| RolloutItem::EventMsg(_) => None,
@@ -2985,6 +2986,7 @@ fn multi_agent_version_from_items(
29852986
| RolloutItem::ResponseItem(_)
29862987
| RolloutItem::InterAgentCommunication(_)
29872988
| RolloutItem::InterAgentCommunicationMetadata { .. }
2989+
| RolloutItem::ServerReasoningIncluded(_)
29882990
| RolloutItem::Compacted(_)
29892991
| RolloutItem::WorldState(_)
29902992
| RolloutItem::EventMsg(_) => None,
@@ -3146,6 +3148,8 @@ pub enum RolloutItem {
31463148
InterAgentCommunicationMetadata {
31473149
trigger_turn: bool,
31483150
},
3151+
/// Local token-accounting metadata that is not sent to protocol clients.
3152+
ServerReasoningIncluded(bool),
31493153
Compacted(CompactedItem),
31503154
TurnContext(TurnContextItem),
31513155
WorldState(WorldStateItem),

‎codex-rs/rollout/src/list.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,8 @@ async fn read_head_summary(path: &Path, head_limit: usize) -> io::Result<HeadTai
11731173
.created_at
11741174
.get_or_insert_with(|| rollout_line.timestamp.clone());
11751175
}
1176-
RolloutItem::InterAgentCommunicationMetadata { .. } => {}
1176+
RolloutItem::InterAgentCommunicationMetadata { .. }
1177+
| RolloutItem::ServerReasoningIncluded(_) => {}
11771178
RolloutItem::TurnContext(_) => {
11781179
// Not included in `head`; skip.
11791180
}
@@ -1247,6 +1248,7 @@ pub async fn read_head_for_summary(path: &Path) -> io::Result<Vec<serde_json::Va
12471248
}
12481249
}
12491250
RolloutItem::InterAgentCommunicationMetadata { .. }
1251+
| RolloutItem::ServerReasoningIncluded(_)
12501252
| RolloutItem::Compacted(_)
12511253
| RolloutItem::TurnContext(_)
12521254
| RolloutItem::WorldState(_)

0 commit comments

Comments
 (0)