mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Move Guardian review evidence into the reviewer extension (#57702)
## What changed Move review evidence storage, authorization snapshots, sender exchange selection, and bounded Node REPL evidence into `codex-guardian-reviewer`. Update core to call or re-export these APIs while retaining host-specific context rendering and capture-mode selection. Move the existing Node REPL evidence and text truncation tests alongside their implementations in the reviewer crate. GitOrigin-RevId: d851cd353cf9f31eb47ce13f43f6f935dc2f5d93
This commit is contained in:
@@ -1,190 +1,13 @@
|
||||
//! Selects Guardian answer evidence from the review's history snapshot and retains reviews.
|
||||
//! Retained answers survive restart even while incompatible checkpoints use legacy review.
|
||||
//! Typed context fragment for bounded, host-supplied synchronous review evidence.
|
||||
//! Runtime evidence storage and selection belong to the reviewer extension.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::PoisonError;
|
||||
|
||||
use codex_extension_api::ConversationHistorySnapshot;
|
||||
use codex_guardian_context::MAX_PREVIOUS_REVIEWS;
|
||||
use super::ContextualUserFragment;
|
||||
pub use codex_guardian_reviewer::GuardianReviewEvidence;
|
||||
pub use codex_guardian_reviewer::GuardianReviewEvidenceRecord;
|
||||
pub use codex_guardian_reviewer::GuardianUserInputSnapshot;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ContentItemKind;
|
||||
use codex_protocol::models::input_text;
|
||||
use codex_protocol::protocol::GuardianAssessmentEvent;
|
||||
use serde_json::json;
|
||||
|
||||
use super::ContextualUserFragment;
|
||||
use crate::codex_thread::GuardianAuthorizationVersion;
|
||||
|
||||
const MAX_TRUSTED_SKILLS: usize = 16;
|
||||
const MAX_TRUSTED_SKILL_PATHS_BYTES: usize = 2_048;
|
||||
|
||||
/// Selected answer fragments and the authorization state they describe.
|
||||
pub struct GuardianUserInputSnapshot {
|
||||
pub fragments: Vec<String>,
|
||||
pub authorization_version: GuardianAuthorizationVersion,
|
||||
}
|
||||
|
||||
/// Selected answer evidence, verified skill paths, and completed Guardian reviews.
|
||||
///
|
||||
/// This runtime-only evidence is never inserted into the agent's conversation.
|
||||
/// Only bounded, turn-matched skill paths are exposed to delegated workers;
|
||||
/// completed reviews remain thread-local, and authorization changes invalidate stale records.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct GuardianReviewEvidence {
|
||||
state: Mutex<GuardianReviewEvidenceState>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct GuardianReviewEvidenceState {
|
||||
reviews: VecDeque<Arc<GuardianReviewEvidenceRecord>>,
|
||||
trusted_skill_turn_id: Option<String>,
|
||||
trusted_skill_paths: BTreeSet<String>,
|
||||
}
|
||||
|
||||
impl GuardianReviewEvidence {
|
||||
/// Reads the selected answer path against the caller's action-time history snapshot.
|
||||
pub fn user_input_snapshot(
|
||||
&self,
|
||||
history: &dyn ConversationHistorySnapshot,
|
||||
) -> GuardianUserInputSnapshot {
|
||||
match history.retained_context() {
|
||||
Some(context) => {
|
||||
let answers = codex_guardian_context::render_verified_answers(context);
|
||||
let authorization_version = GuardianAuthorizationVersion {
|
||||
user_message_revision: history.user_message_revision(),
|
||||
retained_context_complete: answers.complete,
|
||||
};
|
||||
GuardianUserInputSnapshot {
|
||||
fragments: answers.fragments,
|
||||
authorization_version,
|
||||
}
|
||||
}
|
||||
None => GuardianUserInputSnapshot {
|
||||
fragments: Vec::new(),
|
||||
authorization_version: GuardianAuthorizationVersion {
|
||||
user_message_revision: history.user_message_revision(),
|
||||
retained_context_complete: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn authorization_version(
|
||||
&self,
|
||||
history: &dyn ConversationHistorySnapshot,
|
||||
) -> GuardianAuthorizationVersion {
|
||||
self.user_input_snapshot(history).authorization_version
|
||||
}
|
||||
|
||||
/// Records a bounded, verified user-owned skill path for one host-owned turn.
|
||||
pub fn record_trusted_skill(&self, turn_id: &str, path: String) {
|
||||
if turn_id.is_empty() {
|
||||
return;
|
||||
}
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if state.trusted_skill_turn_id.as_deref() != Some(turn_id) {
|
||||
state.trusted_skill_turn_id = Some(turn_id.to_owned());
|
||||
state.trusted_skill_paths.clear();
|
||||
}
|
||||
if state.trusted_skill_paths.contains(&path)
|
||||
|| state.trusted_skill_paths.len() >= MAX_TRUSTED_SKILLS
|
||||
|| state
|
||||
.trusted_skill_paths
|
||||
.iter()
|
||||
.map(String::len)
|
||||
.sum::<usize>()
|
||||
.saturating_add(path.len())
|
||||
> MAX_TRUSTED_SKILL_PATHS_BYTES
|
||||
{
|
||||
return;
|
||||
}
|
||||
state.trusted_skill_paths.insert(path);
|
||||
}
|
||||
|
||||
/// Returns verified skill paths only for their original host-owned turn.
|
||||
pub fn trusted_skill_paths(&self, turn_id: &str) -> Vec<String> {
|
||||
let state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if state.trusted_skill_turn_id.as_deref() != Some(turn_id) {
|
||||
return Vec::new();
|
||||
}
|
||||
state.trusted_skill_paths.iter().cloned().collect()
|
||||
}
|
||||
|
||||
/// Records a genuine allow/deny assessment, not a timeout or fail-closed error.
|
||||
pub(crate) fn record(
|
||||
&self,
|
||||
assessment: &GuardianAssessmentEvent,
|
||||
action: &str,
|
||||
authorization_version: GuardianAuthorizationVersion,
|
||||
review_context_revision: u64,
|
||||
root_authorization_version: Option<GuardianAuthorizationVersion>,
|
||||
root_review_context_revision: Option<u64>,
|
||||
) {
|
||||
let Some(completed_at_ms) = assessment.completed_at_ms else {
|
||||
return;
|
||||
};
|
||||
let review = Arc::new(GuardianReviewEvidenceRecord {
|
||||
delivery_id: codex_protocol::ResponseItemId::new("guardian_review"),
|
||||
completed_at_ms,
|
||||
authorization_version,
|
||||
review_context_revision,
|
||||
root_authorization_version,
|
||||
root_review_context_revision,
|
||||
correlation: json!({
|
||||
"review_id": assessment.id,
|
||||
"turn_id": assessment.turn_id,
|
||||
"target_item_id": assessment.target_item_id,
|
||||
"completed_at_ms": completed_at_ms,
|
||||
}),
|
||||
decision: json!({
|
||||
"status": assessment.status,
|
||||
"risk_level": assessment.risk_level,
|
||||
"user_authorization": assessment.user_authorization,
|
||||
}),
|
||||
action: action.to_owned(),
|
||||
rationale: assessment.rationale.clone(),
|
||||
});
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
state.reviews.push_back(review);
|
||||
state
|
||||
.reviews
|
||||
.make_contiguous()
|
||||
.sort_by_key(|review| review.completed_at_ms);
|
||||
while state.reviews.len() > MAX_PREVIOUS_REVIEWS {
|
||||
state.reviews.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
/// Freezes the latest completed reviews, oldest first, for one classifier sample.
|
||||
pub fn snapshot(&self) -> Vec<Arc<GuardianReviewEvidenceRecord>> {
|
||||
self.state
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.reviews
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Structured synchronous-review evidence retained for Guardian V2 classification.
|
||||
#[derive(Debug)]
|
||||
pub struct GuardianReviewEvidenceRecord {
|
||||
/// Identifies this immutable completion even if the same action is reviewed again.
|
||||
pub delivery_id: codex_protocol::ResponseItemId,
|
||||
pub authorization_version: GuardianAuthorizationVersion,
|
||||
pub review_context_revision: u64,
|
||||
pub root_authorization_version: Option<GuardianAuthorizationVersion>,
|
||||
pub root_review_context_revision: Option<u64>,
|
||||
completed_at_ms: i64,
|
||||
pub correlation: serde_json::Value,
|
||||
pub decision: serde_json::Value,
|
||||
pub action: String,
|
||||
pub rationale: Option<String>,
|
||||
}
|
||||
|
||||
/// A bounded, host-supplied sync-review record for async classifier input only.
|
||||
#[derive(Clone, Debug)]
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
use super::ContextualUserFragment;
|
||||
use codex_guardian_context::GuardianRootMessage;
|
||||
pub(crate) use codex_guardian_reviewer::GuardianSenderExchange;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ContentItemKind;
|
||||
@@ -16,12 +17,6 @@ pub(crate) struct GuardianSenderMessages {
|
||||
pub messages: Vec<GuardianSenderExchange>,
|
||||
}
|
||||
|
||||
/// Recorded adjacency provides context, not a verified question/answer association.
|
||||
pub(crate) struct GuardianSenderExchange {
|
||||
pub user: Option<String>,
|
||||
pub assistant: Option<GuardianRootMessage>,
|
||||
}
|
||||
|
||||
impl ContextualUserFragment for GuardianSenderMessages {
|
||||
fn content_kind(&self) -> ContentItemKind {
|
||||
ContentItemKind("guardian.sender_messages".to_owned())
|
||||
|
||||
@@ -93,7 +93,6 @@ pub use guardian_review_evidence::GuardianReviewEvidence;
|
||||
pub use guardian_review_evidence::GuardianReviewEvidenceFragment;
|
||||
pub use guardian_review_evidence::GuardianReviewEvidenceRecord;
|
||||
pub use guardian_review_evidence::GuardianUserInputSnapshot;
|
||||
pub(crate) use guardian_sender_messages::GuardianSenderExchange;
|
||||
pub(crate) use guardian_sender_messages::GuardianSenderMessages;
|
||||
pub(crate) use hook_additional_context::HookAdditionalContext;
|
||||
pub(crate) use image_resize_notice::ImageResizeNotice;
|
||||
|
||||
@@ -1,31 +1,12 @@
|
||||
//! Thread-owned capture and bounded storage for completed REPL evidence.
|
||||
//! Immutable snapshots feed shared rendering; sequence and eviction stay host-owned.
|
||||
//! Collection shares frozen records; image eviction uses copy-on-write to preserve readers.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::PoisonError;
|
||||
//! Chooses REPL evidence capture mode from the issuing turn.
|
||||
//! The reviewer extension owns the bounded store and immutable snapshots.
|
||||
|
||||
use codex_features::Feature;
|
||||
use codex_guardian_context::NodeReplContext;
|
||||
use codex_guardian_context::NodeReplResponse;
|
||||
pub(crate) use codex_guardian_context::NodeReplReviewEvidenceMode;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use codex_protocol::user_input::UserInput::Image;
|
||||
use codex_protocol::user_input::UserInput::Text;
|
||||
use codex_utils_output_truncation::approx_token_count;
|
||||
use codex_utils_string::take_bytes_at_char_boundary;
|
||||
pub use codex_guardian_reviewer::NodeReplReviewEvidence;
|
||||
|
||||
use crate::guardian::GUARDIAN_MAX_NODE_REPL_TOOL_RESULT_TOKENS;
|
||||
use crate::guardian::guardian_truncate_text;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
|
||||
const MAX_PROVENANCE_BYTES: usize = 128;
|
||||
|
||||
pub(crate) fn node_repl_review_evidence_mode(turn: &TurnContext) -> NodeReplReviewEvidenceMode {
|
||||
let features = &turn.config.features;
|
||||
if turn.model_info().computer_use_review_required()
|
||||
@@ -39,264 +20,3 @@ pub(crate) fn node_repl_review_evidence_mode(turn: &TurnContext) -> NodeReplRevi
|
||||
NodeReplReviewEvidenceMode::Disabled
|
||||
}
|
||||
}
|
||||
|
||||
fn retained_bytes(response: &NodeReplResponse) -> usize {
|
||||
std::mem::size_of::<NodeReplResponse>()
|
||||
.saturating_add(std::mem::size_of::<Arc<NodeReplResponse>>())
|
||||
.saturating_add(response.provenance.len())
|
||||
.saturating_add(
|
||||
response
|
||||
.items
|
||||
.capacity()
|
||||
.saturating_mul(std::mem::size_of::<UserInput>()),
|
||||
)
|
||||
.saturating_add(response.items.iter().fold(0_usize, |bytes, item| {
|
||||
bytes.saturating_add(match item {
|
||||
Text { text, .. } => text.len(),
|
||||
Image {
|
||||
image: ImageReference::Inline { image_url },
|
||||
..
|
||||
} => image_url.len(),
|
||||
Image {
|
||||
image: ImageReference::File { file_id },
|
||||
..
|
||||
} => file_id.len(),
|
||||
_ => 0,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
fn discard_images(response: &mut NodeReplResponse) -> usize {
|
||||
let before = retained_bytes(response);
|
||||
response.items.retain(|item| !matches!(item, Image { .. }));
|
||||
response.items.shrink_to_fit();
|
||||
before.saturating_sub(retained_bytes(response))
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct NodeReplReviewEvidenceState {
|
||||
responses: VecDeque<Arc<NodeReplResponse>>,
|
||||
next_sequence: u64,
|
||||
retained_bytes: usize,
|
||||
image_capture_enabled: bool,
|
||||
}
|
||||
|
||||
/// Bounded, thread-scoped evidence collected from nested `node_repl` or `cua_repl` calls.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct NodeReplReviewEvidence(Mutex<NodeReplReviewEvidenceState>);
|
||||
|
||||
impl NodeReplReviewEvidence {
|
||||
pub(crate) const MAX_RETAINED_BYTES: usize = 8 * 1024 * 1024;
|
||||
|
||||
/// Enables screenshot capture even when synchronous Guardian transcripts are disabled.
|
||||
pub fn enable_image_capture(&self) {
|
||||
self.0
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.image_capture_enabled = true;
|
||||
}
|
||||
|
||||
pub(crate) fn image_capture_enabled(&self) -> bool {
|
||||
self.0
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.image_capture_enabled
|
||||
}
|
||||
|
||||
/// Returns the screenshots currently retained for this thread, oldest first.
|
||||
pub fn images(&self) -> Vec<ContentItem> {
|
||||
let state = self.0.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let mut seen_images = HashSet::new();
|
||||
state
|
||||
.responses
|
||||
.iter()
|
||||
.flat_map(|response| &response.items)
|
||||
.filter_map(|item| match item {
|
||||
Image {
|
||||
image: ImageReference::Inline { image_url },
|
||||
detail,
|
||||
} if seen_images.insert(image_url) => Some(ContentItem::InputImage {
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.clone(),
|
||||
},
|
||||
detail: *detail,
|
||||
}),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn record(
|
||||
&self,
|
||||
tool_name: &str,
|
||||
cell_id: &str,
|
||||
call_id: &str,
|
||||
mut items: Vec<UserInput>,
|
||||
) {
|
||||
if !items.iter().any(|item| matches!(item, Image { .. })) {
|
||||
let text = items
|
||||
.into_iter()
|
||||
.filter_map(|item| match item {
|
||||
Text { text, .. } => Some(text),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
items = Vec::from_iter((!text.is_empty()).then(|| text_input(text)));
|
||||
}
|
||||
let mut bounded_items = Vec::with_capacity(items.len());
|
||||
let mut rendered_text_tokens = 0_usize;
|
||||
let mut remaining_source_text_tokens = items
|
||||
.iter()
|
||||
.map(|item| match item {
|
||||
Text { text, .. } => approx_token_count(text),
|
||||
_ => 0,
|
||||
})
|
||||
.fold(0_usize, usize::saturating_add);
|
||||
for item in items {
|
||||
let Text { text, .. } = item else {
|
||||
if matches!(item, Image { .. }) {
|
||||
bounded_items.push(item);
|
||||
}
|
||||
continue;
|
||||
};
|
||||
if text.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
remaining_source_text_tokens =
|
||||
remaining_source_text_tokens.saturating_sub(approx_token_count(&text));
|
||||
let remaining_tokens =
|
||||
GUARDIAN_MAX_NODE_REPL_TOOL_RESULT_TOKENS.saturating_sub(rendered_text_tokens);
|
||||
let reserved_tail_tokens = remaining_source_text_tokens.min(remaining_tokens / 2);
|
||||
let (text, _) = guardian_truncate_text(
|
||||
&text.replace("</", "<\\/"),
|
||||
remaining_tokens.saturating_sub(reserved_tail_tokens),
|
||||
);
|
||||
let text_tokens = approx_token_count(&text);
|
||||
if text_tokens <= remaining_tokens {
|
||||
rendered_text_tokens = rendered_text_tokens.saturating_add(text_tokens);
|
||||
bounded_items.push(text_input(text));
|
||||
}
|
||||
}
|
||||
|
||||
let mut state = self.0.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
state.next_sequence = state.next_sequence.saturating_add(1);
|
||||
let mut response = Arc::new(NodeReplResponse {
|
||||
sequence: state.next_sequence,
|
||||
provenance: format!(
|
||||
"tool={} cell={} call={}",
|
||||
bounded_provenance(tool_name),
|
||||
bounded_provenance(cell_id),
|
||||
bounded_provenance(call_id)
|
||||
),
|
||||
items: bounded_items,
|
||||
});
|
||||
if retained_bytes(&response) > Self::MAX_RETAINED_BYTES {
|
||||
discard_images(Arc::make_mut(&mut response));
|
||||
if response.items.is_empty() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
while state
|
||||
.retained_bytes
|
||||
.saturating_add(retained_bytes(&response))
|
||||
> Self::MAX_RETAINED_BYTES
|
||||
{
|
||||
if state.responses.front().is_some_and(|retained| {
|
||||
retained
|
||||
.items
|
||||
.iter()
|
||||
.any(|item| matches!(item, Text { .. }))
|
||||
}) {
|
||||
let reclaimed = state
|
||||
.responses
|
||||
.iter_mut()
|
||||
.find(|retained| {
|
||||
retained
|
||||
.items
|
||||
.iter()
|
||||
.any(|item| matches!(item, Image { .. }))
|
||||
})
|
||||
.map_or(0, |retained| discard_images(Arc::make_mut(retained)));
|
||||
state.retained_bytes = state.retained_bytes.saturating_sub(reclaimed);
|
||||
if reclaimed > 0 || discard_images(Arc::make_mut(&mut response)) > 0 {
|
||||
if response.items.is_empty() {
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let Some(evicted) = state.responses.pop_front() else {
|
||||
return;
|
||||
};
|
||||
state.retained_bytes = state
|
||||
.retained_bytes
|
||||
.saturating_sub(retained_bytes(&evicted));
|
||||
}
|
||||
state.retained_bytes = state
|
||||
.retained_bytes
|
||||
.saturating_add(retained_bytes(&response));
|
||||
state.responses.push_back(response);
|
||||
}
|
||||
|
||||
pub(crate) fn snapshot_since(
|
||||
&self,
|
||||
reviewed_sequence: u64,
|
||||
) -> Option<NodeReplReviewEvidenceSnapshot> {
|
||||
let state = self.0.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if state.next_sequence <= reviewed_sequence {
|
||||
return None;
|
||||
}
|
||||
|
||||
let responses = state
|
||||
.responses
|
||||
.iter()
|
||||
.filter(|response| response.sequence > reviewed_sequence)
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
let retained_since_review = u64::try_from(responses.len()).unwrap_or(u64::MAX);
|
||||
|
||||
Some(NodeReplReviewEvidenceSnapshot {
|
||||
omitted_responses: state
|
||||
.next_sequence
|
||||
.saturating_sub(reviewed_sequence)
|
||||
.saturating_sub(retained_since_review),
|
||||
sequence: state.next_sequence,
|
||||
responses,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn bounded_provenance(value: &str) -> String {
|
||||
let sanitized = take_bytes_at_char_boundary(value, MAX_PROVENANCE_BYTES)
|
||||
.replace(['\n', '\r', '[', ']', '='], "_")
|
||||
.replace("</", "<\\/");
|
||||
take_bytes_at_char_boundary(&sanitized, MAX_PROVENANCE_BYTES).to_string()
|
||||
}
|
||||
|
||||
fn text_input(text: String) -> UserInput {
|
||||
UserInput::Text {
|
||||
text,
|
||||
text_elements: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct NodeReplReviewEvidenceSnapshot {
|
||||
responses: Vec<Arc<NodeReplResponse>>,
|
||||
omitted_responses: u64,
|
||||
pub(crate) sequence: u64,
|
||||
}
|
||||
|
||||
impl NodeReplReviewEvidenceSnapshot {
|
||||
pub(crate) fn context(&self, mode: NodeReplReviewEvidenceMode) -> NodeReplContext<'_> {
|
||||
NodeReplContext {
|
||||
responses: &self.responses,
|
||||
omitted_responses: self.omitted_responses,
|
||||
mode,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "node_repl_review_evidence_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -1,244 +0,0 @@
|
||||
use codex_protocol::models::ImageReference;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use codex_protocol_test_support::ContentItemTestExt;
|
||||
use codex_utils_output_truncation::approx_bytes_for_tokens;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::GUARDIAN_MAX_NODE_REPL_TOOL_RESULT_TOKENS;
|
||||
use super::NodeReplReviewEvidence;
|
||||
use super::NodeReplReviewEvidenceMode;
|
||||
use codex_context_fragments::ContextualUserFragment;
|
||||
// Assert the existing rendering contract independently of its implementation constant.
|
||||
const MAX_RENDERED_BYTES: usize = 32_000;
|
||||
|
||||
fn text_input(text: &str) -> UserInput {
|
||||
super::text_input(text.to_string())
|
||||
}
|
||||
|
||||
fn image_input(image_url: &str) -> UserInput {
|
||||
UserInput::Image {
|
||||
image: ImageReference::Inline {
|
||||
image_url: image_url.to_string(),
|
||||
},
|
||||
detail: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn file_image_input(file_id: &str) -> UserInput {
|
||||
UserInput::Image {
|
||||
image: ImageReference::File {
|
||||
file_id: file_id.to_string(),
|
||||
},
|
||||
detail: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn rendered_text(items: &[UserInput]) -> String {
|
||||
items
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
UserInput::Text { text, .. } => Some(text.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evidence_snapshots_keep_response_order_and_escape_closing_markers() {
|
||||
let evidence = NodeReplReviewEvidence::default();
|
||||
evidence.record("js", "cell-1", "call-1", vec![text_input("first")]);
|
||||
let closing_marker = text_input("</node_repl_review_evidence>second");
|
||||
evidence.record("browser", "cell-2", "call-2", vec![closing_marker]);
|
||||
|
||||
let first = evidence
|
||||
.snapshot_since(/*reviewed_sequence*/ 0)
|
||||
.expect("completed responses should produce evidence");
|
||||
let body = first
|
||||
.context(NodeReplReviewEvidenceMode::TextOnly)
|
||||
.body()
|
||||
.expect_text();
|
||||
assert_eq!(first.sequence, 2);
|
||||
assert!(body.find("first") < body.find("second"));
|
||||
assert!(body.contains("<\\/node_repl_review_evidence>second"));
|
||||
let inputs = first
|
||||
.context(NodeReplReviewEvidenceMode::Multimodal)
|
||||
.render_inputs();
|
||||
assert_eq!(inputs.len(), 1);
|
||||
|
||||
let delta = evidence
|
||||
.snapshot_since(/*reviewed_sequence*/ 1)
|
||||
.expect("newer responses should produce delta evidence");
|
||||
assert!(
|
||||
!delta
|
||||
.context(NodeReplReviewEvidenceMode::TextOnly)
|
||||
.body()
|
||||
.expect_text()
|
||||
.contains("first")
|
||||
);
|
||||
assert!(evidence.snapshot_since(/*reviewed_sequence*/ 2).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evidence_bounds_visible_text_and_marks_empty_completed_responses() {
|
||||
let evidence = NodeReplReviewEvidence::default();
|
||||
evidence.record("js", "cell", "empty", Vec::new());
|
||||
let empty = evidence
|
||||
.snapshot_since(/*reviewed_sequence*/ 0)
|
||||
.expect("empty successful responses should produce evidence")
|
||||
.context(NodeReplReviewEvidenceMode::TextOnly)
|
||||
.render()
|
||||
.expect_text();
|
||||
assert!(empty.contains("completed without visible text"));
|
||||
let snapshot = "page-middle".repeat(2_000);
|
||||
evidence.record("js", "cell", "snapshot", vec![text_input(&snapshot)]);
|
||||
let full = evidence
|
||||
.snapshot_since(/*reviewed_sequence*/ 1)
|
||||
.expect("large DOM snapshots should produce evidence")
|
||||
.context(NodeReplReviewEvidenceMode::TextOnly)
|
||||
.render()
|
||||
.expect_text();
|
||||
assert!(full.contains(&snapshot));
|
||||
evidence.record(
|
||||
"js",
|
||||
"cell",
|
||||
"oversized",
|
||||
vec![text_input(&format!("start{}end", "x".repeat(30_000)))],
|
||||
);
|
||||
|
||||
let oversized = evidence
|
||||
.snapshot_since(/*reviewed_sequence*/ 0)
|
||||
.expect("completed responses should produce evidence");
|
||||
assert!(
|
||||
rendered_text(&oversized.responses[2].items).len()
|
||||
<= approx_bytes_for_tokens(GUARDIAN_MAX_NODE_REPL_TOOL_RESULT_TOKENS)
|
||||
);
|
||||
let rendered = oversized
|
||||
.context(NodeReplReviewEvidenceMode::TextOnly)
|
||||
.render()
|
||||
.expect_text();
|
||||
assert!(rendered.contains("start"));
|
||||
assert!(rendered.contains("end"));
|
||||
assert!(rendered.contains("<truncated omitted_approx_tokens="));
|
||||
assert!(rendered.contains("<omitted node_repl_responses="));
|
||||
assert!(rendered.len() <= MAX_RENDERED_BYTES);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evidence_preserves_tail_text_after_oversized_image_response() {
|
||||
let evidence = NodeReplReviewEvidence::default();
|
||||
let items = vec![
|
||||
text_input(&"x".repeat(30_000)),
|
||||
image_input("data:image/png;base64,cHJpdmF0ZQ=="),
|
||||
image_input("data:image/png;base64,cHJpdmF0ZQ=="),
|
||||
text_input("FINAL IMPORTANT"),
|
||||
];
|
||||
evidence.record("browser", "cell", "long", items);
|
||||
let fragment = evidence.snapshot_since(/*reviewed_sequence*/ 0).unwrap();
|
||||
let inputs = fragment
|
||||
.context(NodeReplReviewEvidenceMode::Multimodal)
|
||||
.render_inputs();
|
||||
assert_eq!(inputs.len(), 6);
|
||||
assert!(rendered_text(&inputs).contains("FINAL IMPORTANT"));
|
||||
let pressure = "a".repeat(NodeReplReviewEvidence::MAX_RETAINED_BYTES - 1_024);
|
||||
evidence.record(
|
||||
"browser",
|
||||
"cell",
|
||||
"pressure",
|
||||
vec![image_input(&format!("data:image/png;base64,{pressure}"))],
|
||||
);
|
||||
assert!(evidence.images().is_empty());
|
||||
assert_eq!(
|
||||
fragment
|
||||
.context(NodeReplReviewEvidenceMode::Multimodal)
|
||||
.render_inputs(),
|
||||
inputs,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_backed_images_are_retained_but_omitted_from_guardian_views() {
|
||||
let evidence = NodeReplReviewEvidence::default();
|
||||
let file_id = format!("file_{}", "x".repeat(4_096));
|
||||
let items = vec![text_input("visible"), file_image_input(&file_id)];
|
||||
|
||||
evidence.record("browser", "cell", "file", items.clone());
|
||||
|
||||
let snapshot = evidence.snapshot_since(/*reviewed_sequence*/ 0).unwrap();
|
||||
assert_eq!(snapshot.responses.len(), 1);
|
||||
assert_eq!(snapshot.responses[0].items, items);
|
||||
assert!(
|
||||
snapshot.responses[0]
|
||||
.items
|
||||
.iter()
|
||||
.any(|item| matches!(item, UserInput::Image { .. }))
|
||||
);
|
||||
assert!(super::retained_bytes(&snapshot.responses[0]) >= file_id.len());
|
||||
assert_eq!(evidence.images(), Vec::new());
|
||||
|
||||
let rendered = snapshot
|
||||
.context(NodeReplReviewEvidenceMode::Multimodal)
|
||||
.render_inputs();
|
||||
assert!(rendered_text(&rendered).contains("visible"));
|
||||
assert!(!rendered.iter().any(|item| {
|
||||
matches!(
|
||||
item,
|
||||
UserInput::Image {
|
||||
image: ImageReference::File { .. },
|
||||
..
|
||||
}
|
||||
)
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evidence_evicts_complete_oldest_responses_and_rejects_oversized_items() {
|
||||
let evidence = NodeReplReviewEvidence::default();
|
||||
let max = NodeReplReviewEvidence::MAX_RETAINED_BYTES;
|
||||
let image = format!("data:image/png;base64,{}", "a".repeat(max - 1_024));
|
||||
evidence.record("js", "cell", "evicted", vec![image_input(&image)]);
|
||||
evidence.record("js", "cell", "first", vec![text_input("earlier")]);
|
||||
let items = vec![text_input("recent"), image_input(&image)];
|
||||
evidence.record("browser", "cell", "second", items);
|
||||
let image = image_input(&format!("data:image/png;base64,{}", "c".repeat(max)));
|
||||
let items = vec![text_input(&"oversized text".repeat(128)), image];
|
||||
evidence.record("js", "cell", "oversized", items);
|
||||
let retained = evidence.snapshot_since(/*reviewed_sequence*/ 0).unwrap();
|
||||
let body = retained
|
||||
.context(NodeReplReviewEvidenceMode::TextOnly)
|
||||
.body()
|
||||
.expect_text();
|
||||
for expected in ["earlier", "recent", "oversized text"] {
|
||||
assert!(body.contains(expected));
|
||||
}
|
||||
assert!(evidence.0.lock().unwrap().retained_bytes <= max);
|
||||
assert!(retained.responses.iter().all(|response| {
|
||||
response
|
||||
.items
|
||||
.iter()
|
||||
.all(|item| !matches!(item, UserInput::Image { .. }))
|
||||
}));
|
||||
assert!(body.contains("node_repl_responses=\"1\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_evidence_bounds_headers_and_empty_response_placeholders() {
|
||||
for (response_count, provenance) in [(100, "p".repeat(108)), (512, String::new())] {
|
||||
let evidence = NodeReplReviewEvidence::default();
|
||||
for _ in 0..response_count {
|
||||
let image = image_input("data:image/png;base64,aQ==");
|
||||
let items = Vec::from_iter((!provenance.is_empty()).then_some(image));
|
||||
evidence.record(&provenance, &provenance, &provenance, items);
|
||||
}
|
||||
|
||||
let fragment = evidence.snapshot_since(/*reviewed_sequence*/ 0).unwrap();
|
||||
let text = rendered_text(
|
||||
&fragment
|
||||
.context(NodeReplReviewEvidenceMode::Multimodal)
|
||||
.render_inputs(),
|
||||
);
|
||||
assert!(text.len() <= MAX_RENDERED_BYTES);
|
||||
assert!(text.contains("node_repl_responses="));
|
||||
let has_placeholder = text.contains("completed without visible text");
|
||||
assert_eq!(has_placeholder, provenance.is_empty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user