mirror of
https://github.com/openai/codex.git
synced 2026-04-30 09:26:44 +00:00
## Why Enterprises can already constrain approvals, sandboxing, and web search through `requirements.toml` and MDM, but feature flags were still only configurable as managed defaults. That meant an enterprise could suggest feature values, but it could not actually pin them. This change closes that gap and makes enterprise feature requirements behave like the other constrained settings. The effective feature set now stays consistent with enterprise requirements during config load, when config writes are validated, and when runtime code mutates feature flags later in the session. It also tightens the runtime API for managed features. `ManagedFeatures` now follows the same constraint-oriented shape as `Constrained<T>` instead of exposing panic-prone mutation helpers, and production code can no longer construct it through an unconstrained `From<Features>` path. The PR also hardens the `compact_resume_fork` integration coverage on Windows. After the feature-management changes, `compact_resume_after_second_compaction_preserves_history` was overflowing the libtest/Tokio thread stacks on Windows, so the test now uses an explicit larger-stack harness as a pragmatic mitigation. That may not be the ideal root-cause fix, and it merits a parallel investigation into whether part of the async future chain should be boxed to reduce stack pressure instead. ## What Changed Enterprises can now pin feature values in `requirements.toml` with the requirements-side `features` table: ```toml [features] personality = true unified_exec = false ``` Only canonical feature keys are allowed in the requirements `features` table; omitted keys remain unconstrained. - Added a requirements-side pinned feature map to `ConfigRequirementsToml`, threaded it through source-preserving requirements merge and normalization in `codex-config`, and made the TOML surface use `[features]` (while still accepting legacy `[feature_requirements]` for compatibility). - Exposed `featureRequirements` from `configRequirements/read`, regenerated the JSON/TypeScript schema artifacts, and updated the app-server README. - Wrapped the effective feature set in `ManagedFeatures`, backed by `ConstrainedWithSource<Features>`, and changed its API to mirror `Constrained<T>`: `can_set(...)`, `set(...) -> ConstraintResult<()>`, and result-returning `enable` / `disable` / `set_enabled` helpers. - Removed the legacy-usage and bulk-map passthroughs from `ManagedFeatures`; callers that need those behaviors now mutate a plain `Features` value and reapply it through `set(...)`, so the constrained wrapper remains the enforcement boundary. - Removed the production loophole for constructing unconstrained `ManagedFeatures`. Non-test code now creates it through the configured feature-loading path, and `impl From<Features> for ManagedFeatures` is restricted to `#[cfg(test)]`. - Rejected legacy feature aliases in enterprise feature requirements, and return a load error when a pinned combination cannot survive dependency normalization. - Validated config writes against enterprise feature requirements before persisting changes, including explicit conflicting writes and profile-specific feature states that normalize into invalid combinations. - Updated runtime and TUI feature-toggle paths to use the constrained setter API and to persist or apply the effective post-constraint value rather than the requested value. - Updated the `core_test_support` Bazel target to include the bundled core model-catalog fixtures in its runtime data, so helper code that resolves `core/models.json` through runfiles works in remote Bazel test environments. - Renamed the core config test coverage to emphasize that effective feature values are normalized at runtime, while conflicting persisted config writes are rejected. - Ran `compact_resume_after_second_compaction_preserves_history` inside an explicit 8 MiB test thread and Tokio runtime worker stack, following the existing larger-stack integration-test pattern, to keep the Windows `compact_resume_fork` test slice from aborting while a parallel investigation continues into whether some of the underlying async futures should be boxed. ## Verification - `cargo test -p codex-config` - `cargo test -p codex-core feature_requirements_ -- --nocapture` - `cargo test -p codex-core load_requirements_toml_produces_expected_constraints -- --nocapture` - `cargo test -p codex-core compact_resume_after_second_compaction_preserves_history -- --nocapture` - `cargo test -p codex-core compact_resume_fork -- --nocapture` - Re-ran the built `codex-core` `tests/all` binary with `RUST_MIN_STACK=262144` for `compact_resume_after_second_compaction_preserves_history` to confirm the explicit-stack harness fixes the deterministic low-stack repro. - `cargo test -p codex-core` - This still fails locally in unrelated integration areas that expect the `codex` / `test_stdio_server` binaries or hit existing `search_tool` wiremock mismatches. ## Docs `developers.openai.com/codex` should document the requirements-side `[features]` table for enterprise and MDM-managed configuration, including that it only accepts canonical feature keys and that conflicting config writes are rejected.
319 lines
11 KiB
Rust
319 lines
11 KiB
Rust
use anyhow::Result;
|
|
use codex_core::features::Feature;
|
|
use core_test_support::responses::ResponsesRequest;
|
|
use core_test_support::responses::ev_assistant_message;
|
|
use core_test_support::responses::ev_completed;
|
|
use core_test_support::responses::ev_function_call;
|
|
use core_test_support::responses::ev_response_created;
|
|
use core_test_support::responses::mount_response_once_match;
|
|
use core_test_support::responses::mount_sse_once_match;
|
|
use core_test_support::responses::sse;
|
|
use core_test_support::responses::sse_response;
|
|
use core_test_support::responses::start_mock_server;
|
|
use core_test_support::skip_if_no_network;
|
|
use core_test_support::test_codex::TestCodex;
|
|
use core_test_support::test_codex::test_codex;
|
|
use serde_json::json;
|
|
use std::time::Duration;
|
|
use tokio::time::Instant;
|
|
use tokio::time::sleep;
|
|
use wiremock::MockServer;
|
|
|
|
const SPAWN_CALL_ID: &str = "spawn-call-1";
|
|
const FORKED_SPAWN_AGENT_OUTPUT_MESSAGE: &str = "You are the newly spawned agent. The prior conversation history was forked from your parent agent. Treat the next user message as your new task, and use the forked history only as background context.";
|
|
const TURN_0_FORK_PROMPT: &str = "seed fork context";
|
|
const TURN_1_PROMPT: &str = "spawn a child and continue";
|
|
const TURN_2_NO_WAIT_PROMPT: &str = "follow up without wait";
|
|
const CHILD_PROMPT: &str = "child: do work";
|
|
|
|
fn body_contains(req: &wiremock::Request, text: &str) -> bool {
|
|
let is_zstd = req
|
|
.headers
|
|
.get("content-encoding")
|
|
.and_then(|value| value.to_str().ok())
|
|
.is_some_and(|value| {
|
|
value
|
|
.split(',')
|
|
.any(|entry| entry.trim().eq_ignore_ascii_case("zstd"))
|
|
});
|
|
let bytes = if is_zstd {
|
|
zstd::stream::decode_all(std::io::Cursor::new(&req.body)).ok()
|
|
} else {
|
|
Some(req.body.clone())
|
|
};
|
|
bytes
|
|
.and_then(|body| String::from_utf8(body).ok())
|
|
.is_some_and(|body| body.contains(text))
|
|
}
|
|
|
|
fn has_subagent_notification(req: &ResponsesRequest) -> bool {
|
|
req.message_input_texts("user")
|
|
.iter()
|
|
.any(|text| text.contains("<subagent_notification>"))
|
|
}
|
|
|
|
async fn wait_for_spawned_thread_id(test: &TestCodex) -> Result<String> {
|
|
let deadline = Instant::now() + Duration::from_secs(2);
|
|
loop {
|
|
let ids = test.thread_manager.list_thread_ids().await;
|
|
if let Some(spawned_id) = ids
|
|
.iter()
|
|
.find(|id| **id != test.session_configured.session_id)
|
|
{
|
|
return Ok(spawned_id.to_string());
|
|
}
|
|
if Instant::now() >= deadline {
|
|
anyhow::bail!("timed out waiting for spawned thread id");
|
|
}
|
|
sleep(Duration::from_millis(10)).await;
|
|
}
|
|
}
|
|
|
|
async fn wait_for_requests(
|
|
mock: &core_test_support::responses::ResponseMock,
|
|
) -> Result<Vec<ResponsesRequest>> {
|
|
let deadline = Instant::now() + Duration::from_secs(2);
|
|
loop {
|
|
let requests = mock.requests();
|
|
if !requests.is_empty() {
|
|
return Ok(requests);
|
|
}
|
|
if Instant::now() >= deadline {
|
|
anyhow::bail!("expected at least 1 request, got {}", requests.len());
|
|
}
|
|
sleep(Duration::from_millis(10)).await;
|
|
}
|
|
}
|
|
|
|
async fn setup_turn_one_with_spawned_child(
|
|
server: &MockServer,
|
|
child_response_delay: Option<Duration>,
|
|
) -> Result<(TestCodex, String)> {
|
|
let spawn_args = serde_json::to_string(&json!({
|
|
"message": CHILD_PROMPT,
|
|
}))?;
|
|
|
|
mount_sse_once_match(
|
|
server,
|
|
|req: &wiremock::Request| body_contains(req, TURN_1_PROMPT),
|
|
sse(vec![
|
|
ev_response_created("resp-turn1-1"),
|
|
ev_function_call(SPAWN_CALL_ID, "spawn_agent", &spawn_args),
|
|
ev_completed("resp-turn1-1"),
|
|
]),
|
|
)
|
|
.await;
|
|
|
|
let child_sse = sse(vec![
|
|
ev_response_created("resp-child-1"),
|
|
ev_assistant_message("msg-child-1", "child done"),
|
|
ev_completed("resp-child-1"),
|
|
]);
|
|
let child_request_log = if let Some(delay) = child_response_delay {
|
|
mount_response_once_match(
|
|
server,
|
|
|req: &wiremock::Request| {
|
|
body_contains(req, CHILD_PROMPT) && !body_contains(req, SPAWN_CALL_ID)
|
|
},
|
|
sse_response(child_sse).set_delay(delay),
|
|
)
|
|
.await
|
|
} else {
|
|
mount_sse_once_match(
|
|
server,
|
|
|req: &wiremock::Request| {
|
|
body_contains(req, CHILD_PROMPT) && !body_contains(req, SPAWN_CALL_ID)
|
|
},
|
|
child_sse,
|
|
)
|
|
.await
|
|
};
|
|
|
|
let _turn1_followup = mount_sse_once_match(
|
|
server,
|
|
|req: &wiremock::Request| body_contains(req, SPAWN_CALL_ID),
|
|
sse(vec![
|
|
ev_response_created("resp-turn1-2"),
|
|
ev_assistant_message("msg-turn1-2", "parent done"),
|
|
ev_completed("resp-turn1-2"),
|
|
]),
|
|
)
|
|
.await;
|
|
|
|
#[allow(clippy::expect_used)]
|
|
let mut builder = test_codex().with_config(|config| {
|
|
config
|
|
.features
|
|
.enable(Feature::Collab)
|
|
.expect("test config should allow feature update");
|
|
});
|
|
let test = builder.build(server).await?;
|
|
test.submit_turn(TURN_1_PROMPT).await?;
|
|
if child_response_delay.is_none() {
|
|
let _ = wait_for_requests(&child_request_log).await?;
|
|
let rollout_path = test
|
|
.codex
|
|
.rollout_path()
|
|
.ok_or_else(|| anyhow::anyhow!("expected parent rollout path"))?;
|
|
let deadline = Instant::now() + Duration::from_secs(6);
|
|
loop {
|
|
let has_notification = tokio::fs::read_to_string(&rollout_path)
|
|
.await
|
|
.is_ok_and(|rollout| rollout.contains("<subagent_notification>"));
|
|
if has_notification {
|
|
break;
|
|
}
|
|
if Instant::now() >= deadline {
|
|
anyhow::bail!(
|
|
"timed out waiting for parent rollout to include subagent notification"
|
|
);
|
|
}
|
|
sleep(Duration::from_millis(10)).await;
|
|
}
|
|
}
|
|
let spawned_id = wait_for_spawned_thread_id(&test).await?;
|
|
|
|
Ok((test, spawned_id))
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn subagent_notification_is_included_without_wait() -> Result<()> {
|
|
skip_if_no_network!(Ok(()));
|
|
|
|
let server = start_mock_server().await;
|
|
let (test, _spawned_id) = setup_turn_one_with_spawned_child(&server, None).await?;
|
|
|
|
let turn2 = mount_sse_once_match(
|
|
&server,
|
|
|req: &wiremock::Request| body_contains(req, TURN_2_NO_WAIT_PROMPT),
|
|
sse(vec![
|
|
ev_response_created("resp-turn2-1"),
|
|
ev_assistant_message("msg-turn2-1", "no wait path"),
|
|
ev_completed("resp-turn2-1"),
|
|
]),
|
|
)
|
|
.await;
|
|
test.submit_turn(TURN_2_NO_WAIT_PROMPT).await?;
|
|
|
|
let turn2_requests = wait_for_requests(&turn2).await?;
|
|
assert!(turn2_requests.iter().any(has_subagent_notification));
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn spawned_child_receives_forked_parent_context() -> Result<()> {
|
|
skip_if_no_network!(Ok(()));
|
|
|
|
let server = start_mock_server().await;
|
|
|
|
let seed_turn = mount_sse_once_match(
|
|
&server,
|
|
|req: &wiremock::Request| body_contains(req, TURN_0_FORK_PROMPT),
|
|
sse(vec![
|
|
ev_response_created("resp-seed-1"),
|
|
ev_assistant_message("msg-seed-1", "seeded"),
|
|
ev_completed("resp-seed-1"),
|
|
]),
|
|
)
|
|
.await;
|
|
|
|
let spawn_args = serde_json::to_string(&json!({
|
|
"message": CHILD_PROMPT,
|
|
"fork_context": true,
|
|
}))?;
|
|
let spawn_turn = mount_sse_once_match(
|
|
&server,
|
|
|req: &wiremock::Request| body_contains(req, TURN_1_PROMPT),
|
|
sse(vec![
|
|
ev_response_created("resp-turn1-1"),
|
|
ev_function_call(SPAWN_CALL_ID, "spawn_agent", &spawn_args),
|
|
ev_completed("resp-turn1-1"),
|
|
]),
|
|
)
|
|
.await;
|
|
|
|
let _child_request_log = mount_sse_once_match(
|
|
&server,
|
|
|req: &wiremock::Request| body_contains(req, CHILD_PROMPT),
|
|
sse(vec![
|
|
ev_response_created("resp-child-1"),
|
|
ev_assistant_message("msg-child-1", "child done"),
|
|
ev_completed("resp-child-1"),
|
|
]),
|
|
)
|
|
.await;
|
|
|
|
let _turn1_followup = mount_sse_once_match(
|
|
&server,
|
|
|req: &wiremock::Request| body_contains(req, SPAWN_CALL_ID),
|
|
sse(vec![
|
|
ev_response_created("resp-turn1-2"),
|
|
ev_assistant_message("msg-turn1-2", "parent done"),
|
|
ev_completed("resp-turn1-2"),
|
|
]),
|
|
)
|
|
.await;
|
|
|
|
let mut builder = test_codex().with_config(|config| {
|
|
config
|
|
.features
|
|
.enable(Feature::Collab)
|
|
.expect("test config should allow feature update");
|
|
});
|
|
let test = builder.build(&server).await?;
|
|
|
|
test.submit_turn(TURN_0_FORK_PROMPT).await?;
|
|
let _ = seed_turn.single_request();
|
|
|
|
test.submit_turn(TURN_1_PROMPT).await?;
|
|
let _ = spawn_turn.single_request();
|
|
|
|
let deadline = Instant::now() + Duration::from_secs(2);
|
|
let child_request = loop {
|
|
if let Some(request) = server
|
|
.received_requests()
|
|
.await
|
|
.unwrap_or_default()
|
|
.into_iter()
|
|
.find(|request| {
|
|
body_contains(request, CHILD_PROMPT)
|
|
&& body_contains(request, FORKED_SPAWN_AGENT_OUTPUT_MESSAGE)
|
|
})
|
|
{
|
|
break request;
|
|
}
|
|
if Instant::now() >= deadline {
|
|
anyhow::bail!("timed out waiting for forked child request");
|
|
}
|
|
sleep(Duration::from_millis(10)).await;
|
|
};
|
|
assert!(body_contains(&child_request, TURN_0_FORK_PROMPT));
|
|
assert!(body_contains(&child_request, "seeded"));
|
|
|
|
let child_body = child_request
|
|
.body_json::<serde_json::Value>()
|
|
.expect("forked child request body should be json");
|
|
let function_call_output = child_body["input"]
|
|
.as_array()
|
|
.and_then(|items| {
|
|
items.iter().find(|item| {
|
|
item["type"].as_str() == Some("function_call_output")
|
|
&& item["call_id"].as_str() == Some(SPAWN_CALL_ID)
|
|
})
|
|
})
|
|
.unwrap_or_else(|| panic!("expected forked child request to include spawn_agent output"));
|
|
let (content, success) = match &function_call_output["output"] {
|
|
serde_json::Value::String(text) => (Some(text.as_str()), None),
|
|
serde_json::Value::Object(output) => (
|
|
output.get("content").and_then(serde_json::Value::as_str),
|
|
output.get("success").and_then(serde_json::Value::as_bool),
|
|
),
|
|
_ => (None, None),
|
|
};
|
|
assert_eq!(content, Some(FORKED_SPAWN_AGENT_OUTPUT_MESSAGE));
|
|
assert_ne!(success, Some(false));
|
|
|
|
Ok(())
|
|
}
|