Skip to content

Commit f0c5077

Browse files
ioanbudea-OAIcopyberry
authored andcommitted
Centralize context fragment kind constants (#54316)
## What changed Replace duplicated content-kind strings with constants shared by fragment producers and consumers, including authorization checks, forked-history filtering, and memory instructions. Share the compaction summary kind between core and Guardian's transcript filter. ## Testing Add a regression test verifying that Guardian excludes compaction summaries while retaining surrounding user messages, including a user message with identical text. GitOrigin-RevId: 36dbdf0f82c61b52d2571ac1c392fb151c98151b
1 parent 86ee40c commit f0c5077

14 files changed

Lines changed: 76 additions & 18 deletions

File tree

‎codex-rs/context-fragments/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ pub use fragment::ContextualUserFragment;
1717
pub use fragment::RenderedFragment;
1818

1919
pub use recap_prompt::RecapPrompt;
20+
21+
/// Shared classification for compaction summaries produced by core and filtered by Guardian.
22+
pub const COMPACTION_SUMMARY_KIND: &str = "compaction.summary";

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::context::ContextualUserFragment;
1717
use crate::context::CurrentTimeReminder;
1818
use crate::context::CurrentTimeUnavailable;
1919
use crate::context::DeveloperInstructions;
20+
use crate::context::GuardianApprovedAction;
2021
use crate::context::ManagedDeveloperInstructions;
2122
use crate::context::ModelInstructions;
2223
use crate::context::MultiAgentModeInstructions;
@@ -141,7 +142,9 @@ fn retain_forked_developer_message(item: &mut ResponseItem, usage_hint_texts: &[
141142
// Persisted role hints can predate the current bundled wording and lack markers.
142143
if matches!(
143144
content_item.kind().as_str(),
144-
"guardian.approved_action" | "multi_agent.role_instructions" | "multi_agent.usage_hint"
145+
GuardianApprovedAction::KIND
146+
| MultiAgentRoleInstructions::KIND
147+
| MultiAgentUsageHint::KIND
145148
) {
146149
return false;
147150
}

‎codex-rs/core/src/context/compaction_summary.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub(crate) struct CompactionSummary {
77
}
88

99
impl CompactionSummary {
10+
pub(crate) const KIND: &str = codex_context_fragments::COMPACTION_SUMMARY_KIND;
11+
1012
pub(crate) fn new(summary: impl Into<String>) -> Self {
1113
Self {
1214
summary: summary.into(),
@@ -16,7 +18,7 @@ impl CompactionSummary {
1618

1719
impl ContextualUserFragment for CompactionSummary {
1820
fn content_kind(&self) -> ContentItemKind {
19-
ContentItemKind("compaction.summary".to_string())
21+
ContentItemKind(Self::KIND.to_string())
2022
}
2123

2224
fn role(&self) -> &'static str {

‎codex-rs/core/src/context/contextual_user_message.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ use super::LegacyUnifiedExecProcessLimitWarning;
1414
use super::RecommendedPluginsInstructions;
1515
use super::SubagentNotification;
1616
use super::TurnAborted;
17+
use super::UnsupportedMedia;
1718
use super::UserGoalUpdate;
1819
use super::UserInstructions;
1920
use super::UserShellCommand;
2021
use super::world_state::EnvironmentsState;
22+
use crate::image_preparation::ImagePreparationError;
2123

2224
const CONTEXTUAL_USER_FRAGMENT_MATCHERS: &[fn(&str) -> bool] = &[
2325
UserInstructions::matches_text,
@@ -73,9 +75,9 @@ pub(crate) fn is_user_authorization_message(item: &ResponseItem) -> bool {
7375
kind.0.as_str(),
7476
"" | "unknown"
7577
// Media preparation can replace real user input.
76-
| "images.preparation_error"
77-
| "images.unsupported"
78-
| "audio.unsupported"
78+
| ImagePreparationError::KIND
79+
| UnsupportedMedia::IMAGE_KIND
80+
| UnsupportedMedia::AUDIO_KIND
7981
)
8082
})
8183
})

‎codex-rs/core/src/context/guardian_approved_action.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub(crate) struct GuardianApprovedAction {
88
}
99

1010
impl GuardianApprovedAction {
11+
pub(crate) const KIND: &str = "guardian.approved_action";
12+
1113
pub(crate) fn new(approved_action_json: String) -> Self {
1214
Self {
1315
approved_action_json,
@@ -17,7 +19,7 @@ impl GuardianApprovedAction {
1719

1820
impl ContextualUserFragment for GuardianApprovedAction {
1921
fn content_kind(&self) -> ContentItemKind {
20-
ContentItemKind("guardian.approved_action".to_string())
22+
ContentItemKind(Self::KIND.to_string())
2123
}
2224

2325
fn role(&self) -> &'static str {

‎codex-rs/core/src/context/memory.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ pub enum MemoryContextFragment {
1111
ExtractionEvidence(String),
1212
}
1313

14+
impl MemoryContextFragment {
15+
/// Content classification for memory read-path instructions.
16+
pub const READ_INSTRUCTIONS_KIND: &str = "memories.instructions";
17+
}
18+
1419
impl ContextualUserFragment for MemoryContextFragment {
1520
fn role(&self) -> &'static str {
1621
match self {
@@ -21,7 +26,7 @@ impl ContextualUserFragment for MemoryContextFragment {
2126
fn content_kind(&self) -> ContentItemKind {
2227
ContentItemKind(
2328
match self {
24-
Self::ReadInstructions(_) => "memories.instructions",
29+
Self::ReadInstructions(_) => Self::READ_INSTRUCTIONS_KIND,
2530
Self::ExtractionEvidence(_) => "memories.extraction_evidence",
2631
}
2732
.to_string(),

‎codex-rs/core/src/context/multi_agent_usage_hint.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub(crate) struct MultiAgentUsageHint {
88
}
99

1010
impl MultiAgentUsageHint {
11+
pub(crate) const KIND: &str = "multi_agent.usage_hint";
12+
1113
pub(crate) fn new(text: &str) -> Self {
1214
Self {
1315
text: text.to_string(),
@@ -17,7 +19,7 @@ impl MultiAgentUsageHint {
1719

1820
impl ContextualUserFragment for MultiAgentUsageHint {
1921
fn content_kind(&self) -> ContentItemKind {
20-
ContentItemKind("multi_agent.usage_hint".to_string())
22+
ContentItemKind(Self::KIND.to_string())
2123
}
2224

2325
fn role(&self) -> &'static str {

‎codex-rs/core/src/context/unsupported_media.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ pub(crate) struct UnsupportedMedia {
88
}
99

1010
impl UnsupportedMedia {
11+
pub(crate) const IMAGE_KIND: &str = "images.unsupported";
12+
pub(crate) const AUDIO_KIND: &str = "audio.unsupported";
13+
1114
pub(crate) const IMAGE: Self = Self {
1215
text: "image content omitted because you do not support image input",
13-
content_kind: "images.unsupported",
16+
content_kind: Self::IMAGE_KIND,
1417
};
1518

1619
pub(crate) const AUDIO: Self = Self {
1720
text: "audio content omitted because you do not support audio input",
18-
content_kind: "audio.unsupported",
21+
content_kind: Self::AUDIO_KIND,
1922
};
2023
}
2124

‎codex-rs/core/src/context/user_goal.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub enum UserGoalUpdate {
2323
}
2424

2525
impl UserGoalUpdate {
26+
pub(crate) const KIND: &str = "user.goal";
2627
pub(crate) const OMITTED_OBJECTIVE_KIND: &str = "user.goal.omitted";
2728

2829
/// Captures accepted goal instructions at a compaction input boundary.
@@ -53,7 +54,7 @@ impl UserGoalUpdate {
5354
let [ContentItem::InputText { text }] = content.as_slice() else {
5455
return None;
5556
};
56-
(role == "user" && matches!(kind.0.as_str(), "user.goal" | Self::OMITTED_OBJECTIVE_KIND))
57+
(role == "user" && matches!(kind.0.as_str(), Self::KIND | Self::OMITTED_OBJECTIVE_KIND))
5758
.then_some(text)
5859
}
5960
}
@@ -71,7 +72,7 @@ impl ContextualUserFragment for UserGoalUpdate {
7172
} if serde_json::json!(objective).to_string().len() > MAX_OBJECTIVE_BYTES => {
7273
Self::OMITTED_OBJECTIVE_KIND
7374
}
74-
Self::Set { .. } | Self::Clear => "user.goal",
75+
Self::Set { .. } | Self::Clear => Self::KIND,
7576
};
7677
ContentItemKind(kind.to_owned())
7778
}

‎codex-rs/core/src/image_preparation.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ pub(crate) enum ImagePreparationError {
107107
}
108108

109109
impl ImagePreparationError {
110+
pub(crate) const KIND: &str = "images.preparation_error";
111+
110112
fn placeholder(&self) -> &'static str {
111113
match self {
112114
ImagePreparationError::RemoteUrlUnsupported => REMOTE_IMAGE_URL_PLACEHOLDER,
@@ -285,7 +287,7 @@ async fn prepare_message_content(
285287
warn!(%error, "failed to prepare message image");
286288
*item = AnnotatedContent::text(
287289
error.placeholder(),
288-
ContentItemKind("images.preparation_error".to_string()),
290+
ContentItemKind(ImagePreparationError::KIND.to_string()),
289291
codex_protocol::models::ContentItemMetadata::harness(),
290292
);
291293
}

0 commit comments

Comments
 (0)