mirror of
https://github.com/openai/codex.git
synced 2026-05-19 02:33:10 +00:00
## Summary - Stream proposed plans in Plan Mode using `<proposed_plan>` tags parsed in core, emitting plan deltas plus a plan `ThreadItem`, while stripping tags from normal assistant output. - Persist plan items and rebuild them on resume so proposed plans show in thread history. - Wire plan items/deltas through app-server protocol v2 and render a dedicated proposed-plan view in the TUI, including the “Implement this plan?” prompt only when a plan item is present. ## Changes ### Core (`codex-rs/core`) - Added a generic, line-based tag parser that buffers each line until it can disprove a tag prefix; implements auto-close on `finish()` for unterminated tags. `codex-rs/core/src/tagged_block_parser.rs` - Refactored proposed plan parsing to wrap the generic parser. `codex-rs/core/src/proposed_plan_parser.rs` - In plan mode, stream assistant deltas as: - **Normal text** → `AgentMessageContentDelta` - **Plan text** → `PlanDelta` + `TurnItem::Plan` start/completion (`codex-rs/core/src/codex.rs`) - Final plan item content is derived from the completed assistant message (authoritative), not necessarily the concatenated deltas. - Strips `<proposed_plan>` blocks from assistant text in plan mode so tags don’t appear in normal messages. (`codex-rs/core/src/stream_events_utils.rs`) - Persist `ItemCompleted` events only for plan items for rollout replay. (`codex-rs/core/src/rollout/policy.rs`) - Guard `update_plan` tool in Plan Mode with a clear error message. (`codex-rs/core/src/tools/handlers/plan.rs`) - Updated Plan Mode prompt to: - keep `<proposed_plan>` out of non-final reasoning/preambles - require exact tag formatting - allow only one `<proposed_plan>` block per turn (`codex-rs/core/templates/collaboration_mode/plan.md`) ### Protocol / App-server protocol - Added `TurnItem::Plan` and `PlanDeltaEvent` to core protocol items. (`codex-rs/protocol/src/items.rs`, `codex-rs/protocol/src/protocol.rs`) - Added v2 `ThreadItem::Plan` and `PlanDeltaNotification` with EXPERIMENTAL markers and note that deltas may not match the final plan item. (`codex-rs/app-server-protocol/src/protocol/v2.rs`) - Added plan delta route in app-server protocol common mapping. (`codex-rs/app-server-protocol/src/protocol/common.rs`) - Rebuild plan items from persisted `ItemCompleted` events on resume. (`codex-rs/app-server-protocol/src/protocol/thread_history.rs`) ### App-server - Forward plan deltas to v2 clients and map core plan items to v2 plan items. (`codex-rs/app-server/src/bespoke_event_handling.rs`, `codex-rs/app-server/src/codex_message_processor.rs`) - Added v2 plan item tests. (`codex-rs/app-server/tests/suite/v2/plan_item.rs`) ### TUI - Added a dedicated proposed plan history cell with special background and padding, and moved “• Proposed Plan” outside the highlighted block. (`codex-rs/tui/src/history_cell.rs`, `codex-rs/tui/src/style.rs`) - Only show “Implement this plan?” when a plan item exists. (`codex-rs/tui/src/chatwidget.rs`, `codex-rs/tui/src/chatwidget/tests.rs`) <img width="831" height="847" alt="Screenshot 2026-01-29 at 7 06 24 PM" src="https://github.com/user-attachments/assets/69794c8c-f96b-4d36-92ef-c1f5c3a8f286" /> ### Docs / Misc - Updated protocol docs to mention plan deltas. (`codex-rs/docs/protocol_v1.md`) - Minor plumbing updates in exec/debug clients to tolerate plan deltas. (`codex-rs/debug-client/src/reader.rs`, `codex-rs/exec/...`) ## Tests - Added core integration tests: - Plan mode strips plan from agent messages. - Missing `</proposed_plan>` closes at end-of-message. (`codex-rs/core/tests/suite/items.rs`) - Added unit tests for generic tag parser (prefix buffering, non-tag lines, auto-close). (`codex-rs/core/src/tagged_block_parser.rs`) - Existing app-server plan item tests in v2. (`codex-rs/app-server/tests/suite/v2/plan_item.rs`) ## Notes / Behavior - Plan output no longer appears in standard assistant text in Plan Mode; it streams via `PlanDelta` and completes as a `TurnItem::Plan`. - The final plan item content is authoritative and may diverge from streamed deltas (documented as experimental). - Reasoning summaries are not filtered; prompt instructs the model not to include `<proposed_plan>` outside the final plan message. ## Codex Author `codex fork 019bec2d-b09d-7450-b292-d7bcdddcdbfb`
186 lines
5.6 KiB
Rust
186 lines
5.6 KiB
Rust
use crate::tagged_block_parser::TagSpec;
|
|
use crate::tagged_block_parser::TaggedLineParser;
|
|
use crate::tagged_block_parser::TaggedLineSegment;
|
|
|
|
const OPEN_TAG: &str = "<proposed_plan>";
|
|
const CLOSE_TAG: &str = "</proposed_plan>";
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
enum PlanTag {
|
|
ProposedPlan,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub(crate) enum ProposedPlanSegment {
|
|
Normal(String),
|
|
ProposedPlanStart,
|
|
ProposedPlanDelta(String),
|
|
ProposedPlanEnd,
|
|
}
|
|
|
|
/// Parser for `<proposed_plan>` blocks emitted in plan mode.
|
|
///
|
|
/// This is a thin wrapper around the generic line-based tag parser. It maps
|
|
/// tag-aware segments into plan-specific segments for downstream consumers.
|
|
#[derive(Debug)]
|
|
pub(crate) struct ProposedPlanParser {
|
|
parser: TaggedLineParser<PlanTag>,
|
|
}
|
|
|
|
impl ProposedPlanParser {
|
|
pub(crate) fn new() -> Self {
|
|
Self {
|
|
parser: TaggedLineParser::new(vec![TagSpec {
|
|
open: OPEN_TAG,
|
|
close: CLOSE_TAG,
|
|
tag: PlanTag::ProposedPlan,
|
|
}]),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn parse(&mut self, delta: &str) -> Vec<ProposedPlanSegment> {
|
|
self.parser
|
|
.parse(delta)
|
|
.into_iter()
|
|
.map(map_plan_segment)
|
|
.collect()
|
|
}
|
|
|
|
pub(crate) fn finish(&mut self) -> Vec<ProposedPlanSegment> {
|
|
self.parser
|
|
.finish()
|
|
.into_iter()
|
|
.map(map_plan_segment)
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
fn map_plan_segment(segment: TaggedLineSegment<PlanTag>) -> ProposedPlanSegment {
|
|
match segment {
|
|
TaggedLineSegment::Normal(text) => ProposedPlanSegment::Normal(text),
|
|
TaggedLineSegment::TagStart(PlanTag::ProposedPlan) => {
|
|
ProposedPlanSegment::ProposedPlanStart
|
|
}
|
|
TaggedLineSegment::TagDelta(PlanTag::ProposedPlan, text) => {
|
|
ProposedPlanSegment::ProposedPlanDelta(text)
|
|
}
|
|
TaggedLineSegment::TagEnd(PlanTag::ProposedPlan) => ProposedPlanSegment::ProposedPlanEnd,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn strip_proposed_plan_blocks(text: &str) -> String {
|
|
let mut parser = ProposedPlanParser::new();
|
|
let mut out = String::new();
|
|
for segment in parser.parse(text).into_iter().chain(parser.finish()) {
|
|
if let ProposedPlanSegment::Normal(delta) = segment {
|
|
out.push_str(&delta);
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
pub(crate) fn extract_proposed_plan_text(text: &str) -> Option<String> {
|
|
let mut parser = ProposedPlanParser::new();
|
|
let mut plan_text = String::new();
|
|
let mut saw_plan_block = false;
|
|
for segment in parser.parse(text).into_iter().chain(parser.finish()) {
|
|
match segment {
|
|
ProposedPlanSegment::ProposedPlanStart => {
|
|
saw_plan_block = true;
|
|
plan_text.clear();
|
|
}
|
|
ProposedPlanSegment::ProposedPlanDelta(delta) => {
|
|
plan_text.push_str(&delta);
|
|
}
|
|
ProposedPlanSegment::ProposedPlanEnd | ProposedPlanSegment::Normal(_) => {}
|
|
}
|
|
}
|
|
saw_plan_block.then_some(plan_text)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::ProposedPlanParser;
|
|
use super::ProposedPlanSegment;
|
|
use super::strip_proposed_plan_blocks;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
#[test]
|
|
fn streams_proposed_plan_segments() {
|
|
let mut parser = ProposedPlanParser::new();
|
|
let mut segments = Vec::new();
|
|
|
|
for chunk in [
|
|
"Intro text\n<prop",
|
|
"osed_plan>\n- step 1\n",
|
|
"</proposed_plan>\nOutro",
|
|
] {
|
|
segments.extend(parser.parse(chunk));
|
|
}
|
|
segments.extend(parser.finish());
|
|
|
|
assert_eq!(
|
|
segments,
|
|
vec![
|
|
ProposedPlanSegment::Normal("Intro text\n".to_string()),
|
|
ProposedPlanSegment::ProposedPlanStart,
|
|
ProposedPlanSegment::ProposedPlanDelta("- step 1\n".to_string()),
|
|
ProposedPlanSegment::ProposedPlanEnd,
|
|
ProposedPlanSegment::Normal("Outro".to_string()),
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn preserves_non_tag_lines() {
|
|
let mut parser = ProposedPlanParser::new();
|
|
let mut segments = parser.parse(" <proposed_plan> extra\n");
|
|
segments.extend(parser.finish());
|
|
|
|
assert_eq!(
|
|
segments,
|
|
vec![ProposedPlanSegment::Normal(
|
|
" <proposed_plan> extra\n".to_string()
|
|
)]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn closes_unterminated_plan_block_on_finish() {
|
|
let mut parser = ProposedPlanParser::new();
|
|
let mut segments = parser.parse("<proposed_plan>\n- step 1\n");
|
|
segments.extend(parser.finish());
|
|
|
|
assert_eq!(
|
|
segments,
|
|
vec![
|
|
ProposedPlanSegment::ProposedPlanStart,
|
|
ProposedPlanSegment::ProposedPlanDelta("- step 1\n".to_string()),
|
|
ProposedPlanSegment::ProposedPlanEnd,
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn closes_tag_line_without_trailing_newline() {
|
|
let mut parser = ProposedPlanParser::new();
|
|
let mut segments = parser.parse("<proposed_plan>\n- step 1\n</proposed_plan>");
|
|
segments.extend(parser.finish());
|
|
|
|
assert_eq!(
|
|
segments,
|
|
vec![
|
|
ProposedPlanSegment::ProposedPlanStart,
|
|
ProposedPlanSegment::ProposedPlanDelta("- step 1\n".to_string()),
|
|
ProposedPlanSegment::ProposedPlanEnd,
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn strips_proposed_plan_blocks_from_text() {
|
|
let text = "before\n<proposed_plan>\n- step\n</proposed_plan>\nafter";
|
|
assert_eq!(strip_proposed_plan_blocks(text), "before\nafter");
|
|
}
|
|
}
|