Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Persist world-state snapshots alongside rendered context (#49847)

pakrym-oaib44ca872b105
diff --git a/codex-rs/core/src/compact.rs b/codex-rs/core/src/compact.rs
index 6de37b1e0a..39919cfef6 100644
--- a/codex-rs/core/src/compact.rs
+++ b/codex-rs/core/src/compact.rs
@@ -9,6 +9,7 @@ use crate::client_common::ResponseEvent;
use crate::context::CompactionSummary;
use crate::context::ContextualUserFragment;
use crate::context::world_state::WorldState;
+use crate::context::world_state::WorldStateSnapshot;
use crate::hook_runtime::PostCompactHookOutcome;
use crate::hook_runtime::PreCompactHookOutcome;
use crate::hook_runtime::run_post_compact_hooks;
@@ -91,19 +92,19 @@ pub(crate) struct CompactedHistoryMetadata {
pub(crate) async fn build_compaction_initial_context(
sess: &Session,
initial_context_injection: &InitialContextInjection,
-) -> (Vec<ResponseItemEnvelope>, Option<Arc<WorldState>>) {
+) -> (Vec<ResponseItemEnvelope>, Option<WorldStateSnapshot>) {
// Return the rendered state with its items so history and its baseline stay identical.
match initial_context_injection {
InitialContextInjection::BeforeLastUserMessage {
world_state,
step_context,
} => {
- let items = sess
+ let (items, snapshot) = sess
.build_initial_context_with_world_state(step_context, world_state.as_ref())
.await;
(
items.into_iter().map(ResponseItemEnvelope::new).collect(),
- Some(Arc::clone(world_state)),
+ Some(snapshot),
)
}
InitialContextInjection::DoNotInject => (Vec::new(), None),
diff --git a/codex-rs/core/src/context/world_state/apps_instructions_tests.rs b/codex-rs/core/src/context/world_state/apps_instructions_tests.rs
index dc67acf048..77b71d1bc8 100644
--- a/codex-rs/core/src/context/world_state/apps_instructions_tests.rs
+++ b/codex-rs/core/src/context/world_state/apps_instructions_tests.rs
@@ -35,6 +35,7 @@ fn legacy_guidance_is_not_injected_again() {
assert!(
world_state
.render_history_diff(/*previous*/ None, &[legacy])
+ .1
.is_empty()
);
}
@@ -43,16 +44,20 @@ fn legacy_guidance_is_not_injected_again() {
fn persisted_guidance_is_restored_only_when_missing_from_history() {
let mut world_state = super::super::WorldState::default();
world_state.add_section(AppsInstructionsState::new(/*available*/ true));
- let snapshot = world_state.snapshot();
+ let snapshot = world_state.render_full().0;
let retained: ResponseItem = ContextualUserFragment::into(AppsInstructions);
assert_eq!(
- world_state.render_history_diff(Some(&snapshot), &[]).len(),
+ world_state
+ .render_history_diff(Some(&snapshot), &[])
+ .1
+ .len(),
1
);
assert!(
world_state
.render_history_diff(Some(&snapshot), &[retained])
+ .1
.is_empty()
);
}
diff --git a/codex-rs/core/src/context/world_state/collaboration_mode_tests.rs b/codex-rs/core/src/context/world_state/collaboration_mode_tests.rs
index b6a90aa8e8..21d3883b0e 100644
--- a/codex-rs/core/src/context/world_state/collaboration_mode_tests.rs
+++ b/codex-rs/core/src/context/world_state/collaboration_mode_tests.rs
@@ -54,23 +54,25 @@ fn instruction_updates_are_applied_once_in_retained_history() {
let expected: ResponseItem = ContextualUserFragment::into(CollaborationModeInstructions {
instructions: instructions.unwrap_or_default().to_string(),
});
- let updates = world_state
- .render_history_diff(previous.as_ref(), &history)
+ let (snapshot, fragments) = world_state.render_history_diff(previous.as_ref(), &history);
+ let updates = fragments
.into_iter()
.map(ContextualUserFragment::into_boxed_response_item)
.collect::<Vec<_>>();
assert_eq!(updates, vec![expected]);
history.extend(updates);
- previous = Some(world_state.snapshot());
+ previous = Some(snapshot);
assert!(
world_state
.render_history_diff(previous.as_ref(), &history)
+ .1
.is_empty()
);
assert_eq!(
world_state
.render_history_diff(previous.as_ref(), &[])
+ .1
.len(),
usize::from(instructions.is_some()),
);
diff --git a/codex-rs/core/src/context/world_state/context_window_guidance_tests.rs b/codex-rs/core/src/context/world_state/context_window_guidance_tests.rs
index 71bef5264f..5a10baf8ad 100644
--- a/codex-rs/core/src/context/world_state/context_window_guidance_tests.rs
+++ b/codex-rs/core/src/context/world_state/context_window_guidance_tests.rs
@@ -30,7 +30,12 @@ fn guidance_transitions_render_once(
let mut refreshed = WorldState::default();
refreshed.add_section(ContextWindowGuidanceState::new(current));
- let fragments = refreshed.render_diff(&original.snapshot());
+ let mut history: Vec<ResponseItem> = previous
+ .map(|message| ContextualUserFragment::into(ContextWindowGuidance::new(message)))
+ .into_iter()
+ .collect();
+ let (next_snapshot, fragments) =
+ refreshed.render_history_diff(Some(&original.render_full().0), &history);
assert_eq!(
fragments
.iter()
@@ -44,8 +49,16 @@ fn guidance_transitions_render_once(
// Empty guidance must survive persistence as a known state, not a deleted section.
let snapshot: WorldStateSnapshot =
- serde_json::from_value(serde_json::to_value(refreshed.snapshot()).unwrap()).unwrap();
- assert!(refreshed.render_diff(&snapshot).is_empty());
+ serde_json::from_value(serde_json::to_value(next_snapshot).unwrap()).unwrap();
+ history.extend(
+ fragments
+ .into_iter()
+ .map(ContextualUserFragment::into_boxed_response_item),
+ );
+ let (repeated_snapshot, repeated_fragments) =
+ refreshed.render_history_diff(Some(&snapshot), &history);
+ assert_eq!(repeated_snapshot, snapshot);
+ assert!(repeated_fragments.is_empty());
}
#[test_case(
@@ -69,7 +82,9 @@ fn legacy_guidance_is_reconciled_once(
});
let retained: ResponseItem =
ContextualUserFragment::into(ContextWindowGuidance::new("previous guidance"));
- let fragments = state.render_history_diff(snapshot.as_ref(), std::slice::from_ref(&retained));
+ let fragments = state
+ .render_history_diff(snapshot.as_ref(), std::slice::from_ref(&retained))
+ .1;
assert_eq!(
fragments
.iter()
@@ -89,7 +104,8 @@ fn legacy_guidance_is_reconciled_once(
}
assert!(
state
- .render_history_diff(Some(&state.snapshot()), &history)
+ .render_history_diff(Some(&state.render_full().0), &history)
+ .1
.is_empty()
);
}
diff --git a/codex-rs/core/src/context/world_state/environment_tests.rs b/codex-rs/core/src/context/world_state/environment_tests.rs
index 6bda9b79b6..c0461b2c5c 100644
--- a/codex-rs/core/src/context/world_state/environment_tests.rs
+++ b/codex-rs/core/src/context/world_state/environment_tests.rs
@@ -194,8 +194,9 @@ fn changing_primary_environment_updates_model_context_and_persisted_state() -> R
current_world_state.add_section(after);
assert_eq!(
current_world_state
- .snapshot()
- .merge_patch_from(&previous_world_state.snapshot())
+ .render_full()
+ .0
+ .merge_patch_from(&previous_world_state.render_full().0)
.map(serde_json::Value::Object),
Some(json!({
"environments": {
diff --git a/codex-rs/core/src/context/world_state/environments_instructions_tests.rs b/codex-rs/core/src/context/world_state/environments_instructions_tests.rs
index c3013494e4..3bd037d7b7 100644
--- a/codex-rs/core/src/context/world_state/environments_instructions_tests.rs
+++ b/codex-rs/core/src/context/world_state/environments_instructions_tests.rs
@@ -34,6 +34,7 @@ fn legacy_guidance_is_not_injected_again() {
assert!(
world_state
.render_history_diff(/*previous*/ None, &[legacy])
+ .1
.is_empty()
);
}
@@ -42,16 +43,20 @@ fn legacy_guidance_is_not_injected_again() {
fn persisted_guidance_is_restored_only_when_missing_from_history() {
let mut world_state = super::super::WorldState::default();
world_state.add_section(EnvironmentsInstructionsState::new(/*enabled*/ true));
- let snapshot = world_state.snapshot();
+ let snapshot = world_state.render_full().0;
let retained: ResponseItem = ContextualUserFragment::into(EnvironmentsInstructions);
assert_eq!(
- world_state.render_history_diff(Some(&snapshot), &[]).len(),
+ world_state
+ .render_history_diff(Some(&snapshot), &[])
+ .1
+ .len(),
1
);
assert!(
world_state
.render_history_diff(Some(&snapshot), &[retained])
+ .1
.is_empty()
);
}
diff --git a/codex-rs/core/src/context/world_state/managed_developer_instructions_tests.rs b/codex-rs/core/src/context/world_state/managed_developer_instructions_tests.rs
index 4df1a8ed9b..31301a2f75 100644
--- a/codex-rs/core/src/context/world_state/managed_developer_instructions_tests.rs
+++ b/codex-rs/core/src/context/world_state/managed_developer_instructions_tests.rs
@@ -23,8 +23,8 @@ fn managed_policy_updates_and_removal_are_emitted_once() {
instructions.map(|text| Sourced::new(text.to_string(), RequirementSource::Unknown));
let mut world_state = WorldState::default();
world_state.add_section(ManagedDeveloperInstructionsState::new(requirement.as_ref()));
- let updates = world_state
- .render_history_diff(previous.as_ref(), &history)
+ let (snapshot, fragments) = world_state.render_history_diff(previous.as_ref(), &history);
+ let updates = fragments
.into_iter()
.map(ContextualUserFragment::into_boxed_response_item)
.collect::<Vec<_>>();
@@ -38,10 +38,11 @@ fn managed_policy_updates_and_removal_are_emitted_once() {
.collect::<Vec<_>>()
);
history.extend(updates);
- previous = Some(world_state.snapshot());
+ previous = Some(snapshot);
assert!(
world_state
.render_history_diff(previous.as_ref(), &history)
+ .1
.is_empty()
);
}
@@ -63,6 +64,7 @@ fn managed_policy_updates_and_removal_are_emitted_once() {
assert_eq!(
world_state
.render_history_diff(/*previous*/ None, std::slice::from_ref(&legacy_policy))
+ .1
.into_iter()
.map(ContextualUserFragment::into_boxed_response_item)
.collect::<Vec<_>>(),
diff --git a/codex-rs/core/src/context/world_state/mod.rs b/codex-rs/core/src/context/world_state/mod.rs
index 62e5dcd7b9..1171debf43 100644
--- a/codex-rs/core/src/context/world_state/mod.rs
+++ b/codex-rs/core/src/context/world_state/mod.rs
@@ -56,48 +56,45 @@ pub(crate) use plugins_instructions::PluginsInstructionsState;
pub(crate) use realtime::RealtimeState;
pub(crate) use tools::ToolsState;
-trait ErasedWorldStateSection: Send + Sync {
- fn snapshot(&self) -> Option<Value>;
+type SectionTransition = (Option<Value>, Option<Box<dyn ContextualUserFragment>>);
+trait ErasedWorldStateSection: Send + Sync {
fn matches_legacy_fragment(&self, role: &str, text: &str) -> bool;
fn has_retained_fragment_matcher(&self) -> bool;
fn matches_retained_fragment(&self, role: &str, text: &str) -> bool;
- fn render_diff(
- &self,
- previous: PreviousSectionState<'_, Value>,
- ) -> Option<Box<dyn ContextualUserFragment>>;
+ fn render_diff(&self, previous: PreviousSectionState<'_, Value>) -> SectionTransition;
}
-impl<S: WorldStateSection> ErasedWorldStateSection for S {
- fn snapshot(&self) -> Option<Value> {
- if !WorldStateSection::should_persist(self) {
- return None;
- }
- let mut snapshot = match serde_json::to_value(WorldStateSection::snapshot(self)) {
- Ok(snapshot) => snapshot,
- Err(err) => {
- tracing::error!(
- section_id = S::ID,
- %err,
- "failed to serialize world-state section snapshot"
- );
- return None;
- }
- };
- remove_null_object_fields(&mut snapshot);
- if snapshot.is_null() {
+fn section_snapshot<S: WorldStateSection>(section: &S, snapshot: S::Snapshot) -> Option<Value> {
+ if !WorldStateSection::should_persist(section) {
+ return None;
+ }
+ let mut snapshot = match serde_json::to_value(snapshot) {
+ Ok(snapshot) => snapshot,
+ Err(err) => {
tracing::error!(
section_id = S::ID,
- "world-state section snapshot cannot be null"
+ %err,
+ "failed to serialize world-state section snapshot"
);
return None;
}
- Some(snapshot)
+ };
+ remove_null_object_fields(&mut snapshot);
+ if snapshot.is_null() {
+ tracing::error!(
+ section_id = S::ID,
+ "world-state section snapshot cannot be null"
+ );
+ return None;
}
+ Some(snapshot)
+}
+impl<S: WorldStateSection> ErasedWorldStateSection for S {
fn matches_legacy_fragment(&self, role: &str, text: &str) -> bool {
WorldStateSection::matches_current_legacy_fragment(self, role, text)
}
@@ -110,10 +107,7 @@ impl<S: WorldStateSection> ErasedWorldStateSection for S {
S::matches_retained_fragment(role, text)
}
- fn render_diff(
- &self,
- previous: PreviousSectionState<'_, Value>,
- ) -> Option<Box<dyn ContextualUserFragment>> {
+ fn render_diff(&self, previous: PreviousSectionState<'_, Value>) -> SectionTransition {
let typed_snapshot;
let previous = match previous {
PreviousSectionState::Known(previous) => {
@@ -136,19 +130,17 @@ impl<S: WorldStateSection> ErasedWorldStateSection for S {
PreviousSectionState::Absent => PreviousSectionState::Absent,
PreviousSectionState::Unknown => PreviousSectionState::Unknown,
};
- WorldStateSection::render_diff(self, previous)
+ let snapshot = section_snapshot(self, self.snapshot()).unwrap_or(Value::Null);
+ (
+ Some(snapshot),
+ WorldStateSection::render_diff(self, previous),
+ )
}
}
struct ExtensionWorldStateSection(WorldStateSectionContribution);
impl ErasedWorldStateSection for ExtensionWorldStateSection {
- fn snapshot(&self) -> Option<Value> {
- let mut snapshot = self.0.snapshot().clone();
- remove_null_object_fields(&mut snapshot);
- (!snapshot.is_null()).then_some(snapshot)
- }
-
fn matches_legacy_fragment(&self, role: &str, text: &str) -> bool {
self.0.matches_legacy_fragment(role, text)
}
@@ -161,21 +153,24 @@ impl ErasedWorldStateSection for ExtensionWorldStateSection {
self.0.matches_retained_fragment(role, text)
}
- fn render_diff(
- &self,
- previous: PreviousSectionState<'_, Value>,
- ) -> Option<Box<dyn ContextualUserFragment>> {
+ fn render_diff(&self, previous: PreviousSectionState<'_, Value>) -> SectionTransition {
let previous = match previous {
PreviousSectionState::Absent => PreviousWorldStateSection::Absent,
PreviousSectionState::Unknown => PreviousWorldStateSection::Unknown,
PreviousSectionState::Known(previous) => PreviousWorldStateSection::Known(previous),
};
- self.0.render_diff(previous).map(|fragment| {
+ let (snapshot, fragment) = self.0.render_diff(previous);
+ let snapshot = snapshot.map(|mut snapshot| {
+ remove_null_object_fields(&mut snapshot);
+ snapshot
+ });
+ let fragment = fragment.map(|fragment| {
Box::new(WorldStateContextFragment {
fragment,
content_kind: ContentItemKind(format!("{}.instructions", self.0.id())),
}) as _
- })
+ });
+ (snapshot, fragment)
}
}
@@ -382,35 +377,9 @@ impl WorldState {
}
}
- pub(crate) fn snapshot(&self) -> WorldStateSnapshot {
- WorldStateSnapshot {
- sections: self
- .sections
- .iter()
- .filter_map(|(id, section)| {
- section
- .snapshot()
- .map(|snapshot| ((*id).to_string(), snapshot))
- })
- .collect(),
- }
- }
-
/// Renders every section as new, without any known previous state.
- pub(crate) fn render_full(&self) -> Vec<Box<dyn ContextualUserFragment>> {
- self.render_with(|_, _| PreviousSectionState::Absent)
- }
-
- /// Renders each section against the exact persisted snapshot when available.
- #[cfg(test)]
- pub(crate) fn render_diff(
- &self,
- previous: &WorldStateSnapshot,
- ) -> Vec<Box<dyn ContextualUserFragment>> {
- self.render_with(|id, _| match previous.sections.get(id) {
- Some(previous) => PreviousSectionState::Known(previous),
- None => PreviousSectionState::Absent,
- })
+ pub(crate) fn render_full(&self) -> (WorldStateSnapshot, Vec<Box<dyn ContextualUserFragment>>) {
+ self.render_with(/*stored*/ None, |_, _| PreviousSectionState::Absent)
}
/// Falls back to retained model history when no exact persisted snapshot is available.
@@ -418,8 +387,8 @@ impl WorldState {
&self,
previous: Option<&WorldStateSnapshot>,
items: impl IntoIterator<Item = &'a ResponseItem> + Clone,
- ) -> Vec<Box<dyn ContextualUserFragment>> {
- self.render_with(|id, section| {
+ ) -> (WorldStateSnapshot, Vec<Box<dyn ContextualUserFragment>>) {
+ self.render_with(previous, |id, section| {
if let Some(previous) = previous.and_then(|previous| previous.sections.get(id)) {
if section.has_retained_fragment_matcher()
&& !has_retained_fragment(items.clone(), section)
@@ -438,12 +407,32 @@ impl WorldState {
fn render_with<'a>(
&self,
+ stored: Option<&WorldStateSnapshot>,
mut previous: impl FnMut(&str, &dyn ErasedWorldStateSection) -> PreviousSectionState<'a, Value>,
- ) -> Vec<Box<dyn ContextualUserFragment>> {
- self.sections
- .iter()
- .filter_map(|(id, section)| section.render_diff(previous(id, section.as_ref())))
- .collect()
+ ) -> (WorldStateSnapshot, Vec<Box<dyn ContextualUserFragment>>) {
+ let mut snapshot = WorldStateSnapshot::default();
+ let mut fragments = Vec::new();
+ for (id, section) in &self.sections {
+ let prior = previous(id, section.as_ref());
+ let (section_snapshot, fragment) = section.render_diff(prior);
+ // A skipped snapshot retains stored state even if history required a fresh render.
+ let section_snapshot = section_snapshot.or_else(|| {
+ stored
+ .and_then(|snapshot| snapshot.sections.get(*id))
+ .cloned()
+ });
+ if let Some(section_snapshot) = section_snapshot
+ && !section_snapshot.is_null()
+ {
+ snapshot
+ .sections
+ .insert((*id).to_string(), section_snapshot);
+ }
+ if let Some(fragment) = fragment {
+ fragments.push(fragment);
+ }
+ }
+ (snapshot, fragments)
}
}
diff --git a/codex-rs/core/src/context/world_state/multi_agent_mode_tests.rs b/codex-rs/core/src/context/world_state/multi_agent_mode_tests.rs
index 21fe2f82b0..2741cb3bca 100644
--- a/codex-rs/core/src/context/world_state/multi_agent_mode_tests.rs
+++ b/codex-rs/core/src/context/world_state/multi_agent_mode_tests.rs
@@ -48,21 +48,26 @@ fn persisted_mode_is_restored_only_when_missing_from_history() {
);
let mut world_state = WorldState::default();
world_state.add_section(state);
- let snapshot = world_state.snapshot();
+ let snapshot = world_state.render_full().0;
assert_eq!(
world_state
.render_history_diff(/*previous*/ None, std::slice::from_ref(&retained))
+ .1
.len(),
1,
);
assert_eq!(
- world_state.render_history_diff(Some(&snapshot), &[]).len(),
+ world_state
+ .render_history_diff(Some(&snapshot), &[])
+ .1
+ .len(),
1
);
assert!(
world_state
.render_history_diff(Some(&snapshot), &[retained])
+ .1
.is_empty()
);
}
@@ -105,6 +110,11 @@ fn catalog_role_updates_remain_separate_from_active_mode() {
let mut previous = WorldState::default();
previous.add_section(previous_hint);
previous.add_section(previous_mode);
+ let (previous_snapshot, previous_fragments) = previous.render_full();
+ let history = previous_fragments
+ .into_iter()
+ .map(ContextualUserFragment::into_boxed_response_item)
+ .collect::<Vec<_>>();
let current_role = catalog_role("Current role.");
let current_hint = MultiAgentUsageHintState::new(current_role.clone());
@@ -115,7 +125,9 @@ fn catalog_role_updates_remain_separate_from_active_mode() {
current.add_section(current_mode);
let updates = crate::context_manager::updates::merge_contextual_fragments(
- current.render_diff(&previous.snapshot()),
+ current
+ .render_history_diff(Some(&previous_snapshot), &history)
+ .1,
);
let expected_mode = MultiAgentModeInstructions::from_mode(MultiAgentMode::Proactive)
.expect("proactive mode should render");
diff --git a/codex-rs/core/src/context/world_state/permissions_tests.rs b/codex-rs/core/src/context/world_state/permissions_tests.rs
index 5d994a6ac6..70dc97c4fb 100644
--- a/codex-rs/core/src/context/world_state/permissions_tests.rs
+++ b/codex-rs/core/src/context/world_state/permissions_tests.rs
@@ -141,13 +141,20 @@ fn legacy_snapshot_deserializes_and_only_suppresses_matching_full_permissions()
}))
.expect("legacy world-state snapshot should deserialize");
let expected_permissions = with_approved_prefix.instructions.render();
+ let retained = ContextualUserFragment::into(with_approved_prefix.instructions.clone());
let mut world_state = WorldState::default();
world_state.add_section(with_approved_prefix);
- assert!(world_state.render_diff(&matching_legacy).is_empty());
+ assert!(
+ world_state
+ .render_history_diff(Some(&matching_legacy), std::slice::from_ref(&retained))
+ .1
+ .is_empty()
+ );
assert_eq!(
world_state
- .render_diff(&stale_legacy)
+ .render_history_diff(Some(&stale_legacy), &[retained])
+ .1
.into_iter()
.map(|fragment| fragment.render())
.collect::<Vec<_>>(),
@@ -180,7 +187,7 @@ fn persisted_permissions_are_detected_inside_bundled_developer_messages() {
let retained = ContextualUserFragment::into(state.instructions.clone());
let mut world_state = super::super::WorldState::default();
world_state.add_section(state);
- let snapshot = world_state.snapshot();
+ let snapshot = world_state.render_full().0;
let mut bundled_retained = retained.clone();
let ResponseItem::Message { content, .. } = &mut bundled_retained else {
panic!("permissions should render as a message");
@@ -195,16 +202,21 @@ fn persisted_permissions_are_detected_inside_bundled_developer_messages() {
assert_eq!(
world_state
.render_history_diff(/*previous*/ None, std::slice::from_ref(&retained))
+ .1
.len(),
1,
);
assert_eq!(
- world_state.render_history_diff(Some(&snapshot), &[]).len(),
+ world_state
+ .render_history_diff(Some(&snapshot), &[])
+ .1
+ .len(),
1,
);
assert!(
world_state
.render_history_diff(Some(&snapshot), &[bundled_retained])
+ .1
.is_empty()
);
}
diff --git a/codex-rs/core/src/context/world_state/persistent_mode_tests.rs b/codex-rs/core/src/context/world_state/persistent_mode_tests.rs
index 4ac30b8405..b5cd16d482 100644
--- a/codex-rs/core/src/context/world_state/persistent_mode_tests.rs
+++ b/codex-rs/core/src/context/world_state/persistent_mode_tests.rs
@@ -27,8 +27,8 @@ fn persistent_instructions_follow_mode_and_catalog_updates_without_duplicates()
instructions,
/*send_user_message_async_available*/ false,
));
- let updates = world_state
- .render_history_diff(previous.as_ref(), &history)
+ let (snapshot, fragments) = world_state.render_history_diff(previous.as_ref(), &history);
+ let updates = fragments
.into_iter()
.map(ContextualUserFragment::into_boxed_response_item)
.collect::<Vec<_>>();
@@ -44,7 +44,7 @@ fn persistent_instructions_follow_mode_and_catalog_updates_without_duplicates()
.collect::<Vec<_>>()
);
history.extend(updates);
- previous = Some(world_state.snapshot());
+ previous = Some(snapshot);
}
}
@@ -69,6 +69,7 @@ fn retained_persistent_instructions_are_replaced_or_retired_without_a_snapshot()
assert_eq!(
world_state
.render_history_diff(/*previous*/ None, std::slice::from_ref(&retained))
+ .1
.into_iter()
.map(ContextualUserFragment::into_boxed_response_item)
.collect::<Vec<_>>(),
diff --git a/codex-rs/core/src/context/world_state/plugins_instructions_tests.rs b/codex-rs/core/src/context/world_state/plugins_instructions_tests.rs
index c9a5e1d251..57626d96be 100644
--- a/codex-rs/core/src/context/world_state/plugins_instructions_tests.rs
+++ b/codex-rs/core/src/context/world_state/plugins_instructions_tests.rs
@@ -35,6 +35,7 @@ fn legacy_guidance_is_not_injected_again() {
assert!(
world_state
.render_history_diff(/*previous*/ None, &[legacy])
+ .1
.is_empty()
);
}
@@ -43,16 +44,20 @@ fn legacy_guidance_is_not_injected_again() {
fn persisted_guidance_is_restored_only_when_missing_from_history() {
let mut world_state = super::super::WorldState::default();
world_state.add_section(PluginsInstructionsState::new(/*available*/ true));
- let snapshot = world_state.snapshot();
+ let snapshot = world_state.render_full().0;
let retained: ResponseItem = ContextualUserFragment::into(AvailablePluginsInstructions);
assert_eq!(
- world_state.render_history_diff(Some(&snapshot), &[]).len(),
+ world_state
+ .render_history_diff(Some(&snapshot), &[])
+ .1
+ .len(),
1
);
assert!(
world_state
.render_history_diff(Some(&snapshot), &[retained])
+ .1
.is_empty()
);
}
diff --git a/codex-rs/core/src/context/world_state/test_support.rs b/codex-rs/core/src/context/world_state/test_support.rs
index 62bbae4d5b..d698049e58 100644
--- a/codex-rs/core/src/context/world_state/test_support.rs
+++ b/codex-rs/core/src/context/world_state/test_support.rs
@@ -50,7 +50,7 @@ fn render_diff<S: WorldStateSection>(
PreviousSectionState::Known(&previous_snapshot)
}
};
- ErasedWorldStateSection::render_diff(*after, previous)
+ ErasedWorldStateSection::render_diff(*after, previous).1
}
fn render_snapshot<S: WorldStateSection>(section: &S) -> String {
@@ -78,6 +78,6 @@ fn sort_json(value: serde_json::Value) -> serde_json::Value {
}
fn snapshot_value<S: WorldStateSection>(section: &S) -> serde_json::Value {
- ErasedWorldStateSection::snapshot(section)
+ super::section_snapshot(section, section.snapshot())
.expect("world-state section snapshot should serialize to a non-null value")
}
diff --git a/codex-rs/core/src/context/world_state/world_state_tests.rs b/codex-rs/core/src/context/world_state/world_state_tests.rs
index 0fa309bdbd..7bb0584e43 100644
--- a/codex-rs/core/src/context/world_state/world_state_tests.rs
+++ b/codex-rs/core/src/context/world_state/world_state_tests.rs
@@ -93,7 +93,7 @@ fn snapshot_uses_stable_section_ids_and_omits_null_fields() {
});
assert_eq!(
- serde_json::to_value(world_state.snapshot()).expect("serialize world-state snapshot"),
+ serde_json::to_value(world_state.render_full().0).expect("serialize world-state snapshot"),
json!({"test": {"value": "current", "array": [{"value": null}]}})
);
}
@@ -113,7 +113,9 @@ fn render_diff_restores_the_typed_section_snapshot() {
array: Vec::new(),
});
- let rendered = current.render_diff(&previous.snapshot());
+ let rendered = current
+ .render_history_diff(Some(&previous.render_full().0), &[])
+ .1;
assert_eq!(
vec!["after"],
@@ -129,30 +131,34 @@ fn extension_owned_section_uses_its_snapshot_and_renderer() {
let mut world_state = WorldState::default();
world_state.add_extension_section(WorldStateSectionContribution::new(
"extension_test",
- json!({"value": "after", "optional": null}),
- |previous| match previous {
- PreviousWorldStateSection::Known(previous)
- if previous == &json!({"value": "before"}) =>
- {
- Some(RenderedWorldStateFragment::new(
- "developer",
- ("<extension_test>", "</extension_test>"),
- "after",
- ))
- }
- PreviousWorldStateSection::Absent
- | PreviousWorldStateSection::Unknown
- | PreviousWorldStateSection::Known(_) => None,
+ |previous| {
+ (
+ Some(json!({"value": "after", "optional": null})),
+ match previous {
+ PreviousWorldStateSection::Known(previous)
+ if previous == &json!({"value": "before"}) =>
+ {
+ Some(RenderedWorldStateFragment::new(
+ "developer",
+ ("<extension_test>", "</extension_test>"),
+ "after",
+ ))
+ }
+ PreviousWorldStateSection::Absent
+ | PreviousWorldStateSection::Unknown
+ | PreviousWorldStateSection::Known(_) => None,
+ },
+ )
},
));
let previous = WorldStateSnapshot {
sections: BTreeMap::from([("extension_test".to_string(), json!({"value": "before"}))]),
};
- let rendered = world_state.render_diff(&previous);
+ let rendered = world_state.render_history_diff(Some(&previous), &[]).1;
assert_eq!(
- serde_json::to_value(world_state.snapshot()).expect("serialize world-state snapshot"),
+ serde_json::to_value(world_state.render_full().0).expect("serialize world-state snapshot"),
json!({"extension_test": {"value": "after"}})
);
assert_eq!(rendered.len(), 1);
@@ -166,19 +172,20 @@ fn extension_owned_section_uses_its_snapshot_and_renderer() {
#[test]
fn extension_owned_section_uses_its_stable_id_as_content_kind_feature() {
let mut world_state = WorldState::default();
- world_state.add_extension_section(WorldStateSectionContribution::new(
- "extension_test",
- json!({"value": "after"}),
- |_| {
+ world_state.add_extension_section(WorldStateSectionContribution::new("extension_test", |_| {
+ (
+ Some(json!({"value": "after"})),
Some(RenderedWorldStateFragment::new(
"developer",
("<extension_test>", "</extension_test>"),
"after",
- ))
- },
- ));
+ )),
+ )
+ }));
- let rendered = world_state.render_diff(&WorldStateSnapshot::default());
+ let rendered = world_state
+ .render_history_diff(Some(&WorldStateSnapshot::default()), &[])
+ .1;
assert_eq!(
rendered
@@ -199,23 +206,26 @@ fn extension_owned_section_uses_its_stable_id_as_content_kind_feature() {
fn missing_retained_fragment_is_rendered_again() {
let mut world_state = WorldState::default();
world_state.add_extension_section(
- WorldStateSectionContribution::new(
- "extension_test",
- json!({"body": "current catalog"}),
- |previous| match previous {
- PreviousWorldStateSection::Absent => Some(RenderedWorldStateFragment::new(
- "developer",
- ("<extension_test>", "</extension_test>"),
- "current catalog",
- )),
- PreviousWorldStateSection::Unknown | PreviousWorldStateSection::Known(_) => None,
- },
- )
+ WorldStateSectionContribution::new("extension_test", |previous| {
+ (
+ Some(json!({"body": "current catalog"})),
+ match previous {
+ PreviousWorldStateSection::Absent => Some(RenderedWorldStateFragment::new(
+ "developer",
+ ("<extension_test>", "</extension_test>"),
+ "current catalog",
+ )),
+ PreviousWorldStateSection::Unknown | PreviousWorldStateSection::Known(_) => {
+ None
+ }
+ },
+ )
+ })
.with_retained_fragment_matcher(|role, text| {
role == "developer" && text.contains("current catalog")
}),
);
- let previous = world_state.snapshot();
+ let previous = world_state.render_full().0;
let retained = ResponseItem::Message {
id: None,
role: "developer".to_string(),
@@ -229,6 +239,7 @@ fn missing_retained_fragment_is_rendered_again() {
assert_eq!(
world_state
.render_history_diff(Some(&previous), &[])
+ .1
.into_iter()
.map(|fragment| fragment.body())
.collect::<Vec<_>>(),
@@ -237,10 +248,41 @@ fn missing_retained_fragment_is_rendered_again() {
assert!(
world_state
.render_history_diff(Some(&previous), &[retained])
+ .1
.is_empty()
);
}
+#[test]
+fn extension_snapshot_updates_preserve_skipped_state() {
+ let previous = WorldStateSnapshot {
+ sections: BTreeMap::from([("extension_test".to_string(), json!({"published": true}))]),
+ };
+ let mut removed = WorldState::default();
+ removed.add_extension_section(WorldStateSectionContribution::new("extension_test", |_| {
+ (Some(Value::Null), None)
+ }));
+ let (snapshot, fragments) = removed.render_history_diff(Some(&previous), &[]);
+ assert!(fragments.is_empty());
+ assert_eq!(snapshot, WorldStateSnapshot::default());
+ assert_eq!(
+ snapshot.merge_patch_from(&previous).unwrap()["extension_test"],
+ Value::Null
+ );
+
+ let mut invalidated = WorldState::default();
+ invalidated.add_extension_section(
+ WorldStateSectionContribution::new("extension_test", |previous| {
+ assert!(matches!(previous, PreviousWorldStateSection::Absent));
+ (None, None)
+ })
+ .with_retained_fragment_matcher(|_, _| false),
+ );
+ let (snapshot, fragments) = invalidated.render_history_diff(Some(&previous), &[]);
+ assert!(fragments.is_empty());
+ assert_eq!(snapshot, previous);
+}
+
#[test]
fn unreadable_section_snapshot_is_treated_as_unknown() {
let mut current = WorldState::default();
@@ -253,7 +295,7 @@ fn unreadable_section_snapshot_is_treated_as_unknown() {
sections: BTreeMap::from([("test".to_string(), json!({"invalid": true}))]),
};
- let rendered = current.render_diff(&previous);
+ let rendered = current.render_history_diff(Some(&previous), &[]).1;
assert_eq!(
vec!["unknown"],
diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs
index 2475e685ea..7e9531695e 100644
--- a/codex-rs/core/src/context_manager/history.rs
+++ b/codex-rs/core/src/context_manager/history.rs
@@ -447,19 +447,36 @@ impl ContextManager {
&mut self,
world_state: &WorldState,
) -> (Vec<Box<dyn ContextualUserFragment>>, Option<WorldStateItem>) {
- let snapshot = world_state.snapshot();
- let fragments =
+ let (snapshot, fragments) =
world_state.render_history_diff(self.world_state_baseline.as_ref(), self.raw_items());
- let rollout_item = self.world_state_baseline.as_ref().map_or_else(
+ let rollout_item = self.world_state_item(&snapshot);
+ self.world_state_baseline = Some(snapshot);
+ (fragments, rollout_item)
+ }
+
+ pub(crate) fn render_step_world_state(
+ &self,
+ world_state: &WorldState,
+ ) -> (
+ WorldStateSnapshot,
+ Vec<Box<dyn ContextualUserFragment>>,
+ Option<WorldStateItem>,
+ ) {
+ let (snapshot, fragments) =
+ world_state.render_history_diff(self.world_state_baseline.as_ref(), self.raw_items());
+ let rollout_item = self.world_state_item(&snapshot);
+ (snapshot, fragments, rollout_item)
+ }
+
+ fn world_state_item(&self, snapshot: &WorldStateSnapshot) -> Option<WorldStateItem> {
+ self.world_state_baseline.as_ref().map_or_else(
|| Some(WorldStateItem::full(snapshot.clone().into_object())),
|previous| {
snapshot
.merge_patch_from(previous)
.map(WorldStateItem::patch)
},
- );
- self.world_state_baseline = Some(snapshot);
- (fragments, rollout_item)
+ )
}
pub(crate) fn set_world_state_baseline(&mut self, snapshot: WorldStateSnapshot) {
diff --git a/codex-rs/core/src/context_manager/history_tests.rs b/codex-rs/core/src/context_manager/history_tests.rs
index 90eaec0068..baede4a4cc 100644
--- a/codex-rs/core/src/context_manager/history_tests.rs
+++ b/codex-rs/core/src/context_manager/history_tests.rs
@@ -574,6 +574,45 @@ fn world_state_baseline_deduplicates_until_history_is_replaced() {
assert!(replacement_item.is_some_and(|item| item.full));
}
+#[test]
+fn world_state_transitions_persist_changed_state_and_skip_unchanged_state() {
+ use codex_extension_api::PreviousWorldStateSection;
+ use codex_extension_api::RenderedWorldStateFragment;
+ use codex_extension_api::WorldStateSectionContribution;
+
+ let world_state = |value: &'static str| {
+ let mut state = WorldState::default();
+ state.add_extension_section(WorldStateSectionContribution::new("test", move |previous| {
+ let snapshot = serde_json::json!(value);
+ if matches!(previous, PreviousWorldStateSection::Known(previous) if previous == &snapshot) {
+ return (None, None);
+ }
+ (Some(snapshot), Some(RenderedWorldStateFragment::new(
+ "developer", ("<test>", "</test>"), value,
+ )))
+ }));
+ state
+ };
+ let mut history = ContextManager::new();
+ let (_, full) = history.update_world_state(&world_state("before"));
+ let full = full.expect("full checkpoint");
+ assert!(full.full);
+
+ let (snapshot, fragments, patch) = history.render_step_world_state(&world_state("after"));
+ assert_eq!(fragments[0].body(), "after");
+ let patch = patch.expect("changed snapshot must be persisted");
+ assert!(!patch.full);
+ let mut replayed = WorldStateSnapshot::from(&full.state);
+ replayed.apply_merge_patch(&patch.state);
+ assert_eq!(snapshot, replayed);
+ history.set_world_state_baseline(snapshot.clone());
+
+ let (unchanged, fragments, patch) = history.render_step_world_state(&world_state("after"));
+ assert_eq!(unchanged, snapshot);
+ assert!(fragments.is_empty());
+ assert_eq!(patch, None);
+}
+
#[test]
fn world_state_reconciles_matching_legacy_history_once() {
let item = crate::context::ContextualUserFragment::into(UserInstructions {
@@ -584,7 +623,8 @@ fn world_state_reconciles_matching_legacy_history_once() {
let mut world_state = WorldState::default();
world_state.add_section(TestWorldStateSection);
- let (fragments, rollout_item) = history.update_world_state(&world_state);
+ let (snapshot, fragments, rollout_item) = history.render_step_world_state(&world_state);
+ history.set_world_state_baseline(snapshot);
assert_eq!(
vec!["\n\n<INSTRUCTIONS>\nunknown\n"],
fragments
diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs
index 0fedcdd4b7..1697cd0e83 100644
--- a/codex-rs/core/src/session/mod.rs
+++ b/codex-rs/core/src/session/mod.rs
@@ -3637,31 +3637,23 @@ impl Session {
pub(crate) async fn record_step_world_state_if_changed(
&self,
- previous_world_state: &Arc<WorldState>,
step_context: &step_context::StepContext,
) -> CodexResult<Arc<WorldState>> {
let turn_context = step_context.turn.as_ref();
// Render model-visible state from the same step used to build and run tools.
let world_state = Arc::new(self.build_world_state_for_step(step_context).await?);
- // Derive the model update and persisted patch from the same two snapshots.
- let previous_snapshot = previous_world_state.snapshot();
- let world_state_snapshot = world_state.snapshot();
- let world_state_item = world_state_snapshot
- .merge_patch_from(&previous_snapshot)
- .map(WorldStateItem::patch);
- // A catalog may have left history during compaction even when its snapshot survives.
- let items = crate::context_manager::updates::merge_contextual_fragments(
- world_state.render_history_diff(
- Some(&previous_snapshot),
- self.state.lock().await.history.raw_items(),
- ),
- );
+ let (world_state_snapshot, fragments, world_state_item) = self
+ .state
+ .lock()
+ .await
+ .history
+ .render_step_world_state(world_state.as_ref());
+ let items = crate::context_manager::updates::merge_contextual_fragments(fragments);
if !items.is_empty() {
self.record_conversation_items(turn_context, &step_context.settings.model_info, &items)
.await;
}
- // ContextManager remembers this for later turns; run_turn owns the live value.
self.state
.lock()
.await
@@ -4059,7 +4051,7 @@ impl Session {
&self,
mut items: Vec<ResponseItemEnvelope>,
reference_context_item: Option<TurnContextItem>,
- world_state_baseline: Option<Arc<WorldState>>,
+ world_state_baseline: Option<WorldStateSnapshot>,
metadata: CompactedHistoryMetadata,
) {
for envelope in &mut items {
@@ -4094,18 +4086,15 @@ impl Session {
let mut world_state_item = None;
let compacted_item = {
let mut state = self.state.lock().await;
- let snapshot = world_state_baseline
- .map(|world_state| world_state.snapshot())
- .or_else(|| {
- let previous = state.history.world_state_checkpoint()?;
- let mut retained = serde_json::Map::new();
- for contributor in self.services.extensions.context_contributors() {
- retained.extend(
- contributor.retain_world_state_after_compaction(&previous.state),
- );
- }
- (!retained.is_empty()).then(|| WorldStateSnapshot::from(&retained))
- });
+ let snapshot = world_state_baseline.or_else(|| {
+ let previous = state.history.world_state_checkpoint()?;
+ let mut retained = serde_json::Map::new();
+ for contributor in self.services.extensions.context_contributors() {
+ retained
+ .extend(contributor.retain_world_state_after_compaction(&previous.state));
+ }
+ (!retained.is_empty()).then(|| WorldStateSnapshot::from(&retained))
+ });
// Goal edits are published outside the running task. Keep edits accepted after
// the compaction input snapshot, in their original order, after its replacement.
let replacement_goal_ids = crate::context::UserGoalUpdate::message_ids(
@@ -4275,7 +4264,7 @@ impl Session {
&self,
step_context: &StepContext,
world_state: &WorldState,
- ) -> Vec<ResponseItem> {
+ ) -> (Vec<ResponseItem>, WorldStateSnapshot) {
let turn_context = step_context.turn.as_ref();
let mut developer_sections = Vec::<RenderedFragment>::with_capacity(8);
let mut contextual_user_sections = Vec::<RenderedFragment>::with_capacity(2);
@@ -4421,7 +4410,8 @@ impl Session {
// Render the active mode after the usage hint so it can override that hint.
let mut initial_multi_agent_mode = None;
let mut managed_developer_instructions = None;
- for fragment in world_state.render_full() {
+ let (world_state_snapshot, fragments) = world_state.render_full();
+ for fragment in fragments {
match fragment.role() {
"developer"
if fragment.markers().0 == ModelSwitchInstructions::type_markers().0 =>
@@ -4508,7 +4498,7 @@ impl Session {
for item in &mut items {
item.set_turn_id_if_missing(&turn_context.sub_id);
}
- items
+ (items, world_state_snapshot)
}
#[tracing::instrument(level = "trace", skip_all, fields(item_count = items.len()))]
@@ -4603,9 +4593,10 @@ impl Session {
state.start_new_context_window()
};
let (window_number, window_ids) = window;
- let context_items = self
+ let (context_items, world_state_snapshot) = self
.build_initial_context_with_world_state(step_context, world_state.as_ref())
- .await
+ .await;
+ let context_items = context_items
.into_iter()
.map(ResponseItemEnvelope::new)
.chain(retained_client_developer_messages)
@@ -4614,7 +4605,7 @@ impl Session {
self.replace_compacted_history(
context_items,
Some(turn_context_item),
- Some(world_state),
+ Some(world_state_snapshot),
CompactedHistoryMetadata {
input_goal_ids,
message: String::new(),
@@ -4664,10 +4655,9 @@ impl Session {
let world_state = Arc::new(self.build_world_state_for_step(step_context).await?);
// Full initial context resets the baseline; later turns persist only its changes.
let (mut context_items, world_state_item) = if should_inject_full_context {
- let context_items = self
+ let (context_items, snapshot) = self
.build_initial_context_with_world_state(step_context, world_state.as_ref())
.await;
- let snapshot = world_state.snapshot();
self.state
.lock()
.await
diff --git a/codex-rs/core/src/session/rollout_reconstruction_tests.rs b/codex-rs/core/src/session/rollout_reconstruction_tests.rs
index 05a7d37f91..c7c0407d96 100644
--- a/codex-rs/core/src/session/rollout_reconstruction_tests.rs
+++ b/codex-rs/core/src/session/rollout_reconstruction_tests.rs
@@ -369,6 +369,7 @@ async fn record_initial_history_restores_world_state_baseline(input: BaselineTur
let world_state = build_world_state_from_turn_context(&session, &turn_context).await;
let expected_history = world_state
.render_full()
+ .1
.into_iter()
.map(ContextualUserFragment::into_boxed_response_item)
.collect::<Vec<_>>();
@@ -379,7 +380,7 @@ async fn record_initial_history_restores_world_state_baseline(input: BaselineTur
.map(RolloutItem::ResponseItem)
.collect::<Vec<_>>();
world_state_items.push(RolloutItem::WorldState(WorldStateItem::full(
- world_state.snapshot().into_object(),
+ world_state.render_full().0.into_object(),
)));
let context_item = turn_context.to_turn_context_item();
let rollout_items = match input {
diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs
index c61c012db4..4e5ae1ae94 100644
--- a/codex-rs/core/src/session/tests.rs
+++ b/codex-rs/core/src/session/tests.rs
@@ -4018,12 +4018,16 @@ async fn start_new_context_window_persists_checkpoint_state() {
.await
.expect("world state should build"),
);
-
+ let expected_snapshot = world_state.render_full().0.into_object();
session
.start_new_context_window(&step_context, world_state)
.await;
let live_history = session.clone_history().await;
+ assert_eq!(
+ live_history.world_state_checkpoint().unwrap().state,
+ expected_snapshot
+ );
assert!(live_history.raw_items().next().is_some());
assert!(live_history.raw_items().all(|item| item.id().is_some()));
@@ -4034,6 +4038,17 @@ async fn start_new_context_window_persists_checkpoint_state() {
else {
panic!("expected resumed rollout history");
};
+ let persisted_world_state = resumed
+ .history
+ .iter()
+ .rev()
+ .find_map(|item| match item {
+ RolloutItem::WorldState(world_state) => Some(world_state),
+ _ => None,
+ })
+ .expect("new window should persist a world state");
+ assert!(persisted_world_state.full);
+ assert_eq!(persisted_world_state.state, expected_snapshot);
let persisted_compacted = resumed.history.iter().rev().find_map(|item| match item {
RolloutItem::Compacted(compacted) => Some(compacted),
RolloutItem::SessionMeta(_)
@@ -6162,7 +6177,7 @@ async fn compaction_persists_resume_metadata_and_companion_records() {
.replace_compacted_history(
vec![ResponseItemEnvelope::new(user_message("compacted context"))],
with_baselines.then_some(turn_context_baseline.clone()),
- with_baselines.then_some(Arc::clone(&world_state)),
+ with_baselines.then(|| world_state.render_full().0),
CompactedHistoryMetadata {
input_goal_ids,
message: String::new(),
@@ -6205,7 +6220,7 @@ async fn compaction_persists_resume_metadata_and_companion_records() {
assert_eq!(second.resume_metadata.as_ref(), Some(&expected));
assert_eq!(
first_world_state,
- &WorldStateItem::full(world_state.snapshot().into_object())
+ &WorldStateItem::full(world_state.render_full().0.into_object())
);
assert_eq!(first_turn_context, &turn_context_baseline);
assert_eq!(first_settings, &expected_settings);
@@ -6655,6 +6670,7 @@ async fn build_initial_context(
session
.build_initial_context_with_world_state(&step_context, &world_state)
.await
+ .0
}
pub(crate) async fn build_world_state_from_turn_context(
@@ -10553,7 +10569,7 @@ async fn build_initial_context_reuses_in_flight_recommendation_prewarm() {
tokio::pin!(initial_context);
assert!(futures::poll!(initial_context.as_mut()).is_pending());
- let (_, initial_context) = tokio::join!(prewarm, initial_context);
+ let (_, (initial_context, _)) = tokio::join!(prewarm, initial_context);
assert_eq!(
developer_input_texts(&initial_context)
.into_iter()
@@ -10742,7 +10758,7 @@ async fn record_context_updates_includes_turn_context_fragments_on_steady_state_
state.set_reference_context_item(Some(previous_context_item));
state
.history
- .set_world_state_baseline(world_state.snapshot());
+ .set_world_state_baseline(world_state.render_full().0);
}
let step_context = StepContext::for_test(Arc::clone(&turn_context));
@@ -11129,6 +11145,7 @@ async fn record_context_updates_and_set_reference_context_item_persists_baseline
let world_state = build_world_state_from_turn_context(&session, &previous_context).await;
let retained_world_state = world_state
.render_full()
+ .1
.into_iter()
.map(ContextualUserFragment::into_boxed_response_item)
.collect::<Vec<_>>();
@@ -11145,7 +11162,7 @@ async fn record_context_updates_and_set_reference_context_item_persists_baseline
let mut state = session.state.lock().await;
state
.history
- .set_world_state_baseline(world_state.snapshot());
+ .set_world_state_baseline(world_state.render_full().0);
}
let rollout_path = attach_thread_persistence(&mut session).await;
@@ -11315,13 +11332,14 @@ async fn build_initial_context_uses_retained_step_after_model_change() {
let world_b = session.build_world_state_for_step(&step_b).await.unwrap();
let initial_b = session
.build_initial_context_with_world_state(&step_b, &world_b)
- .await;
+ .await
+ .0;
let turn_contributions_b = session.build_turn_context_contribution_items(&step_b).await;
- let (restored_a, restored_world) =
+ let (restored_a, restored_snapshot) =
crate::compact::build_compaction_initial_context(&session, &retained).await;
assert_eq!(restored_a, initial_a);
- assert!(Arc::ptr_eq(restored_world.as_ref().unwrap(), &world_a));
+ assert_eq!(restored_snapshot, Some(world_a.render_full().0));
let initial_a = initial_a
.into_iter()
.map(ResponseItemEnvelope::into_item)
diff --git a/codex-rs/core/src/session/turn.rs b/codex-rs/core/src/session/turn.rs
index e56106dfb6..32f16fe4c5 100644
--- a/codex-rs/core/src/session/turn.rs
+++ b/codex-rs/core/src/session/turn.rs
@@ -505,7 +505,7 @@ pub(crate) async fn run_turn(
.await?;
world_state = sess
- .record_step_world_state_if_changed(&world_state, step_context.as_ref())
+ .record_step_world_state_if_changed(step_context.as_ref())
.await?;
// Keep the override after accepted input so history truncation removes them together.
diff --git a/codex-rs/core/src/thread_manager_tests.rs b/codex-rs/core/src/thread_manager_tests.rs
index af0848466e..e0074648bd 100644
--- a/codex-rs/core/src/thread_manager_tests.rs
+++ b/codex-rs/core/src/thread_manager_tests.rs
@@ -841,7 +841,8 @@ async fn ignores_session_prefix_messages_when_truncating() {
let step_context = StepContext::for_test(turn_context);
let mut items = session
.build_initial_context_with_world_state(&step_context, &world_state)
- .await;
+ .await
+ .0;
items.push(user_msg("feature request"));
items.push(assistant_msg("ack"));
items.push(user_msg("second question"));
@@ -1456,7 +1457,8 @@ async fn spawn_internal_session_preserves_parent_lineage_without_forking_history
.thread
.session
.build_initial_context_with_world_state(&reviewer_step, &reviewer_world_state)
- .await;
+ .await
+ .0;
assert!(
!serde_json::to_string(&reviewer_context)
.expect("reviewer context should serialize")
diff --git a/codex-rs/core/src/thread_rollout_truncation_tests.rs b/codex-rs/core/src/thread_rollout_truncation_tests.rs
index 71a80665e2..b92c252f01 100644
--- a/codex-rs/core/src/thread_rollout_truncation_tests.rs
+++ b/codex-rs/core/src/thread_rollout_truncation_tests.rs
@@ -369,7 +369,8 @@ async fn ignores_session_prefix_messages_when_truncating_rollout_from_start() {
let step_context = StepContext::for_test(turn_context);
let mut items = session
.build_initial_context_with_world_state(&step_context, &world_state)
- .await;
+ .await
+ .0;
items.push(user_msg("feature request"));
items.push(assistant_msg("ack"));
items.push(user_msg("second question"));
diff --git a/codex-rs/core/src/tools/handlers/multi_agents_tests.rs b/codex-rs/core/src/tools/handlers/multi_agents_tests.rs
index e7c0f2dbc3..1667105ab8 100644
--- a/codex-rs/core/src/tools/handlers/multi_agents_tests.rs
+++ b/codex-rs/core/src/tools/handlers/multi_agents_tests.rs
@@ -1182,7 +1182,7 @@ async fn multi_agent_v2_spawn_returns_path_and_send_message_accepts_relative_pat
.await
.expect("world state should build");
assert_eq!(
- world_state.snapshot().into_object()["environments"]["subagents"],
+ world_state.render_full().0.into_object()["environments"]["subagents"],
json!(r#"<agent name="/root/test_process" />"#),
);
diff --git a/codex-rs/core/tests/suite/remote_env.rs b/codex-rs/core/tests/suite/remote_env.rs
index 622a031e52..f264ad5038 100644
--- a/codex-rs/core/tests/suite/remote_env.rs
+++ b/codex-rs/core/tests/suite/remote_env.rs
@@ -197,13 +197,15 @@ impl ContextContributor for ReadyCapabilityRootsTestExtension {
let body = root_ids.join(",");
vec![WorldStateSectionContribution::new(
"ready_capability_roots_test",
- json!(root_ids),
move |_| {
- Some(RenderedWorldStateFragment::new(
- "user",
- ("<ready_capability_roots>", "</ready_capability_roots>"),
- body.clone(),
- ))
+ (
+ Some(json!(root_ids)),
+ Some(RenderedWorldStateFragment::new(
+ "user",
+ ("<ready_capability_roots>", "</ready_capability_roots>"),
+ body.clone(),
+ )),
+ )
},
)]
})
diff --git a/codex-rs/ext/extension-api/src/contributors/world_state.rs b/codex-rs/ext/extension-api/src/contributors/world_state.rs
index c2379b8b7f..e17873b86c 100644
--- a/codex-rs/ext/extension-api/src/contributors/world_state.rs
+++ b/codex-rs/ext/extension-api/src/contributors/world_state.rs
@@ -73,7 +73,7 @@ impl RenderedWorldStateFragment {
}
}
-type RenderDiff = dyn for<'a> Fn(PreviousWorldStateSection<'a>) -> Option<RenderedWorldStateFragment>
+type RenderDiff = dyn for<'a> Fn(PreviousWorldStateSection<'a>) -> (Option<Value>, Option<RenderedWorldStateFragment>)
+ Send
+ Sync;
type LegacyFragmentMatcher = dyn Fn(&str, &str) -> bool + Send + Sync;
@@ -85,26 +85,26 @@ type LegacyFragmentMatcher = dyn Fn(&str, &str) -> bool + Send + Sync;
#[derive(Clone)]
pub struct WorldStateSectionContribution {
id: &'static str,
- snapshot: Value,
render_diff: Arc<RenderDiff>,
matches_legacy_fragment: Arc<LegacyFragmentMatcher>,
matches_retained_fragment: Option<Arc<LegacyFragmentMatcher>>,
}
impl WorldStateSectionContribution {
+ /// Return a replacement snapshot and/or fragment. A missing snapshot retains the
+ /// previous section state; a missing fragment emits no model-visible update. A null
+ /// snapshot removes the section from persisted state.
pub fn new(
id: &'static str,
- snapshot: Value,
render_diff: impl for<'a> Fn(
PreviousWorldStateSection<'a>,
- ) -> Option<RenderedWorldStateFragment>
+ ) -> (Option<Value>, Option<RenderedWorldStateFragment>)
+ Send
+ Sync
+ 'static,
) -> Self {
Self {
id,
- snapshot,
render_diff: Arc::new(render_diff),
matches_legacy_fragment: Arc::new(|_, _| false),
matches_retained_fragment: None,
@@ -132,14 +132,10 @@ impl WorldStateSectionContribution {
self.id
}
- pub fn snapshot(&self) -> &Value {
- &self.snapshot
- }
-
pub fn render_diff(
&self,
previous: PreviousWorldStateSection<'_>,
- ) -> Option<RenderedWorldStateFragment> {
+ ) -> (Option<Value>, Option<RenderedWorldStateFragment>) {
(self.render_diff)(previous)
}
diff --git a/codex-rs/ext/git-attribution/src/world_state.rs b/codex-rs/ext/git-attribution/src/world_state.rs
index 4f96f80109..bdec77c4bf 100644
--- a/codex-rs/ext/git-attribution/src/world_state.rs
+++ b/codex-rs/ext/git-attribution/src/world_state.rs
@@ -28,35 +28,31 @@ Codex commit and pull request attribution is disabled for the current workspace.
";
pub(super) fn git_attribution_world_state_section(enabled: bool) -> WorldStateSectionContribution {
- let contribution =
- WorldStateSectionContribution::new(WORLD_STATE_ID, Value::Bool(enabled), move |previous| {
- match (enabled, previous) {
- (true, PreviousWorldStateSection::Known(Value::Bool(true)))
- | (true, PreviousWorldStateSection::Unknown) => None,
- (true, PreviousWorldStateSection::Absent)
- | (true, PreviousWorldStateSection::Known(_)) => {
- Some(RenderedWorldStateFragment::new(
- "developer",
- (START_MARKER, END_MARKER),
- ENABLED_INSTRUCTIONS,
- ))
- }
- (false, PreviousWorldStateSection::Known(Value::Bool(true)))
- | (false, PreviousWorldStateSection::Unknown) => {
- Some(RenderedWorldStateFragment::new(
- "developer",
- (START_MARKER, END_MARKER),
- DISABLED_INSTRUCTIONS,
- ))
- }
- (false, PreviousWorldStateSection::Absent)
- | (false, PreviousWorldStateSection::Known(_)) => None,
- }
- })
- .with_legacy_matcher(move |role, text| {
- is_enabled_fragment(role, text)
- || (!enabled && is_legacy_commit_attribution_fragment(role, text))
- });
+ let contribution = WorldStateSectionContribution::new(WORLD_STATE_ID, move |previous| {
+ let fragment = match (enabled, previous) {
+ (true, PreviousWorldStateSection::Known(Value::Bool(true)))
+ | (true, PreviousWorldStateSection::Unknown) => None,
+ (true, PreviousWorldStateSection::Absent)
+ | (true, PreviousWorldStateSection::Known(_)) => Some(RenderedWorldStateFragment::new(
+ "developer",
+ (START_MARKER, END_MARKER),
+ ENABLED_INSTRUCTIONS,
+ )),
+ (false, PreviousWorldStateSection::Known(Value::Bool(true)))
+ | (false, PreviousWorldStateSection::Unknown) => Some(RenderedWorldStateFragment::new(
+ "developer",
+ (START_MARKER, END_MARKER),
+ DISABLED_INSTRUCTIONS,
+ )),
+ (false, PreviousWorldStateSection::Absent)
+ | (false, PreviousWorldStateSection::Known(_)) => None,
+ };
+ (Some(Value::Bool(enabled)), fragment)
+ })
+ .with_legacy_matcher(move |role, text| {
+ is_enabled_fragment(role, text)
+ || (!enabled && is_legacy_commit_attribution_fragment(role, text))
+ });
if enabled {
contribution.with_retained_fragment_matcher(is_enabled_fragment)
} else {
diff --git a/codex-rs/ext/skills/src/world_state.rs b/codex-rs/ext/skills/src/world_state.rs
index 31a9bd80bb..d4ad2dc041 100644
--- a/codex-rs/ext/skills/src/world_state.rs
+++ b/codex-rs/ext/skills/src/world_state.rs
@@ -64,43 +64,47 @@ pub(crate) fn executor_skills_world_state_section(
"lastAvailableFingerprint": last_available_fingerprint,
});
let retained_body = body.clone();
- let contribution = WorldStateSectionContribution::new(
- SKILLS_WORLD_STATE_ID,
- snapshot,
- move |previous| {
+ let contribution = WorldStateSectionContribution::new(SKILLS_WORLD_STATE_ID, move |previous| {
if let PreviousWorldStateSection::Known(previous) = &previous
&& previous.get("includeInstructions").and_then(serde_json::Value::as_bool)
== Some(include_instructions)
{
if previous.get("body").and_then(serde_json::Value::as_str) == body.as_deref() {
- return None;
+ return (Some(snapshot.clone()), None);
}
if let Some(body) = body.as_deref()
&& previous.get("lastAvailableFingerprint").and_then(serde_json::Value::as_str)
== Some(blake3::hash(body.as_bytes()).to_hex().as_str())
{
- return Some(RenderedWorldStateFragment::new(
- "developer",
- (SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG),
- "\n## Skills update\nThe previously listed selected-environment skills are available again.\n",
- ));
+ return (
+ Some(snapshot.clone()),
+ Some(RenderedWorldStateFragment::new(
+ "developer",
+ (SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG),
+ "\n## Skills update\nThe previously listed selected-environment skills are available again.\n",
+ )),
+ );
}
}
let body = match body.as_deref() {
Some(body) => body,
- None if matches!(previous, PreviousWorldStateSection::Absent) => return None,
+ None if matches!(previous, PreviousWorldStateSection::Absent) => {
+ return (Some(snapshot.clone()), None);
+ }
None if !include_instructions => HIDDEN_EXECUTOR_SKILLS_BODY,
None => NO_EXECUTOR_SKILLS_BODY,
};
on_render();
- Some(RenderedWorldStateFragment::new(
- "developer",
- (SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG),
- body,
- ))
- },
- )
+ (
+ Some(snapshot.clone()),
+ Some(RenderedWorldStateFragment::new(
+ "developer",
+ (SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG),
+ body,
+ )),
+ )
+ })
.with_legacy_matcher(|role, text| {
role == "developer"
&& text.trim_start().starts_with(SKILLS_INSTRUCTIONS_OPEN_TAG)
@@ -156,7 +160,7 @@ fn skills_world_state_section(
} = state;
let retained_body = body.clone();
- let contribution = WorldStateSectionContribution::new(id, snapshot, move |previous| {
+ let contribution = WorldStateSectionContribution::new(id, move |previous| {
if let PreviousWorldStateSection::Known(previous) = &previous {
let previous_body = previous.get("body").and_then(serde_json::Value::as_str);
let previous_include_instructions = previous
@@ -167,23 +171,28 @@ fn skills_world_state_section(
&& previous_include_instructions == Some(include_instructions)
&& previous_enabled == enabled
{
- return None;
+ return (Some(snapshot.clone()), None);
}
}
let body = match body.as_deref() {
Some(body) => body,
- None if matches!(previous, PreviousWorldStateSection::Absent) => return None,
+ None if matches!(previous, PreviousWorldStateSection::Absent) => {
+ return (Some(snapshot.clone()), None);
+ }
None if !include_instructions => hidden_skills_body,
None => no_skills_body,
};
on_render();
- Some(RenderedWorldStateFragment::new(
- "developer",
- (SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG),
- body,
- ))
+ (
+ Some(snapshot.clone()),
+ Some(RenderedWorldStateFragment::new(
+ "developer",
+ (SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG),
+ body,
+ )),
+ )
});
match retained_body {
Some(body) => contribution.with_retained_fragment_matcher(move |role, text| {
diff --git a/codex-rs/ext/skills/tests/skills_extension.rs b/codex-rs/ext/skills/tests/skills_extension.rs
index efa388f29f..db0718cbde 100644
--- a/codex-rs/ext/skills/tests/skills_extension.rs
+++ b/codex-rs/ext/skills/tests/skills_extension.rs
@@ -148,9 +148,11 @@ async fn skill_world_state_fragments(
let executor = world_state_section(&sections, "skills")
.render_diff(PreviousWorldStateSection::Absent)
+ .1
.ok_or("executor skills should render through world state")?;
let cloud = world_state_section(&sections, "cloud_skills")
.render_diff(PreviousWorldStateSection::Absent)
+ .1
.ok_or("cloud skills should render through world state")?;
Ok((executor, cloud))
}
@@ -333,12 +335,10 @@ async fn host_world_state_records_catalog_metrics_on_publish_and_change() -> Tes
assert_eq!(sections.len(), 2);
let host_section = world_state_section(&sections, "host_skills");
- let published_snapshot = host_section.snapshot().clone();
- assert!(
- host_section
- .render_diff(PreviousWorldStateSection::Absent)
- .is_some()
- );
+ let (published_snapshot, fragment) =
+ host_section.render_diff(PreviousWorldStateSection::Absent);
+ let published_snapshot = published_snapshot.unwrap();
+ assert!(fragment.is_some());
let mut expected = expected_catalog_metric_samples("host_world_state", /*count*/ 1);
assert!(startup_metrics.samples().is_empty());
assert_eq!(turn_metrics.samples(), expected);
@@ -361,6 +361,7 @@ async fn host_world_state_records_catalog_metrics_on_publish_and_change() -> Tes
assert!(
world_state_section(&sections, "host_skills")
.render_diff(PreviousWorldStateSection::Known(&published_snapshot))
+ .1
.is_none()
);
assert!(startup_metrics.samples().is_empty());
@@ -399,6 +400,7 @@ async fn host_world_state_records_catalog_metrics_on_publish_and_change() -> Tes
assert!(
world_state_section(&sections, "host_skills")
.render_diff(PreviousWorldStateSection::Known(&published_snapshot))
+ .1
.is_some()
);
expected.extend(expected_catalog_metric_samples(
@@ -469,12 +471,10 @@ async fn persisted_host_snapshot_deduplicates_warning_after_reinitialization() -
})
.await;
let host_section = world_state_section(&sections, "host_skills");
- let published_snapshot = host_section.snapshot().clone();
- assert!(
- host_section
- .render_diff(PreviousWorldStateSection::Absent)
- .is_some()
- );
+ let (published_snapshot, fragment) =
+ host_section.render_diff(PreviousWorldStateSection::Absent);
+ let published_snapshot = published_snapshot.unwrap();
+ assert!(fragment.is_some());
event_rx.try_recv()?.into_warning();
assert!(event_rx.try_recv().is_err());
@@ -513,6 +513,7 @@ async fn persisted_host_snapshot_deduplicates_warning_after_reinitialization() -
assert!(
world_state_section(&sections, "host_skills")
.render_diff(PreviousWorldStateSection::Known(&published_snapshot))
+ .1
.is_none()
);
assert!(event_rx.try_recv().is_err());
@@ -627,6 +628,7 @@ async fn executor_cloud_and_host_share_catalog_world_state_flow() -> TestResult
] {
let fragment = world_state_section(&sections, section_id)
.render_diff(PreviousWorldStateSection::Absent)
+ .1
.ok_or("skill catalog should render through world state")?;
assert!(fragment.body().contains(expected_line));
}
@@ -715,11 +717,13 @@ async fn nonempty_executor_empty_host_records_catalog_metrics() -> TestResult {
assert!(
sections[0]
.render_diff(PreviousWorldStateSection::Absent)
+ .1
.is_some()
);
assert!(
world_state_section(&sections, "host_skills")
.render_diff(PreviousWorldStateSection::Absent)
+ .1
.is_none()
);
let expected = expected_catalog_metric_samples("executor_world_state", /*count*/ 1);
@@ -791,6 +795,7 @@ async fn host_world_state_uses_provider_catalog_with_core_compatible_rendering()
.await;
let host_fragment = world_state_section(&sections, "host_skills")
.render_diff(PreviousWorldStateSection::Absent)
+ .1
.ok_or("host provider catalog should render")?;
assert!(host_fragment.body().contains("Fix lint errors."));
@@ -890,6 +895,7 @@ async fn shadow_selection_uses_host_catalog_when_instructions_are_disabled() ->
assert!(
world_state_section(&sections, "host_skills")
.render_diff(PreviousWorldStateSection::Absent)
+ .1
.is_none()
);
assert!(fragments.is_empty());
@@ -1147,10 +1153,10 @@ async fn selected_executor_catalog_follows_step_availability_and_reuses_its_cach
})
.await;
assert_eq!(1, available_sections.len());
- let available_snapshot = available_sections[0].snapshot().clone();
- let available_fragment = available_sections[0]
- .render_diff(PreviousWorldStateSection::Absent)
- .ok_or("available skills should render")?;
+ let (available_snapshot, available_fragment) =
+ available_sections[0].render_diff(PreviousWorldStateSection::Absent);
+ let available_snapshot = available_snapshot.unwrap();
+ let available_fragment = available_fragment.ok_or("available skills should render")?;
assert!(available_fragment.body().contains("lint-fix"));
assert!(
available_fragment
@@ -1205,7 +1211,9 @@ async fn selected_executor_catalog_follows_step_availability_and_reuses_its_cach
turn_store: &unavailable_turn_store,
})
.await;
- let unavailable_snapshot = unavailable_sections[0].snapshot().clone();
+ let (unavailable_snapshot, unavailable_fragment) =
+ unavailable_sections[0].render_diff(PreviousWorldStateSection::Known(&available_snapshot));
+ let unavailable_snapshot = unavailable_snapshot.unwrap();
assert_eq!(
unavailable_snapshot,
serde_json::json!({
@@ -1216,9 +1224,7 @@ async fn selected_executor_catalog_follows_step_availability_and_reuses_its_cach
).to_hex().to_string(),
}),
);
- let unavailable_fragment = unavailable_sections[0]
- .render_diff(PreviousWorldStateSection::Known(&available_snapshot))
- .ok_or("removed skills should render")?;
+ let unavailable_fragment = unavailable_fragment.ok_or("removed skills should render")?;
assert!(
unavailable_fragment
.body()
@@ -1243,10 +1249,10 @@ async fn selected_executor_catalog_follows_step_availability_and_reuses_its_cach
turn_store: &restored_turn_store,
})
.await;
- let restored_snapshot = restored_sections[0].snapshot().clone();
- let restored_fragment = restored_sections[0]
- .render_diff(PreviousWorldStateSection::Known(&unavailable_snapshot))
- .ok_or("restored skills should render")?;
+ let (restored_snapshot, restored_fragment) =
+ restored_sections[0].render_diff(PreviousWorldStateSection::Known(&unavailable_snapshot));
+ let restored_snapshot = restored_snapshot.unwrap();
+ let restored_fragment = restored_fragment.ok_or("restored skills should render")?;
assert_eq!(
restored_fragment.body(),
"\n## Skills update\nThe previously listed selected-environment skills are available again.\n",
@@ -1256,7 +1262,9 @@ async fn selected_executor_catalog_follows_step_availability_and_reuses_its_cach
!restored_sections[0].matches_retained_fragment("developer", unavailable_fragment.body())
);
assert_eq!(
- restored_sections[0].render_diff(PreviousWorldStateSection::Absent),
+ restored_sections[0]
+ .render_diff(PreviousWorldStateSection::Absent)
+ .1,
Some(available_fragment),
);
assert_eq!(1, list_calls.load(Ordering::Relaxed));
@@ -1326,14 +1334,15 @@ async fn selected_executor_catalog_follows_step_availability_and_reuses_its_cach
turn_store: &listing_disabled_turn_store,
})
.await;
- let listing_disabled_fragment = listing_disabled_sections[0]
- .render_diff(PreviousWorldStateSection::Known(&restored_snapshot))
- .ok_or("disabled skill listing should render")?;
+ let (listing_disabled_snapshot, listing_disabled_fragment) = listing_disabled_sections[0]
+ .render_diff(PreviousWorldStateSection::Known(&restored_snapshot));
+ let listing_disabled_fragment =
+ listing_disabled_fragment.ok_or("disabled skill listing should render")?;
assert_eq!(
"\n## Skills update\nSelected-environment skills are not listed automatically. Explicit skill mentions can still be resolved when available.\n",
listing_disabled_fragment.body()
);
- let mut normalized_listing_disabled_snapshot = listing_disabled_sections[0].snapshot().clone();
+ let mut normalized_listing_disabled_snapshot = listing_disabled_snapshot.unwrap();
normalized_listing_disabled_snapshot
.as_object_mut()
.ok_or("skills snapshot should be an object")?
@@ -1343,6 +1352,7 @@ async fn selected_executor_catalog_follows_step_availability_and_reuses_its_cach
.render_diff(PreviousWorldStateSection::Known(
&normalized_listing_disabled_snapshot
))
+ .1
.is_none()
);
@@ -1589,17 +1599,26 @@ async fn catalog_rebalances_only_to_avoid_omissions_and_retains_the_allocation()
turn_store: &turn_store,
})
.await;
- let cloud = world_state_section(&sections, "cloud_skills");
- let cloud_body = cloud.snapshot()["body"].as_str().ok_or("cloud catalog")?;
+ let rendered = sections
+ .iter()
+ .map(|section| {
+ let prior = previous.get(section.id()).map_or(
+ PreviousWorldStateSection::Absent,
+ PreviousWorldStateSection::Known,
+ );
+ (section.id().to_string(), section.render_diff(prior))
+ })
+ .collect::<std::collections::BTreeMap<_, _>>();
+ let cloud = &rendered["cloud_skills"];
+ let cloud_snapshot = cloud.0.as_ref().expect("cloud snapshot");
+ let cloud_body = cloud_snapshot["body"].as_str().ok_or("cloud catalog")?;
match phase {
Phase::Initial => initial_cloud = Some(cloud_body.to_string()),
Phase::FirstReady => {
assert_eq!(initial_cloud.as_deref() != Some(cloud_body), rebalance);
- accepted_cloud = Some(cloud.snapshot().clone());
- let executor = world_state_section(&sections, "skills");
- let executor_body = executor.snapshot()["body"]
- .as_str()
- .ok_or("executor catalog")?;
+ accepted_cloud = Some(cloud_snapshot.clone());
+ let executor = rendered["skills"].0.as_ref().expect("executor snapshot");
+ let executor_body = executor["body"].as_str().ok_or("executor catalog")?;
assert_eq!(
executor_body
.lines()
@@ -1619,7 +1638,7 @@ async fn catalog_rebalances_only_to_avoid_omissions_and_retains_the_allocation()
}
}
Phase::CloudCatalogGrew => {
- let allocation = &cloud.snapshot()["allocation"];
+ let allocation = &cloud_snapshot["allocation"];
let previous_limit = previous["cloud_skills"]["allocation"]["cloudLimit"]
.as_u64()
.ok_or("previous cloud limit")?;
@@ -1638,9 +1657,9 @@ async fn catalog_rebalances_only_to_avoid_omissions_and_retains_the_allocation()
.count(),
15
);
- let executor = world_state_section(&sections, "skills");
+ let executor = rendered["skills"].0.as_ref().expect("executor snapshot");
assert_eq!(
- executor.snapshot()["body"]
+ executor["body"]
.as_str()
.ok_or("executor catalog")?
.lines()
@@ -1648,22 +1667,16 @@ async fn catalog_rebalances_only_to_avoid_omissions_and_retains_the_allocation()
.count(),
executor_count
);
- accepted_cloud = Some(cloud.snapshot().clone());
+ accepted_cloud = Some(cloud_snapshot.clone());
}
Phase::Disconnected | Phase::Reconnected | Phase::Resumed => {
- assert_eq!(Some(cloud.snapshot()), accepted_cloud.as_ref());
- assert!(
- cloud
- .render_diff(PreviousWorldStateSection::Known(
- &previous["cloud_skills"]
- ))
- .is_none()
- );
+ assert_eq!(Some(cloud_snapshot), accepted_cloud.as_ref());
+ assert!(cloud.1.is_none());
}
Phase::BudgetChanged => {
// A genuinely different total budget starts a new allocation.
assert_eq!(
- cloud.snapshot()["allocation"],
+ cloud_snapshot["allocation"],
serde_json::json!({
"totalBudget": {"tokens": 4_000}, "cloudLimit": 3_000,
"cloudCatalogFingerprint": previous["cloud_skills"]["allocation"]["cloudCatalogFingerprint"],
@@ -1671,9 +1684,9 @@ async fn catalog_rebalances_only_to_avoid_omissions_and_retains_the_allocation()
);
}
}
- previous = sections
- .iter()
- .map(|section| (section.id().to_string(), section.snapshot().clone()))
+ previous = rendered
+ .into_iter()
+ .map(|(id, (snapshot, _))| (id, snapshot.expect("skills snapshot")))
.collect();
}
}
@@ -2358,13 +2371,14 @@ async fn model_context_window_scales_executor_and_cloud_catalogs() -> TestResult
.await;
assert!(event_rx.try_recv().is_err());
let executor_section = world_state_section(&sections, "skills");
- let snapshot = executor_section.snapshot().clone();
- let executor_fragment = executor_section
- .render_diff(PreviousWorldStateSection::Absent)
- .ok_or("bounded executor catalog should render")?;
+ let (snapshot, executor_fragment) =
+ executor_section.render_diff(PreviousWorldStateSection::Absent);
+ let snapshot = snapshot.unwrap();
+ let executor_fragment = executor_fragment.ok_or("bounded executor catalog should render")?;
assert!(!executor_fragment.body().contains("skill-39"));
let cloud_fragment = world_state_section(&sections, "cloud_skills")
.render_diff(PreviousWorldStateSection::Absent)
+ .1
.ok_or("bounded cloud catalog should render")?;
assert!(cloud_fragment.body().contains("additional skills omitted"));
let warnings = event_rx
@@ -2389,6 +2403,7 @@ async fn model_context_window_scales_executor_and_cloud_catalogs() -> TestResult
assert!(
executor_section
.render_diff(PreviousWorldStateSection::Known(&snapshot))
+ .1
.is_none()
);
assert!(event_rx.try_recv().is_err());
diff --git a/codex-rs/ext/skills/tests/skills_extension/presentation_dedup_tests.rs b/codex-rs/ext/skills/tests/skills_extension/presentation_dedup_tests.rs
index ad078fba53..3ab7d57630 100644
--- a/codex-rs/ext/skills/tests/skills_extension/presentation_dedup_tests.rs
+++ b/codex-rs/ext/skills/tests/skills_extension/presentation_dedup_tests.rs
@@ -118,11 +118,22 @@ async fn cloud_preference_preserves_aliases_reads_and_executor_fallback() -> Tes
turn_store: &turn_store,
})
.await;
- let executor_body = world_state_section(&sections, "skills").snapshot()["body"]
+ let snapshots: serde_json::Map<String, serde_json::Value> = sections
+ .iter()
+ .map(|section| {
+ let previous = previous.get(section.id()).map_or(
+ PreviousWorldStateSection::Absent,
+ PreviousWorldStateSection::Known,
+ );
+ let snapshot = section.render_diff(previous).0.expect("skills snapshot");
+ (section.id().to_string(), snapshot)
+ })
+ .collect();
+ let executor_body = snapshots["skills"]["body"]
.as_str()
.unwrap_or_default()
.to_string();
- let cloud_body = world_state_section(&sections, "cloud_skills").snapshot()["body"]
+ let cloud_body = snapshots["cloud_skills"]["body"]
.as_str()
.unwrap_or_default()
.to_string();
@@ -156,10 +167,7 @@ async fn cloud_preference_preserves_aliases_reads_and_executor_fallback() -> Tes
assert!(executor_body.contains(&format!("- `e{index}` = `{root}`")));
}
}
- previous = sections
- .iter()
- .map(|section| (section.id().to_string(), section.snapshot().clone()))
- .collect();
+ previous = snapshots;
if !ready {
continue;
}