Files
codex/codex-rs/tui/src/chatwidget/plan_implementation.rs
T
Dylan Hurd 3a89580f08 Hand off approved plans through attachments in fresh sessions (#60763)
## What changed

Make “Yes, clear context and implement” save the approved plan as an app-server attachment and submit a short prompt referencing it. Add the experimental `thread/prepareApprovedPlan` request to verify that the fresh thread's executor can read the file and that its contents match the approved plan.

If the attachment cannot be written or read, inject the complete plan as `proposed_plan` tool output within the model's context and truncation limits. If the complete plan cannot fit, report an error and keep the current session. Clear the UI and switch threads only after plan preparation succeeds.

## Testing

Add TUI tests covering a large plan read from its attachment, complete fallback context when attachment writes or reads fail, and preservation of the original session when the fallback is too large. Snapshot the fresh thread's initial request.

GitOrigin-RevId: b717f30359256994c6efb3f4fe7f87aabe8936af
2026-11-11 09:01:04 +00:00

107 lines
4.4 KiB
Rust

use codex_protocol::config_types::CollaborationModeMask;
use crate::app_event::AppEvent;
use crate::bottom_pane::SelectionAction;
use crate::bottom_pane::SelectionItem;
use crate::bottom_pane::SelectionViewParams;
use crate::bottom_pane::popup_consts::standard_popup_hint_line;
pub(super) const PLAN_IMPLEMENTATION_TITLE: &str = "Implement this plan?";
const PLAN_IMPLEMENTATION_YES: &str = "Yes, implement this plan";
const PLAN_IMPLEMENTATION_CLEAR_CONTEXT: &str = "Yes, clear context and implement";
const PLAN_IMPLEMENTATION_NO: &str = "No, stay in Plan mode";
pub(super) const PLAN_IMPLEMENTATION_CODING_MESSAGE: &str = "Implement the plan.";
pub(super) const PLAN_IMPLEMENTATION_DEFAULT_UNAVAILABLE: &str = "Default mode unavailable";
pub(super) const PLAN_IMPLEMENTATION_NO_APPROVED_PLAN: &str = "No approved plan available";
/// Builds the confirmation prompt shown after a plan is approved in Plan mode.
///
/// The optional usage label is already phrased for display, such as `89% used`
/// or `123K used`. This module only decides where that label belongs in the
/// decision copy so action wiring stays separate from token accounting.
pub(super) fn selection_view_params(
default_mask: Option<CollaborationModeMask>,
plan_markdown: Option<&str>,
clear_context_usage_label: Option<&str>,
) -> SelectionViewParams {
let (implement_actions, implement_disabled_reason) = match default_mask.clone() {
Some(mask) => {
let user_text = PLAN_IMPLEMENTATION_CODING_MESSAGE.to_string();
let actions: Vec<SelectionAction> = vec![Box::new(move |tx| {
tx.send(AppEvent::FollowTranscript);
tx.send(AppEvent::SubmitUserMessageWithMode {
text: user_text.clone(),
collaboration_mode: mask.clone(),
});
})];
(actions, None)
}
None => (
Vec::new(),
Some(PLAN_IMPLEMENTATION_DEFAULT_UNAVAILABLE.to_string()),
),
};
let (clear_context_actions, clear_context_disabled_reason) = match (default_mask, plan_markdown)
{
(None, _) => (
Vec::new(),
Some(PLAN_IMPLEMENTATION_DEFAULT_UNAVAILABLE.to_string()),
),
(Some(_), Some(plan_markdown)) if !plan_markdown.trim().is_empty() => {
let plan = plan_markdown.to_owned();
let actions: Vec<SelectionAction> = vec![Box::new(move |tx| {
tx.send(AppEvent::ImplementPlanInFreshSession { plan: plan.clone() });
})];
(actions, None)
}
(Some(_), _) => (
Vec::new(),
Some(PLAN_IMPLEMENTATION_NO_APPROVED_PLAN.to_string()),
),
};
let clear_context_description = clear_context_usage_label.map_or_else(
|| "Fresh thread with this plan".to_string(),
|label| format!("Start a fresh thread (current context: {label})"),
);
SelectionViewParams {
title: Some(PLAN_IMPLEMENTATION_TITLE.to_string()),
subtitle: None,
footer_hint: Some(standard_popup_hint_line()),
items: vec![
SelectionItem {
name: PLAN_IMPLEMENTATION_YES.to_string(),
description: Some("Switch to Default and start coding".to_string()),
selected_description: None,
is_current: false,
actions: implement_actions,
disabled_reason: implement_disabled_reason,
dismiss_on_select: true,
..Default::default()
},
SelectionItem {
name: PLAN_IMPLEMENTATION_CLEAR_CONTEXT.to_string(),
description: Some(clear_context_description),
selected_description: None,
is_current: false,
actions: clear_context_actions,
disabled_reason: clear_context_disabled_reason,
dismiss_on_select: true,
..Default::default()
},
SelectionItem {
name: PLAN_IMPLEMENTATION_NO.to_string(),
description: Some("Continue planning with the model".to_string()),
selected_description: None,
is_current: false,
actions: Vec::new(),
dismiss_on_select: true,
..Default::default()
},
],
..SelectionViewParams::picker()
}
}