mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
state: use compact guardian thread metadata
This commit is contained in:
@@ -3,6 +3,8 @@ use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::RolloutItem;
|
||||
use codex_protocol::protocol::SessionMetaLine;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use codex_protocol::protocol::TurnContextItem;
|
||||
use codex_protocol::protocol::USER_MESSAGE_BEGIN;
|
||||
use codex_protocol::protocol::UserMessageEvent;
|
||||
@@ -10,6 +12,26 @@ use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
|
||||
const IMAGE_ONLY_USER_MESSAGE_PLACEHOLDER: &str = "[Image]";
|
||||
pub const GUARDIAN_THREAD_TITLE: &str = "Guardian review";
|
||||
pub const GUARDIAN_THREAD_PREVIEW: &str = "Approval review";
|
||||
|
||||
pub fn is_guardian_review_source(source: &SessionSource) -> bool {
|
||||
matches!(
|
||||
source,
|
||||
SessionSource::SubAgent(SubAgentSource::Other(name)) if name == "guardian"
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn apply_guardian_thread_metadata_defaults(metadata: &mut ThreadMetadata) {
|
||||
// Empty titles and titles copied from first_user_message are derived; preserve
|
||||
// any other title as potentially explicit.
|
||||
let title = metadata.title.trim();
|
||||
if title.is_empty() || metadata.first_user_message.as_deref().map(str::trim) == Some(title) {
|
||||
metadata.title = GUARDIAN_THREAD_TITLE.to_string();
|
||||
}
|
||||
metadata.preview = Some(GUARDIAN_THREAD_PREVIEW.to_string());
|
||||
metadata.first_user_message = None;
|
||||
}
|
||||
|
||||
/// Apply a rollout item to the metadata structure.
|
||||
pub fn apply_rollout_item(
|
||||
@@ -70,6 +92,9 @@ fn apply_session_meta_from_item(metadata: &mut ThreadMetadata, meta_line: &Sessi
|
||||
metadata.git_branch = git.branch.clone();
|
||||
metadata.git_origin_url = git.repository_url.clone();
|
||||
}
|
||||
if is_guardian_review_source(&meta_line.meta.source) {
|
||||
apply_guardian_thread_metadata_defaults(metadata);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_turn_context(metadata: &mut ThreadMetadata, turn_ctx: &TurnContextItem) {
|
||||
@@ -91,6 +116,9 @@ fn apply_event_msg(metadata: &mut ThreadMetadata, event: &EventMsg) {
|
||||
}
|
||||
}
|
||||
EventMsg::UserMessage(user) => {
|
||||
if metadata_is_guardian_review(metadata) {
|
||||
return;
|
||||
}
|
||||
let preview = user_message_preview(user);
|
||||
if metadata.first_user_message.is_none() {
|
||||
metadata.first_user_message = preview.clone();
|
||||
@@ -104,6 +132,9 @@ fn apply_event_msg(metadata: &mut ThreadMetadata, event: &EventMsg) {
|
||||
}
|
||||
}
|
||||
EventMsg::ThreadGoalUpdated(event) => {
|
||||
if metadata_is_guardian_review(metadata) {
|
||||
return;
|
||||
}
|
||||
let objective = event.goal.objective.trim();
|
||||
if !objective.is_empty() {
|
||||
set_preview_if_empty(metadata, Some(objective.to_string()));
|
||||
@@ -113,6 +144,12 @@ fn apply_event_msg(metadata: &mut ThreadMetadata, event: &EventMsg) {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn metadata_is_guardian_review(metadata: &ThreadMetadata) -> bool {
|
||||
serde_json::from_str::<SessionSource>(metadata.source.as_str())
|
||||
.as_ref()
|
||||
.is_ok_and(is_guardian_review_source)
|
||||
}
|
||||
|
||||
fn apply_response_item(_metadata: &mut ThreadMetadata, _item: &ResponseItem) {}
|
||||
|
||||
fn set_preview_if_empty(metadata: &mut ThreadMetadata, preview: Option<String>) {
|
||||
@@ -170,6 +207,7 @@ mod tests {
|
||||
use codex_protocol::protocol::SessionMeta;
|
||||
use codex_protocol::protocol::SessionMetaLine;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use codex_protocol::protocol::ThreadGoal;
|
||||
use codex_protocol::protocol::ThreadGoalStatus;
|
||||
use codex_protocol::protocol::ThreadGoalUpdatedEvent;
|
||||
@@ -223,6 +261,73 @@ mod tests {
|
||||
assert_eq!(metadata.title, "actual user request");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_user_messages_use_defaults_without_projection() {
|
||||
let mut metadata = metadata_for_test();
|
||||
let thread_id = metadata.id;
|
||||
apply_rollout_item(
|
||||
&mut metadata,
|
||||
&RolloutItem::SessionMeta(SessionMetaLine {
|
||||
meta: SessionMeta {
|
||||
session_id: thread_id.into(),
|
||||
id: thread_id,
|
||||
source: SessionSource::SubAgent(SubAgentSource::Other("guardian".to_string())),
|
||||
..Default::default()
|
||||
},
|
||||
git: None,
|
||||
}),
|
||||
"test-provider",
|
||||
);
|
||||
apply_rollout_item(
|
||||
&mut metadata,
|
||||
&RolloutItem::EventMsg(EventMsg::UserMessage(UserMessageEvent {
|
||||
client_id: None,
|
||||
message: "large synthetic guardian prompt".to_string(),
|
||||
images: Some(vec![]),
|
||||
local_images: vec![],
|
||||
text_elements: vec![],
|
||||
..Default::default()
|
||||
})),
|
||||
"test-provider",
|
||||
);
|
||||
|
||||
assert_eq!(metadata.title, super::GUARDIAN_THREAD_TITLE);
|
||||
assert_eq!(
|
||||
metadata.preview.as_deref(),
|
||||
Some(super::GUARDIAN_THREAD_PREVIEW)
|
||||
);
|
||||
assert_eq!(metadata.first_user_message, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_session_meta_preserves_existing_explicit_title() {
|
||||
let mut metadata = metadata_for_test();
|
||||
let thread_id = metadata.id;
|
||||
metadata.title = "Named Guardian review".to_string();
|
||||
metadata.first_user_message = Some("large synthetic guardian prompt".to_string());
|
||||
|
||||
apply_rollout_item(
|
||||
&mut metadata,
|
||||
&RolloutItem::SessionMeta(SessionMetaLine {
|
||||
meta: SessionMeta {
|
||||
session_id: thread_id.into(),
|
||||
id: thread_id,
|
||||
source: SessionSource::SubAgent(SubAgentSource::Other("guardian".to_string())),
|
||||
..Default::default()
|
||||
},
|
||||
git: None,
|
||||
}),
|
||||
"test-provider",
|
||||
);
|
||||
|
||||
assert_eq!(metadata.title, "Named Guardian review");
|
||||
assert_eq!(
|
||||
metadata.preview.as_deref(),
|
||||
Some(super::GUARDIAN_THREAD_PREVIEW)
|
||||
);
|
||||
assert_eq!(metadata.first_user_message, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn event_msg_image_only_user_message_sets_image_placeholder_preview() {
|
||||
let mut metadata = metadata_for_test();
|
||||
|
||||
@@ -27,10 +27,13 @@ pub use runtime::StateRuntime;
|
||||
|
||||
pub use audit::ThreadStateAuditRow;
|
||||
pub use audit::read_thread_state_audit_rows;
|
||||
pub use extract::GUARDIAN_THREAD_PREVIEW;
|
||||
pub use extract::GUARDIAN_THREAD_TITLE;
|
||||
/// Low-level storage engine: useful for focused tests.
|
||||
///
|
||||
/// Most consumers should prefer [`StateRuntime`].
|
||||
pub use extract::apply_rollout_item;
|
||||
pub use extract::is_guardian_review_source;
|
||||
pub use extract::rollout_item_affects_thread_metadata;
|
||||
pub use model::AgentJob;
|
||||
pub use model::AgentJobCreateParams;
|
||||
|
||||
@@ -203,7 +203,7 @@ impl ThreadMetadataBuilder {
|
||||
.recency_at
|
||||
.map(canonicalize_datetime)
|
||||
.unwrap_or(updated_at);
|
||||
ThreadMetadata {
|
||||
let mut metadata = ThreadMetadata {
|
||||
id: self.id,
|
||||
rollout_path: self.rollout_path.clone(),
|
||||
created_at,
|
||||
@@ -235,7 +235,11 @@ impl ThreadMetadataBuilder {
|
||||
git_sha: self.git_sha.clone(),
|
||||
git_branch: self.git_branch.clone(),
|
||||
git_origin_url: self.git_origin_url.clone(),
|
||||
};
|
||||
if crate::extract::is_guardian_review_source(&self.source) {
|
||||
crate::extract::apply_guardian_thread_metadata_defaults(&mut metadata);
|
||||
}
|
||||
metadata
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,7 +267,13 @@ impl ThreadMetadata {
|
||||
}
|
||||
|
||||
let title = self.title.trim();
|
||||
if title.is_empty() || self.first_user_message.as_deref().map(str::trim) == Some(title) {
|
||||
// Treat the compact default as derived only for Guardian threads; a
|
||||
// non-Guardian thread may explicitly use the same title.
|
||||
if title.is_empty()
|
||||
|| self.first_user_message.as_deref().map(str::trim) == Some(title)
|
||||
|| (title == crate::GUARDIAN_THREAD_TITLE
|
||||
&& crate::extract::metadata_is_guardian_review(self))
|
||||
{
|
||||
self.title = existing.title.clone();
|
||||
}
|
||||
}
|
||||
@@ -527,11 +537,14 @@ pub struct BackfillStats {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ThreadMetadata;
|
||||
use super::ThreadMetadataBuilder;
|
||||
use super::ThreadRow;
|
||||
use chrono::DateTime;
|
||||
use chrono::Utc;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::openai_models::ReasoningEffort;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -617,4 +630,45 @@ mod tests {
|
||||
expected_thread_metadata(Some(ReasoningEffort::Custom("future".to_string())))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_thread_builder_uses_contextual_defaults() {
|
||||
let id =
|
||||
ThreadId::from_string("00000000-0000-0000-0000-000000000123").expect("valid thread id");
|
||||
let created_at = DateTime::<Utc>::from_timestamp(1_700_000_000, 0).expect("timestamp");
|
||||
let metadata = ThreadMetadataBuilder::new(
|
||||
id,
|
||||
PathBuf::from("/tmp/rollout-123.jsonl"),
|
||||
created_at,
|
||||
SessionSource::SubAgent(SubAgentSource::Other("guardian".to_string())),
|
||||
)
|
||||
.build("openai");
|
||||
|
||||
assert_eq!(metadata.title, crate::GUARDIAN_THREAD_TITLE);
|
||||
assert_eq!(
|
||||
metadata.preview.as_deref(),
|
||||
Some(crate::GUARDIAN_THREAD_PREVIEW)
|
||||
);
|
||||
assert_eq!(metadata.first_user_message, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guardian_default_title_does_not_replace_existing_explicit_title() {
|
||||
let id =
|
||||
ThreadId::from_string("00000000-0000-0000-0000-000000000123").expect("valid thread id");
|
||||
let created_at = DateTime::<Utc>::from_timestamp(1_700_000_000, 0).expect("timestamp");
|
||||
let mut metadata = ThreadMetadataBuilder::new(
|
||||
id,
|
||||
PathBuf::from("/tmp/rollout-123.jsonl"),
|
||||
created_at,
|
||||
SessionSource::SubAgent(SubAgentSource::Other("guardian".to_string())),
|
||||
)
|
||||
.build("openai");
|
||||
let mut existing = metadata.clone();
|
||||
existing.title = "Named Guardian review".to_string();
|
||||
|
||||
metadata.prefer_existing_explicit_title(&existing);
|
||||
|
||||
assert_eq!(metadata.title, "Named Guardian review");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user