fix(next-prompt): require stable suggestion history

This commit is contained in:
Felipe Coury
2026-06-03 15:40:59 -03:00
parent 586c386cef
commit 7ac197318e
4 changed files with 68 additions and 5 deletions
@@ -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 {
@@ -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: "<next_prompt_suggestion>\nrun the tests\n</next_prompt_suggestion>".to_string(),
}));
}
#[test]
fn detects_hook_prompt_fragment_and_roundtrips_escaping() {
let message = build_hook_prompt_message(&[HookPromptFragment::from_single_hook(
@@ -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<String> {
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<String> {
let raw = raw.trim();
let suggestion = raw.split_whitespace().collect::<Vec<_>>().join(" ");
if suggestion.is_empty()
|| raw.chars().any(|ch| matches!(ch, '\n' | '\r' | '\t'))
@@ -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(