From 7ac197318e16a1bc7c3253e6207b8e7ec5ec5e79 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Fri, 22 May 2026 22:48:30 -0300 Subject: [PATCH] fix(next-prompt): require stable suggestion history --- .../src/context/contextual_user_message.rs | 5 -- .../context/contextual_user_message_tests.rs | 7 +++ codex-rs/core/src/next_prompt_suggestion.rs | 9 ++++ .../core/src/next_prompt_suggestion_tests.rs | 52 +++++++++++++++++++ 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/codex-rs/core/src/context/contextual_user_message.rs b/codex-rs/core/src/context/contextual_user_message.rs index 72b5e631f8..79768dd3a3 100644 --- a/codex-rs/core/src/context/contextual_user_message.rs +++ b/codex-rs/core/src/context/contextual_user_message.rs @@ -10,7 +10,6 @@ use super::InternalModelContextFragment; use super::LegacyApplyPatchExecCommandWarning; use super::LegacyModelMismatchWarning; use super::LegacyUnifiedExecProcessLimitWarning; -use super::NextPromptSuggestionInstructions; use super::SkillInstructions; use super::SubagentNotification; use super::TurnAborted; @@ -43,9 +42,6 @@ static LEGACY_APPLY_PATCH_EXEC_COMMAND_WARNING_REGISTRATION: FragmentRegistratio static LEGACY_MODEL_MISMATCH_WARNING_REGISTRATION: FragmentRegistrationProxy< LegacyModelMismatchWarning, > = FragmentRegistrationProxy::new(); -static NEXT_PROMPT_SUGGESTION_INSTRUCTIONS_REGISTRATION: FragmentRegistrationProxy< - NextPromptSuggestionInstructions, -> = FragmentRegistrationProxy::new(); static CONTEXTUAL_USER_FRAGMENTS: &[&dyn FragmentRegistration] = &[ &USER_INSTRUCTIONS_REGISTRATION, @@ -59,7 +55,6 @@ static CONTEXTUAL_USER_FRAGMENTS: &[&dyn FragmentRegistration] = &[ &LEGACY_UNIFIED_EXEC_PROCESS_LIMIT_WARNING_REGISTRATION, &LEGACY_APPLY_PATCH_EXEC_COMMAND_WARNING_REGISTRATION, &LEGACY_MODEL_MISMATCH_WARNING_REGISTRATION, - &NEXT_PROMPT_SUGGESTION_INSTRUCTIONS_REGISTRATION, ]; fn is_standard_contextual_user_text(text: &str) -> bool { diff --git a/codex-rs/core/src/context/contextual_user_message_tests.rs b/codex-rs/core/src/context/contextual_user_message_tests.rs index dc7cb0f534..b354493872 100644 --- a/codex-rs/core/src/context/contextual_user_message_tests.rs +++ b/codex-rs/core/src/context/contextual_user_message_tests.rs @@ -90,6 +90,13 @@ fn ignores_regular_user_text() { })); } +#[test] +fn ignores_next_prompt_suggestion_tags_in_regular_user_text() { + assert!(!is_contextual_user_fragment(&ContentItem::InputText { + text: "\nrun the tests\n".to_string(), + })); +} + #[test] fn detects_hook_prompt_fragment_and_roundtrips_escaping() { let message = build_hook_prompt_message(&[HookPromptFragment::from_single_hook( diff --git a/codex-rs/core/src/next_prompt_suggestion.rs b/codex-rs/core/src/next_prompt_suggestion.rs index a544ce8568..925cf8bfa1 100644 --- a/codex-rs/core/src/next_prompt_suggestion.rs +++ b/codex-rs/core/src/next_prompt_suggestion.rs @@ -81,6 +81,10 @@ pub(crate) async fn suggest_next_prompt( return Ok(None); } let mut prompt_input = history.for_prompt(&turn_context.model_info.input_modalities); + if !history_ends_at_assistant_response(&prompt_input) { + tracing::debug!("next prompt suggestion skipped before assistant boundary"); + return Ok(None); + } if assistant_message_count(&prompt_input) < 2 { return Ok(None); } @@ -312,6 +316,10 @@ fn assistant_message_count(items: &[ResponseItem]) -> usize { .count() } +fn history_ends_at_assistant_response(items: &[ResponseItem]) -> bool { + matches!(items.last(), Some(ResponseItem::Message { role, .. }) if role == "assistant") +} + fn assistant_output_text(item: &ResponseItem) -> Option { let ResponseItem::Message { role, content, .. } = item else { return None; @@ -441,6 +449,7 @@ fn preset_supports_effort(preset: &ModelPreset, effort: ReasoningEffort) -> bool /// replies, assistant-voice phrasing, and sentence-like outputs are rejected so /// the UI only receives concise text the user could plausibly type verbatim. fn filter_next_prompt_suggestion(raw: &str) -> Option { + let raw = raw.trim(); let suggestion = raw.split_whitespace().collect::>().join(" "); if suggestion.is_empty() || raw.chars().any(|ch| matches!(ch, '\n' | '\r' | '\t')) diff --git a/codex-rs/core/src/next_prompt_suggestion_tests.rs b/codex-rs/core/src/next_prompt_suggestion_tests.rs index 7170aeeaf6..196e6cfd7b 100644 --- a/codex-rs/core/src/next_prompt_suggestion_tests.rs +++ b/codex-rs/core/src/next_prompt_suggestion_tests.rs @@ -1,6 +1,7 @@ use super::HistorySnapshot; use super::filter_next_prompt_suggestion; use super::has_unpaired_tool_flow; +use super::history_ends_at_assistant_response; use super::history_matches_snapshot; use super::suggestion_prompt_has_headroom; use crate::context_manager::ContextManager; @@ -18,6 +19,14 @@ fn filter_keeps_specific_prompt() { ); } +#[test] +fn filter_keeps_prompt_with_edge_whitespace() { + assert_eq!( + filter_next_prompt_suggestion(" run the tests\n"), + Some("run the tests".to_string()) + ); +} + #[test] fn filter_keeps_allowed_single_word_prompt() { assert_eq!( @@ -68,6 +77,49 @@ fn history_snapshot_detects_appends_and_rewrites() { assert!(!history_matches_snapshot(&history, appended_snapshot)); } +#[test] +fn history_boundary_requires_final_assistant_message() { + let assistant = ResponseItem::Message { + id: None, + role: "assistant".to_string(), + content: vec![ContentItem::OutputText { + text: "done".to_string(), + }], + phase: None, + }; + let user = ResponseItem::Message { + id: None, + role: "user".to_string(), + content: vec![ContentItem::InputText { + text: "next".to_string(), + }], + phase: None, + }; + + assert!(history_ends_at_assistant_response(std::slice::from_ref( + &assistant + ))); + assert!(!history_ends_at_assistant_response(&[assistant, user])); +} + +#[test] +fn history_boundary_rejects_tool_output_tail() { + assert!(!history_ends_at_assistant_response(&[ + ResponseItem::Message { + id: None, + role: "assistant".to_string(), + content: vec![ContentItem::OutputText { + text: "calling tool".to_string(), + }], + phase: None, + }, + ResponseItem::FunctionCallOutput { + call_id: "call-1".to_string(), + output: FunctionCallOutputPayload::from_text("done".to_string()), + }, + ])); +} + #[test] fn suggestion_prompt_skips_without_context_window() { assert!(!suggestion_prompt_has_headroom(