mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
## Why Contextual fragments and extension-owned world-state sections currently return strings, limiting them to text content. ## What changed - Return `ContentItem` from fragment bodies and rendering, preserving classification and attribution. Apply text markers only to `InputText`; pass other content through unchanged. - Allow extension prompt and world-state contributions to carry typed content, and preserve media in context-window hints. - Hash non-text world-state fragments by their serialized content and role while retaining the existing text hashing behavior. - Add shared content-to-tool-output conversion that preserves image references, image detail, and audio. ## Testing Add coverage for typed world-state hashing and preservation of image content and attribution. Expand code-mode output tests to cover input text, output text, file-backed and inline images, and audio. GitOrigin-RevId: 2ed00d466a3f169b151202f3eef71e667ea64be1
158 lines
5.5 KiB
Rust
158 lines
5.5 KiB
Rust
//! Covers policy substitution, terminal output contracts, truncation, and rejection-text overrides.
|
|
|
|
use codex_context_fragments::ContextualUserFragment;
|
|
use codex_guardian_context::TRANSCRIPT_JSON_INSTRUCTIONS;
|
|
use codex_guardian_context::truncate_text;
|
|
use codex_protocol::TranscriptFormat;
|
|
use codex_protocol_test_support::ContentItemTestExt;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use super::GuardianClassifierInstructions;
|
|
use super::GuardianPolicyInstructions;
|
|
use super::render_guardian_rejection;
|
|
|
|
#[test]
|
|
fn reviewer_policy_substitution_preserves_template_layout_and_appends_contract() {
|
|
for (template, policy, expected) in [
|
|
(
|
|
" Review:\n{{ tenant_policy_config }}\nAgain: {{ tenant_policy_config }}\n \t",
|
|
" \nTenant policy.\n ",
|
|
" Review:\nTenant policy.\nAgain: Tenant policy.\n\nReview contract.\n",
|
|
),
|
|
(
|
|
"Policy: {{ tenant_policy_config }}",
|
|
"",
|
|
"Policy: \n\nReview contract.\n",
|
|
),
|
|
(
|
|
"Tenant: {{ tenant_policy_config }}\nAdditional: {{ extra_policy }}",
|
|
"Tenant policy.",
|
|
"Tenant: Tenant policy.\nAdditional: \n\nReview contract.\n",
|
|
),
|
|
("", "Tenant policy.", "\n\nReview contract.\n"),
|
|
] {
|
|
assert_eq!(
|
|
GuardianPolicyInstructions::new(
|
|
TranscriptFormat::Json,
|
|
policy,
|
|
"",
|
|
template,
|
|
"Review contract."
|
|
)
|
|
.render()
|
|
.expect_text(),
|
|
format!("{TRANSCRIPT_JSON_INSTRUCTIONS}\n\n{expected}"),
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn reviewer_extra_policy_substitution_preserves_tenant_policy() {
|
|
for (template, extra_policy, expected) in [
|
|
(
|
|
"Tenant: {{ tenant_policy_config }}\nAdditional: {{ extra_policy }}\nAgain: {{ extra_policy }}",
|
|
" \nAdditional policy.\n ",
|
|
"Tenant: Tenant policy.\nAdditional: Additional policy.\nAgain: Additional policy.\n\nReview contract.\n",
|
|
),
|
|
(
|
|
"Tenant: {{ tenant_policy_config }}\nAdditional: {{ extra_policy }}",
|
|
" \n\t",
|
|
"Tenant: Tenant policy.\nAdditional: \n\nReview contract.\n",
|
|
),
|
|
(
|
|
"Tenant: {{ tenant_policy_config }}",
|
|
"Additional policy.",
|
|
"Tenant: Tenant policy.\n\nReview contract.\n",
|
|
),
|
|
] {
|
|
assert_eq!(
|
|
GuardianPolicyInstructions::new(
|
|
TranscriptFormat::Json,
|
|
"Tenant policy.",
|
|
extra_policy,
|
|
template,
|
|
"Review contract.",
|
|
)
|
|
.render()
|
|
.expect_text(),
|
|
format!("{TRANSCRIPT_JSON_INSTRUCTIONS}\n\n{expected}"),
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn reviewer_policy_substitution_keeps_inserted_policy_text_literal() {
|
|
assert_eq!(
|
|
GuardianPolicyInstructions::new(
|
|
TranscriptFormat::Json,
|
|
"Tenant says {{ tenant_policy_config }} and {{ extra_policy }}.",
|
|
"Additional says {{ tenant_policy_config }} and {{ extra_policy }}.",
|
|
"Tenant: {{ tenant_policy_config }}\nAdditional: {{ extra_policy }}",
|
|
"Review contract.",
|
|
)
|
|
.render()
|
|
.expect_text(),
|
|
format!(
|
|
"{TRANSCRIPT_JSON_INSTRUCTIONS}\n\nTenant: Tenant says {{{{ tenant_policy_config }}}} and {{{{ extra_policy }}}}.\nAdditional: Additional says {{{{ tenant_policy_config }}}} and {{{{ extra_policy }}}}.\n\nReview contract.\n"
|
|
),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn classifier_policy_substitution_and_legacy_append_keep_one_terminal_contract_before_truncation() {
|
|
for (instructions, policy, expected) in [
|
|
(
|
|
"Classify: {{ tenant_policy_config }}",
|
|
" Tenant policy.\n",
|
|
"Classify: Tenant policy.\n\n\nClassifier contract.",
|
|
),
|
|
(
|
|
"Classify: {{ tenant_policy_config }}\n\nClassifier contract.\n \t",
|
|
"Tenant policy.",
|
|
"Classify: Tenant policy.\n\nClassifier contract.\n \t",
|
|
),
|
|
(
|
|
"Legacy classifier.",
|
|
"Tenant policy.",
|
|
"Legacy classifier.\n\n# Security Policy\nTenant policy.\n\nClassifier contract.",
|
|
),
|
|
] {
|
|
for max_tokens in [None, Some(20)] {
|
|
let expected = match max_tokens {
|
|
Some(max_tokens) => truncate_text(expected, max_tokens),
|
|
None => expected.to_owned(),
|
|
};
|
|
assert_eq!(
|
|
GuardianClassifierInstructions::new(
|
|
TranscriptFormat::Json,
|
|
instructions,
|
|
policy,
|
|
"Classifier contract.",
|
|
max_tokens,
|
|
)
|
|
.render()
|
|
.expect_text(),
|
|
format!("{TRANSCRIPT_JSON_INSTRUCTIONS}\n\n{expected}"),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn rejection_feedback_trims_rationale_and_preserves_instruction_overrides() {
|
|
for (rationale, instructions, expected) in [
|
|
(
|
|
" \nSensitive data would leave the workspace.\t ",
|
|
" Ask for approval.\n",
|
|
"This action was rejected due to unacceptable risk.\nReason: Sensitive data would leave the workspace.\n Ask for approval.\n",
|
|
),
|
|
(
|
|
" \n\t",
|
|
"",
|
|
"This action was rejected due to unacceptable risk.\nReason: Auto-reviewer denied the action without a specific rationale.\n",
|
|
),
|
|
] {
|
|
assert_eq!(render_guardian_rejection(rationale, instructions), expected);
|
|
}
|
|
}
|