Improve Guardian report diagnostics (#42033)

## Why

Reports from a large thread tree can exceed the rollout attachment limit, making
the threads involved in a failed Guardian review harder to diagnose.

## What changed

- Prioritize descendants with retained failed reviews when selecting bounded
  report rollouts, while always preserving the reported thread.
- Include each selected thread's available Guardian trunk rollout alongside its
  conversation rollout.
- Add a thread index attachment with selected filenames, bounded omission details,
  retained failure threads, and the process-wide discarded-record count.
- Link failed-review records to their reviewed turn and target item when available.

## Testing

- Cover failed-review prioritization, bounded omission metadata, discarded-record
  accounting, and turn/item linkage in Guardian report records.

GitOrigin-RevId: ae7916c27a3c279e899cd5a6ead8a4b7e0cc26a1
This commit is contained in:
jif
2026-09-01 11:14:20 +00:00
committed by copyberry
parent 8209978616
commit 2e5ee418ad
13 changed files with 293 additions and 66 deletions
@@ -1,3 +1,4 @@
use super::feedback_thread_index::FeedbackThreadIndex;
use super::*;
use codex_connectors::ConnectorDirectoryCacheContext;
use codex_connectors::ConnectorDirectoryCacheKey;
@@ -6,13 +7,11 @@ use codex_feedback::CODEX_APP_DIRECTORY_CACHE_ATTACHMENT_FILENAME;
use codex_feedback::CODEX_APPS_TOOLS_CACHE_ATTACHMENT_FILENAME;
#[cfg(target_os = "windows")]
use codex_feedback::WINDOWS_SANDBOX_LOG_ATTACHMENT_FILENAME;
use codex_feedback::guardian_review_failures_attachment;
use codex_feedback::guardian_review_failures;
use codex_rollout::RolloutRecorder;
use sha2::Digest;
use sha2::Sha256;
const MAX_FEEDBACK_TREE_THREADS: usize = 8;
#[derive(Clone)]
pub(crate) struct FeedbackRequestProcessor {
auth_manager: Arc<AuthManager>,
@@ -110,7 +109,8 @@ impl FeedbackRequestProcessor {
let snapshot = self.feedback.snapshot(conversation_id);
let thread_id = snapshot.thread_id.clone();
let mut extra_attachments = Vec::new();
let (feedback_thread_ids, sqlite_feedback_logs, state_db_ctx) = if include_logs {
let mut feedback_index = None;
let (sqlite_feedback_logs, state_db_ctx) = if include_logs {
if let Some(log_db) = self.log_db.as_ref() {
log_db.flush().await;
}
@@ -131,28 +131,19 @@ impl FeedbackRequestProcessor {
},
None => Vec::new(),
};
extra_attachments.extend(guardian_review_failures_attachment(&feedback_thread_ids));
let failures = guardian_review_failures(&feedback_thread_ids);
let mut feedback_thread_ids = feedback_thread_ids;
let original_len = feedback_thread_ids.len();
if let Some(conversation_id) = conversation_id {
let mut descendant_thread_ids = feedback_thread_ids
.into_iter()
.filter(|thread_id| *thread_id != conversation_id)
.collect::<Vec<_>>();
// Thread ids are UUIDv7, so lexicographic order tracks creation time.
descendant_thread_ids.sort_unstable_by_key(ToString::to_string);
if original_len > MAX_FEEDBACK_TREE_THREADS {
let keep_descendants = MAX_FEEDBACK_TREE_THREADS.saturating_sub(1);
let split_index = descendant_thread_ids.len().saturating_sub(keep_descendants);
descendant_thread_ids = descendant_thread_ids.split_off(split_index);
warn!(
"feedback log upload for thread_id={conversation_id:?} truncated from {original_len} threads to root plus {keep_descendants} most recent descendants"
);
}
feedback_thread_ids = Vec::with_capacity(descendant_thread_ids.len() + 1);
feedback_thread_ids.push(conversation_id);
feedback_thread_ids.extend(descendant_thread_ids);
let index =
FeedbackThreadIndex::new(conversation_id, feedback_thread_ids, &failures);
feedback_thread_ids = index
.threads
.iter()
.map(|thread| thread.thread_id)
.collect();
feedback_index = Some(index);
}
extra_attachments.extend(failures.attachment);
let sqlite_feedback_logs = if let Some(state_db_ctx) = state_db_ctx.as_ref()
&& !feedback_thread_ids.is_empty()
{
@@ -181,43 +172,51 @@ impl FeedbackRequestProcessor {
} else {
None
};
(feedback_thread_ids, sqlite_feedback_logs, state_db_ctx)
(sqlite_feedback_logs, state_db_ctx)
} else {
(Vec::new(), None, None)
(None, None)
};
let mut attachment_paths = Vec::new();
let mut seen_attachment_paths = HashSet::new();
// File priority after logs and generated diagnostics: reported thread, subagent
// descendants (oldest to newest within the retained recent set), guardian rollout,
// sandbox log, tool caches, then caller files. Keep this order for size budgeting.
// Keep actor/reviewer pairs together: reported thread, recent failed-review
// children, then newest remaining children. Captured failures precede these files.
if include_logs {
for feedback_thread_id in &feedback_thread_ids {
let Some(rollout_path) = self
.resolve_rollout_path(*feedback_thread_id, state_db_ctx.as_ref())
for thread in feedback_index
.iter_mut()
.flat_map(|index| &mut index.threads)
{
if let Some(rollout_path) = self
.resolve_rollout_path(thread.thread_id, state_db_ctx.as_ref())
.await
else {
continue;
};
if seen_attachment_paths.insert(rollout_path.clone()) {
&& seen_attachment_paths.insert(rollout_path.clone())
{
thread.rollout_filename = rollout_path
.file_name()
.map(|name| name.to_string_lossy().into_owned());
attachment_paths.push(FeedbackAttachmentPath {
path: rollout_path,
attachment_filename_override: None,
});
}
if let Ok(conversation) = self.thread_manager.get_thread(thread.thread_id).await
&& let Some(guardian_rollout_path) =
conversation.guardian_trunk_rollout_path().await
&& seen_attachment_paths.insert(guardian_rollout_path.clone())
{
let filename = auto_review_rollout_filename(thread.thread_id);
thread.guardian_rollout_filename = Some(filename.clone());
attachment_paths.push(FeedbackAttachmentPath {
path: guardian_rollout_path,
attachment_filename_override: Some(filename),
});
}
}
if let Some(conversation_id) = conversation_id
&& let Ok(conversation) = self.thread_manager.get_thread(conversation_id).await
&& let Some(guardian_rollout_path) =
conversation.guardian_trunk_rollout_path().await
&& seen_attachment_paths.insert(guardian_rollout_path.clone())
{
attachment_paths.push(FeedbackAttachmentPath {
path: guardian_rollout_path,
attachment_filename_override: Some(auto_review_rollout_filename(
conversation_id,
)),
});
if let Some(index) = feedback_index {
match index.attachment() {
Ok(attachment) => extra_attachments.insert(0, attachment),
Err(err) => warn!("failed to serialize feedback thread index: {err}"),
}
}
if let Some(sandbox_log_attachment) =
windows_sandbox_log_attachment(&self.config.codex_home)
@@ -0,0 +1,90 @@
//! Selects a bounded feedback subtree, preserving the reported thread and prioritizing
//! children with retained failed reviews. The index describes selection, not delivery.
use codex_feedback::FeedbackAttachment;
use codex_feedback::GuardianReviewFailures;
use codex_protocol::ThreadId;
use serde::Serialize;
use std::cmp::Reverse;
use std::collections::HashSet;
const MAX_FEEDBACK_TREE_THREADS: usize = 8;
const MAX_LISTED_OMISSIONS: usize = 64;
#[derive(Serialize)]
pub(super) struct FeedbackThreadIndex {
pub threads: Vec<FeedbackThread>,
retained_failure_thread_ids: Vec<ThreadId>,
omitted_thread_ids: Vec<ThreadId>,
unlisted_omitted_thread_count: usize,
process_discarded_review_records: usize,
notes: &'static str,
}
#[derive(Serialize)]
pub(super) struct FeedbackThread {
pub thread_id: ThreadId,
pub rollout_filename: Option<String>,
pub guardian_rollout_filename: Option<String>,
}
impl FeedbackThreadIndex {
pub fn new(
reported_thread_id: ThreadId,
mut subtree: Vec<ThreadId>,
failures: &GuardianReviewFailures,
) -> Self {
// UUIDv7 ordering provides a deterministic newest-child fallback.
subtree.sort_unstable_by_key(|id| Reverse(id.to_string()));
subtree.dedup();
subtree.retain(|id| *id != reported_thread_id);
let descendants = subtree.iter().copied().collect::<HashSet<_>>();
let mut selected = HashSet::from([reported_thread_id]);
let mut thread_ids = vec![reported_thread_id];
for id in failures.thread_ids.iter().chain(&subtree) {
if thread_ids.len() == MAX_FEEDBACK_TREE_THREADS {
break;
}
if descendants.contains(id) && selected.insert(*id) {
thread_ids.push(*id);
}
}
let omitted_count = subtree.len().saturating_sub(thread_ids.len() - 1);
let omitted_thread_ids = subtree
.into_iter()
.filter(|id| !selected.contains(id))
.take(MAX_LISTED_OMISSIONS)
.collect::<Vec<_>>();
Self {
threads: thread_ids
.into_iter()
.map(|thread_id| FeedbackThread {
thread_id,
rollout_filename: None,
guardian_rollout_filename: None,
})
.collect(),
retained_failure_thread_ids: failures.thread_ids.clone(),
unlisted_omitted_thread_count: omitted_count - omitted_thread_ids.len(),
omitted_thread_ids,
process_discarded_review_records: failures.process_discarded_records,
notes: "Selection only, not an upload receipt. Null filenames mean no path was available. \
Omitted threads exceeded the rollout thread limit; their retained reviews are \
still in auto-review-failures.jsonl. Review evidence is process-local and bounded; \
missing evidence does not imply no denial. The discarded-record count is \
process-wide, not specific to this tree, and resets on restart.",
}
}
pub fn attachment(&self) -> serde_json::Result<FeedbackAttachment> {
Ok(FeedbackAttachment {
filename: "feedback-thread-index.json".to_string(),
buffer: serde_json::to_vec(self)?,
content_type: Some("application/json".to_string()),
})
}
}
#[cfg(test)]
#[path = "feedback_thread_index_tests.rs"]
mod tests;
@@ -0,0 +1,80 @@
use super::*;
use pretty_assertions::assert_eq;
use serde_json::json;
fn thread_ids(count: usize) -> Vec<ThreadId> {
(0..count)
.map(|index| {
ThreadId::from_string(&format!("00000000-0000-7000-8000-{index:012x}"))
.expect("thread id")
})
.collect()
}
#[test]
fn old_failed_child_precedes_new_children_and_index_explains_selection() -> anyhow::Result<()> {
let ids = thread_ids(/*count*/ 12);
let failures = GuardianReviewFailures {
attachment: None,
thread_ids: vec![ids[1], ids[0]],
process_discarded_records: 3,
};
let mut index = FeedbackThreadIndex::new(ids[0], ids.clone(), &failures);
index.threads[0].rollout_filename = Some("parent.jsonl".to_string());
index.threads[1].rollout_filename = Some("child.jsonl".to_string());
index.threads[1].guardian_rollout_filename = Some("child-reviewer.jsonl".to_string());
let attachment = index.attachment()?;
let selected = [0, 1, 11, 10, 9, 8, 7, 6];
assert_eq!(
serde_json::from_slice::<serde_json::Value>(&attachment.buffer)?,
json!({
"threads": selected.into_iter().map(|i| json!({
"thread_id": ids[i],
"rollout_filename": match i {
0 => Some("parent.jsonl"),
1 => Some("child.jsonl"),
_ => None,
},
"guardian_rollout_filename": (i == 1).then_some("child-reviewer.jsonl"),
})).collect::<Vec<_>>(),
"retained_failure_thread_ids": [ids[1], ids[0]],
"omitted_thread_ids": [ids[5], ids[4], ids[3], ids[2]],
"unlisted_omitted_thread_count": 0,
"process_discarded_review_records": 3,
"notes": index.notes,
})
);
Ok(())
}
#[test]
fn failure_priority_keeps_the_reported_thread_and_bounds_omission_details() {
let ids = thread_ids(/*count*/ 100);
let failures = GuardianReviewFailures {
attachment: None,
thread_ids: ids[1..10].to_vec(),
process_discarded_records: 0,
};
// Exercise unsorted and duplicate subtree entries without inflating omission counts.
let mut subtree = ids.iter().rev().copied().collect::<Vec<_>>();
subtree.extend_from_slice(&ids[..3]);
let index = FeedbackThreadIndex::new(ids[0], subtree, &failures);
assert_eq!(
(
index
.threads
.iter()
.map(|thread| thread.thread_id)
.collect::<Vec<_>>(),
index.omitted_thread_ids,
index.unlisted_omitted_thread_count,
index.retained_failure_thread_ids,
),
(
ids[..8].to_vec(),
ids[36..].iter().rev().copied().collect::<Vec<_>>(),
28,
failures.thread_ids,
)
);
}