mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Enable Guardian V2 scoring with an explicit Guardian provider (#62607)
## Why An explicit Guardian provider could handle approval reviews, but configuring it disabled Guardian V2 scoring. This prevented automatic reviews from using background scores from that provider. ## What changed Route classification through the configured Guardian provider and use its classifier catalog for instructions, reasoning effort, review thresholds, and input limits. Apply the Guardian provider's API-key access policy to classifier requests and ignore assessments from the actor's response stream when a Guardian provider is configured. Keep mandatory review requirements in force and skip background scoring in User approval mode and Full Access. ## Testing Extend coverage for separate actor and Guardian providers at startup and after cold resume, including when both use the same model name. Cover catalog settings, scoring restrictions, and provider-specific API-key policy. GitOrigin-RevId: 5f83d1704bacc63033e144bba0d78752283cb590
This commit is contained in:
@@ -39,12 +39,22 @@ pub enum ApiKeyCyberAccessPrograms {
|
||||
impl ApiKeyCyberAccessPrograms {
|
||||
/// Captures the same provider and feature policy used by ordinary model requests.
|
||||
pub fn from_config(config: &crate::config::Config) -> Self {
|
||||
if !config
|
||||
.model_provider
|
||||
.supports_cyber_access_programs(&config.model_provider_id)
|
||||
{
|
||||
Self::for_provider(
|
||||
&config.model_provider,
|
||||
&config.model_provider_id,
|
||||
&config.features,
|
||||
)
|
||||
}
|
||||
|
||||
/// Resolves API-key policy for the provider that will receive the request.
|
||||
pub fn for_provider(
|
||||
provider: &codex_model_provider_info::ModelProviderInfo,
|
||||
provider_id: &str,
|
||||
features: &codex_features::Features,
|
||||
) -> Self {
|
||||
if !provider.supports_cyber_access_programs(provider_id) {
|
||||
Self::UnsupportedProvider
|
||||
} else if config.features.enabled(Feature::ApiKeyCyberAccessPrograms) {
|
||||
} else if features.enabled(Feature::ApiKeyCyberAccessPrograms) {
|
||||
Self::Enabled
|
||||
} else {
|
||||
Self::Disabled
|
||||
|
||||
@@ -3159,6 +3159,7 @@ async fn try_run_sampling_request(
|
||||
let mut pending_assessment: Option<codex_api::GuardianAssessment> = None;
|
||||
let mut response_completed = false;
|
||||
let mut includes_reasoning = step_context.settings.model_info.use_responses_lite;
|
||||
let accept_actor_assessments = turn_context.config.guardian_model_provider.is_none();
|
||||
let mut resampling = crate::guardian::ResponseResampling::new(&sess);
|
||||
let prompt = Arc::new(prompt);
|
||||
let interrupt_at_boundary = SamplingInterrupt::enabled(&turn_context);
|
||||
@@ -3701,7 +3702,10 @@ async fn try_run_sampling_request(
|
||||
.await;
|
||||
}
|
||||
ResponseEvent::GuardianAssessment(assessment) => {
|
||||
if response_id_for_assessment.as_deref() == Some(assessment.response_id.as_str()) {
|
||||
if accept_actor_assessments
|
||||
&& response_id_for_assessment.as_deref()
|
||||
== Some(assessment.response_id.as_str())
|
||||
{
|
||||
pending_assessment = Some(assessment);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,8 +38,10 @@ use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::openai_models::GuardianModelPolicy;
|
||||
use codex_protocol::openai_models::GuardianReviewMode;
|
||||
use codex_protocol::openai_models::GuardianV2ModelConfig;
|
||||
use codex_protocol::openai_models::InputModality;
|
||||
use codex_protocol::openai_models::ModelsResponse;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::ThreadSettingsOverrides;
|
||||
@@ -65,6 +67,7 @@ use opentelemetry_sdk::metrics::data::AggregatedMetrics;
|
||||
use opentelemetry_sdk::metrics::data::MetricData;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
use test_case::test_case;
|
||||
use tokio::sync::oneshot;
|
||||
use tokio::time::timeout;
|
||||
use wiremock::Mock;
|
||||
@@ -83,14 +86,21 @@ const BYO_HEADERS: [(&str, &str); 2] = [
|
||||
("authorization", "Bearer actor-only"),
|
||||
];
|
||||
|
||||
#[test_case(ApprovalsReviewer::User, AskForApproval::OnRequest, PermissionProfile::read_only(); "user_approvals")]
|
||||
#[test_case(ApprovalsReviewer::AutoReview, AskForApproval::Never, PermissionProfile::Disabled; "full_access")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn compacted_cold_resume_respects_guardian_provider_override() -> Result<()> {
|
||||
async fn compacted_cold_resume_respects_guardian_provider_override(
|
||||
reviewer: ApprovalsReviewer,
|
||||
approval_policy: AskForApproval,
|
||||
profile: PermissionProfile,
|
||||
) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
let actor = responses::start_mock_server().await;
|
||||
let guardian = responses::start_mock_server().await;
|
||||
let guardian_url = format!("{}/v1", guardian.uri());
|
||||
let builder = || {
|
||||
let guardian_url = guardian_url.clone();
|
||||
let profile = profile.clone();
|
||||
test_codex()
|
||||
.with_model_info_override(BYO_ACTOR_MODEL, |model| {
|
||||
model.guardian = Some(GuardianModelPolicy {
|
||||
@@ -106,12 +116,11 @@ async fn compacted_cold_resume_respects_guardian_provider_override() -> Result<(
|
||||
.expect("enable scoring");
|
||||
pin_guardian_parent_compaction_reuse(config);
|
||||
config.guardian_policy_config = None;
|
||||
config.approvals_reviewer = ApprovalsReviewer::User;
|
||||
config.permissions.approval_policy =
|
||||
Constrained::allow_any(AskForApproval::OnRequest);
|
||||
config.approvals_reviewer = reviewer;
|
||||
config.permissions.approval_policy = Constrained::allow_any(approval_policy);
|
||||
config
|
||||
.permissions
|
||||
.set_permission_profile(PermissionProfile::read_only())
|
||||
.set_permission_profile(profile)
|
||||
.expect("set permissions");
|
||||
})
|
||||
};
|
||||
@@ -193,20 +202,40 @@ fn configure_explicit_guardian_provider(config: &mut Config, guardian_url: &str)
|
||||
}));
|
||||
}
|
||||
|
||||
#[test_case(BYO_ACTOR_MODEL, None, false; "different_model_names_at_start")]
|
||||
#[test_case(LUNA_MODEL, Some(11_000), true; "same_model_name_after_start_and_cold_resume")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn explicit_guardian_provider_is_separate() -> Result<()> {
|
||||
let actor_model_name = LUNA_MODEL;
|
||||
let actor_context_window = Some(11_000);
|
||||
async fn explicit_guardian_provider_is_separate(
|
||||
actor_model_name: &'static str,
|
||||
actor_context_window: Option<i64>,
|
||||
cold_resume: bool,
|
||||
) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let actor = responses::start_mock_server().await;
|
||||
let guardian = MockServer::start().await;
|
||||
let guardian_url = format!("{}/v1", guardian.uri());
|
||||
let chatgpt = actor_model_name != LUNA_MODEL;
|
||||
let reviewer_model_name = if chatgpt {
|
||||
"codex-auto-review"
|
||||
} else {
|
||||
LUNA_MODEL
|
||||
};
|
||||
let guardian_auth = if chatgpt {
|
||||
"Bearer Access Token"
|
||||
} else {
|
||||
"Bearer dummy"
|
||||
};
|
||||
let guardian_threshold = if chatgpt { 0 } else { 5_000 };
|
||||
let mut reviewer_model = codex_models_manager::bundled_models_response()?
|
||||
.models
|
||||
.into_iter()
|
||||
.find(|model| model.slug == LUNA_MODEL)
|
||||
.expect("bundled Luna model");
|
||||
reviewer_model.guardian = Some(GuardianModelPolicy {
|
||||
permissions: Some(GuardianReviewMode::Adaptive),
|
||||
..Default::default()
|
||||
});
|
||||
reviewer_model.supported_in_api = true;
|
||||
reviewer_model.use_responses_lite = false;
|
||||
reviewer_model.input_modalities = vec![InputModality::Text, InputModality::Image];
|
||||
@@ -215,18 +244,33 @@ async fn explicit_guardian_provider_is_separate() -> Result<()> {
|
||||
serde_json::from_value(json!({"policy": "Trusted Guardian catalog policy."}))
|
||||
.expect("catalog policy"),
|
||||
);
|
||||
let catalog = ModelsResponse {
|
||||
models: vec![reviewer_model],
|
||||
};
|
||||
messages.guardian_v2 = Some(GuardianV2ModelConfig {
|
||||
classifier_instructions: Some("Trusted Guardian classifier instructions.".to_owned()),
|
||||
reasoning_effort: Some(ReasoningEffort::Minimal),
|
||||
review_threshold_basis_points: Some(guardian_threshold),
|
||||
..Default::default()
|
||||
});
|
||||
let mut models = vec![reviewer_model.clone()];
|
||||
if chatgpt {
|
||||
models[0]
|
||||
.model_messages
|
||||
.as_mut()
|
||||
.expect("classifier messages")
|
||||
.auto_review
|
||||
.as_mut()
|
||||
.expect("classifier policy")
|
||||
.policy = Some("Classifier-only policy that must not authorize actions.".to_owned());
|
||||
reviewer_model.slug = reviewer_model_name.to_owned();
|
||||
models.push(reviewer_model);
|
||||
}
|
||||
let catalog = ModelsResponse { models };
|
||||
responses::mount_models_once(&guardian, catalog.clone()).await;
|
||||
|
||||
let builder = |guardian_url: &str| {
|
||||
let guardian_url = guardian_url.to_owned();
|
||||
test_codex()
|
||||
.with_pre_build_hook(|home| {
|
||||
std::fs::write(
|
||||
home.join("config.toml"),
|
||||
r#"[auto_review]
|
||||
.with_pre_build_hook(move |home| {
|
||||
let actor_config = r#"[auto_review]
|
||||
policy = "Actor config policy."
|
||||
|
||||
[features]
|
||||
@@ -235,9 +279,20 @@ guardian_node_repl_transcript_images = true
|
||||
|
||||
[features.guardianv2]
|
||||
enabled = true
|
||||
"#,
|
||||
)
|
||||
.expect("write Guardian configuration");
|
||||
classifier_instructions = "Actor config classifier instructions."
|
||||
reasoning_effort = "high"
|
||||
review_threshold = ACTOR_THRESHOLD
|
||||
"#
|
||||
.replace(
|
||||
"ACTOR_THRESHOLD",
|
||||
if guardian_threshold == 0 {
|
||||
"0.5"
|
||||
} else {
|
||||
"0.0"
|
||||
},
|
||||
);
|
||||
std::fs::write(home.join("config.toml"), actor_config)
|
||||
.expect("write Guardian configuration");
|
||||
})
|
||||
.with_model_info_override(actor_model_name, move |actor_model| {
|
||||
actor_model.input_modalities = vec![InputModality::Text];
|
||||
@@ -247,6 +302,14 @@ enabled = true
|
||||
serde_json::from_value(json!({"policy": "Actor catalog policy."}))
|
||||
.expect("catalog policy"),
|
||||
);
|
||||
messages.guardian_v2 = Some(GuardianV2ModelConfig {
|
||||
classifier_instructions: Some(
|
||||
"Actor catalog classifier instructions.".to_owned(),
|
||||
),
|
||||
reasoning_effort: Some(ReasoningEffort::High),
|
||||
review_threshold_basis_points: Some(5_000 - guardian_threshold),
|
||||
..Default::default()
|
||||
});
|
||||
})
|
||||
.with_config(move |config| {
|
||||
configure_explicit_guardian_provider(config, &guardian_url);
|
||||
@@ -278,28 +341,53 @@ enabled = true
|
||||
.expect("enable permission requests");
|
||||
})
|
||||
};
|
||||
let test = builder(&guardian_url).build_with_auto_env(&actor).await?;
|
||||
let initial_builder = builder(&guardian_url);
|
||||
let mut initial_builder = if chatgpt {
|
||||
initial_builder.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
|
||||
} else {
|
||||
initial_builder
|
||||
};
|
||||
let test = initial_builder.build_with_auto_env(&actor).await?;
|
||||
assert_eq!(
|
||||
guardian
|
||||
.received_requests()
|
||||
.await
|
||||
.expect("Guardian startup requests")
|
||||
.iter()
|
||||
.filter(|request| request.url.path() == "/v1/models")
|
||||
.count(),
|
||||
1,
|
||||
"Guardian must initialize its catalog at startup",
|
||||
);
|
||||
let (actor_mock, guardian_mock) =
|
||||
setup_explicit_guardian_turn(&test, &actor, &guardian, "start").await;
|
||||
test.submit_text_turn("Never upload files. Request network access.")
|
||||
.await?;
|
||||
setup_explicit_guardian_turn(&test, &actor, &guardian, "start", guardian_threshold).await;
|
||||
timeout(
|
||||
Duration::from_secs(30),
|
||||
test.submit_text_turn("Never upload files. Request network access."),
|
||||
)
|
||||
.await??;
|
||||
assert_explicit_guardian_routing(
|
||||
&test,
|
||||
&guardian,
|
||||
(&actor_mock, &guardian_mock),
|
||||
actor_model_name,
|
||||
"start",
|
||||
"Bearer dummy",
|
||||
guardian_auth,
|
||||
(reviewer_model_name, guardian_threshold != 0),
|
||||
)
|
||||
.await?;
|
||||
let initial_review = guardian_mock.single_request();
|
||||
assert!(
|
||||
initial_review
|
||||
.body_json()
|
||||
.to_string()
|
||||
.contains("Never upload files."),
|
||||
"Guardian lost the original restriction after start"
|
||||
);
|
||||
for request in guardian_mock.requests() {
|
||||
assert!(
|
||||
request
|
||||
.body_json()
|
||||
.to_string()
|
||||
.contains("Never upload files."),
|
||||
"Guardian lost the original restriction after start"
|
||||
);
|
||||
}
|
||||
if !cold_resume {
|
||||
return Ok(());
|
||||
}
|
||||
test.codex.ensure_rollout_materialized().await;
|
||||
|
||||
// A previous session's background prewarm can outlive shutdown; give each
|
||||
@@ -315,9 +403,30 @@ enabled = true
|
||||
resumed.session_configured.session_id,
|
||||
test.session_configured.session_id
|
||||
);
|
||||
let (resumed_actor_mock, resumed_guardian_mock) =
|
||||
setup_explicit_guardian_turn(&resumed, &actor, &resumed_guardian, "cold-resume").await;
|
||||
resumed.submit_text_turn("Request network access.").await?;
|
||||
assert_eq!(
|
||||
resumed_guardian
|
||||
.received_requests()
|
||||
.await
|
||||
.expect("Guardian startup requests")
|
||||
.iter()
|
||||
.filter(|request| request.url.path() == "/v1/models")
|
||||
.count(),
|
||||
1,
|
||||
"Guardian must initialize its catalog at startup",
|
||||
);
|
||||
let (resumed_actor_mock, resumed_guardian_mock) = setup_explicit_guardian_turn(
|
||||
&resumed,
|
||||
&actor,
|
||||
&resumed_guardian,
|
||||
"cold-resume",
|
||||
guardian_threshold,
|
||||
)
|
||||
.await;
|
||||
timeout(
|
||||
Duration::from_secs(30),
|
||||
resumed.submit_text_turn("Request network access."),
|
||||
)
|
||||
.await??;
|
||||
assert_explicit_guardian_routing(
|
||||
&resumed,
|
||||
&resumed_guardian,
|
||||
@@ -325,16 +434,18 @@ enabled = true
|
||||
actor_model_name,
|
||||
"cold-resume",
|
||||
"Bearer resumed-key",
|
||||
(reviewer_model_name, guardian_threshold != 0),
|
||||
)
|
||||
.await?;
|
||||
let resumed_review = resumed_guardian_mock.single_request();
|
||||
assert!(
|
||||
resumed_review
|
||||
.body_json()
|
||||
.to_string()
|
||||
.contains("Never upload files."),
|
||||
"Guardian lost the original restriction after cold-resume"
|
||||
);
|
||||
for request in resumed_guardian_mock.requests() {
|
||||
assert!(
|
||||
request
|
||||
.body_json()
|
||||
.to_string()
|
||||
.contains("Never upload files."),
|
||||
"Guardian lost the original restriction after cold-resume"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -343,6 +454,7 @@ async fn setup_explicit_guardian_turn(
|
||||
actor: &MockServer,
|
||||
guardian: &MockServer,
|
||||
phase: &str,
|
||||
guardian_threshold: u16,
|
||||
) -> (responses::ResponseMock, responses::ResponseMock) {
|
||||
let call_id = format!("{phase}-permission");
|
||||
let done_id = format!("{phase}-done");
|
||||
@@ -351,6 +463,8 @@ async fn setup_explicit_guardian_turn(
|
||||
"permissions": {"network": {"enabled": true}},
|
||||
})
|
||||
.to_string();
|
||||
let mut permission_call = ev_function_call(&call_id, "request_permissions", &permission_args);
|
||||
permission_call["item"]["id"] = json!(&call_id);
|
||||
let actor_mock = responses::mount_sse_sequence(
|
||||
actor,
|
||||
vec![
|
||||
@@ -364,7 +478,14 @@ async fn setup_explicit_guardian_turn(
|
||||
"encrypted_content": "actor-only-checkpoint",
|
||||
},
|
||||
}),
|
||||
ev_function_call(&call_id, "request_permissions", &permission_args),
|
||||
json!({
|
||||
"type": "response.metadata",
|
||||
"response_id": &call_id,
|
||||
"metadata": {
|
||||
"guardian": {"item_id": &call_id, "requires_review": false}
|
||||
},
|
||||
}),
|
||||
permission_call,
|
||||
ev_completed(&call_id),
|
||||
]),
|
||||
sse(vec![
|
||||
@@ -375,11 +496,29 @@ async fn setup_explicit_guardian_turn(
|
||||
],
|
||||
)
|
||||
.await;
|
||||
let blocking = responses::mount_sse_once(guardian, sse(vec![
|
||||
ev_response_created("reviewer"),
|
||||
ev_assistant_message("reviewer", r#"{"risk_level":"high","user_authorization":"low","outcome":"deny","rationale":"Test review."}"#),
|
||||
ev_completed("reviewer"),
|
||||
])).await;
|
||||
let guardian_mock = responses::mount_response_handler(guardian, move |request| {
|
||||
let body = request.body_json();
|
||||
if body["text"]["format"]["type"] == "json_schema" {
|
||||
let response = responses::sse_response(sse(vec![
|
||||
ev_response_created("reviewer"),
|
||||
ev_assistant_message("reviewer", r#"{"risk_level":"high","user_authorization":"low","outcome":"deny","rationale":"Test review."}"#),
|
||||
ev_completed("reviewer"),
|
||||
]));
|
||||
if guardian_threshold == 0 {
|
||||
response
|
||||
} else {
|
||||
// Keep the reviewer pending beyond the turn timeout so only a LOW score can approve.
|
||||
response.set_delay(Duration::from_secs(60))
|
||||
}
|
||||
} else {
|
||||
responses::sse_response(sse(vec![
|
||||
ev_response_created("classifier"),
|
||||
ev_assistant_message("classifier", "low"),
|
||||
ev_completed("classifier"),
|
||||
]))
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
test.codex
|
||||
.thread_extension_data()
|
||||
@@ -395,17 +534,35 @@ async fn setup_explicit_guardian_turn(
|
||||
detail: None,
|
||||
}],
|
||||
);
|
||||
(actor_mock, blocking)
|
||||
(actor_mock, guardian_mock)
|
||||
}
|
||||
|
||||
async fn assert_explicit_guardian_routing(
|
||||
test: &TestCodex,
|
||||
guardian: &MockServer,
|
||||
(actor_mock, blocking): (&responses::ResponseMock, &responses::ResponseMock),
|
||||
(actor_mock, guardian_mock): (&responses::ResponseMock, &responses::ResponseMock),
|
||||
actor_model_name: &str,
|
||||
phase: &str,
|
||||
guardian_auth: &str,
|
||||
(reviewer_model_name, fast_approval): (&str, bool),
|
||||
) -> Result<()> {
|
||||
let call_id = format!("{phase}-permission");
|
||||
timeout(Duration::from_secs(10), async {
|
||||
loop {
|
||||
let requests = guardian_mock.requests();
|
||||
let has_classifier = requests
|
||||
.iter()
|
||||
.any(|request| request.body_json()["text"]["format"]["type"] != "json_schema");
|
||||
let has_reviewer = requests
|
||||
.iter()
|
||||
.any(|request| request.body_json()["text"]["format"]["type"] == "json_schema");
|
||||
if has_classifier && (fast_approval || has_reviewer) {
|
||||
break;
|
||||
}
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
let snapshot = test.codex.conversation_history_snapshot().await;
|
||||
assert!(snapshot.uses_independent_review_history());
|
||||
let checkpoint = snapshot
|
||||
@@ -417,6 +574,18 @@ async fn assert_explicit_guardian_routing(
|
||||
if encrypted_content == "actor-only-checkpoint"
|
||||
));
|
||||
let actor_requests = actor_mock.requests();
|
||||
let followup = actor_requests.last().context("actor followup")?;
|
||||
let output = followup
|
||||
.function_call_output_text(&call_id)
|
||||
.context("permission result")?;
|
||||
let permission: RequestPermissionsResponse = serde_json::from_str(&output)?;
|
||||
assert_eq!(
|
||||
permission.permissions.network,
|
||||
fast_approval.then_some(NetworkPermissions {
|
||||
enabled: Some(true)
|
||||
}),
|
||||
"the Guardian threshold must control the actual approval after {phase}",
|
||||
);
|
||||
for request in &actor_requests {
|
||||
let body = request.body_json();
|
||||
assert_eq!(body["model"], actor_model_name);
|
||||
@@ -438,46 +607,52 @@ async fn assert_explicit_guardian_routing(
|
||||
1,
|
||||
"catalog should be fetched once after {phase}"
|
||||
);
|
||||
assert_eq!(
|
||||
responses::received_responses_requests(guardian).await.len(),
|
||||
1,
|
||||
"blocking layer must not classify"
|
||||
);
|
||||
let review = blocking.single_request();
|
||||
let body = review.body_json();
|
||||
let serialized = body.to_string();
|
||||
assert!(
|
||||
body["service_tier"].is_null(),
|
||||
"Guardian inherited the actor tier after {phase}"
|
||||
);
|
||||
assert!(
|
||||
serialized.contains("Trusted Guardian catalog policy."),
|
||||
"{serialized}"
|
||||
);
|
||||
assert!(
|
||||
!serialized.contains("Actor config") && !serialized.contains("Actor catalog"),
|
||||
"{serialized}"
|
||||
);
|
||||
assert_eq!(
|
||||
(&body["model"], &body["text"]["format"]["type"]),
|
||||
(&json!(LUNA_MODEL), &json!("json_schema"))
|
||||
);
|
||||
assert!(
|
||||
body["input"]
|
||||
.as_array()
|
||||
.expect("Guardian input")
|
||||
.iter()
|
||||
.all(|item| !matches!(
|
||||
item["type"].as_str(),
|
||||
Some("compaction" | "context_compaction")
|
||||
)),
|
||||
"Guardian reused an actor checkpoint after {phase}"
|
||||
);
|
||||
assert_eq!(
|
||||
review.message_input_image_urls("user"),
|
||||
[GUARDIAN_SCREENSHOT],
|
||||
"blocking review should use the Guardian catalog's image support after {phase}",
|
||||
);
|
||||
for request in guardian_mock.requests() {
|
||||
let body = request.body_json();
|
||||
let serialized = body.to_string();
|
||||
assert!(
|
||||
body["service_tier"].is_null(),
|
||||
"Guardian inherited the actor tier after {phase}"
|
||||
);
|
||||
assert!(
|
||||
serialized.contains("Trusted Guardian catalog policy."),
|
||||
"{serialized}"
|
||||
);
|
||||
assert!(
|
||||
!serialized.contains("Actor config")
|
||||
&& !serialized.contains("Actor catalog")
|
||||
&& !serialized.contains("Classifier-only policy"),
|
||||
"{serialized}"
|
||||
);
|
||||
if body["text"]["format"]["type"] == "json_schema" {
|
||||
assert_eq!(body["model"], reviewer_model_name);
|
||||
if !fast_approval {
|
||||
assert_eq!(
|
||||
request.message_input_image_urls("user"),
|
||||
[GUARDIAN_SCREENSHOT],
|
||||
"Guardian image support after {phase}"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
assert_eq!(body["model"], LUNA_MODEL);
|
||||
assert!(
|
||||
serialized.contains("Trusted Guardian classifier instructions."),
|
||||
"{serialized}"
|
||||
);
|
||||
assert_eq!(body["reasoning"]["effort"], "minimal");
|
||||
}
|
||||
assert!(
|
||||
body["input"]
|
||||
.as_array()
|
||||
.expect("Guardian input")
|
||||
.iter()
|
||||
.all(|item| !matches!(
|
||||
item["type"].as_str(),
|
||||
Some("compaction" | "context_compaction")
|
||||
)),
|
||||
"Guardian reused an actor checkpoint after {phase}"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -73,31 +73,45 @@ impl GuardianApprovalReviewer {
|
||||
let model = input
|
||||
.thread_store
|
||||
.get::<codex_protocol::openai_models::ModelInfo>();
|
||||
let has_explicit_guardian_provider = config.guardian_model_provider.is_some();
|
||||
let guardian_classifier_result =
|
||||
GuardianV2Config::explicit_guardian_classifier_model(config, input.thread_store).await;
|
||||
let guardian_config = input
|
||||
.thread_store
|
||||
.get::<GuardianV2Config>()
|
||||
.map(|config| (*config).clone())
|
||||
.map_or_else(|| GuardianV2Config::resolve(config), Ok)
|
||||
.and_then(|config| match &guardian_classifier_result {
|
||||
Ok(Some(model)) => config.with_classifier_defaults(Some(model)),
|
||||
Ok(None) => Ok(config),
|
||||
Err(error) => Err(error.clone()),
|
||||
})
|
||||
.ok();
|
||||
let requirements = config.config_layer_stack.requirements();
|
||||
let mut policy = if config.guardian_model_provider.is_some() {
|
||||
let mut policy = codex_config::GuardianPolicyLoader::new(Some(
|
||||
&codex_features::FeatureToml::Enabled(true),
|
||||
))
|
||||
.resolve(/*model*/ None, requirements);
|
||||
policy.disable_scoring();
|
||||
policy
|
||||
let guardian_classifier_model = guardian_classifier_result.ok().flatten();
|
||||
let review_scope_model = if has_explicit_guardian_provider {
|
||||
guardian_classifier_model.as_deref()
|
||||
} else {
|
||||
guardian_config.as_ref().map_or_else(
|
||||
|| {
|
||||
codex_config::GuardianPolicyLoader::new(Some(
|
||||
&codex_features::FeatureToml::Enabled(true),
|
||||
))
|
||||
.resolve(model.as_deref(), requirements)
|
||||
},
|
||||
|config| config.policy_for_model(model.as_deref(), requirements),
|
||||
)
|
||||
model.as_deref()
|
||||
};
|
||||
let requirements = config.config_layer_stack.requirements();
|
||||
let mut policy = guardian_config.as_ref().map_or_else(
|
||||
|| {
|
||||
codex_config::GuardianPolicyLoader::new(Some(
|
||||
&codex_features::FeatureToml::Enabled(true),
|
||||
))
|
||||
.resolve(review_scope_model, requirements)
|
||||
},
|
||||
|config| config.policy_for_model(review_scope_model, requirements),
|
||||
);
|
||||
if has_explicit_guardian_provider {
|
||||
if guardian_config.is_none() {
|
||||
policy.disable_scoring();
|
||||
}
|
||||
requirements.constrain_guardian_policy(
|
||||
&mut policy,
|
||||
model.as_ref().map(|model| model.slug.as_str()),
|
||||
);
|
||||
}
|
||||
// Live mandatory restrictions may harden the turn's captured policy.
|
||||
thread
|
||||
.config()
|
||||
|
||||
@@ -79,6 +79,7 @@ pub(super) struct Classification {
|
||||
pub(super) config: Arc<Config>,
|
||||
pub(super) context_mode: GuardianContextMode,
|
||||
pub(super) parent_compaction: Option<ResponseItem>,
|
||||
pub(super) max_input_tokens: usize,
|
||||
pub(super) call_id: String,
|
||||
pub(super) mcp_tool: Option<McpToolContext>,
|
||||
pub(super) planned_action: String,
|
||||
@@ -135,6 +136,7 @@ impl Classification {
|
||||
config,
|
||||
context_mode,
|
||||
parent_compaction,
|
||||
max_input_tokens,
|
||||
call_id,
|
||||
mcp_tool,
|
||||
planned_action,
|
||||
@@ -302,7 +304,8 @@ impl Classification {
|
||||
let mut classification_finished_at = None;
|
||||
let mut classifier_turn_id = None;
|
||||
let result: Result<ClassificationOutcome, String> = async {
|
||||
let review_model = if config.guardian_policy_config.is_none()
|
||||
let review_model = if (config.guardian_model_provider.is_some()
|
||||
|| config.guardian_policy_config.is_none())
|
||||
&& let Some(parent_model) = parent_model.as_ref()
|
||||
{
|
||||
Some(codex_core::guardian_policy_model(&thread, &config, Arc::clone(parent_model)).await.map_err(|error| error.to_string())?)
|
||||
@@ -336,6 +339,7 @@ impl Classification {
|
||||
let request = ConversationRequest {
|
||||
evidence,
|
||||
sampling,
|
||||
max_input_tokens,
|
||||
reset_token_limit: guardian_config.async_classifier_conversation_token_limit,
|
||||
ready,
|
||||
authorization: score_authorization.clone(),
|
||||
|
||||
@@ -3,14 +3,19 @@ use std::borrow::Cow;
|
||||
use codex_config::GuardianPolicyLoader;
|
||||
use codex_context_fragments::ContextualUserFragment;
|
||||
use codex_context_fragments::RenderedFragment;
|
||||
use codex_core::GuardianInferenceContext;
|
||||
use codex_core::config::Config;
|
||||
use codex_extension_api::ExtensionData;
|
||||
use codex_features::Feature;
|
||||
use codex_features::FeatureToml;
|
||||
use codex_features::GuardianV2ConfigToml;
|
||||
use codex_features::GuardianV2TranscriptConfigToml;
|
||||
use codex_prompts::GuardianClassifierInstructions;
|
||||
use codex_prompts::ResolvedModelMessages;
|
||||
use codex_protocol::openai_models::CatalogModel;
|
||||
use codex_protocol::openai_models::GuardianModelPolicy;
|
||||
use codex_protocol::openai_models::GuardianV2ModelConfig;
|
||||
use codex_protocol::openai_models::ModelInfo;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
|
||||
use super::transcript::MAX_MESSAGE_ENTRY_TOKENS;
|
||||
@@ -54,6 +59,27 @@ pub(crate) struct GuardianV2Config {
|
||||
|
||||
impl GuardianV2Config {
|
||||
pub(crate) fn resolve(config: &Config) -> Result<Self, String> {
|
||||
if config.guardian_model_provider.is_some() {
|
||||
let enabled = config
|
||||
.config_layer_stack
|
||||
.requirements_toml()
|
||||
.feature_requirements
|
||||
.as_ref()
|
||||
.and_then(|requirements| requirements.entries.get(Feature::GuardianV2.key()))
|
||||
.copied()
|
||||
.unwrap_or(true);
|
||||
let configured = GuardianV2ConfigToml {
|
||||
enabled: Some(enabled),
|
||||
reuse_parent_compaction: Some(false),
|
||||
..Default::default()
|
||||
};
|
||||
let mut resolved = Self::from_overrides(configured.clone())?;
|
||||
resolved.policy = GuardianPolicyLoader::new(Some(&FeatureToml::Config(configured)));
|
||||
resolved.review_multi_agent_calls =
|
||||
config.guardian_native_multi_agent_history_enabled();
|
||||
resolved.multi_agent_namespace = config.multi_agent_v2.tool_namespace.clone();
|
||||
return Ok(resolved);
|
||||
}
|
||||
let effective_config = config.config_layer_stack.effective_config();
|
||||
let mut configured = match effective_config
|
||||
.get("features")
|
||||
@@ -86,6 +112,35 @@ impl GuardianV2Config {
|
||||
Ok(resolved)
|
||||
}
|
||||
|
||||
/// Read the available classifier metadata without waiting for catalog discovery.
|
||||
pub(super) async fn explicit_guardian_classifier_model(
|
||||
config: &Config,
|
||||
store: &ExtensionData,
|
||||
) -> Result<Option<CatalogModel>, String> {
|
||||
let inference = GuardianInferenceContext::for_config(config, store)
|
||||
.map_err(|error| error.to_string())?;
|
||||
Ok(match inference {
|
||||
Some(inference) => Some(
|
||||
inference
|
||||
.models_manager
|
||||
.get_model_info(super::sampler::MODEL, &Default::default())
|
||||
.await,
|
||||
),
|
||||
None => None,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn with_classifier_defaults(
|
||||
&self,
|
||||
model: Option<&ModelInfo>,
|
||||
) -> Result<Self, String> {
|
||||
self.with_model_defaults(
|
||||
model
|
||||
.and_then(|model| model.model_messages.as_ref())
|
||||
.and_then(|messages| messages.guardian_v2.as_ref()),
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn policy_for_model(
|
||||
&self,
|
||||
model: Option<&codex_protocol::openai_models::ModelInfo>,
|
||||
|
||||
@@ -72,6 +72,7 @@ pub(super) struct Reservation {
|
||||
pub(super) struct ConversationRequest {
|
||||
pub(super) evidence: CollectedTranscript,
|
||||
pub(super) sampling: LunaSamplingRequest,
|
||||
pub(super) max_input_tokens: usize,
|
||||
pub(super) reset_token_limit: usize,
|
||||
pub(super) authorization: ScoreAuthorization,
|
||||
pub(super) thread: Arc<CodexThread>,
|
||||
@@ -256,11 +257,7 @@ impl Reservation {
|
||||
let had_history = state.snapshot().is_some();
|
||||
let review_request = request
|
||||
.sampling
|
||||
.prepare_retained(
|
||||
&request.evidence,
|
||||
&mut state,
|
||||
self.sampler.max_input_tokens(),
|
||||
)
|
||||
.prepare_retained(&request.evidence, &mut state, request.max_input_tokens)
|
||||
.filter(|review_request| {
|
||||
review_request.existing_context_tokens == 0
|
||||
|| review_request.input_tokens <= request.reset_token_limit
|
||||
@@ -273,7 +270,7 @@ impl Reservation {
|
||||
request.sampling.prepare_retained(
|
||||
&request.evidence,
|
||||
&mut state,
|
||||
self.sampler.max_input_tokens(),
|
||||
request.max_input_tokens,
|
||||
)
|
||||
});
|
||||
let Some(review_request) = review_request else {
|
||||
|
||||
@@ -67,6 +67,11 @@ async fn pending_request(
|
||||
Ok((
|
||||
ConversationRequest {
|
||||
evidence,
|
||||
max_input_tokens: thread
|
||||
.thread_extension_data()
|
||||
.get::<LunaSampler>()
|
||||
.expect("test sampler")
|
||||
.max_input_tokens(),
|
||||
reset_token_limit: config.async_classifier_conversation_token_limit,
|
||||
authorization,
|
||||
thread,
|
||||
|
||||
@@ -2,10 +2,19 @@
|
||||
|
||||
use codex_extension_api::ToolPayload;
|
||||
use codex_protocol::ToolName;
|
||||
use codex_protocol::config_types::ApprovalsReviewer;
|
||||
use codex_protocol::openai_models::GuardianModelPolicy;
|
||||
use codex_protocol::openai_models::GuardianReviewMode::Adaptive;
|
||||
use codex_protocol::openai_models::GuardianScope;
|
||||
|
||||
/// Full Access can require inspecting selected environments; evaluate it only for Auto Review.
|
||||
pub(super) fn background_scoring_allowed(
|
||||
reviewer: ApprovalsReviewer,
|
||||
full_access: impl FnOnce() -> bool,
|
||||
) -> bool {
|
||||
reviewer == ApprovalsReviewer::AutoReview && !full_access()
|
||||
}
|
||||
|
||||
pub(super) fn scores_tool(
|
||||
policy: &GuardianModelPolicy,
|
||||
tool: &ToolName,
|
||||
|
||||
@@ -23,12 +23,12 @@ use codex_extension_api::ToolLifecycleFuture;
|
||||
use codex_extension_api::ToolStartInput;
|
||||
use codex_features::Feature;
|
||||
use codex_login::AuthManager;
|
||||
use codex_protocol::config_types::ApprovalsReviewer;
|
||||
use codex_protocol::openai_models::ModelInfo;
|
||||
use codex_protocol::protocol::has_full_access;
|
||||
|
||||
use super::config::GuardianV2Config;
|
||||
use super::conversation::ConversationBackend;
|
||||
use super::coverage::background_scoring_allowed;
|
||||
use super::sampler::LunaSampler;
|
||||
use super::trusted_skills::TrustedSkillRoots;
|
||||
|
||||
@@ -45,13 +45,16 @@ impl ThreadLifecycleContributor<Config> for GuardianV2Extension {
|
||||
input: ThreadStartInput<'a, Config>,
|
||||
) -> ExtensionFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
if !input.config.features.enabled(Feature::GuardianApproval)
|
||||
|| input.config.guardian_model_provider.is_some()
|
||||
{
|
||||
if !input.config.features.enabled(Feature::GuardianApproval) {
|
||||
return;
|
||||
}
|
||||
|
||||
let model = input.thread_store.get::<ModelInfo>();
|
||||
let has_explicit_guardian_provider = input.config.guardian_model_provider.is_some();
|
||||
let model = if has_explicit_guardian_provider {
|
||||
None
|
||||
} else {
|
||||
input.thread_store.get::<ModelInfo>()
|
||||
};
|
||||
let thread_id = input.thread_store.level_id().to_string();
|
||||
let guardian_config = match GuardianV2Config::resolve(input.config) {
|
||||
Ok(config) => config,
|
||||
@@ -69,7 +72,28 @@ impl ThreadLifecycleContributor<Config> for GuardianV2Extension {
|
||||
input.config.config_layer_stack.requirements(),
|
||||
);
|
||||
let scoring_enabled = policy.scoring_enabled();
|
||||
let sampler_config = super::startup::sampler_config(&input).await;
|
||||
let should_start_scoring = scoring_enabled
|
||||
&& background_scoring_allowed(input.config.approvals_reviewer, || {
|
||||
has_full_access(
|
||||
input.config.permissions.approval_policy.value(),
|
||||
&input.config.permissions.effective_permission_profile(),
|
||||
input
|
||||
.environments
|
||||
.iter()
|
||||
.map(|environment| &environment.config),
|
||||
)
|
||||
});
|
||||
let sampler_config = match super::startup::sampler_config(&input).await {
|
||||
Ok(config) => config,
|
||||
Err(error) => {
|
||||
self.event_sink.emit_warning(ExtensionWarning {
|
||||
thread_id,
|
||||
turn_id: None,
|
||||
message: format!("Guardian inference setup failed: {error}"),
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if scoring_enabled && guardian_config.transcript.include_images {
|
||||
input
|
||||
@@ -81,6 +105,9 @@ impl ThreadLifecycleContributor<Config> for GuardianV2Extension {
|
||||
let sampler = input
|
||||
.thread_store
|
||||
.get_or_init(|| LunaSampler::new(sampler_config));
|
||||
if should_start_scoring {
|
||||
sampler.ensure_catalog_initialized().await;
|
||||
}
|
||||
ConversationBackend::install(
|
||||
input.thread_store,
|
||||
ConversationBackend::new(
|
||||
@@ -92,10 +119,11 @@ impl ThreadLifecycleContributor<Config> for GuardianV2Extension {
|
||||
input
|
||||
.thread_store
|
||||
.remove::<super::decisions::DecisionsSampler>();
|
||||
if input
|
||||
.config
|
||||
.features
|
||||
.enabled(Feature::GuardianV2DecisionsComparison)
|
||||
if !has_explicit_guardian_provider
|
||||
&& input
|
||||
.config
|
||||
.features
|
||||
.enabled(Feature::GuardianV2DecisionsComparison)
|
||||
&& let Some(decisions_sampler) =
|
||||
super::startup::decisions_sampler(&input, &self.auth_manager)
|
||||
{
|
||||
@@ -116,17 +144,7 @@ impl ThreadLifecycleContributor<Config> for GuardianV2Extension {
|
||||
|
||||
// Keep the sampler available for later automatic review, but do not
|
||||
// prewarm while User approval mode or Full Access is selected.
|
||||
if scoring_enabled
|
||||
&& input.config.approvals_reviewer == ApprovalsReviewer::AutoReview
|
||||
&& !has_full_access(
|
||||
input.config.permissions.approval_policy.value(),
|
||||
&input.config.permissions.effective_permission_profile(),
|
||||
input
|
||||
.environments
|
||||
.iter()
|
||||
.map(|environment| &environment.config),
|
||||
)
|
||||
{
|
||||
if should_start_scoring {
|
||||
tokio::spawn(async move {
|
||||
sampler.prewarm().await;
|
||||
});
|
||||
|
||||
@@ -4053,6 +4053,10 @@ async fn cached_approval_respects_action_order_and_wrapper_lag() -> Result<()> {
|
||||
index: wrapper - 1,
|
||||
max_lag: 2,
|
||||
};
|
||||
// Changing classifier instructions does not invalidate an already-published LOW.
|
||||
let mut config = store.get::<GuardianV2Config>().unwrap().as_ref().clone();
|
||||
config.classifier_instructions = "Reject every read.".to_owned();
|
||||
store.insert(config);
|
||||
for (call_id, expected) in [
|
||||
("first", Some(ReviewDecision::Approved)),
|
||||
("second", Some(ReviewDecision::Approved)),
|
||||
|
||||
@@ -98,10 +98,35 @@ impl GuardianV2Extension {
|
||||
}
|
||||
let parent_model = input.thread_store.get::<ModelInfo>();
|
||||
let probe_comparison = Some(super::probe_shadow::ProbeComparison::capture(&input));
|
||||
let policy = guardian_config.policy_for_model(
|
||||
parent_model.as_deref(),
|
||||
config.config_layer_stack.requirements(),
|
||||
);
|
||||
let has_explicit_guardian_provider = config.guardian_model_provider.is_some();
|
||||
let guardian_classifier_model =
|
||||
match GuardianV2Config::explicit_guardian_classifier_model(&config, input.thread_store)
|
||||
.await
|
||||
{
|
||||
Ok(model) => model,
|
||||
Err(error) => {
|
||||
score_progress.clear_score();
|
||||
self.event_sink.emit_warning(ExtensionWarning {
|
||||
thread_id: input.thread_store.level_id().to_owned(),
|
||||
turn_id: Some(input.turn_id.to_owned()),
|
||||
message: error,
|
||||
});
|
||||
return;
|
||||
}
|
||||
};
|
||||
// Hosted Guardian always provides its own classifier metadata or fails;
|
||||
// only ordinary Codex uses the parent model's Guardian V2 defaults.
|
||||
let scoring_config_model = guardian_classifier_model
|
||||
.as_deref()
|
||||
.or(parent_model.as_deref());
|
||||
let requirements = config.config_layer_stack.requirements();
|
||||
let mut policy = guardian_config.policy_for_model(scoring_config_model, requirements);
|
||||
if has_explicit_guardian_provider {
|
||||
requirements.constrain_guardian_policy(
|
||||
&mut policy,
|
||||
parent_model.as_ref().map(|model| model.slug.as_str()),
|
||||
);
|
||||
}
|
||||
if !policy.scoring_enabled() {
|
||||
input.thread_store.remove::<GuardianV2Enabled>();
|
||||
}
|
||||
@@ -124,8 +149,7 @@ impl GuardianV2Extension {
|
||||
.or_else(|| GuardianScope::for_tool(input.tool_name));
|
||||
// Mandatory review constraints must not turn transparent catalog wrappers into actions.
|
||||
// The legacy all-tools policy still scores wrappers through `other_tools`.
|
||||
let wrapper_mode = parent_model
|
||||
.as_ref()
|
||||
let wrapper_mode = scoring_config_model
|
||||
.and_then(|model| model.guardian.as_ref())
|
||||
.map_or(policy.other_tools, |catalog| catalog.other_tools);
|
||||
if scope.is_none()
|
||||
@@ -260,11 +284,7 @@ impl GuardianV2Extension {
|
||||
return;
|
||||
}
|
||||
input.thread_store.insert(GuardianV2Enabled);
|
||||
let model_defaults = parent_model
|
||||
.as_ref()
|
||||
.and_then(|model| model.model_messages.as_ref())
|
||||
.and_then(|messages| messages.guardian_v2.as_ref());
|
||||
let guardian_config = match guardian_config.with_model_defaults(model_defaults) {
|
||||
let guardian_config = match guardian_config.with_classifier_defaults(scoring_config_model) {
|
||||
Ok(config) => config,
|
||||
Err(error) => {
|
||||
score_progress.fail_closed(sampled_at, tool_call_index);
|
||||
@@ -291,10 +311,18 @@ impl GuardianV2Extension {
|
||||
.get_or_init(NodeReplReviewEvidence::default)
|
||||
.enable_image_capture();
|
||||
}
|
||||
input.thread_store.insert(guardian_config.clone());
|
||||
if !has_explicit_guardian_provider {
|
||||
input.thread_store.insert(guardian_config.clone());
|
||||
}
|
||||
let guardian_evidence = input
|
||||
.thread_store
|
||||
.get_or_init(GuardianReviewEvidence::default);
|
||||
let max_input_tokens = match guardian_classifier_model.as_ref() {
|
||||
Some(model) => codex_guardian_context::effective_input_token_limit(
|
||||
model, /*configured_window*/ None,
|
||||
),
|
||||
None => sampler.max_input_tokens(),
|
||||
};
|
||||
let selected_compaction = match select_parent_compaction(
|
||||
context_mode,
|
||||
&guardian_config,
|
||||
@@ -443,6 +471,7 @@ impl GuardianV2Extension {
|
||||
config,
|
||||
context_mode,
|
||||
parent_compaction,
|
||||
max_input_tokens,
|
||||
call_id,
|
||||
mcp_tool,
|
||||
planned_action,
|
||||
|
||||
@@ -112,6 +112,7 @@ impl LunaSamplingRequest {
|
||||
section_costs,
|
||||
existing_context_tokens,
|
||||
input_tokens,
|
||||
max_input_tokens,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -124,4 +125,5 @@ pub(super) struct PreparedRequest {
|
||||
pub(super) section_costs: Vec<(&'static str, codex_guardian_context::SectionCost)>,
|
||||
pub(super) existing_context_tokens: usize,
|
||||
pub(super) input_tokens: usize,
|
||||
pub(super) max_input_tokens: usize,
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ async fn retained_websocket_requests_preserve_model_output_and_ids() -> Result<(
|
||||
.map(ResponseItemEnvelope::new)
|
||||
.collect(),
|
||||
input_tokens,
|
||||
max_input_tokens: sampler.max_input_tokens(),
|
||||
existing_context_tokens: 0,
|
||||
cursor: TranscriptCursor {
|
||||
parent_history_version: 0,
|
||||
@@ -150,6 +151,7 @@ async fn retention_overflow_or_incomplete_stream_keeps_early_score() -> Result<(
|
||||
.map(ResponseItemEnvelope::new)
|
||||
.collect(),
|
||||
input_tokens,
|
||||
max_input_tokens: sampler.max_input_tokens(),
|
||||
existing_context_tokens: 0,
|
||||
cursor: TranscriptCursor {
|
||||
parent_history_version: 0,
|
||||
|
||||
@@ -19,6 +19,7 @@ use codex_api::Reasoning;
|
||||
use codex_api::ReasoningContext;
|
||||
use codex_api::ResponsesApiRequest;
|
||||
use codex_context_fragments::RenderedFragment;
|
||||
use codex_core::SharedModelsManager;
|
||||
use codex_core::cyber_access_program::ApiKeyCyberAccessPrograms;
|
||||
use codex_extension_api::ExtensionMetrics;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
@@ -67,6 +68,8 @@ pub struct LunaSamplerConfig {
|
||||
pub originator: Option<String>,
|
||||
/// Optional inference service tier.
|
||||
pub service_tier: Option<String>,
|
||||
/// Host-selected catalog, initialized when scoring needs Luna metadata.
|
||||
pub(super) guardian_catalog: Option<SharedModelsManager>,
|
||||
/// Luna model's host-resolved encrypted-compaction compatibility hash.
|
||||
pub luna_compaction_hash: Option<String>,
|
||||
/// Complete input allowance resolved for the classifier model.
|
||||
@@ -161,6 +164,18 @@ impl LunaSampler {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn ensure_catalog_initialized(&self) {
|
||||
if let Some(catalog) = &self.config.guardian_catalog {
|
||||
catalog
|
||||
.ensure_initialized(self.config.http_client_factory.clone())
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn max_input_tokens(&self) -> usize {
|
||||
self.config.max_input_tokens
|
||||
}
|
||||
|
||||
pub(super) async fn prewarm(&self) {
|
||||
self.connections.prewarm().await;
|
||||
}
|
||||
@@ -266,10 +281,6 @@ impl LunaSampler {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn max_input_tokens(&self) -> usize {
|
||||
self.config.max_input_tokens
|
||||
}
|
||||
|
||||
/// Publishes the early score and returns only validated, completed history.
|
||||
pub(super) async fn sample_retained(
|
||||
&self,
|
||||
@@ -285,8 +296,7 @@ impl LunaSampler {
|
||||
ready: Some(ready),
|
||||
output: Some(Vec::new()),
|
||||
early_score: None,
|
||||
remaining_tokens: self
|
||||
.config
|
||||
remaining_tokens: review_request
|
||||
.max_input_tokens
|
||||
.saturating_sub(INPUT_TOKEN_MARGIN)
|
||||
.saturating_sub(review_request.input_tokens),
|
||||
|
||||
@@ -461,6 +461,7 @@ async fn cancellation_closes_http_before_headers_and_while_draining_the_body() -
|
||||
input: PreparedRequest {
|
||||
input,
|
||||
input_tokens,
|
||||
max_input_tokens: sampler.max_input_tokens(),
|
||||
existing_context_tokens: 0,
|
||||
cursor: TranscriptCursor {
|
||||
parent_history_version: 0,
|
||||
|
||||
@@ -5,6 +5,8 @@ use super::proxy_websocket_servers;
|
||||
use super::sample_request;
|
||||
use super::sampler_config;
|
||||
use anyhow::Result;
|
||||
use codex_core::config::GuardianInferenceProvider;
|
||||
use codex_features::Feature;
|
||||
use codex_login::AuthManager;
|
||||
use codex_login::CodexAuth;
|
||||
use codex_login::ExternalAuth;
|
||||
@@ -16,6 +18,7 @@ use codex_login::WorkspaceRoutingResolver;
|
||||
use codex_model_provider::ModelProviderFuture;
|
||||
use codex_model_provider::create_model_provider;
|
||||
use codex_model_provider_info::ModelProviderInfo;
|
||||
use codex_protocol::turn_input::CyberAccessProgram;
|
||||
use core_test_support::responses;
|
||||
use core_test_support::responses::WebSocketConnectionConfig;
|
||||
use core_test_support::responses::ev_assistant_message;
|
||||
@@ -215,7 +218,7 @@ async fn idle_classifier_retains_the_owning_threads_workspace_binding() -> Resul
|
||||
.thread_store
|
||||
.get::<codex_model_provider::WorkspaceRoutingContext>()
|
||||
.expect("Core seeds the owning thread's routing context before extensions start");
|
||||
let classifier = crate::async_scorer::startup::sampler_config(&input).await;
|
||||
let classifier = crate::async_scorer::startup::sampler_config(&input).await?;
|
||||
classifier
|
||||
.provider
|
||||
.responses_api_provider(&owning_context)
|
||||
@@ -227,6 +230,63 @@ async fn idle_classifier_retains_the_owning_threads_workspace_binding() -> Resul
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn host_guardian_classification_uses_its_own_access_program_policy() -> Result<()> {
|
||||
core_test_support::skip_if_no_network!(Ok(()));
|
||||
let actor = responses::start_mock_server().await;
|
||||
let guardian = responses::start_mock_server().await;
|
||||
let response = responses::mount_sse_once(
|
||||
&guardian,
|
||||
responses::sse(vec![
|
||||
ev_assistant_message("score", "low"),
|
||||
ev_completed("score"),
|
||||
]),
|
||||
)
|
||||
.await;
|
||||
let guardian_url = format!("{}/v1", guardian.uri());
|
||||
let test = core_test_support::test_codex::test_codex()
|
||||
.with_auth(CodexAuth::from_api_key("test-api-key"))
|
||||
.with_config(move |config| {
|
||||
config.model_provider_id = "actor".to_owned();
|
||||
config
|
||||
.features
|
||||
.enable(Feature::ApiKeyCyberAccessPrograms)
|
||||
.expect("enable API-key programs");
|
||||
config.model_provider.capabilities = None;
|
||||
let mut provider = ModelProviderInfo::create_openai_provider(Some(guardian_url));
|
||||
provider.supports_websockets = false;
|
||||
config.guardian_model_provider = Some(Arc::new(GuardianInferenceProvider {
|
||||
id: "openai".to_owned(),
|
||||
info: provider,
|
||||
}));
|
||||
})
|
||||
.build_with_auto_env(&actor)
|
||||
.await?;
|
||||
|
||||
let session_store = codex_extension_api::ExtensionData::new("session-1");
|
||||
let input = codex_extension_api::ThreadStartInput {
|
||||
config: &test.config,
|
||||
session_source: &codex_protocol::protocol::SessionSource::Exec,
|
||||
persistent_thread_state_available: false,
|
||||
environments: &[],
|
||||
mcp_resource_client: None,
|
||||
extension_metrics: None,
|
||||
session_store: &session_store,
|
||||
thread_store: test.codex.thread_extension_data(),
|
||||
};
|
||||
let classifier = crate::async_scorer::startup::sampler_config(&input).await?;
|
||||
let sampler = super::LunaSampler::new(classifier);
|
||||
let mut request = sample_request("parent");
|
||||
request.cyber_access_program = Some(CyberAccessProgram::DaybreakRed);
|
||||
|
||||
assert_eq!(sampler.sample(request).await?, "low");
|
||||
assert_eq!(
|
||||
response.single_request().body_json()["access_programs"],
|
||||
serde_json::json!({"cyber": "daybreak_blue"}),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn account_switch_cancels_request_opening_and_first_token_waits() -> Result<()> {
|
||||
core_test_support::skip_if_no_network!(Ok(()));
|
||||
|
||||
@@ -78,6 +78,7 @@ impl LunaSampler {
|
||||
input: crate::async_scorer::request::PreparedRequest {
|
||||
input,
|
||||
input_tokens,
|
||||
max_input_tokens: self.max_input_tokens(),
|
||||
existing_context_tokens: 0,
|
||||
cursor: codex_guardian_context::TranscriptCursor {
|
||||
parent_history_version: 0,
|
||||
@@ -261,6 +262,7 @@ pub(in crate::async_scorer) fn sampler_config(base_url: String) -> LunaSamplerCo
|
||||
originator: Some("guardian-v2-test".to_owned()),
|
||||
|
||||
service_tier: None,
|
||||
guardian_catalog: None,
|
||||
luna_compaction_hash: None,
|
||||
max_input_tokens: codex_guardian_context::DEFAULT_MAX_INPUT_TOKENS,
|
||||
metrics: None,
|
||||
@@ -674,6 +676,7 @@ async fn preconnected_sampler_reuses_authenticated_websocket_for_classifications
|
||||
originator: Some("guardian-v2-test".to_owned()),
|
||||
|
||||
service_tier: None,
|
||||
guardian_catalog: None,
|
||||
luna_compaction_hash: None,
|
||||
max_input_tokens: codex_guardian_context::DEFAULT_MAX_INPUT_TOKENS,
|
||||
metrics: None,
|
||||
@@ -859,6 +862,7 @@ async fn sampler_returns_classification_token_before_terminal_response_events()
|
||||
originator: None,
|
||||
|
||||
service_tier: None,
|
||||
guardian_catalog: None,
|
||||
luna_compaction_hash: None,
|
||||
max_input_tokens: codex_guardian_context::DEFAULT_MAX_INPUT_TOKENS,
|
||||
metrics: None,
|
||||
@@ -880,6 +884,7 @@ async fn sampler_returns_classification_token_before_terminal_response_events()
|
||||
input: crate::async_scorer::request::PreparedRequest {
|
||||
input,
|
||||
input_tokens,
|
||||
max_input_tokens: sampler.max_input_tokens(),
|
||||
existing_context_tokens: 0,
|
||||
cursor: codex_guardian_context::TranscriptCursor {
|
||||
parent_history_version: 0,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex_core::GuardianInferenceContext;
|
||||
use codex_core::SharedModelsManager;
|
||||
use codex_core::config::Config;
|
||||
use codex_core::cyber_access_program::ApiKeyCyberAccessPrograms;
|
||||
@@ -107,10 +108,38 @@ pub(super) fn valid_implicit_decisions_key(key: &str) -> bool {
|
||||
!key.is_empty() && key.trim() == key && !codex_login::auth::is_fedramp_api_key(key)
|
||||
}
|
||||
|
||||
pub(super) async fn sampler_config(input: &ThreadStartInput<'_, Config>) -> LunaSamplerConfig {
|
||||
pub(super) async fn sampler_config(
|
||||
input: &ThreadStartInput<'_, Config>,
|
||||
) -> anyhow::Result<LunaSamplerConfig> {
|
||||
let inherit_parent_cyber_access_program =
|
||||
codex_core::RequiredCyberAccessPrograms::for_thread(input.thread_store).is_none();
|
||||
let luna_model = if let Some(catalog) = input.thread_store.get::<SharedModelsManager>() {
|
||||
let inference = GuardianInferenceContext::for_config(input.config, input.thread_store)?;
|
||||
let (provider_id, provider, http_client_factory, service_tier, guardian_catalog) =
|
||||
if let Some(inference) = inference {
|
||||
(
|
||||
inference.provider_id.clone(),
|
||||
Arc::clone(&inference.provider),
|
||||
inference.http_client_factory.clone(),
|
||||
inference.service_tier.clone(),
|
||||
Some(Arc::clone(&inference.models_manager)),
|
||||
)
|
||||
} else {
|
||||
#[expect(clippy::expect_used, reason = "Required thread startup invariant")]
|
||||
let auth = input
|
||||
.thread_store
|
||||
.get::<ProviderAuthContext>()
|
||||
.expect("Core installs provider auth before starting Guardian");
|
||||
(
|
||||
input.config.model_provider_id.clone(),
|
||||
auth.model_provider(input.config.model_provider.clone()),
|
||||
input.config.http_client_factory(),
|
||||
input.config.service_tier.clone(),
|
||||
None,
|
||||
)
|
||||
};
|
||||
let luna_model = if guardian_catalog.is_none()
|
||||
&& let Some(catalog) = input.thread_store.get::<SharedModelsManager>()
|
||||
{
|
||||
let mut model_config = input.config.to_models_manager_config();
|
||||
model_config.model_context_window = None;
|
||||
Some(catalog.get_model_info(MODEL, &model_config).await)
|
||||
@@ -126,21 +155,20 @@ pub(super) async fn sampler_config(input: &ThreadStartInput<'_, Config>) -> Luna
|
||||
)
|
||||
});
|
||||
let luna_compaction_hash = luna_model.and_then(|model| model.comp_hash.clone());
|
||||
#[expect(clippy::expect_used, reason = "Required thread startup invariant")]
|
||||
let auth = input
|
||||
.thread_store
|
||||
.get::<codex_model_provider::ProviderAuthContext>()
|
||||
.expect("Core installs provider auth before starting Guardian");
|
||||
LunaSamplerConfig {
|
||||
api_key_cyber_access_programs: ApiKeyCyberAccessPrograms::from_config(input.config),
|
||||
Ok(LunaSamplerConfig {
|
||||
api_key_cyber_access_programs: ApiKeyCyberAccessPrograms::for_provider(
|
||||
provider.info(),
|
||||
&provider_id,
|
||||
&input.config.features,
|
||||
),
|
||||
inherit_parent_cyber_access_program,
|
||||
workspace_routing: input
|
||||
.thread_store
|
||||
.get_or_init(|| input.config.workspace_routing_context())
|
||||
.as_ref()
|
||||
.clone(),
|
||||
provider: auth.model_provider(input.config.model_provider.clone()),
|
||||
http_client_factory: input.config.http_client_factory(),
|
||||
provider,
|
||||
http_client_factory,
|
||||
agent_identity_policy: if input.config.features.enabled(Feature::UseAgentIdentity) {
|
||||
AgentIdentityAuthPolicy::ChatGptAuth
|
||||
} else {
|
||||
@@ -153,9 +181,10 @@ pub(super) async fn sampler_config(input: &ThreadStartInput<'_, Config>) -> Luna
|
||||
.thread_store
|
||||
.get::<ThreadOriginator>()
|
||||
.map(|originator| originator.0.clone()),
|
||||
service_tier: input.config.service_tier.clone(),
|
||||
service_tier,
|
||||
guardian_catalog,
|
||||
luna_compaction_hash,
|
||||
max_input_tokens,
|
||||
metrics: input.extension_metrics.clone(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user