Files
codex/codex-rs/core/src/session/retained_context.rs
T
felixxia-oai 1cd253c987 Preserve explicit user goal updates in Guardian authorization (#47811)
## Why

Explicit user goal edits can restrict permitted actions, such as “Inspect only; do not publish.” Guardian needs these edits as authorization evidence even though they are recorded as contextual messages.

## What changed

- Add `UserGoalUpdate` and a recording API for objective, status, and clear updates without scheduling a model response.
- Use host annotations to retain goal updates in Guardian review history and root authorization evidence, and invalidate cached authorization when updates are recorded.
- Omit oversized objectives whole and mark their evidence incomplete so truncation cannot turn a restriction into a grant.
- Keep item injection from updating shared model and multi-agent metadata.

## Testing

Extend authorization, retained-context, and review snapshot tests to cover goal-edit cache invalidation, root goal visibility from child threads, and restrictive goals reaching Guardian alongside managed policies.

GitOrigin-RevId: 17b99d9fa8c1620840d6f408d79b405a78a778f8
2026-09-24 10:46:46 +00:00

87 lines
3.1 KiB
Rust

//! Orders retained inputs at acceptance and assistant messages at source-stream start.
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::PoisonError;
use crate::context::ContextualUserFragment;
use crate::context::UserGoalUpdate;
use codex_history::RetainedContextEvent;
use codex_history::RolloutItem;
use codex_protocol::models::ResponseItem;
use super::Session;
use super::TurnContext;
use super::thread_settings;
/// Only unrecorded starts need a reservation; abandoned entries expire with the turn.
#[derive(Default)]
pub(super) struct PendingAssistantMessageOrders(pub(super) Mutex<HashMap<String, u64>>);
impl Session {
/// Reserve before deriving display items, including plans, from the source message.
/// Completion-only responses use the same path before publishing their text.
pub(super) async fn reserve_assistant_message_order(
&self,
turn_context: &TurnContext,
item: &ResponseItem,
) {
if let ResponseItem::Message {
id: Some(id), role, ..
} = item
&& role == "assistant"
{
let mut state = self.state.lock().await;
if !state
.history
.raw_items()
.any(|item| item.id().is_some_and(|recorded_id| recorded_id == id))
{
turn_context
.extension_data
.get_or_init(PendingAssistantMessageOrders::default)
.0
.lock()
.unwrap_or_else(PoisonError::into_inner)
.entry(id.to_string())
.or_insert_with(|| state.history.reserve_input_order());
}
}
}
/// Records authorization without adding pending input or reopening an active turn.
pub(crate) async fn record_user_goal_update(&self, update: UserGoalUpdate) {
// Goal metadata must not initialize a model step, even on a goal-first thread.
// Keep context construction off callers' stacks, including the TUI RPC dispatcher.
let context = Box::pin(self.new_inject_items_context()).await;
let _guard = thread_settings::acquire_persistence_lock(self).await;
self.record_conversation_items(
&context,
context.model_info(),
&[ContextualUserFragment::into(update)],
)
.await;
}
pub(crate) async fn reserve_user_input_order(&self) -> u64 {
self.state.lock().await.history.reserve_input_order()
}
pub(crate) async fn record_retained_context(&self, mut event: RetainedContextEvent) {
event.bound();
// Share the checkpoint persistence lock so a fact cannot land on the wrong side
// of the checkpoint/suffix boundary. Ephemeral threads use the same live state.
let _guard = thread_settings::acquire_persistence_lock(self).await;
if self
.state
.lock()
.await
.history
.record_retained_context(&event)
{
self.persist_rollout_items(&[RolloutItem::RetainedContext(event)])
.await;
}
}
}