Extract guardian transcript selection into guardian-context (#45417)

## What changed

Move full/delta transcript selection into the shared `TranscriptMode::select` API and use it when building guardian prompts. Export `TranscriptCursor`, `TranscriptMode`, and `TranscriptSelection` from `codex-guardian-context`.

Preserve full-transcript fallback when the history version changes or the saved cursor exceeds the collected entry count. Select entries before profile retention, preserving their numbering and returning a proposed cursor that counts all collected entries. Hosts remain responsible for committing and invalidating cursors.

## Testing

Add a regression test verifying that sliding-window retention preserves the collected-entry cursor and that an appended entry is selected and numbered correctly in the next delta.

GitOrigin-RevId: a2192c08e23c18302b0105ba47aeb2780eb4a015
This commit is contained in:
felixxia-oai authored and copyberry committed 2026-09-14 12:52:16 +00:00
1 parent 99cda7a9a5
commit b3e0c49dfb
4 files changed
+142 -48

No files matched your search

+13 -46
View File
@@ -13,6 +13,9 @@ use codex_guardian_context::PlannedActionKind;
use codex_guardian_context::SectionError;
use codex_guardian_context::SectionHistory;
use codex_guardian_context::SectionInput;
pub(crate) use codex_guardian_context::TranscriptCursor as GuardianTranscriptCursor;
pub(crate) use codex_guardian_context::TranscriptMode as GuardianPromptMode;
use codex_guardian_context::TranscriptSelection;
use codex_guardian_context::default_registry;
use codex_protocol::models::ResponseItem;
@@ -44,19 +47,6 @@ pub(crate) struct GuardianPromptItems {
pub(crate) node_repl_evidence_sequence: u64,
}
/// Points to the end of the transcript that the guardian has already reviewed.
/// The saved count is only reusable when `parent_history_version` still matches.
#[derive(Clone, Copy, Debug)]
pub(crate) struct GuardianTranscriptCursor {
pub(crate) parent_history_version: u64,
pub(crate) transcript_entry_count: usize,
}
pub(crate) enum GuardianPromptMode {
Full,
Delta { cursor: GuardianTranscriptCursor },
}
/// Builds the guardian user content items from:
/// - a compact transcript for authorization and local context
/// - the exact action JSON being proposed for approval
@@ -180,41 +170,23 @@ pub(crate) async fn build_guardian_prompt_items_with_parent_turn(
permissions.as_ref(),
node_repl_context.as_ref(),
)?;
let transcript_entries = sections.transcript_entries();
let transcript_cursor = GuardianTranscriptCursor {
parent_history_version: history.review_history_version(),
transcript_entry_count: transcript_entries.len(),
};
let prompt_shape = match mode {
GuardianPromptMode::Full => GuardianPromptShape::Full,
GuardianPromptMode::Delta { cursor } => {
if cursor.parent_history_version == transcript_cursor.parent_history_version
&& cursor.transcript_entry_count <= transcript_cursor.transcript_entry_count
{
GuardianPromptShape::Delta {
already_seen_entry_count: cursor.transcript_entry_count,
}
} else {
GuardianPromptShape::Full
}
}
};
let (selection, transcript_cursor) = mode.select(
sections.transcript_entries(),
history.review_history_version(),
);
let session_id = session.thread_id.to_string();
let (transcript_entries, offset, placeholder, presentation) = match prompt_shape {
GuardianPromptShape::Full => (
transcript_entries,
let (transcript_entries, offset, placeholder, presentation) = match selection {
TranscriptSelection::Full(entries) => (
entries,
0,
"<no retained transcript entries>",
ContextPresentation::SyncFull {
session_id: &session_id,
},
),
GuardianPromptShape::Delta {
already_seen_entry_count,
} => (
&transcript_entries[already_seen_entry_count..],
already_seen_entry_count,
TranscriptSelection::Delta { entries, offset } => (
entries,
offset,
"<no retained transcript delta entries>",
ContextPresentation::SyncDelta {
session_id: &session_id,
@@ -257,11 +229,6 @@ fn parent_turn_permissions(context: &GuardianReviewContext) -> PermissionContext
}
}
enum GuardianPromptShape {
Full,
Delta { already_seen_entry_count: usize },
}
/// Exercises the sync profile through the host's existing transcript tests.
#[cfg(test)]
pub(crate) fn render_guardian_transcript_entries(