Ensure side-chat policy reaches the first model request (#60067)

## Why

An inherited turn can suppress a fork's configured developer instructions, leaving the first side-chat request without its developer policy.

## What changed

- Move shared side-conversation instructions and the history boundary into `codex-context-fragments`.
- Inject the developer policy before the user-role boundary when creating a TUI side-chat fork, while retaining the configured instructions.
- Distinguish attached and temporary policies, applying the worktree restriction only to temporary side chats.
- Add guidance to set up recurring monitors in the main chat or a new chat because temporary side chats cannot run scheduled updates.

## Testing

Add a TUI integration test that checks the first side-chat model request contains the policy and boundary. Add a core request-history snapshot covering an attached side chat that preserves inherited context, policy, and its prior answer after resuming from disk.

GitOrigin-RevId: 889cb317dc3327457cd9ef0879ec9f77dbfaeeba
This commit is contained in:
Felipe Coury
2026-11-07 05:59:44 +00:00
committed by copyberry
parent 1c414e0d65
commit 0ed34eb9ce
10 changed files with 589 additions and 74 deletions
@@ -3,6 +3,7 @@
use super::session_lifecycle_requests::HistoryCapabilities;
use super::session_lifecycle_requests::start_recording_app_server_with_history;
use super::*;
use codex_context_fragments::side_conversation_boundary;
use pretty_assertions::assert_eq;
#[tokio::test]
@@ -71,7 +72,7 @@ sandbox = "unelevated"
server
.thread_inject_items(
app.chat_widget.thread_id().expect("fresh task"),
vec![App::side_boundary_prompt_item()],
vec![side_conversation_boundary()],
)
.await?;
let fresh_destination = tempfile::tempdir()?;
@@ -103,10 +104,7 @@ sandbox = "unelevated"
))?;
let saved = server.start_thread(&readonly).await?;
server
.thread_inject_items(
saved.session.thread_id,
vec![App::side_boundary_prompt_item()],
)
.thread_inject_items(saved.session.thread_id, vec![side_conversation_boundary()])
.await?;
tui.pause_events();
app.launch_cwd = source.path().to_path_buf();
@@ -218,7 +216,7 @@ sandbox = "unelevated"
server
.thread_inject_items(
app.chat_widget.thread_id().expect("new project task"),
vec![App::side_boundary_prompt_item()],
vec![side_conversation_boundary()],
)
.await?;
app.change_working_directory(&mut tui, &mut server, source.path().abs())
@@ -20,6 +20,7 @@ use codex_app_server_protocol::ThreadItemsListCursor;
use codex_app_server_protocol::ThreadItemsListParams;
use codex_app_server_protocol::ThreadItemsListResponse;
use codex_app_server_protocol::ThreadStatus;
use codex_context_fragments::side_conversation_boundary;
use codex_protocol::AgentPath;
use codex_protocol::items::AgentMessageContent;
use codex_protocol::items::AgentMessageItem;
@@ -1798,7 +1799,7 @@ async fn external_transport_registers_dynamic_tools_and_finds_task_mentions() ->
}
let target_id = started.session.thread_id;
app_server
.thread_inject_items(target_id, vec![App::side_boundary_prompt_item()])
.thread_inject_items(target_id, vec![side_conversation_boundary()])
.await?;
crate::init_state_db_for_app_server_target(&app.config, &crate::AppServerTarget::Embedded)
.await?
@@ -0,0 +1,105 @@
//! Covers the model request sent by a real TUI side-chat fork after a completed parent turn.
#![cfg(test)]
use super::*;
use crate::app::session_lifecycle::ThreadAttachPresentation;
use codex_app_server_client::AppServerEvent;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::TurnStartParams;
use codex_app_server_protocol::TurnStartResponse;
use core_test_support::responses;
/// Captures one real model request and waits until its turn can be inherited by a fork.
async fn ask(
server: &mut AppServerSession,
model: &wiremock::MockServer,
thread: ThreadId,
text: &str,
) -> Result<responses::ResponsesRequest> {
let mock = responses::mount_sse_once(
model,
responses::sse(vec![
responses::ev_assistant_message("reply", "Answer"),
responses::ev_completed("done"),
]),
)
.await;
let request_id = server.next_request_id();
let _: TurnStartResponse = server
.request_handle()
.request_typed(ClientRequest::TurnStart {
request_id,
params: TurnStartParams {
thread_id: thread.to_string(),
input: vec![UserInput::Text {
text: text.to_owned(),
text_elements: Vec::new(),
}],
..Default::default()
},
})
.await?;
loop {
let event = server.next_event().await.expect("connected app-server");
if let AppServerEvent::ServerNotification(n) = event.event
&& matches!(n.as_ref(), ServerNotification::TurnCompleted(t) if t.thread_id == thread.to_string())
{
return Ok(mock.single_request());
}
}
}
#[tokio::test]
async fn temporary_side_chat_sends_policy_on_its_first_request() -> Result<()> {
let mut app = make_test_app().await;
let model = responses::start_mock_server().await;
app_test_support::MockResponsesConfig::new(&model.uri()).write(&app.config.codex_home)?;
app.config = app
.rebuild_config_for_cwd(app.config.cwd.to_path_buf())
.await?;
let mut server = crate::start_embedded_app_server_for_picker(&app.config).await?;
let started = server.start_thread(&app.config).await?;
let parent = started.session.thread_id;
let mut tui = crate::tui::test_support::make_test_tui()?;
app.replace_chat_widget_with_app_server_thread(
&mut tui,
started,
ThreadAttachPresentation::SessionLineage,
/*initial_user_message*/ None,
)
.await?;
ask(&mut server, &model, parent, "Parent question").await?;
let fork = server
.sidebar_fork(
app.local_settings.clone(),
app.side_fork_config(),
parent,
/*profile*/ None,
)
.await?;
fork.boundary.expect("side-chat context injection");
let child = ask(
&mut server,
&model,
fork.thread.session.thread_id,
"Side question",
)
.await?;
let policy = child
.message_input_texts("developer")
.into_iter()
.find(|text| text.contains("<side_conversation>"))
.expect("first child request must contain the side-chat developer policy");
assert!(policy.contains("Scheduled updates"));
assert!(policy.contains("main conversation without calling create_worktree"));
assert!(
child
.message_input_texts("user")
.iter()
.any(|text| text.starts_with("Side conversation boundary."))
);
server.shutdown().await?;
Ok(())
}