mirror of
https://github.com/openai/codex.git
synced 2026-10-26 18:38:47 +00:00
**Why We Did This** - The goal is to reduce MCP tool context pollution by not exposing the full MCP tool list up front - It forces an explicit discovery step (`search_tool_bm25`) so the model narrows tool scope before making MCP calls, which helps relevance and lowers prompt/tool clutter. **What It Changed** - Added a new experimental feature flag `search_tool` in `core/src/features.rs:90` and `core/src/features.rs:430`. - Added config/schema support for that flag in `core/config.schema.json:214` and `core/config.schema.json:1235`. - Added BM25 dependency (`bm25`) in `Cargo.toml:129` and `core/Cargo.toml:23`. - Added new tool handler `search_tool_bm25` in `core/src/tools/handlers/search_tool_bm25.rs:18`. - Registered the handler and tool spec in `core/src/tools/handlers/mod.rs:11` and `core/src/tools/spec.rs:780` and `core/src/tools/spec.rs:1344`. - Extended `ToolsConfig` to carry `search_tool` enablement in `core/src/tools/spec.rs:32` and `core/src/tools/spec.rs:56`. - Injected dedicated developer instructions for tool-discovery workflow in `core/src/codex.rs:483` and `core/src/codex.rs:1976`, using `core/templates/search_tool/developer_instructions.md:1`. - Added session state to store one-shot selected MCP tools in `core/src/state/session.rs:27` and `core/src/state/session.rs:131`. - Added filtering so when feature is enabled, only selected MCP tools are exposed on the next request (then consumed) in `core/src/codex.rs:3800` and `core/src/codex.rs:3843`. - Added E2E suite coverage for enablement/instructions/hide-until-search/one-turn-selection in `core/tests/suite/search_tool.rs:72`, `core/tests/suite/search_tool.rs:109`, `core/tests/suite/search_tool.rs:147`, and `core/tests/suite/search_tool.rs:218`. - Refactored test helper utilities to support config-driven tool collection in `core/tests/suite/tools.rs:281`. **Net Behavioral Effect** - With `search_tool` **off**: existing MCP behavior (tools exposed normally). - With `search_tool` **on**: MCP tools start hidden, model must call `search_tool_bm25`, and only returned `selected_tools` are available for the next model call.
262 lines
8.5 KiB
Rust
262 lines
8.5 KiB
Rust
//! Session-wide mutable state.
|
|
|
|
use codex_protocol::models::ResponseItem;
|
|
use std::collections::HashMap;
|
|
use std::collections::HashSet;
|
|
|
|
use crate::codex::SessionConfiguration;
|
|
use crate::context_manager::ContextManager;
|
|
use crate::protocol::RateLimitSnapshot;
|
|
use crate::protocol::TokenUsage;
|
|
use crate::protocol::TokenUsageInfo;
|
|
use crate::tasks::RegularTask;
|
|
use crate::truncate::TruncationPolicy;
|
|
|
|
/// Persistent, session-scoped state previously stored directly on `Session`.
|
|
pub(crate) struct SessionState {
|
|
pub(crate) session_configuration: SessionConfiguration,
|
|
pub(crate) history: ContextManager,
|
|
pub(crate) latest_rate_limits: Option<RateLimitSnapshot>,
|
|
pub(crate) server_reasoning_included: bool,
|
|
pub(crate) dependency_env: HashMap<String, String>,
|
|
pub(crate) mcp_dependency_prompted: HashSet<String>,
|
|
/// Whether the session's initial context has been seeded into history.
|
|
///
|
|
/// TODO(owen): This is a temporary solution to avoid updating a thread's updated_at
|
|
/// timestamp when resuming a session. Remove this once SQLite is in place.
|
|
pub(crate) initial_context_seeded: bool,
|
|
/// Previous rollout model for one-shot model-switch handling on first turn after resume.
|
|
pub(crate) pending_resume_previous_model: Option<String>,
|
|
/// Startup regular task pre-created during session initialization.
|
|
pub(crate) startup_regular_task: Option<RegularTask>,
|
|
pub(crate) active_mcp_tool_selection: Option<Vec<String>>,
|
|
}
|
|
|
|
impl SessionState {
|
|
/// Create a new session state mirroring previous `State::default()` semantics.
|
|
pub(crate) fn new(session_configuration: SessionConfiguration) -> Self {
|
|
let history = ContextManager::new();
|
|
Self {
|
|
session_configuration,
|
|
history,
|
|
latest_rate_limits: None,
|
|
server_reasoning_included: false,
|
|
dependency_env: HashMap::new(),
|
|
mcp_dependency_prompted: HashSet::new(),
|
|
initial_context_seeded: false,
|
|
pending_resume_previous_model: None,
|
|
startup_regular_task: None,
|
|
active_mcp_tool_selection: None,
|
|
}
|
|
}
|
|
|
|
// History helpers
|
|
pub(crate) fn record_items<I>(&mut self, items: I, policy: TruncationPolicy)
|
|
where
|
|
I: IntoIterator,
|
|
I::Item: std::ops::Deref<Target = ResponseItem>,
|
|
{
|
|
self.history.record_items(items, policy);
|
|
}
|
|
|
|
pub(crate) fn clone_history(&self) -> ContextManager {
|
|
self.history.clone()
|
|
}
|
|
|
|
pub(crate) fn replace_history(&mut self, items: Vec<ResponseItem>) {
|
|
self.history.replace(items);
|
|
}
|
|
|
|
pub(crate) fn set_token_info(&mut self, info: Option<TokenUsageInfo>) {
|
|
self.history.set_token_info(info);
|
|
}
|
|
|
|
// Token/rate limit helpers
|
|
pub(crate) fn update_token_info_from_usage(
|
|
&mut self,
|
|
usage: &TokenUsage,
|
|
model_context_window: Option<i64>,
|
|
) {
|
|
self.history.update_token_info(usage, model_context_window);
|
|
}
|
|
|
|
pub(crate) fn token_info(&self) -> Option<TokenUsageInfo> {
|
|
self.history.token_info()
|
|
}
|
|
|
|
pub(crate) fn set_rate_limits(&mut self, snapshot: RateLimitSnapshot) {
|
|
self.latest_rate_limits = Some(merge_rate_limit_fields(
|
|
self.latest_rate_limits.as_ref(),
|
|
snapshot,
|
|
));
|
|
}
|
|
|
|
pub(crate) fn token_info_and_rate_limits(
|
|
&self,
|
|
) -> (Option<TokenUsageInfo>, Option<RateLimitSnapshot>) {
|
|
(self.token_info(), self.latest_rate_limits.clone())
|
|
}
|
|
|
|
pub(crate) fn set_token_usage_full(&mut self, context_window: i64) {
|
|
self.history.set_token_usage_full(context_window);
|
|
}
|
|
|
|
pub(crate) fn get_total_token_usage(&self, server_reasoning_included: bool) -> i64 {
|
|
self.history
|
|
.get_total_token_usage(server_reasoning_included)
|
|
}
|
|
|
|
pub(crate) fn set_server_reasoning_included(&mut self, included: bool) {
|
|
self.server_reasoning_included = included;
|
|
}
|
|
|
|
pub(crate) fn server_reasoning_included(&self) -> bool {
|
|
self.server_reasoning_included
|
|
}
|
|
|
|
pub(crate) fn record_mcp_dependency_prompted<I>(&mut self, names: I)
|
|
where
|
|
I: IntoIterator<Item = String>,
|
|
{
|
|
self.mcp_dependency_prompted.extend(names);
|
|
}
|
|
|
|
pub(crate) fn mcp_dependency_prompted(&self) -> HashSet<String> {
|
|
self.mcp_dependency_prompted.clone()
|
|
}
|
|
|
|
pub(crate) fn set_dependency_env(&mut self, values: HashMap<String, String>) {
|
|
for (key, value) in values {
|
|
self.dependency_env.insert(key, value);
|
|
}
|
|
}
|
|
|
|
pub(crate) fn dependency_env(&self) -> HashMap<String, String> {
|
|
self.dependency_env.clone()
|
|
}
|
|
|
|
pub(crate) fn set_startup_regular_task(&mut self, task: RegularTask) {
|
|
self.startup_regular_task = Some(task);
|
|
}
|
|
|
|
pub(crate) fn take_startup_regular_task(&mut self) -> Option<RegularTask> {
|
|
self.startup_regular_task.take()
|
|
}
|
|
|
|
pub(crate) fn merge_mcp_tool_selection(&mut self, tool_names: Vec<String>) -> Vec<String> {
|
|
if tool_names.is_empty() {
|
|
return self.active_mcp_tool_selection.clone().unwrap_or_default();
|
|
}
|
|
|
|
let mut merged = self.active_mcp_tool_selection.take().unwrap_or_default();
|
|
let mut seen: HashSet<String> = merged.iter().cloned().collect();
|
|
|
|
for tool_name in tool_names {
|
|
if seen.insert(tool_name.clone()) {
|
|
merged.push(tool_name);
|
|
}
|
|
}
|
|
|
|
self.active_mcp_tool_selection = Some(merged.clone());
|
|
merged
|
|
}
|
|
|
|
pub(crate) fn get_mcp_tool_selection(&self) -> Option<Vec<String>> {
|
|
self.active_mcp_tool_selection.clone()
|
|
}
|
|
|
|
pub(crate) fn clear_mcp_tool_selection(&mut self) {
|
|
self.active_mcp_tool_selection = None;
|
|
}
|
|
}
|
|
|
|
// Sometimes new snapshots don't include credits or plan information.
|
|
fn merge_rate_limit_fields(
|
|
previous: Option<&RateLimitSnapshot>,
|
|
mut snapshot: RateLimitSnapshot,
|
|
) -> RateLimitSnapshot {
|
|
if snapshot.credits.is_none() {
|
|
snapshot.credits = previous.and_then(|prior| prior.credits.clone());
|
|
}
|
|
if snapshot.plan_type.is_none() {
|
|
snapshot.plan_type = previous.and_then(|prior| prior.plan_type);
|
|
}
|
|
snapshot
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::codex::make_session_configuration_for_tests;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
#[tokio::test]
|
|
async fn merge_mcp_tool_selection_deduplicates_and_preserves_order() {
|
|
let session_configuration = make_session_configuration_for_tests().await;
|
|
let mut state = SessionState::new(session_configuration);
|
|
|
|
let merged = state.merge_mcp_tool_selection(vec![
|
|
"mcp__rmcp__echo".to_string(),
|
|
"mcp__rmcp__image".to_string(),
|
|
"mcp__rmcp__echo".to_string(),
|
|
]);
|
|
assert_eq!(
|
|
merged,
|
|
vec![
|
|
"mcp__rmcp__echo".to_string(),
|
|
"mcp__rmcp__image".to_string(),
|
|
]
|
|
);
|
|
|
|
let merged = state.merge_mcp_tool_selection(vec![
|
|
"mcp__rmcp__image".to_string(),
|
|
"mcp__rmcp__search".to_string(),
|
|
]);
|
|
assert_eq!(
|
|
merged,
|
|
vec![
|
|
"mcp__rmcp__echo".to_string(),
|
|
"mcp__rmcp__image".to_string(),
|
|
"mcp__rmcp__search".to_string(),
|
|
]
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn merge_mcp_tool_selection_empty_input_is_noop() {
|
|
let session_configuration = make_session_configuration_for_tests().await;
|
|
let mut state = SessionState::new(session_configuration);
|
|
state.merge_mcp_tool_selection(vec![
|
|
"mcp__rmcp__echo".to_string(),
|
|
"mcp__rmcp__image".to_string(),
|
|
]);
|
|
|
|
let merged = state.merge_mcp_tool_selection(Vec::new());
|
|
assert_eq!(
|
|
merged,
|
|
vec![
|
|
"mcp__rmcp__echo".to_string(),
|
|
"mcp__rmcp__image".to_string(),
|
|
]
|
|
);
|
|
assert_eq!(
|
|
state.get_mcp_tool_selection(),
|
|
Some(vec![
|
|
"mcp__rmcp__echo".to_string(),
|
|
"mcp__rmcp__image".to_string(),
|
|
])
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn clear_mcp_tool_selection_removes_selection() {
|
|
let session_configuration = make_session_configuration_for_tests().await;
|
|
let mut state = SessionState::new(session_configuration);
|
|
state.merge_mcp_tool_selection(vec!["mcp__rmcp__echo".to_string()]);
|
|
|
|
state.clear_mcp_tool_selection();
|
|
|
|
assert_eq!(state.get_mcp_tool_selection(), None);
|
|
}
|
|
}
|