[2 of 3] Support long pasted text in TUI goals (#27509)

## Stack

1. [1 of 3] Support long raw TUI goal objectives - #27508
2. **[2 of 3] Support long pasted text in TUI goals** - this PR
3. [3 of 3] Support images in TUI goals - #27510

## Why

Large text pasted into the TUI composer is represented as a paste
placeholder plus pending paste metadata. For `/goal`, preserving only
the visible placeholder is not enough: the agent would see a short
placeholder string instead of the actual pasted text, and the long-text
support from the first PR would never see the payload.

The TUI also needs to avoid writing stale sidecar files when a user
pastes a large block and then deletes its placeholder before submitting
the goal.

## What Changed

- Introduces a TUI `GoalDraft` for goal submissions so `/goal`, `/goal
edit`, and queued goal commands can carry objective text plus text
elements and pending paste payloads.
- Materializes active pasted-text placeholders to `pasted-text-N.txt`
files through the app-server filesystem path introduced in #27508.
- Rewrites active paste placeholders in the persisted objective to file
references, while leaving literal placeholder-looking text alone.
- Filters out deleted paste placeholders so otherwise-small goals do not
require `$CODEX_HOME` or remote filesystem writes.
- Preserves pending paste metadata when a `/goal` command is queued
before a thread exists.

## Verification

- Added goal materialization tests for active paste placeholders,
deleted paste placeholders, and whitespace-only paste payloads.
- Added/updated TUI slash-command tests for large pasted text, queued
`/goal` commands before thread start, and queued oversized goal
behavior.

## Manual Testing

- Used real terminal bracketed-paste sequences through a remote TUI
session. A 1,228-byte multiline paste became `pasted-text-1.txt`; its
first/last lines and byte count matched exactly, and the persisted
objective referenced the server-host path.
- Pasted a large block, deleted its placeholder, and submitted a small
replacement objective. No new directory or sidecar file was created.
- Added two same-length large pastes to one goal. The composer
disambiguated their visible placeholders, and materialization preserved
order and contents in `pasted-text-1.txt` and `pasted-text-2.txt`.
- Submitted a whitespace-only large paste and verified the goal was
rejected as empty without writing a file.
- Submitted a pasted-text replacement while another goal was active,
verified no file was written before confirmation, then canceled and
confirmed the original goal remained unchanged.
- Combined a large paste with enough raw text to exceed 4,000 characters
after placeholder rewriting. The paste sidecar and `goal-objective.md`
were created in the same remote attachment directory, and `/goal edit`
restored the rewritten objective with its sidecar reference.
This commit is contained in:
Eric Traut authored and GitHub committed 2026-06-12 15:34:04 -07:00
1 parent 216dee1189
commit c0f1ec5afd
17 files changed
+600 -201

No files matched your search

+3 -3
View File
@@ -728,12 +728,12 @@ impl App {
AppEvent::OpenThreadGoalEditor { thread_id } => {
self.open_thread_goal_editor(app_server, thread_id).await;
}
AppEvent::SetThreadGoalObjective {
AppEvent::SetThreadGoalDraft {
thread_id,
objective,
draft,
mode,
} => {
self.set_thread_goal_objective(app_server, thread_id, objective, mode)
self.set_thread_goal_draft(app_server, thread_id, draft, mode)
.await;
}
AppEvent::SetThreadGoalStatus { thread_id, status } => {
+91 -6
View File
@@ -4155,7 +4155,8 @@ async fn make_test_app_with_channels() -> (
}
#[tokio::test]
async fn set_thread_goal_objective_materializes_long_objective_before_goal_set() -> Result<()> {
async fn set_thread_goal_draft_materializes_long_objective_and_confirms_before_paste() -> Result<()>
{
let mut app = make_test_app().await;
let mut app_server =
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
@@ -4167,10 +4168,13 @@ async fn set_thread_goal_objective_materializes_long_objective_before_goal_set()
.await?;
let objective = "x".repeat(MAX_THREAD_GOAL_OBJECTIVE_CHARS + 1);
app.set_thread_goal_objective(
app.set_thread_goal_draft(
&mut app_server,
thread_id,
objective.clone(),
crate::goal_files::GoalDraft {
objective: objective.clone(),
..Default::default()
},
crate::app_event::ThreadGoalSetMode::ConfirmIfExists,
)
.await;
@@ -4209,11 +4213,20 @@ async fn set_thread_goal_objective_materializes_long_objective_before_goal_set()
assert_eq!(unix_path.as_str(), "/tmp/codex\\/a");
let attachments_dir = app.chat_widget.config_ref().codex_home.join("attachments");
let attachment_count = std::fs::read_dir(&attachments_dir)?.count();
let placeholder = "[Pasted Content 5 chars]";
let paste_draft = crate::goal_files::GoalDraft {
objective: format!("Use {placeholder}"),
text_elements: vec![TextElement::new(
(4..4 + placeholder.len()).into(),
Some(placeholder.to_string()),
)],
pending_pastes: vec![(placeholder.to_string(), "hello".to_string())],
};
app.set_thread_goal_objective(
app.set_thread_goal_draft(
&mut app_server,
thread_id,
"x".repeat(MAX_THREAD_GOAL_OBJECTIVE_CHARS + 1),
paste_draft.clone(),
crate::app_event::ThreadGoalSetMode::ConfirmIfExists,
)
.await;
@@ -4231,6 +4244,72 @@ async fn set_thread_goal_objective_materializes_long_objective_before_goal_set()
.objective,
saved_objective
);
app.set_thread_goal_draft(
&mut app_server,
thread_id,
paste_draft,
crate::app_event::ThreadGoalSetMode::ReplaceExisting,
)
.await;
let goal = app_server
.thread_goal_get(thread_id)
.await?
.goal
.expect("replacement goal should be set");
let paste_path = goal
.objective
.strip_prefix("Use pasted text file: ")
.and_then(|text| text.strip_suffix(". Read this file before continuing."))
.expect("paste file reference");
assert_eq!(std::fs::read_to_string(paste_path)?, "hello");
let attachment_count = std::fs::read_dir(&attachments_dir)?.count();
let stale_paste = (placeholder.to_string(), "hello".to_string());
app.set_thread_goal_draft(
&mut app_server,
thread_id,
crate::goal_files::GoalDraft {
objective: "small goal".to_string(),
pending_pastes: vec![stale_paste],
..Default::default()
},
crate::app_event::ThreadGoalSetMode::ReplaceExisting,
)
.await;
assert_eq!(
std::fs::read_dir(&attachments_dir)?.count(),
attachment_count
);
let whitespace_placeholder = "[Pasted Content 3 chars]";
app.set_thread_goal_draft(
&mut app_server,
thread_id,
crate::goal_files::GoalDraft {
objective: whitespace_placeholder.to_string(),
text_elements: vec![TextElement::new(
(0..whitespace_placeholder.len()).into(),
Some(whitespace_placeholder.to_string()),
)],
pending_pastes: vec![(whitespace_placeholder.to_string(), " \n\t".to_string())],
},
crate::app_event::ThreadGoalSetMode::ReplaceExisting,
)
.await;
assert_eq!(
std::fs::read_dir(&attachments_dir)?.count(),
attachment_count
);
assert_eq!(
app_server
.thread_goal_get(thread_id)
.await?
.goal
.expect("small goal should remain set")
.objective,
"small goal"
);
app_server.shutdown().await?;
Ok(())
}
@@ -4238,7 +4317,13 @@ async fn set_thread_goal_objective_materializes_long_objective_before_goal_set()
#[tokio::test]
async fn replace_goal_confirmation_snapshot() {
let mut app = make_test_app().await;
app.show_replace_thread_goal_confirmation(ThreadId::new(), "New goal".to_string());
app.show_replace_thread_goal_confirmation(
ThreadId::new(),
goal_files::GoalDraft {
objective: "New goal".to_string(),
..Default::default()
},
);
assert_app_snapshot!(
"replace_goal_confirmation",
render_bottom_popup(&app.chat_widget, /*width*/ 80)
+11 -10
View File
@@ -125,11 +125,11 @@ impl App {
self.chat_widget.show_goal_edit_prompt(thread_id, goal);
}
pub(super) async fn set_thread_goal_objective(
pub(super) async fn set_thread_goal_draft(
&mut self,
app_server: &mut AppServerSession,
thread_id: ThreadId,
objective: String,
draft: goal_files::GoalDraft,
mode: ThreadGoalSetMode,
) {
let codex_home = app_server.codex_home_path(&self.config.codex_home);
@@ -142,7 +142,7 @@ impl App {
match result {
Ok(response) => match response.goal.as_ref() {
Some(goal) if should_confirm_before_replacing_goal(goal) => {
self.show_replace_thread_goal_confirmation(thread_id, objective);
self.show_replace_thread_goal_confirmation(thread_id, draft);
return;
}
Some(_) => ThreadGoalSetMode::ReplaceExisting,
@@ -158,14 +158,14 @@ impl App {
mode
};
let (objective, output_dir) = match goal_files::materialize_goal_objective(
let (objective, output_dir) = match goal_files::materialize_goal_draft(
app_server,
codex_home.as_ref(),
objective,
draft,
)
.await
{
Ok(objective) => objective,
Ok(materialized) => materialized,
Err(err) => {
if self.current_displayed_thread_id() == Some(thread_id) {
self.chat_widget.add_error_message(err.to_string());
@@ -286,13 +286,14 @@ impl App {
pub(super) fn show_replace_thread_goal_confirmation(
&mut self,
thread_id: ThreadId,
objective: String,
draft: goal_files::GoalDraft,
) {
let replace_objective = objective.clone();
let objective = draft.objective.clone();
let replace_draft = draft;
let replace_actions: Vec<SelectionAction> = vec![Box::new(move |tx| {
tx.send(AppEvent::SetThreadGoalObjective {
tx.send(AppEvent::SetThreadGoalDraft {
thread_id,
objective: replace_objective.clone(),
draft: replace_draft.clone(),
mode: ThreadGoalSetMode::ReplaceExisting,
});
})];