Extract Guardian conversation bookkeeping into the reviewer crate (#45418)

## What changed

Add `ConversationState` and `ConversationCheckpoint` to `codex-guardian-reviewer` and use them in core review sessions to track transcript cursors, completed review counts, and committed snapshots. Keep history and admitted evidence host-owned.

Preserve the separation between live review progress and committed checkpoints so forks inherit the history, cursor, and review count from the last committed snapshot.

## Testing

Add a unit test verifying that forks retain committed history and progress after an uncommitted review, then advance when the next snapshot is committed.

GitOrigin-RevId: 9f92410b11beec6b8f413c4c922fabba65852399
This commit is contained in:
felixxia-oai authored and copyberry committed 2026-09-14 12:52:37 +00:00
1 parent b3e0c49dfb
commit 9d036249da
8 files changed
+170 -54

No files matched your search

@@ -18,6 +18,7 @@ codex-analytics = { workspace = true }
codex-extension-api = { workspace = true }
codex-features = { workspace = true }
codex-feedback = { workspace = true }
codex-guardian-context = { workspace = true }
codex-mcp = { workspace = true }
codex-otel = { workspace = true }
codex-protocol = { workspace = true }
@@ -0,0 +1,86 @@
//! Tracks reviewed transcript progress separately from committed conversation history.
//! Hosts serialize reviews and checkpoint commits, and supply opaque completed history.
use codex_guardian_context::TranscriptCursor;
/// A completed history and the transcript progress that produced it.
#[derive(Clone)]
pub struct ConversationCheckpoint<H> {
history: H,
cursor: Option<TranscriptCursor>,
completed_review_count: usize,
}
impl<H> ConversationCheckpoint<H> {
pub fn history(&self) -> &H {
&self.history
}
}
/// Shared bookkeeping for a host-owned Guardian conversation.
pub struct ConversationState<H> {
cursor: Option<TranscriptCursor>,
completed_review_count: usize,
checkpoint: Option<ConversationCheckpoint<H>>,
}
impl<H> Default for ConversationState<H> {
fn default() -> Self {
Self {
cursor: None,
completed_review_count: 0,
checkpoint: None,
}
}
}
impl<H> ConversationState<H> {
/// Starts an independent conversation from exactly the committed history and cursor.
pub fn fork(checkpoint: ConversationCheckpoint<H>) -> (Self, H) {
(
Self {
cursor: checkpoint.cursor,
completed_review_count: checkpoint.completed_review_count,
..Self::default()
},
checkpoint.history,
)
}
pub fn cursor(&self) -> Option<TranscriptCursor> {
self.cursor
}
pub fn completed_review_count(&self) -> usize {
self.completed_review_count
}
/// Rebuilds the active transcript after compaction. Forks can still use the last
/// committed history; it owns its own cursor and is independent of this reset.
pub fn reset_transcript(&mut self) {
self.cursor = None;
}
/// Advances live progress once the host's review completes.
pub fn complete_review(&mut self, cursor: TranscriptCursor) {
self.cursor = Some(cursor);
self.completed_review_count = self.completed_review_count.saturating_add(1);
}
/// Pairs host-supplied history with the current progress for subsequent forks.
pub fn commit_snapshot(&mut self, history: H) {
self.checkpoint = Some(ConversationCheckpoint {
history,
cursor: self.cursor,
completed_review_count: self.completed_review_count,
});
}
pub fn snapshot(&self) -> Option<&ConversationCheckpoint<H>> {
self.checkpoint.as_ref()
}
}
#[cfg(test)]
#[path = "conversation_tests.rs"]
mod tests;
@@ -0,0 +1,33 @@
//! Checks that forks use committed history and progress while the next review runs.
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn fork_keeps_committed_history_and_cursor_while_next_review_is_uncommitted() {
let mut state = ConversationState::default();
let first = TranscriptCursor {
parent_history_version: 1,
transcript_entry_count: 2,
};
state.complete_review(first);
state.commit_snapshot(vec!["first review"]);
let second = TranscriptCursor {
transcript_entry_count: 4,
..first
};
state.complete_review(second);
let (fork, history) = ConversationState::fork(state.snapshot().unwrap().clone());
assert_eq!(
(history, fork.cursor(), fork.completed_review_count()),
(vec!["first review"], Some(first), 1),
);
state.commit_snapshot(vec!["first review", "second review"]);
let (fork, history) = ConversationState::fork(state.snapshot().unwrap().clone());
assert_eq!(
(history, fork.cursor(), fork.completed_review_count()),
(vec!["first review", "second review"], Some(second), 2),
);
}
+5 -1
View File
@@ -1,9 +1,13 @@
//! Owns synchronous Guardian review policy independently of the host session runtime.
//! Owns Guardian conversation bookkeeping and synchronous review policy independently
//! of the host session runtime.
//! The host supplies review attempts and enforces the resulting decision on the bound action.
mod assessment;
mod circuit_breaker;
mod completion;
mod conversation;
pub use conversation::ConversationCheckpoint;
pub use conversation::ConversationState;
mod deadline;
mod execution;
mod feedback;