mirror of
https://github.com/openai/codex.git
synced 2026-09-14 11:57:03 +00:00
## Summary
- bundle contextual prompt injection into at most one developer message
plus one contextual user message in both:
- per-turn settings updates
- initial context insertion
- preserve `<model_switch>` across compaction by rebuilding it through
canonical initial-context injection, instead of relying on
strip/reattach hacks
- centralize contextual user fragment detection in one shared definition
table and reuse it for parsing/compaction logic
- keep `AGENTS.md` in its natural serialized format:
- `# AGENTS.md instructions for {dirname}`
- `<INSTRUCTIONS>...</INSTRUCTIONS>`
- simplify related tests/helpers and accept the expected snapshot/layout
updates from bundled multi-part messages
## Why
The goal is to converge toward a simpler, more intentional prompt shape
where contextual updates are consistently represented as one developer
envelope plus one contextual user envelope, while keeping parsing and
compaction behavior aligned with that representation.
## Notable details
- the temporary `SettingsUpdateEnvelope` wrapper was removed; these
paths now return `Vec<ResponseItem>` directly
- local/remote compaction no longer rely on model-switch strip/restore
helpers
- contextual user detection is now driven by shared fragment definitions
instead of ad hoc matcher assembly
- AGENTS/user instructions are still the same logical context; only the
synthetic `<user_instructions>` wrapper was replaced by the natural
AGENTS text format
## Testing
- `just fmt`
- `cargo test -p codex-app-server
codex_message_processor::tests::extract_conversation_summary_prefers_plain_user_messages
-- --exact`
- `cargo test -p codex-core
compact::tests::collect_user_messages_filters_session_prefix_entries
--lib -- --exact`
- `cargo test -p codex-core --test all
'suite::compact::snapshot_request_shape_pre_turn_compaction_strips_incoming_model_switch'
-- --exact`
- `cargo test -p codex-core --test all
'suite::compact_remote::snapshot_request_shape_remote_pre_turn_compaction_strips_incoming_model_switch'
-- --exact`
- `cargo test -p codex-core --test all
'suite::client::includes_apps_guidance_as_developer_message_when_enabled'
-- --exact`
- `cargo test -p codex-core --test all
'suite::client::includes_developer_instructions_message_in_request' --
--exact`
- `cargo test -p codex-core --test all
'suite::client::includes_user_instructions_message_in_request' --
--exact`
- `cargo test -p codex-core --test all
'suite::client::resume_includes_initial_messages_and_sends_prior_items'
-- --exact`
- `cargo test -p codex-core --test all
'suite::review::review_input_isolated_from_parent_history' -- --exact`
- `cargo test -p codex-exec --test all
'suite::resume::exec_resume_last_respects_cwd_filter_and_all_flag' --
--exact`
- `cargo test -p core_test_support
context_snapshot::tests::full_text_mode_preserves_unredacted_text --
--exact`
## Notes
- I also ran several targeted `compact`, `compact_remote`,
`prompt_caching`, `model_visible_layout`, and `event_mapping` tests
while iterating on prompt-shape changes.
- I have not claimed a clean full-workspace `cargo test` from this
environment because local sandbox/resource conditions have previously
produced unrelated failures in large workspace runs.
140 lines
4.9 KiB
Rust
140 lines
4.9 KiB
Rust
use codex_protocol::models::ContentItem;
|
|
use codex_protocol::models::ResponseItem;
|
|
use codex_protocol::protocol::ENVIRONMENT_CONTEXT_CLOSE_TAG;
|
|
use codex_protocol::protocol::ENVIRONMENT_CONTEXT_OPEN_TAG;
|
|
|
|
pub(crate) const AGENTS_MD_START_MARKER: &str = "# AGENTS.md instructions for ";
|
|
pub(crate) const AGENTS_MD_END_MARKER: &str = "</INSTRUCTIONS>";
|
|
pub(crate) const SKILL_OPEN_TAG: &str = "<skill>";
|
|
pub(crate) const SKILL_CLOSE_TAG: &str = "</skill>";
|
|
pub(crate) const USER_SHELL_COMMAND_OPEN_TAG: &str = "<user_shell_command>";
|
|
pub(crate) const USER_SHELL_COMMAND_CLOSE_TAG: &str = "</user_shell_command>";
|
|
pub(crate) const TURN_ABORTED_OPEN_TAG: &str = "<turn_aborted>";
|
|
pub(crate) const TURN_ABORTED_CLOSE_TAG: &str = "</turn_aborted>";
|
|
pub(crate) const SUBAGENT_NOTIFICATION_OPEN_TAG: &str = "<subagent_notification>";
|
|
pub(crate) const SUBAGENT_NOTIFICATION_CLOSE_TAG: &str = "</subagent_notification>";
|
|
|
|
#[derive(Clone, Copy)]
|
|
pub(crate) struct ContextualUserFragmentDefinition {
|
|
start_marker: &'static str,
|
|
end_marker: &'static str,
|
|
}
|
|
|
|
impl ContextualUserFragmentDefinition {
|
|
pub(crate) const fn new(start_marker: &'static str, end_marker: &'static str) -> Self {
|
|
Self {
|
|
start_marker,
|
|
end_marker,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn matches_text(&self, text: &str) -> bool {
|
|
let trimmed = text.trim_start();
|
|
let starts_with_marker = trimmed
|
|
.get(..self.start_marker.len())
|
|
.is_some_and(|candidate| candidate.eq_ignore_ascii_case(self.start_marker));
|
|
let trimmed = trimmed.trim_end();
|
|
let ends_with_marker = trimmed
|
|
.get(trimmed.len().saturating_sub(self.end_marker.len())..)
|
|
.is_some_and(|candidate| candidate.eq_ignore_ascii_case(self.end_marker));
|
|
starts_with_marker && ends_with_marker
|
|
}
|
|
|
|
pub(crate) const fn start_marker(&self) -> &'static str {
|
|
self.start_marker
|
|
}
|
|
|
|
pub(crate) const fn end_marker(&self) -> &'static str {
|
|
self.end_marker
|
|
}
|
|
|
|
pub(crate) fn wrap(&self, body: String) -> String {
|
|
format!("{}\n{}\n{}", self.start_marker, body, self.end_marker)
|
|
}
|
|
|
|
pub(crate) fn into_message(self, text: String) -> ResponseItem {
|
|
ResponseItem::Message {
|
|
id: None,
|
|
role: "user".to_string(),
|
|
content: vec![ContentItem::InputText { text }],
|
|
end_turn: None,
|
|
phase: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) const AGENTS_MD_FRAGMENT: ContextualUserFragmentDefinition =
|
|
ContextualUserFragmentDefinition::new(AGENTS_MD_START_MARKER, AGENTS_MD_END_MARKER);
|
|
pub(crate) const ENVIRONMENT_CONTEXT_FRAGMENT: ContextualUserFragmentDefinition =
|
|
ContextualUserFragmentDefinition::new(
|
|
ENVIRONMENT_CONTEXT_OPEN_TAG,
|
|
ENVIRONMENT_CONTEXT_CLOSE_TAG,
|
|
);
|
|
pub(crate) const SKILL_FRAGMENT: ContextualUserFragmentDefinition =
|
|
ContextualUserFragmentDefinition::new(SKILL_OPEN_TAG, SKILL_CLOSE_TAG);
|
|
pub(crate) const USER_SHELL_COMMAND_FRAGMENT: ContextualUserFragmentDefinition =
|
|
ContextualUserFragmentDefinition::new(
|
|
USER_SHELL_COMMAND_OPEN_TAG,
|
|
USER_SHELL_COMMAND_CLOSE_TAG,
|
|
);
|
|
pub(crate) const TURN_ABORTED_FRAGMENT: ContextualUserFragmentDefinition =
|
|
ContextualUserFragmentDefinition::new(TURN_ABORTED_OPEN_TAG, TURN_ABORTED_CLOSE_TAG);
|
|
pub(crate) const SUBAGENT_NOTIFICATION_FRAGMENT: ContextualUserFragmentDefinition =
|
|
ContextualUserFragmentDefinition::new(
|
|
SUBAGENT_NOTIFICATION_OPEN_TAG,
|
|
SUBAGENT_NOTIFICATION_CLOSE_TAG,
|
|
);
|
|
|
|
const CONTEXTUAL_USER_FRAGMENTS: &[ContextualUserFragmentDefinition] = &[
|
|
AGENTS_MD_FRAGMENT,
|
|
ENVIRONMENT_CONTEXT_FRAGMENT,
|
|
SKILL_FRAGMENT,
|
|
USER_SHELL_COMMAND_FRAGMENT,
|
|
TURN_ABORTED_FRAGMENT,
|
|
SUBAGENT_NOTIFICATION_FRAGMENT,
|
|
];
|
|
|
|
pub(crate) fn is_contextual_user_fragment(content_item: &ContentItem) -> bool {
|
|
let ContentItem::InputText { text } = content_item else {
|
|
return false;
|
|
};
|
|
CONTEXTUAL_USER_FRAGMENTS
|
|
.iter()
|
|
.any(|definition| definition.matches_text(text))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn detects_environment_context_fragment() {
|
|
assert!(is_contextual_user_fragment(&ContentItem::InputText {
|
|
text: "<environment_context>\n<cwd>/tmp</cwd>\n</environment_context>".to_string(),
|
|
}));
|
|
}
|
|
|
|
#[test]
|
|
fn detects_agents_instructions_fragment() {
|
|
assert!(is_contextual_user_fragment(&ContentItem::InputText {
|
|
text: "# AGENTS.md instructions for /tmp\n\n<INSTRUCTIONS>\nbody\n</INSTRUCTIONS>"
|
|
.to_string(),
|
|
}));
|
|
}
|
|
|
|
#[test]
|
|
fn detects_subagent_notification_fragment_case_insensitively() {
|
|
assert!(
|
|
SUBAGENT_NOTIFICATION_FRAGMENT
|
|
.matches_text("<SUBAGENT_NOTIFICATION>{}</subagent_notification>")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn ignores_regular_user_text() {
|
|
assert!(!is_contextual_user_fragment(&ContentItem::InputText {
|
|
text: "hello".to_string(),
|
|
}));
|
|
}
|
|
}
|