Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,7 @@ async fn app_server_uses_configured_notes_backend_for_context_window_hints(
})
.count();
assert_eq!(bridge_calls, usize::from(!use_history_notes_extension));
let developer_messages: Vec<_> = request
.message_input_text_groups("developer")
.into_iter()
.map(|parts| parts.concat())
.collect();
let developer_messages = request.message_input_texts("developer");
let context_window = developer_messages
.iter()
.find(|text| text.contains("<context_window>"))
Expand Down
1 change: 0 additions & 1 deletion codex-rs/codex-mcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub use runtime::McpRuntime;
pub use runtime::McpRuntimeContext;
pub use runtime::McpRuntimeInput;
pub use runtime::McpStartupPolicy;
pub use runtime::McpToolCallResult;
pub use runtime::SandboxState;
pub use runtime::apply_http_headers_helper;
pub use tool_catalog_cache::McpToolCatalogCache;
Expand Down
54 changes: 13 additions & 41 deletions codex-rs/codex-mcp/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ pub enum McpStartupPolicy {
LazyWhenCached,
}

/// A host-requested tool result and its identity in the published MCP catalog.
pub struct McpToolCallResult {
pub result: CallToolResult,
/// None when no retained binding identifies the tool. This is not MCP wire metadata.
pub source_tool_namespace: Option<String>,
}

/// Configuration and owning-thread state needed to materialize an MCP runtime.
pub struct McpRuntimeInput {
pub startup_policy: McpStartupPolicy,
Expand Down Expand Up @@ -650,18 +643,6 @@ impl McpRuntime {
self.latest_connections().list_all_tools().await
}

pub async fn latest_read_resource(
&self,
server: &str,
params: ReadResourceRequestParams,
) -> anyhow::Result<ReadResourceResult> {
self.latest_connections()
.read_resource(server, params)
.await
}

/// Calls a tool for the host and reports its callable namespace from the same runtime.
/// Missing cached identity never blocks execution; its namespace remains unknown.
#[allow(clippy::too_many_arguments)]
pub async fn latest_call_tool(
&self,
Expand All @@ -672,23 +653,8 @@ impl McpRuntime {
meta: Option<serde_json::Value>,
requested_timeout: Option<Duration>,
wait_for_server: bool,
) -> anyhow::Result<McpToolCallResult> {
let current = self.current.load_full();
// Attribution must never delay a successful call. The binding is immutable;
// if it does not describe this host-only tool, keep its namespace unknown.
let namespace = current
.cached_binding
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.as_ref()
.and_then(|cached| cached.binding.upgrade())
.and_then(|binding| {
binding
.tool_info(server, tool)
.map(|info| info.callable_namespace.clone())
});
let result = current
.connections
) -> anyhow::Result<CallToolResult> {
self.latest_connections()
.call_tool(
server,
tool,
Expand All @@ -698,11 +664,17 @@ impl McpRuntime {
requested_timeout,
wait_for_server,
)
.await?;
Ok(McpToolCallResult {
result,
source_tool_namespace: namespace,
})
.await
}

pub async fn latest_read_resource(
&self,
server: &str,
params: ReadResourceRequestParams,
) -> anyhow::Result<ReadResourceResult> {
self.latest_connections()
.read_resource(server, params)
.await
}

pub async fn latest_wait_for_server_ready(&self, server: &str, timeout: Duration) -> bool {
Expand Down
86 changes: 19 additions & 67 deletions codex-rs/context-fragments/src/annotated_content.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
use codex_protocol::models::ContentItem;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::ContentItemMetadata;
use codex_protocol::models::ResponseItem;

/// Content, classification and attribution travel together through edits and truncation.
/// Model-visible content paired with its harness-owned classification.
#[derive(Clone, Debug, PartialEq)]
pub struct AnnotatedContent {
content: ContentItem,
kind: ContentItemKind,
metadata: ContentItemMetadata,
}

impl AnnotatedContent {
/// Creates content and its classification together.
pub fn new(content: ContentItem, kind: ContentItemKind) -> Self {
Self {
content,
kind,
metadata: ContentItemMetadata::default(),
}
Self { content, kind }
}

/// Creates text with the attribution supplied by its producer.
pub fn text(
text: impl Into<String>,
kind: ContentItemKind,
metadata: ContentItemMetadata,
) -> Self {
Self {
content: ContentItem::InputText { text: text.into() },
kind,
metadata,
}
/// Creates model-visible input text and its classification together.
pub fn input_text(text: impl Into<String>, kind: ContentItemKind) -> Self {
Self::new(ContentItem::InputText { text: text.into() }, kind)
}

/// Returns the model-visible content.
Expand All @@ -49,29 +35,16 @@ impl AnnotatedContent {
&self.kind
}

/// Separates content and its positional annotations at an API boundary.
pub fn into_parts(self) -> (ContentItem, ContentItemKind, ContentItemMetadata) {
(self.content, self.kind, self.metadata)
/// Separates the content from its classification at an API boundary.
pub fn into_parts(self) -> (ContentItem, ContentItemKind) {
(self.content, self.kind)
}
}

/// Assembles a message without separating its content from its attribution.
pub fn message_from_parts(role: impl Into<String>, parts: Vec<AnnotatedContent>) -> ResponseItem {
let mut item = ResponseItem::Message {
id: None,
role: role.into(),
content: Vec::new(),
phase: None,
internal_chat_message_metadata_passthrough: None,
};
set_annotated_content(&mut item, parts);
item
}

/// Takes a message's content together with its classification and source metadata.
/// Takes a message's content together with its positional classifications.
///
/// Legacy messages, including persisted rollouts, may not have classifications.
/// Missing entries have unknown classification and source so the message remains usable.
/// Missing entries are classified as unknown so the message remains usable.
pub fn to_annotated_content(item: &mut ResponseItem) -> Option<Vec<AnnotatedContent>> {
let ResponseItem::Message {
content,
Expand All @@ -86,32 +59,19 @@ pub fn to_annotated_content(item: &mut ResponseItem) -> Option<Vec<AnnotatedCont
.as_mut()
.and_then(|metadata| metadata.content_item_kinds.take())
.unwrap_or_default();
let metadata = internal_chat_message_metadata_passthrough
.as_mut()
.and_then(|metadata| metadata.content_item_metadata.take())
.unwrap_or_default();

Some(
std::mem::take(content)
.into_iter()
.zip(kinds.into_iter().chain(std::iter::repeat_with(|| {
ContentItemKind("unknown".to_string())
})))
.zip(
metadata
.into_iter()
.chain(std::iter::repeat_with(ContentItemMetadata::default)),
)
.map(|((content, kind), metadata)| AnnotatedContent {
content,
kind,
metadata,
})
.map(|(content, kind)| AnnotatedContent::new(content, kind))
.collect(),
)
}

/// Replaces content and its positional annotations together.
/// Replaces a message's content and positional classifications together.
pub fn set_annotated_content(
item: &mut ResponseItem,
annotated_content: Vec<AnnotatedContent>,
Expand All @@ -125,22 +85,14 @@ pub fn set_annotated_content(
return None;
};

let mut updated_content = Vec::with_capacity(annotated_content.len());
let mut content_item_kinds = Vec::with_capacity(annotated_content.len());
let mut content_item_metadata = Vec::with_capacity(annotated_content.len());
for annotated in annotated_content {
let (content, kind, metadata) = annotated.into_parts();
updated_content.push(content);
content_item_kinds.push(kind);
content_item_metadata.push(metadata);
}
let (updated_content, content_item_kinds): (Vec<_>, Vec<_>) = annotated_content
.into_iter()
.map(AnnotatedContent::into_parts)
.unzip();
*content = updated_content;
let metadata = internal_chat_message_metadata_passthrough.get_or_insert_default();
metadata.content_item_kinds = Some(content_item_kinds);
metadata.content_item_metadata = content_item_metadata
.iter()
.any(|metadata| metadata != &ContentItemMetadata::default())
.then_some(content_item_metadata);
internal_chat_message_metadata_passthrough
.get_or_insert_default()
.content_item_kinds = Some(content_item_kinds);

Some(())
}
66 changes: 16 additions & 50 deletions codex-rs/context-fragments/src/fragment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::AnnotatedContent;
use crate::message_from_parts;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::ContentItemMetadata;
use codex_protocol::models::InternalChatMessageMetadataPassthrough;
use codex_protocol::models::ResponseItem;

/// A rendered contextual fragment and the role that owns its annotated content.
Expand Down Expand Up @@ -36,7 +35,20 @@ impl RenderedFragment {
impl From<RenderedFragment> for ResponseItem {
fn from(fragment: RenderedFragment) -> Self {
let (role, annotated_content) = fragment.into_parts();
message_from_parts(role, vec![annotated_content])
let (content, content_kind) = annotated_content.into_parts();

Self::Message {
id: None,
role: role.to_string(),
content: vec![content],
phase: None,
internal_chat_message_metadata_passthrough: Some(
InternalChatMessageMetadataPassthrough {
content_item_kinds: Some(vec![content_kind]),
..Default::default()
},
),
}
}
}

Expand All @@ -52,22 +64,6 @@ impl From<RenderedFragment> for ResponseItem {
pub trait ContextualUserFragment {
fn role(&self) -> &'static str;

/// Attribution is supplied when the fragment is produced, never inferred from rendered text.
fn content_metadata(&self) -> ContentItemMetadata {
ContentItemMetadata::harness()
}

/// Binds producer attribution before this fragment is rendered or persisted.
fn with_metadata(self, metadata: ContentItemMetadata) -> AttributedFragment<Self>
where
Self: Sized,
{
AttributedFragment {
fragment: self,
metadata,
}
}

/// Returns a stable `<feature>.<name>` classification, using `generic` for shared fragments.
fn content_kind(&self) -> ContentItemKind;

Expand Down Expand Up @@ -101,7 +97,7 @@ pub trait ContextualUserFragment {
fn render_fragment(&self) -> RenderedFragment {
RenderedFragment::new(
self.role(),
AnnotatedContent::text(self.render(), self.content_kind(), self.content_metadata()),
AnnotatedContent::input_text(self.render(), self.content_kind()),
)
}

Expand All @@ -117,36 +113,6 @@ pub trait ContextualUserFragment {
}
}

/// A contextual fragment with attribution supplied by its producer.
pub struct AttributedFragment<F> {
fragment: F,
metadata: ContentItemMetadata,
}

impl<F: ContextualUserFragment> ContextualUserFragment for AttributedFragment<F> {
fn role(&self) -> &'static str {
self.fragment.role()
}
fn content_kind(&self) -> ContentItemKind {
self.fragment.content_kind()
}
fn markers(&self) -> (&'static str, &'static str) {
self.fragment.markers()
}
fn type_markers() -> (&'static str, &'static str) {
F::type_markers()
}
fn body(&self) -> String {
self.fragment.body()
}
fn render(&self) -> String {
self.fragment.render()
}
fn content_metadata(&self) -> ContentItemMetadata {
self.metadata.clone()
}
}

pub(crate) fn matches_marked_text(start_marker: &str, end_marker: &str, text: &str) -> bool {
if start_marker.is_empty() || end_marker.is_empty() {
return false;
Expand Down
2 changes: 0 additions & 2 deletions codex-rs/context-fragments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ mod recap_prompt;
pub use additional_context::AdditionalContextDeveloperFragment;
pub use additional_context::AdditionalContextUserFragment;
pub use annotated_content::AnnotatedContent;
pub use annotated_content::message_from_parts;
pub use annotated_content::set_annotated_content;
pub use annotated_content::to_annotated_content;
pub use fragment::AttributedFragment;
pub use fragment::ContextualUserFragment;
pub use fragment::RenderedFragment;

Expand Down
Loading
Loading