mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Support paginated thread forks (#35220)
## Why `thread/fork` rejected threads using paginated history, so they could not use the same fork workflow as legacy threads. ## What changed - Create paginated forks by referencing a frozen source-history prefix while persisting only child-owned records in the new rollout. - Support latest, `lastTurnId`, and `beforeTurnId` boundaries, including inherited lineage, model-context reconstruction, interruption markers, and approval-reviewer inheritance. - Make paginated reads and occurrence search traverse referenced history while excluding source records added after the fork. - Coordinate fork preparation with archive and delete operations, and materialize compressed rollouts before they become fork references. ## Testing Added app-server and thread-store coverage for boundary selection, active turns, inherited reads and search, compressed lineage, and concurrent source lifecycle operations. GitOrigin-RevId: 66052d69517d7fb97a3b7817ab77a39c5ad995fd
This commit is contained in:
@@ -115,6 +115,7 @@ use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_protocol::protocol::AdditionalContextEntry;
|
||||
use codex_protocol::protocol::FileChange;
|
||||
use codex_protocol::protocol::HasLegacyEvent;
|
||||
use codex_protocol::protocol::HistoryPosition;
|
||||
use codex_protocol::protocol::InterAgentCommunication;
|
||||
use codex_protocol::protocol::ItemCompletedEvent;
|
||||
use codex_protocol::protocol::ItemStartedEvent;
|
||||
@@ -413,6 +414,15 @@ pub(crate) enum GitEnrichmentPolicy {
|
||||
Skip,
|
||||
}
|
||||
|
||||
/// Controls which fork history belongs in the newly created thread's own rollout.
|
||||
pub(crate) enum ForkPersistence {
|
||||
Copied,
|
||||
Referenced {
|
||||
history_base: Option<HistoryPosition>,
|
||||
inherited_item_count: usize,
|
||||
},
|
||||
}
|
||||
|
||||
pub(crate) struct SessionSpawnArgs {
|
||||
pub(crate) config: Config,
|
||||
pub(crate) allow_provider_model_fallback: bool,
|
||||
@@ -428,6 +438,7 @@ pub(crate) struct SessionSpawnArgs {
|
||||
pub(crate) extensions: Arc<codex_extension_api::ExtensionRegistry<crate::config::Config>>,
|
||||
pub(crate) conversation_history: InitialHistory,
|
||||
pub(crate) requested_history_mode: Option<ThreadHistoryMode>,
|
||||
pub(crate) fork_persistence: ForkPersistence,
|
||||
pub(crate) session_source: SessionSource,
|
||||
pub(crate) forked_from_thread_id: Option<ThreadId>,
|
||||
pub(crate) parent_thread_id: Option<ThreadId>,
|
||||
@@ -523,6 +534,7 @@ impl Session {
|
||||
extensions,
|
||||
conversation_history,
|
||||
requested_history_mode,
|
||||
fork_persistence,
|
||||
session_source,
|
||||
forked_from_thread_id,
|
||||
parent_thread_id,
|
||||
@@ -716,6 +728,7 @@ impl Session {
|
||||
tx_event.clone(),
|
||||
agent_status_tx.clone(),
|
||||
conversation_history,
|
||||
fork_persistence,
|
||||
session_source_clone,
|
||||
skills_service,
|
||||
plugins_manager,
|
||||
@@ -1352,16 +1365,29 @@ impl Session {
|
||||
|
||||
let thread_settings_applied =
|
||||
RolloutItem::EventMsg(handlers::thread_settings_applied_event(self).await);
|
||||
if is_paginated_subagent {
|
||||
// Paginated subagents persist inherited model context while creating the live
|
||||
// thread so the copied prefix is not observed as child-owned metadata.
|
||||
self.persist_rollout_items(&[thread_settings_applied]).await;
|
||||
} else {
|
||||
// Keep the copied prefix and the child's effective settings in one append so a
|
||||
// cold resume cannot observe inherited settings as the child's latest value.
|
||||
rollout_items.push(thread_settings_applied);
|
||||
self.persist_rollout_items(&rollout_items).await;
|
||||
match &self.fork_persistence {
|
||||
ForkPersistence::Referenced {
|
||||
inherited_item_count,
|
||||
..
|
||||
} => {
|
||||
// Ancestor records remain behind history_base; only effective child
|
||||
// settings and boundaries synthesized by snapshot processing are local.
|
||||
rollout_items.drain(..*inherited_item_count);
|
||||
rollout_items.insert(0, thread_settings_applied);
|
||||
}
|
||||
ForkPersistence::Copied if is_paginated_subagent => {
|
||||
// Paginated subagents already persist inherited context when their live
|
||||
// thread is created.
|
||||
rollout_items.clear();
|
||||
rollout_items.push(thread_settings_applied);
|
||||
}
|
||||
ForkPersistence::Copied => {
|
||||
// Keep the copied prefix and effective child settings in one append so a
|
||||
// cold resume cannot observe inherited settings as the latest value.
|
||||
rollout_items.push(thread_settings_applied);
|
||||
}
|
||||
}
|
||||
self.persist_rollout_items(&rollout_items).await;
|
||||
|
||||
// Forked threads should remain file-backed immediately after startup.
|
||||
self.ensure_rollout_materialized().await;
|
||||
|
||||
@@ -54,6 +54,7 @@ pub(crate) struct Session {
|
||||
pub(crate) guardian_review_session: GuardianReviewSessionManager,
|
||||
pub(crate) services: SessionServices,
|
||||
pub(super) git_enrichment_policy: GitEnrichmentPolicy,
|
||||
pub(super) fork_persistence: ForkPersistence,
|
||||
pub(super) next_internal_sub_id: AtomicU64,
|
||||
}
|
||||
|
||||
@@ -501,6 +502,7 @@ impl Session {
|
||||
tx_event: Sender<Event>,
|
||||
agent_status: watch::Sender<AgentStatus>,
|
||||
mut initial_history: InitialHistory,
|
||||
fork_persistence: ForkPersistence,
|
||||
session_source: SessionSource,
|
||||
skills_service: Arc<SkillsService>,
|
||||
plugins_manager: Arc<PluginsManager>,
|
||||
@@ -631,6 +633,10 @@ impl Session {
|
||||
selected_capability_roots: selected_capability_roots.clone(),
|
||||
multi_agent_version: initial_multi_agent_version,
|
||||
history_mode: session_configuration.history_mode,
|
||||
history_base: match &fork_persistence {
|
||||
ForkPersistence::Copied => None,
|
||||
ForkPersistence::Referenced { history_base, .. } => *history_base,
|
||||
},
|
||||
subagent_history_start_ordinal: None,
|
||||
initial_window_id: initial_auto_compact_window_ids
|
||||
.window_id
|
||||
@@ -646,6 +652,7 @@ impl Session {
|
||||
},
|
||||
};
|
||||
if is_paginated_subagent
|
||||
&& matches!(&fork_persistence, ForkPersistence::Copied)
|
||||
&& let InitialHistory::Forked(items) = &initial_history
|
||||
{
|
||||
LiveThread::create_with_inherited_model_context(
|
||||
@@ -1172,6 +1179,7 @@ impl Session {
|
||||
guardian_review_session: GuardianReviewSessionManager::default(),
|
||||
services,
|
||||
git_enrichment_policy,
|
||||
fork_persistence,
|
||||
next_internal_sub_id: AtomicU64::new(0),
|
||||
});
|
||||
if let Some(network_policy_decider_session) = network_policy_decider_session {
|
||||
@@ -1262,6 +1270,10 @@ impl Session {
|
||||
|
||||
// record_initial_history can emit events. We record only after the SessionConfiguredEvent is emitted.
|
||||
Box::pin(sess.record_initial_history(initial_history)).await;
|
||||
if matches!(&sess.fork_persistence, ForkPersistence::Referenced { .. }) {
|
||||
// Keep the source reserved until the child's history reference is durable.
|
||||
sess.try_ensure_rollout_materialized().await?;
|
||||
}
|
||||
{
|
||||
let mut state = sess.state.lock().await;
|
||||
state.queue_pending_session_start_source(session_start_source);
|
||||
|
||||
@@ -4309,6 +4309,7 @@ async fn attach_thread_persistence(session: &mut Session) -> PathBuf {
|
||||
multi_agent_version: None,
|
||||
history_mode: Default::default(),
|
||||
subagent_history_start_ordinal: None,
|
||||
history_base: None,
|
||||
initial_window_id: Uuid::now_v7().to_string(),
|
||||
metadata: ThreadPersistenceMetadata {
|
||||
cwd: Some(config.cwd.to_path_buf()),
|
||||
@@ -5352,6 +5353,7 @@ async fn session_new_fails_when_zsh_fork_enabled_without_packaged_zsh() {
|
||||
tx_event,
|
||||
agent_status_tx,
|
||||
InitialHistory::New,
|
||||
ForkPersistence::Copied,
|
||||
SessionSource::Exec,
|
||||
skills_service,
|
||||
plugins_manager,
|
||||
@@ -5640,6 +5642,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
|
||||
guardian_review_session: crate::guardian::GuardianReviewSessionManager::default(),
|
||||
services,
|
||||
git_enrichment_policy: GitEnrichmentPolicy::Fresh,
|
||||
fork_persistence: ForkPersistence::Copied,
|
||||
next_internal_sub_id: AtomicU64::new(0),
|
||||
};
|
||||
|
||||
@@ -5743,6 +5746,7 @@ async fn make_session_with_config_and_rx(
|
||||
tx_event,
|
||||
agent_status_tx,
|
||||
InitialHistory::New,
|
||||
ForkPersistence::Copied,
|
||||
SessionSource::Exec,
|
||||
skills_service,
|
||||
plugins_manager,
|
||||
@@ -5852,6 +5856,7 @@ async fn make_session_with_history_source_and_agent_control_and_rx(
|
||||
tx_event,
|
||||
agent_status_tx,
|
||||
initial_history,
|
||||
ForkPersistence::Copied,
|
||||
session_source,
|
||||
skills_service,
|
||||
plugins_manager,
|
||||
@@ -7120,6 +7125,7 @@ async fn shutdown_complete_does_not_append_to_thread_store_after_shutdown() {
|
||||
multi_agent_version: None,
|
||||
history_mode: Default::default(),
|
||||
subagent_history_start_ordinal: None,
|
||||
history_base: None,
|
||||
initial_window_id: Uuid::now_v7().to_string(),
|
||||
metadata: ThreadPersistenceMetadata {
|
||||
cwd: Some(config.cwd.to_path_buf()),
|
||||
@@ -7198,6 +7204,7 @@ async fn submission_loop_channel_close_runs_full_thread_teardown() {
|
||||
multi_agent_version: None,
|
||||
history_mode: Default::default(),
|
||||
subagent_history_start_ordinal: None,
|
||||
history_base: None,
|
||||
initial_window_id: Uuid::now_v7().to_string(),
|
||||
metadata: ThreadPersistenceMetadata {
|
||||
cwd: Some(config.cwd.to_path_buf()),
|
||||
@@ -7808,6 +7815,7 @@ where
|
||||
guardian_review_session: crate::guardian::GuardianReviewSessionManager::default(),
|
||||
services,
|
||||
git_enrichment_policy: GitEnrichmentPolicy::Fresh,
|
||||
fork_persistence: ForkPersistence::Copied,
|
||||
next_internal_sub_id: AtomicU64::new(0),
|
||||
});
|
||||
|
||||
@@ -9593,6 +9601,7 @@ async fn attach_in_memory_thread_store(
|
||||
multi_agent_version: None,
|
||||
history_mode: Default::default(),
|
||||
subagent_history_start_ordinal: None,
|
||||
history_base: None,
|
||||
initial_window_id: Uuid::now_v7().to_string(),
|
||||
metadata: ThreadPersistenceMetadata {
|
||||
cwd: Some(config.cwd.to_path_buf()),
|
||||
|
||||
@@ -742,6 +742,7 @@ async fn guardian_subagent_does_not_inherit_parent_exec_policy_rules() {
|
||||
extensions: codex_extension_api::empty_extension_registry(),
|
||||
conversation_history: InitialHistory::New,
|
||||
requested_history_mode: None,
|
||||
fork_persistence: ForkPersistence::Copied,
|
||||
session_source: SessionSource::SubAgent(SubAgentSource::Other(
|
||||
GUARDIAN_REVIEWER_NAME.to_string(),
|
||||
)),
|
||||
|
||||
Reference in New Issue
Block a user