mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
29364f3a9b
This adds support for a new variant of the shell tool behind a flag. To
test, run `codex` with `--enable shell_command_tool`, which will
register the tool with Codex under the name `shell_command` that accepts
the following shape:
```python
{
command: str
workdir: str | None,
timeout_ms: int | None,
with_escalated_permissions: bool | None,
justification: str | None,
}
```
This is comparable to the existing tool registered under
`shell`/`container.exec`. The primary difference is that it accepts
`command` as a `str` instead of a `str[]`. The `shell_command` tool
executes by running `execvp(["bash", "-lc", command])`, though the exact
arguments to `execvp(3)` depend on the user's default shell.
The hypothesis is that this will simplify things for the model. For
example, on Windows, instead of generating:
```json
{"command": ["pwsh.exe", "-NoLogo", "-Command", "ls -Name"]}
```
The model could simply generate:
```json
{"command": "ls -Name"}
```
As part of this change, I extracted some logic out of `user_shell.rs` as
`Shell::derive_exec_args()` so that it can be reused in
`codex-rs/core/src/tools/handlers/shell.rs`. Note the original code
generated exec arg lists like:
```javascript
["bash", "-lc", command]
["zsh", "-lc", command]
["pwsh.exe", "-NoProfile", "-Command", command]
```
Using `-l` for Bash and Zsh, but then specifying `-NoProfile` for
PowerShell seemed inconsistent to me, so I changed this in the new
implementation while also adding a `use_login_shell: bool` option to
make this explicit. If we decide to add a `login: bool` to
`ShellCommandToolCallParams` like we have for unified exec:
https://github.com/openai/codex/blob/807e2c27f0a9f2e85c50e7e6df5533f0d9b853c7/codex-rs/core/src/tools/handlers/unified_exec.rs#L33-L34
Then this should make it straightforward to support.
305 lines
9.0 KiB
Rust
305 lines
9.0 KiB
Rust
//! Centralized feature flags and metadata.
|
|
//!
|
|
//! This module defines a small set of toggles that gate experimental and
|
|
//! optional behavior across the codebase. Instead of wiring individual
|
|
//! booleans through multiple types, call sites consult a single `Features`
|
|
//! container attached to `Config`.
|
|
|
|
use crate::config::ConfigToml;
|
|
use crate::config::profile::ConfigProfile;
|
|
use serde::Deserialize;
|
|
use std::collections::BTreeMap;
|
|
use std::collections::BTreeSet;
|
|
|
|
mod legacy;
|
|
pub(crate) use legacy::LegacyFeatureToggles;
|
|
|
|
/// High-level lifecycle stage for a feature.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum Stage {
|
|
Experimental,
|
|
Beta,
|
|
Stable,
|
|
Deprecated,
|
|
Removed,
|
|
}
|
|
|
|
/// Unique features toggled via configuration.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
pub enum Feature {
|
|
/// Use the single unified PTY-backed exec tool.
|
|
UnifiedExec,
|
|
/// Use the shell command tool that takes `command` as a single string of
|
|
/// shell instead of an array of args passed to `execvp(3)`.
|
|
ShellCommandTool,
|
|
/// Enable experimental RMCP features such as OAuth login.
|
|
RmcpClient,
|
|
/// Include the freeform apply_patch tool.
|
|
ApplyPatchFreeform,
|
|
/// Include the view_image tool.
|
|
ViewImageTool,
|
|
/// Allow the model to request web searches.
|
|
WebSearchRequest,
|
|
/// Enable the model-based risk assessments for sandboxed commands.
|
|
SandboxCommandAssessment,
|
|
/// Create a ghost commit at each turn.
|
|
GhostCommit,
|
|
/// Enable Windows sandbox (restricted token) on Windows.
|
|
WindowsSandbox,
|
|
}
|
|
|
|
impl Feature {
|
|
pub fn key(self) -> &'static str {
|
|
self.info().key
|
|
}
|
|
|
|
pub fn stage(self) -> Stage {
|
|
self.info().stage
|
|
}
|
|
|
|
pub fn default_enabled(self) -> bool {
|
|
self.info().default_enabled
|
|
}
|
|
|
|
fn info(self) -> &'static FeatureSpec {
|
|
FEATURES
|
|
.iter()
|
|
.find(|spec| spec.id == self)
|
|
.unwrap_or_else(|| unreachable!("missing FeatureSpec for {:?}", self))
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
pub struct LegacyFeatureUsage {
|
|
pub alias: String,
|
|
pub feature: Feature,
|
|
}
|
|
|
|
/// Holds the effective set of enabled features.
|
|
#[derive(Debug, Clone, Default, PartialEq)]
|
|
pub struct Features {
|
|
enabled: BTreeSet<Feature>,
|
|
legacy_usages: BTreeSet<LegacyFeatureUsage>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct FeatureOverrides {
|
|
pub include_apply_patch_tool: Option<bool>,
|
|
pub web_search_request: Option<bool>,
|
|
pub experimental_sandbox_command_assessment: Option<bool>,
|
|
}
|
|
|
|
impl FeatureOverrides {
|
|
fn apply(self, features: &mut Features) {
|
|
LegacyFeatureToggles {
|
|
include_apply_patch_tool: self.include_apply_patch_tool,
|
|
tools_web_search: self.web_search_request,
|
|
..Default::default()
|
|
}
|
|
.apply(features);
|
|
}
|
|
}
|
|
|
|
impl Features {
|
|
/// Starts with built-in defaults.
|
|
pub fn with_defaults() -> Self {
|
|
let mut set = BTreeSet::new();
|
|
for spec in FEATURES {
|
|
if spec.default_enabled {
|
|
set.insert(spec.id);
|
|
}
|
|
}
|
|
Self {
|
|
enabled: set,
|
|
legacy_usages: BTreeSet::new(),
|
|
}
|
|
}
|
|
|
|
pub fn enabled(&self, f: Feature) -> bool {
|
|
self.enabled.contains(&f)
|
|
}
|
|
|
|
pub fn enable(&mut self, f: Feature) -> &mut Self {
|
|
self.enabled.insert(f);
|
|
self
|
|
}
|
|
|
|
pub fn disable(&mut self, f: Feature) -> &mut Self {
|
|
self.enabled.remove(&f);
|
|
self
|
|
}
|
|
|
|
pub fn record_legacy_usage_force(&mut self, alias: &str, feature: Feature) {
|
|
self.legacy_usages.insert(LegacyFeatureUsage {
|
|
alias: alias.to_string(),
|
|
feature,
|
|
});
|
|
}
|
|
|
|
pub fn record_legacy_usage(&mut self, alias: &str, feature: Feature) {
|
|
if alias == feature.key() {
|
|
return;
|
|
}
|
|
self.record_legacy_usage_force(alias, feature);
|
|
}
|
|
|
|
pub fn legacy_feature_usages(&self) -> impl Iterator<Item = (&str, Feature)> + '_ {
|
|
self.legacy_usages
|
|
.iter()
|
|
.map(|usage| (usage.alias.as_str(), usage.feature))
|
|
}
|
|
|
|
/// Apply a table of key -> bool toggles (e.g. from TOML).
|
|
pub fn apply_map(&mut self, m: &BTreeMap<String, bool>) {
|
|
for (k, v) in m {
|
|
match feature_for_key(k) {
|
|
Some(feat) => {
|
|
if k != feat.key() {
|
|
self.record_legacy_usage(k.as_str(), feat);
|
|
}
|
|
if *v {
|
|
self.enable(feat);
|
|
} else {
|
|
self.disable(feat);
|
|
}
|
|
}
|
|
None => {
|
|
tracing::warn!("unknown feature key in config: {k}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn from_config(
|
|
cfg: &ConfigToml,
|
|
config_profile: &ConfigProfile,
|
|
overrides: FeatureOverrides,
|
|
) -> Self {
|
|
let mut features = Features::with_defaults();
|
|
|
|
let base_legacy = LegacyFeatureToggles {
|
|
experimental_sandbox_command_assessment: cfg.experimental_sandbox_command_assessment,
|
|
experimental_use_freeform_apply_patch: cfg.experimental_use_freeform_apply_patch,
|
|
experimental_use_unified_exec_tool: cfg.experimental_use_unified_exec_tool,
|
|
experimental_use_rmcp_client: cfg.experimental_use_rmcp_client,
|
|
tools_web_search: cfg.tools.as_ref().and_then(|t| t.web_search),
|
|
tools_view_image: cfg.tools.as_ref().and_then(|t| t.view_image),
|
|
..Default::default()
|
|
};
|
|
base_legacy.apply(&mut features);
|
|
|
|
if let Some(base_features) = cfg.features.as_ref() {
|
|
features.apply_map(&base_features.entries);
|
|
}
|
|
|
|
let profile_legacy = LegacyFeatureToggles {
|
|
include_apply_patch_tool: config_profile.include_apply_patch_tool,
|
|
experimental_sandbox_command_assessment: config_profile
|
|
.experimental_sandbox_command_assessment,
|
|
experimental_use_freeform_apply_patch: config_profile
|
|
.experimental_use_freeform_apply_patch,
|
|
|
|
experimental_use_unified_exec_tool: config_profile.experimental_use_unified_exec_tool,
|
|
experimental_use_rmcp_client: config_profile.experimental_use_rmcp_client,
|
|
tools_web_search: config_profile.tools_web_search,
|
|
tools_view_image: config_profile.tools_view_image,
|
|
};
|
|
profile_legacy.apply(&mut features);
|
|
if let Some(profile_features) = config_profile.features.as_ref() {
|
|
features.apply_map(&profile_features.entries);
|
|
}
|
|
|
|
overrides.apply(&mut features);
|
|
|
|
features
|
|
}
|
|
}
|
|
|
|
/// Keys accepted in `[features]` tables.
|
|
fn feature_for_key(key: &str) -> Option<Feature> {
|
|
for spec in FEATURES {
|
|
if spec.key == key {
|
|
return Some(spec.id);
|
|
}
|
|
}
|
|
legacy::feature_for_key(key)
|
|
}
|
|
|
|
/// Returns `true` if the provided string matches a known feature toggle key.
|
|
pub fn is_known_feature_key(key: &str) -> bool {
|
|
feature_for_key(key).is_some()
|
|
}
|
|
|
|
/// Deserializable features table for TOML.
|
|
#[derive(Deserialize, Debug, Clone, Default, PartialEq)]
|
|
pub struct FeaturesToml {
|
|
#[serde(flatten)]
|
|
pub entries: BTreeMap<String, bool>,
|
|
}
|
|
|
|
/// Single, easy-to-read registry of all feature definitions.
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub struct FeatureSpec {
|
|
pub id: Feature,
|
|
pub key: &'static str,
|
|
pub stage: Stage,
|
|
pub default_enabled: bool,
|
|
}
|
|
|
|
pub const FEATURES: &[FeatureSpec] = &[
|
|
FeatureSpec {
|
|
id: Feature::UnifiedExec,
|
|
key: "unified_exec",
|
|
stage: Stage::Experimental,
|
|
default_enabled: false,
|
|
},
|
|
FeatureSpec {
|
|
id: Feature::ShellCommandTool,
|
|
key: "shell_command_tool",
|
|
stage: Stage::Experimental,
|
|
default_enabled: false,
|
|
},
|
|
FeatureSpec {
|
|
id: Feature::RmcpClient,
|
|
key: "rmcp_client",
|
|
stage: Stage::Experimental,
|
|
default_enabled: false,
|
|
},
|
|
FeatureSpec {
|
|
id: Feature::ApplyPatchFreeform,
|
|
key: "apply_patch_freeform",
|
|
stage: Stage::Beta,
|
|
default_enabled: false,
|
|
},
|
|
FeatureSpec {
|
|
id: Feature::ViewImageTool,
|
|
key: "view_image_tool",
|
|
stage: Stage::Stable,
|
|
default_enabled: true,
|
|
},
|
|
FeatureSpec {
|
|
id: Feature::WebSearchRequest,
|
|
key: "web_search_request",
|
|
stage: Stage::Stable,
|
|
default_enabled: false,
|
|
},
|
|
FeatureSpec {
|
|
id: Feature::SandboxCommandAssessment,
|
|
key: "experimental_sandbox_command_assessment",
|
|
stage: Stage::Experimental,
|
|
default_enabled: false,
|
|
},
|
|
FeatureSpec {
|
|
id: Feature::GhostCommit,
|
|
key: "ghost_commit",
|
|
stage: Stage::Experimental,
|
|
default_enabled: true,
|
|
},
|
|
FeatureSpec {
|
|
id: Feature::WindowsSandbox,
|
|
key: "enable_experimental_windows_sandbox",
|
|
stage: Stage::Experimental,
|
|
default_enabled: false,
|
|
},
|
|
];
|