mirror of
https://github.com/openai/codex.git
synced 2026-11-29 00:00:54 +00:00
## Why Transcript content can contain forged role headers, transcript boundaries, or claims of user approval. Guardian needs to distinguish that content from host-assigned authorship metadata. ## What changed - Add `features.guardianv2.transcript_mode` with `line` (the default) and `json` options for synchronous review and asynchronous scoring. - Render JSON transcript entries with host-assigned metadata and escaped `text`, and prepend matching provenance instructions to reviewer and classifier prompts. - Preserve JSON metadata when budget recovery shortens historical text, and keep provenance instructions outside the configured classifier prompt limit. ## Testing Add coverage for forged roles and approvals remaining within their original entries, JSON metadata surviving budget recovery, and provenance instructions surviving prompt limits. Extend review and scoring integration tests to verify JSON evidence and matching instructions. GitOrigin-RevId: 81a3580390d3c4d1a664e554976cda5f1adc9a2c
172 lines
5.5 KiB
Rust
172 lines
5.5 KiB
Rust
//! Composes Guardian prompts and rejection feedback from selected text and runtime inputs.
|
|
//! Callers select policy overrides, output contracts, calibration, and truncation limits.
|
|
|
|
use codex_context_fragments::ContextualUserFragment;
|
|
use codex_guardian_context::TRANSCRIPT_JSON_INSTRUCTIONS;
|
|
use codex_guardian_context::TranscriptFormat;
|
|
use codex_guardian_context::truncate_text;
|
|
use codex_protocol::models::ContentItemKind;
|
|
|
|
const TENANT_POLICY_CONFIG_PLACEHOLDER: &str = "{{ tenant_policy_config }}";
|
|
const EXTRA_POLICY_PLACEHOLDER: &str = "{{ extra_policy }}";
|
|
|
|
/// Reviewer base instructions composed from the effective policy and caller-owned output contract.
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub struct GuardianPolicyInstructions<'a> {
|
|
transcript_format: TranscriptFormat,
|
|
tenant_policy_config: &'a str,
|
|
extra_policy: &'a str,
|
|
policy_template: &'a str,
|
|
output_contract: &'a str,
|
|
}
|
|
|
|
impl<'a> GuardianPolicyInstructions<'a> {
|
|
pub fn new(
|
|
transcript_format: TranscriptFormat,
|
|
tenant_policy_config: &'a str,
|
|
extra_policy: &'a str,
|
|
policy_template: &'a str,
|
|
output_contract: &'a str,
|
|
) -> Self {
|
|
Self {
|
|
transcript_format,
|
|
tenant_policy_config,
|
|
extra_policy,
|
|
policy_template,
|
|
output_contract,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ContextualUserFragment for GuardianPolicyInstructions<'_> {
|
|
fn role(&self) -> &'static str {
|
|
"developer"
|
|
}
|
|
|
|
fn content_kind(&self) -> ContentItemKind {
|
|
ContentItemKind("guardian.policy_instructions".to_owned())
|
|
}
|
|
|
|
fn markers(&self) -> (&'static str, &'static str) {
|
|
Self::type_markers()
|
|
}
|
|
|
|
fn type_markers() -> (&'static str, &'static str) {
|
|
("", "")
|
|
}
|
|
|
|
fn body(&self) -> String {
|
|
let Self {
|
|
transcript_format,
|
|
tenant_policy_config,
|
|
extra_policy,
|
|
policy_template,
|
|
output_contract,
|
|
} = *self;
|
|
// Substitute only template text, leaving placeholder-like text in either policy literal.
|
|
let prompt = policy_template
|
|
.trim_end()
|
|
.split(TENANT_POLICY_CONFIG_PLACEHOLDER)
|
|
.map(|part| part.replace(EXTRA_POLICY_PLACEHOLDER, extra_policy.trim()))
|
|
.collect::<Vec<_>>()
|
|
.join(tenant_policy_config.trim());
|
|
let instructions = format!("{prompt}\n\n{output_contract}\n");
|
|
match transcript_format {
|
|
TranscriptFormat::Line => instructions,
|
|
TranscriptFormat::Json => format!("{TRANSCRIPT_JSON_INSTRUCTIONS}\n\n{instructions}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Classifier instructions composed from selected text, policy, and the caller-owned output contract.
|
|
/// A supplied token limit applies before the fixed transcript provenance instructions.
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub struct GuardianClassifierInstructions<'a> {
|
|
transcript_format: TranscriptFormat,
|
|
classifier_instructions: &'a str,
|
|
policy: &'a str,
|
|
output_contract: &'a str,
|
|
max_tokens: Option<usize>,
|
|
}
|
|
|
|
impl<'a> GuardianClassifierInstructions<'a> {
|
|
pub fn new(
|
|
transcript_format: TranscriptFormat,
|
|
classifier_instructions: &'a str,
|
|
policy: &'a str,
|
|
output_contract: &'a str,
|
|
max_tokens: Option<usize>,
|
|
) -> Self {
|
|
Self {
|
|
transcript_format,
|
|
classifier_instructions,
|
|
policy,
|
|
output_contract,
|
|
max_tokens,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ContextualUserFragment for GuardianClassifierInstructions<'_> {
|
|
fn role(&self) -> &'static str {
|
|
"developer"
|
|
}
|
|
|
|
fn content_kind(&self) -> ContentItemKind {
|
|
ContentItemKind("guardian.classifier_instructions".to_owned())
|
|
}
|
|
|
|
fn markers(&self) -> (&'static str, &'static str) {
|
|
Self::type_markers()
|
|
}
|
|
|
|
fn type_markers() -> (&'static str, &'static str) {
|
|
("", "")
|
|
}
|
|
|
|
fn body(&self) -> String {
|
|
let Self {
|
|
transcript_format,
|
|
classifier_instructions,
|
|
policy,
|
|
output_contract,
|
|
max_tokens,
|
|
} = *self;
|
|
let instructions = if classifier_instructions.contains(TENANT_POLICY_CONFIG_PLACEHOLDER) {
|
|
classifier_instructions.replace(TENANT_POLICY_CONFIG_PLACEHOLDER, policy)
|
|
} else {
|
|
format!("{classifier_instructions}\n\n# Security Policy\n{policy}")
|
|
};
|
|
let instructions = if instructions.trim_end().ends_with(output_contract) {
|
|
instructions
|
|
} else {
|
|
format!("{instructions}\n\n{output_contract}")
|
|
};
|
|
let instructions = match max_tokens {
|
|
Some(max_tokens) => truncate_text(&instructions, max_tokens),
|
|
None => instructions,
|
|
};
|
|
// Fixed host provenance must survive even the smallest configured prompt cap.
|
|
match transcript_format {
|
|
TranscriptFormat::Line => instructions,
|
|
TranscriptFormat::Json => format!("{TRANSCRIPT_JSON_INSTRUCTIONS}\n\n{instructions}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Renders feedback for an action the caller has already decided to reject.
|
|
pub fn render_guardian_rejection(rationale: &str, rejection_instructions: &str) -> String {
|
|
let rationale = if rationale.trim().is_empty() {
|
|
"Auto-reviewer denied the action without a specific rationale."
|
|
} else {
|
|
rationale.trim()
|
|
};
|
|
format!(
|
|
"This action was rejected due to unacceptable risk.\nReason: {rationale}\n{rejection_instructions}"
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "guardian_instructions_tests.rs"]
|
|
mod tests;
|