Persist Guardian review failures for reports across restarts (#51651)

## Why

Failed Guardian review evidence was process-local, so restarting Codex lost the context needed for later diagnostic reports.

## What changed

- Persist failed-review records in SQLite before reviewer cleanup, retaining at most 64 records globally, eight per thread, and 8 MiB of evidence. Delete persisted evidence when its reviewed thread is deleted.
- Recover and deduplicate persisted and in-memory records for the reported task tree when uploading `auto-review-failures.jsonl`.
- Bound persistence attempts to 250 ms and retain an in-memory fallback when storage is unavailable. Keep capture gated by user opt-in and non-ephemeral sessions, and export evidence only when logs are included.

## Testing

Add coverage for persistence across reopen and process restart, retention limits, cascading deletion, busy-storage fallback, and report uploads with and without logs, including SQLite log failures.

GitOrigin-RevId: ea2da1818d4a5da3da29f211042ca45e834342bc
This commit is contained in:
jif
2026-10-07 11:18:05 +00:00
committed by copyberry
parent 37eaae6eeb
commit 30bdfec59d
17 changed files with 442 additions and 74 deletions
@@ -3,7 +3,7 @@
use crate::GuardianReviewSessionOutcome;
use crate::parse_guardian_assessment;
use codex_feedback::record_guardian_review_failure;
use codex_feedback::GuardianReviewRecord;
use codex_protocol::ThreadId;
use codex_protocol::models::ResponseItem;
use codex_protocol::protocol::GuardianAssessmentOutcome;
@@ -82,7 +82,7 @@ impl<'a> FailedReviewFeedback<'a> {
Some(Self { status, decision })
}
pub fn store(self, context: ReviewFeedbackContext<'_>) {
pub fn into_record(self, context: ReviewFeedbackContext<'_>) -> Option<GuardianReviewRecord> {
ReviewFeedbackRecord {
reviewed_thread_id: context.reviewed_thread_id,
reviewed_turn_id: context.reviewed_turn_id,
@@ -97,12 +97,12 @@ impl<'a> FailedReviewFeedback<'a> {
decision: self.decision,
context_omitted: false,
}
.store();
.into_record()
}
}
impl ReviewFeedbackRecord<'_> {
fn store(mut self) {
fn into_record(mut self) -> Option<GuardianReviewRecord> {
let mut buffer = BoundedBuffer(Vec::new());
if serde_json::to_writer(&mut buffer, &self).is_err() {
// Preserve the action and decision even when optional reviewer history is too large.
@@ -112,10 +112,10 @@ impl ReviewFeedbackRecord<'_> {
buffer = BoundedBuffer(Vec::new());
if serde_json::to_writer(&mut buffer, &self).is_err() {
tracing::warn!("Guardian feedback action and decision exceed the record limit");
return;
return None;
}
}
record_guardian_review_failure(self.reviewed_thread_id, buffer.0);
Some(GuardianReviewRecord::new(self.reviewed_thread_id, buffer.0))
}
}
@@ -3,12 +3,24 @@ use pretty_assertions::assert_eq;
use serde_json::json;
#[test]
fn oversized_context_keeps_the_action_and_decision() -> anyhow::Result<()> {
fn capture_honors_privacy_and_keeps_oversized_context_action_and_decision() -> anyhow::Result<()> {
let thread_id = ThreadId::new();
let reviewer_thread_id = ThreadId::new();
let decision = r#"{"outcome":"deny","rationale":"Missing approval."}"#;
let action = r#"{"command":"git push"}"#;
let outcome = GuardianReviewSessionOutcome::Completed(Ok(Some(decision.to_owned())));
for settings in [
ReviewFeedbackSettings {
enabled: false,
ephemeral: false,
},
ReviewFeedbackSettings {
enabled: true,
ephemeral: true,
},
] {
assert!(FailedReviewFeedback::for_outcome(&outcome, settings).is_none());
}
let feedback = FailedReviewFeedback::for_outcome(
&outcome,
ReviewFeedbackSettings {
@@ -17,7 +29,7 @@ fn oversized_context_keeps_the_action_and_decision() -> anyhow::Result<()> {
},
)
.expect("denials retain feedback");
feedback.store(ReviewFeedbackContext {
let record = feedback.into_record(ReviewFeedbackContext {
reviewed_thread_id: thread_id,
reviewed_turn_id: "parent-turn",
target_item_id: Some("push-call"),
@@ -28,10 +40,7 @@ fn oversized_context_keeps_the_action_and_decision() -> anyhow::Result<()> {
instructions: Some(&"x".repeat(MAX_RECORD_BYTES)),
history: Vec::new(),
});
let contents = codex_feedback::guardian_review_failures(&[thread_id])
.attachment
.expect("failed-review record")
.buffer;
let contents = record.expect("failed-review record").record;
assert_eq!(
serde_json::from_slice::<serde_json::Value>(&contents)?,
json!({