Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Render follow-up directive labels in the TUI and copied responses (#49089)

Eric Traut222e24b737e3
diff --git a/codex-rs/tui/src/assistant_directives.rs b/codex-rs/tui/src/assistant_directives.rs
index 9e89bf0e02..e8e9008359 100644
--- a/codex-rs/tui/src/assistant_directives.rs
+++ b/codex-rs/tui/src/assistant_directives.rs
@@ -15,6 +15,7 @@ use std::collections::BTreeMap;
#[derive(Debug, Eq, PartialEq)]
pub(crate) struct AssistantDirective<'source> {
pub(crate) name: &'source str,
+ pub(crate) label: Option<&'source str>,
pub(crate) attributes: BTreeMap<&'source str, Cow<'source, str>>,
pub(crate) raw: &'source str,
}
@@ -47,7 +48,7 @@ pub(crate) fn parse_assistant_directive_with_budget<'source>(
) -> Option<AssistantDirective<'source>> {
spend_scan_budget(remaining, /*scanned*/ 1)?;
// `::git-create-pr{...}` starts with a one-to-three-colon marker and a name;
- // require `{` immediately after the name so `::git-create-pr prose` is not parsed.
+ // Follow-ups additionally accept a `[label]` before their attributes.
let rest = source.trim_start_matches(':');
spend_scan_budget(remaining, source.len() - rest.len())?;
if !(1..=3).contains(&(source.len() - rest.len())) {
@@ -56,6 +57,63 @@ pub(crate) fn parse_assistant_directive_with_budget<'source>(
let name_len = rest.bytes().take_while(|byte| is_name_byte(*byte)).count();
spend_scan_budget(remaining, name_len + 1)?;
let (name, suffix) = rest.split_at(name_len);
+ let (label, suffix) = if let Some(label) = suffix.strip_prefix('[') {
+ if name != "codex-followup" {
+ return None;
+ }
+ let mut characters = label.char_indices().peekable();
+ let mut depth = 1;
+ let mut code_ticks = 0;
+ let mut code_start = None;
+ let end = loop {
+ let (index, character) = match characters.next() {
+ Some((_, '\n' | '\r')) | None => {
+ // An unmatched backtick run is literal Markdown. Replay its contents
+ // with the same scan budget so brackets and escapes regain their meaning.
+ characters = code_start.take()?;
+ code_ticks = 0;
+ continue;
+ }
+ Some(character) => character,
+ };
+ spend_scan_budget(remaining, character.len_utf8())?;
+ match character {
+ '`' => {
+ let mut ticks = 1;
+ while characters.next_if(|(_, next)| *next == '`').is_some() {
+ spend_scan_budget(remaining, /*scanned*/ 1)?;
+ ticks += 1;
+ }
+ if code_ticks == 0 {
+ code_ticks = ticks;
+ code_start = Some(characters.clone());
+ } else if code_ticks == ticks {
+ code_ticks = 0;
+ code_start = None;
+ }
+ }
+ _ if code_ticks > 0 => {}
+ '\\' => {
+ let (_, escaped) = characters.next()?;
+ spend_scan_budget(remaining, escaped.len_utf8())?;
+ if matches!(escaped, '\n' | '\r') {
+ return None;
+ }
+ }
+ '[' => depth += 1,
+ ']' => {
+ depth -= 1;
+ if depth == 0 {
+ break index;
+ }
+ }
+ _ => {}
+ }
+ };
+ (Some(&label[..end]), label[end..].strip_prefix(']')?)
+ } else {
+ (None, suffix)
+ };
let mut rest = suffix.strip_prefix('{')?;
if !name.as_bytes().first().is_some_and(u8::is_ascii_alphabetic) {
return None;
@@ -68,6 +126,7 @@ pub(crate) fn parse_assistant_directive_with_budget<'source>(
// In `::git-push{cwd="/repo"} done`, retain the directive but not ` done`.
return Some(AssistantDirective {
name,
+ label,
attributes,
raw: &source[..source.len() - suffix.len()],
});
diff --git a/codex-rs/tui/src/assistant_directives_tests.rs b/codex-rs/tui/src/assistant_directives_tests.rs
index 2babca6c7d..e1ad16408d 100644
--- a/codex-rs/tui/src/assistant_directives_tests.rs
+++ b/codex-rs/tui/src/assistant_directives_tests.rs
@@ -16,6 +16,7 @@ fn parses_git_receipts_with_unquoted_attributes() {
parse_assistant_directive(raw, QuoteEscaping::Literal),
Some(AssistantDirective {
name: "git-create-pr",
+ label: None,
attributes: BTreeMap::from([
("branch", Cow::Borrowed("feature/report")),
("cwd", Cow::Borrowed(r"/repo\")),
@@ -35,6 +36,7 @@ fn parses_triple_colon_comments_with_escaped_quotes_and_closing_braces() {
parse_assistant_directive(&source, QuoteEscaping::Backslash),
Some(AssistantDirective {
name: "code-comment",
+ label: None,
attributes: BTreeMap::from([
(
"body",
@@ -58,6 +60,7 @@ fn preserves_artifact_metadata_and_single_quoted_values() {
parse_assistant_directive(raw, QuoteEscaping::Backslash),
Some(AssistantDirective {
name: "artifact",
+ label: None,
attributes: BTreeMap::from([
("label", Cow::Owned("team's report".to_string())),
("path", Cow::Borrowed("Quarterly Report.xlsx")),
@@ -74,6 +77,9 @@ fn rejects_ambiguous_or_incomplete_directives() {
for source in [
r#"::git-push{cwd="/repo" cwd="/other"}"#,
r#"::git-push{cwd="/repo" branch="feature""#,
+ "::git-push[anything]{cwd=/repo branch=main}",
+ "::code-comment[anything]{body=comment file=foo}",
+ ":codex-file-citation[caption]{path=foo}",
"::::code-comment{title=comment}",
":artifact{path=/tmp/a path=:artifact{path=/tmp/b}}",
":artifact{invalid:key=:artifact{path=/tmp/a}}",
diff --git a/codex-rs/tui/src/chatwidget/tests/copy_export_picker_tests.rs b/codex-rs/tui/src/chatwidget/tests/copy_export_picker_tests.rs
index 58e4fb6257..ad9631c072 100644
--- a/codex-rs/tui/src/chatwidget/tests/copy_export_picker_tests.rs
+++ b/codex-rs/tui/src/chatwidget/tests/copy_export_picker_tests.rs
@@ -5,6 +5,35 @@ use crate::app_event::TranscriptExportDestination;
use crate::clipboard_copy::CopyFormat;
use pretty_assertions::assert_eq;
+#[tokio::test]
+async fn whole_response_copy_uses_followup_labels() {
+ let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
+ let directive = r#":codex-followup[**Inspect items[0]**]{prompt="private"}"#;
+ let literal =
+ format!("\n\n`{directive}`\n\n```text\n{directive}\n```\n\n:codex-followup[unfinished");
+ let source = format!("- {directive}{literal}");
+ let expected = format!("- **Inspect items[0]**{literal}");
+ replay_agent_message(
+ &mut chat,
+ "followups",
+ source.clone(),
+ ReplayKind::ThreadSnapshot,
+ );
+ let shortcut = match chat.prepare_last_response_copy() {
+ crate::chatwidget::KeyEventAction::CopyLastResponse(text) => Some(text.to_string()),
+ _ => None,
+ };
+ assert_eq!(shortcut, Some(expected.clone()));
+ chat.show_copy_picker();
+ chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
+ let copied = std::iter::from_fn(|| rx.try_recv().ok()).find_map(|event| match event {
+ AppEvent::CopySelection { text, format, .. } => Some((text.to_string(), format)),
+ _ => None,
+ });
+ assert_eq!(copied, Some((expected, CopyFormat::Markdown)));
+ assert_eq!(chat.transcript.last_agent_source, Some(source));
+}
+
#[tokio::test]
async fn completed_response_copy_preserves_markdown_line_endings() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
diff --git a/codex-rs/tui/src/chatwidget/transcript.rs b/codex-rs/tui/src/chatwidget/transcript.rs
index 72489d49d5..f055cd1b7b 100644
--- a/codex-rs/tui/src/chatwidget/transcript.rs
+++ b/codex-rs/tui/src/chatwidget/transcript.rs
@@ -91,7 +91,8 @@ impl TranscriptState {
pub(super) fn record_agent_markdown(&mut self, markdown: String, source: String) {
self.last_status_copy_targets = None;
- self.last_agent_markdown = Some(markdown);
+ self.last_agent_markdown =
+ Some(crate::markdown_render::followup_labels(&markdown).into_owned());
self.last_agent_source = Some(source);
self.saw_copy_source_this_turn = true;
}
diff --git a/codex-rs/tui/src/markdown_render.rs b/codex-rs/tui/src/markdown_render.rs
index 09d03c38c2..6c129f3b76 100644
--- a/codex-rs/tui/src/markdown_render.rs
+++ b/codex-rs/tui/src/markdown_render.rs
@@ -73,7 +73,7 @@ use std::ops::Range;
use std::path::Path;
use std::path::PathBuf;
-mod file_citations;
+mod inline_directives;
mod list_spacing;
mod local_links;
mod math;
@@ -85,7 +85,8 @@ mod table_key_value;
mod task_lists;
mod web_links;
-use file_citations::FileCitations;
+use inline_directives::InlineDirectives;
+pub(crate) use inline_directives::followup_labels;
pub(crate) use list_spacing::ListSpacing;
use list_spacing::UniformList;
use local_links::is_local_path_like_link;
diff --git a/codex-rs/tui/src/markdown_render/followups_tests.rs b/codex-rs/tui/src/markdown_render/followups_tests.rs
new file mode 100644
index 0000000000..194b93be67
--- /dev/null
+++ b/codex-rs/tui/src/markdown_render/followups_tests.rs
@@ -0,0 +1,147 @@
+//! Follow-up labels keep Markdown styling and stream offsets without exposing prompts.
+
+use super::super::ListSpacing;
+use super::super::render_streaming_markdown_lines_with_width_and_cwd;
+use crate::markdown::render_markdown_agent_with_links_and_cwd;
+use crate::terminal_hyperlinks::visible_lines;
+use pretty_assertions::assert_eq;
+use ratatui::text::Text;
+
+#[test]
+fn followup_labels_render_like_ordinary_markdown_snapshot() {
+ let source = concat!(
+ "Next actions:\n\n",
+ "- :codex-followup[Add **revenue charts**]{prompt=\"Add a \\\"Q3\\\" chart with } labels\"}\n",
+ "- :codex-followup[Format `Sheet1`]{prompt=\"Format the spreadsheet\"}\n\n",
+ "Or :codex-followup[explain café totals]{prompt=\"Explain $x$ and *all* totals\"}.\n",
+ );
+ let plain =
+ "Next actions:\n\n- Add **revenue charts**\n- Format `Sheet1`\n\nOr explain café totals.\n";
+ let streamed = render_streaming_markdown_lines_with_width_and_cwd(
+ source,
+ Some(32),
+ /*cwd*/ None,
+ &|_| false,
+ ListSpacing::AfterMultiline,
+ );
+ let rendered = Text::from(visible_lines(streamed.lines));
+ assert_eq!(
+ rendered,
+ Text::from(visible_lines(render_markdown_agent_with_links_and_cwd(
+ plain,
+ Some(32),
+ /*cwd*/ None
+ )))
+ );
+ assert_eq!(streamed.last_top_level_block_start, source.find("Or "));
+ insta::assert_snapshot!(rendered.to_string(), @"
+ Next actions:
+
+ • Add revenue charts
+ • Format Sheet1
+
+ Or explain café totals.
+ ");
+}
+
+#[test]
+fn followups_preserve_literal_and_malformed_source() {
+ let directive = r#":codex-followup[Add charts]{prompt="Add charts"}"#;
+ for source in [
+ format!("`{directive}`"),
+ format!("```text\n{directive}\n```"),
+ format!(r"\{directive}"),
+ format!("<!-- {directive} -->"),
+ format!("[{directive}](https://example.com)"),
+ ":codex-followup[Add charts]{prompt=\"unfinished".to_string(),
+ ":codex-followup[Add charts".to_string(),
+ ":codex-followup[Add charts]{prompt=one prompt=two}".to_string(),
+ ] {
+ assert_eq!(
+ super::InlineDirectives::new(&source, pulldown_cmark::Options::empty()).markdown,
+ source
+ );
+ }
+}
+
+#[test]
+fn followups_do_not_consume_neighbors_or_nested_directives() {
+ let source = concat!(
+ "&amp; :codex-followup[**First**]{prompt=\"Use :codex-file-citation{path=\\\"/tmp/private\\\"}\"} ",
+ ":codex-followup[*Second*]{prompt=\"Then continue\"} &copy;\n",
+ );
+ assert_eq!(
+ render_markdown_agent_with_links_and_cwd(source, /*width*/ None, /*cwd*/ None),
+ render_markdown_agent_with_links_and_cwd(
+ "&amp; **First** *Second* &copy;\n",
+ /*width*/ None,
+ /*cwd*/ None
+ ),
+ );
+ let source = format!("{source}\n:codex-file-citation{{path=\"/tmp/report.pdf\"}}\n");
+ let rendered = crate::markdown::render_markdown_agent_with_links_and_cwd(
+ &source, /*width*/ None, /*cwd*/ None,
+ );
+ assert_eq!(
+ Text::from(visible_lines(rendered)).to_string(),
+ "& First Second ©\n\n/tmp/report.pdf"
+ );
+}
+
+#[test]
+fn followup_labels_cannot_introduce_blocks() {
+ for label in [
+ "# Heading",
+ "- List item",
+ "---",
+ " Indented",
+ "~~Old~~ new",
+ "Inspect `items[0]`",
+ "Inspect ``items[`key`]``",
+ "Type a ` character",
+ "Type a ` character and ``items]``",
+ r"Type a \` character",
+ "Inspect [nested [labels]]",
+ r"Inspect \] and \[ brackets",
+ ] {
+ let source = format!("Before :codex-followup[{label}]{{prompt=action}} after.");
+ let plain = format!("Before {label} after.");
+ assert_eq!(
+ render_markdown_agent_with_links_and_cwd(
+ &source, /*width*/ None, /*cwd*/ None
+ ),
+ render_markdown_agent_with_links_and_cwd(
+ &plain, /*width*/ None, /*cwd*/ None
+ ),
+ );
+ }
+}
+
+#[test]
+fn unmatched_label_backticks_render_and_copy_as_literal_text() {
+ let source = ":codex-followup[Type a ` character]{prompt=action}\n";
+ insta::assert_snapshot!(
+ Text::from(visible_lines(render_markdown_agent_with_links_and_cwd(
+ source, /*width*/ None, /*cwd*/ None
+ ))).to_string(),
+ @"Type a ` character"
+ );
+ assert_eq!(super::followup_labels(source), "Type a ` character\n");
+}
+
+#[test]
+fn malformed_directives_leave_budget_for_followups() {
+ for prefix in [":x0[:x1[", ":codex-followup[` "] {
+ let source = format!("{prefix}:codex-followup[Open]{{prompt=secret}}");
+ let expected = format!("{prefix}Open");
+ assert_eq!(super::followup_labels(&source), expected);
+ assert_eq!(
+ render_markdown_agent_with_links_and_cwd(
+ &source, /*width*/ None, /*cwd*/ None
+ ),
+ render_markdown_agent_with_links_and_cwd(
+ &expected, /*width*/ None, /*cwd*/ None
+ ),
+ );
+ }
+}
diff --git a/codex-rs/tui/src/markdown_render/file_citations.rs b/codex-rs/tui/src/markdown_render/inline_directives.rs
similarity index 61%
rename from codex-rs/tui/src/markdown_render/file_citations.rs
rename to codex-rs/tui/src/markdown_render/inline_directives.rs
index afac14f9b8..ea757f183d 100644
--- a/codex-rs/tui/src/markdown_render/file_citations.rs
+++ b/codex-rs/tui/src/markdown_render/inline_directives.rs
@@ -1,4 +1,6 @@
-//! Prepare file citations once, then feed them to the ordinary Markdown link renderer.
+//! Render file citations as links and follow-up directives as labels.
+//!
+//! Preserve source offsets and literal Markdown boundaries for streaming and completed messages.
use super::local_links::extract_colon_location_suffix;
use super::local_links::is_local_path_like_link;
@@ -16,21 +18,48 @@ use std::borrow::Cow;
use std::ops::Range;
use std::path::Path;
-/// Offset-preserving Markdown plus the original, fully parsed citation metadata.
-pub(super) struct FileCitations<'a> {
+/// Preserve source Markdown when copying, replacing only recognized follow-up directives.
+pub(crate) fn followup_labels(input: &str) -> Cow<'_, str> {
+ if !input.contains(":codex-followup[") {
+ return Cow::Borrowed(input);
+ }
+ let prepared = InlineDirectives::new(
+ input,
+ Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TABLES | Options::ENABLE_TASKLISTS,
+ );
+ let mut copied = String::new();
+ let mut offset = 0;
+ for (range, directive) in prepared.directives {
+ if directive.name == "codex-followup"
+ && let Some(label) = directive.label
+ {
+ copied.push_str(&input[offset..range.start]);
+ copied.push_str(label);
+ offset = range.end;
+ }
+ }
+ if offset == 0 {
+ return Cow::Borrowed(input);
+ }
+ copied.push_str(&input[offset..]);
+ Cow::Owned(copied)
+}
+
+/// Offset-preserving Markdown plus the original, fully parsed directive metadata.
+pub(super) struct InlineDirectives<'a> {
input: &'a str,
pub(super) markdown: Cow<'a, str>,
- citations: Vec<(Range<usize>, AssistantDirective<'a>)>,
+ directives: Vec<(Range<usize>, AssistantDirective<'a>)>,
}
-impl<'a> FileCitations<'a> {
+impl<'a> InlineDirectives<'a> {
pub(super) fn new(input: &'a str, options: Options) -> Self {
let mut prepared = Self {
input,
markdown: Cow::Borrowed(input),
- citations: Vec::new(),
+ directives: Vec::new(),
};
- if !input.contains("codex-file-citation") {
+ if !input.contains("codex-file-citation") && !input.contains(":codex-followup[") {
return prepared;
}
@@ -55,8 +84,9 @@ impl<'a> FileCitations<'a> {
let mut literal_ranges = literal_ranges.into_iter().peekable();
let mut directive_end = 0;
- // Share the allowance across offsets and quote modes: malformed retries must stay linear.
- let mut scan_budget = input.len().saturating_mul(/*rhs*/ 4);
+ // Share separate preferred/fallback allowances across offsets, so fallback retries
+ // cannot starve later preferred parses. Both allowances stay proportional to input size.
+ let mut scan_budget = [input.len().saturating_mul(/*rhs*/ 4); 2];
for (start, _) in input.match_indices(':') {
while literal_ranges.next_if(|range| range.end <= start).is_some() {}
if start < directive_end
@@ -77,9 +107,14 @@ impl<'a> FileCitations<'a> {
} else {
[QuoteEscaping::Backslash, QuoteEscaping::Literal]
};
- let Some(directive) = escaping.into_iter().find_map(|escaping| {
- parse_assistant_directive_with_budget(source, escaping, &mut scan_budget)
- }) else {
+ let Some(directive) =
+ escaping
+ .into_iter()
+ .zip(&mut scan_budget)
+ .find_map(|(escaping, remaining)| {
+ parse_assistant_directive_with_budget(source, escaping, remaining)
+ })
+ else {
continue;
};
let end = start + directive.raw.len();
@@ -91,18 +126,24 @@ impl<'a> FileCitations<'a> {
.count()
% 2
!= 0
- || directive.name != "codex-file-citation"
- || directive
- .attributes
- .get("path")
- .is_none_or(|path| path.is_empty())
+ || match directive.name {
+ "codex-file-citation" => directive
+ .attributes
+ .get("path")
+ .is_none_or(|path| path.is_empty()),
+ "codex-followup" => {
+ !directive.raw.starts_with(":codex-followup[")
+ || directive.label.is_none_or(|label| label.trim().is_empty())
+ }
+ _ => true,
+ }
{
continue;
}
// Mask the interior without moving offsets or changing Markdown delimiter flanking.
let markdown = prepared.markdown.to_mut();
markdown.replace_range(start + 1..end - 1, &"x".repeat(end - start - 2));
- prepared.citations.push((start..end, directive));
+ prepared.directives.push((start..end, directive));
}
prepared
}
@@ -113,24 +154,24 @@ impl<'a> FileCitations<'a> {
events: impl Iterator<Item = (Event<'s>, Range<usize>)>,
cwd: Option<&'s Path>,
) -> impl Iterator<Item = (Event<'s>, Range<usize>)> {
- let mut citations = self.citations.iter().peekable();
+ let mut directives = self.directives.iter().peekable();
events.flat_map(move |(event, range)| {
- while citations
+ while directives
.next_if(|(span, _)| span.end <= range.start)
.is_some()
{}
let Event::Text(text) = event else {
return Either::Left(std::iter::once((event, range)));
};
- if citations
+ if directives
.peek()
.is_none_or(|(span, _)| span.start >= range.end)
{
return Either::Left(std::iter::once((Event::Text(text), range)));
}
- // Never apply source offsets to entity-decoded text or a partial citation.
+ // Never apply source offsets to entity-decoded text or a partial directive.
if text.as_ref() != &self.markdown[range.clone()]
- || citations
+ || directives
.peek()
.is_some_and(|(span, _)| span.start < range.start || span.end > range.end)
{
@@ -140,7 +181,8 @@ impl<'a> FileCitations<'a> {
let mut events = Vec::new();
let mut offset = range.start;
- while let Some((span, directive)) = citations.next_if(|(span, _)| span.end <= range.end)
+ while let Some((span, directive)) =
+ directives.next_if(|(span, _)| span.end <= range.end)
{
if offset < span.start {
events.push((
@@ -148,6 +190,31 @@ impl<'a> FileCitations<'a> {
offset..span.start,
));
}
+ offset = span.end;
+ if let Some(label) = directive
+ .label
+ .filter(|_| directive.name == "codex-followup")
+ {
+ // Keep the unmatched `[` as a sentinel so labels cannot introduce blocks.
+ let inline = &directive.raw
+ [":codex-followup".len()..":codex-followup[".len() + label.len()];
+ events.extend(
+ Parser::new_ext(inline, Options::ENABLE_STRIKETHROUGH)
+ .into_offset_iter()
+ .filter_map(|(event, range)| {
+ let event = match event {
+ Event::Start(Tag::Paragraph)
+ | Event::End(TagEnd::Paragraph) => return None,
+ Event::Text(text) if range.start == 0 => {
+ Event::Text(text[1..].to_owned().into())
+ }
+ event => event,
+ };
+ Some((event, span.clone()))
+ }),
+ );
+ continue;
+ }
let path = directive.attributes["path"].as_ref();
let destination = if is_local_path_like_link(path) {
path.to_string()
@@ -180,7 +247,6 @@ impl<'a> FileCitations<'a> {
(Event::Text(destination.into()), span.clone()),
(Event::End(TagEnd::Link), span.clone()),
]);
- offset = span.end;
}
if offset < range.end {
events.push((
@@ -196,3 +262,7 @@ impl<'a> FileCitations<'a> {
#[cfg(test)]
#[path = "file_citations_tests.rs"]
mod tests;
+
+#[cfg(test)]
+#[path = "followups_tests.rs"]
+mod followups_tests;
diff --git a/codex-rs/tui/src/markdown_render/streaming.rs b/codex-rs/tui/src/markdown_render/streaming.rs
index e306fa3d40..9a5641a784 100644
--- a/codex-rs/tui/src/markdown_render/streaming.rs
+++ b/codex-rs/tui/src/markdown_render/streaming.rs
@@ -5,8 +5,8 @@
use super::DecodedTextMerge;
use super::Event;
-use super::FileCitations;
use super::HyperlinkLine;
+use super::InlineDirectives;
use super::ListSpacing;
use super::Options;
use super::Parser;
@@ -50,14 +50,14 @@ pub(crate) fn render_streaming_markdown_lines_with_width_and_cwd(
Options::ENABLE_TASKLISTS,
super::preferences::current().lists,
);
- let citations = FileCitations::new(input, options);
- let math = MathMarkdown::new(&citations.markdown, options, width);
+ let directives = InlineDirectives::new(input, options);
+ let math = MathMarkdown::new(&directives.markdown, options, width);
let parser = Parser::new_ext(&math.markdown, options);
let has_reference_link_definition = parser.reference_definitions().iter().next().is_some();
let parser = TopLevelBlockTracker {
iter: DecodedTextMerge::new(super::source_tables::preserve(
input,
- citations.events(math.events(parser.into_offset_iter()), cwd),
+ directives.events(math.events(parser.into_offset_iter()), cwd),
)),
depth: 0,
block_count: 0,
diff --git a/codex-rs/tui/src/streaming/controller_preview_tests.rs b/codex-rs/tui/src/streaming/controller_preview_tests.rs
index bad0220331..45268f6fe5 100644
--- a/codex-rs/tui/src/streaming/controller_preview_tests.rs
+++ b/codex-rs/tui/src/streaming/controller_preview_tests.rs
@@ -2,6 +2,180 @@ use super::*;
use crate::terminal_hyperlinks::visible_lines;
use pretty_assertions::assert_eq;
+#[test]
+fn followup_preview_hides_prompts_until_complete() {
+ let cwd = std::env::temp_dir();
+ let mut controller = StreamController::new(Some(40), &cwd, HistoryRenderMode::Rich);
+ controller.push("Before ");
+ let prefix = controller.current_tail_lines();
+ controller.push(":codex-followup[");
+ for character in r#"Inspect `items[0]`]{prompt="Use \\"}\" and `ticks`""#.chars() {
+ controller.push(&character.to_string());
+ assert_eq!(controller.current_tail_lines(), prefix);
+ }
+ insta::assert_debug_snapshot!(visible_lines(controller.current_tail_lines()), @r#"
+ [
+ Line::from("Before"),
+ ]
+ "#);
+ controller.push("} after.");
+ assert_eq!(
+ controller.current_tail_lines(),
+ render_source(
+ "Before Inspect `items[0]` after.",
+ Some(40),
+ &cwd,
+ HistoryRenderMode::Rich,
+ /*inline_visualization_context*/ None
+ ),
+ );
+ let mut controller = StreamController::new(Some(40), &cwd, HistoryRenderMode::Rich);
+ controller.push("Before ");
+ let prefix = controller.current_tail_lines();
+ controller.push(&format!(
+ ":codex-followup[Long]{{prompt=\"{}",
+ "界".repeat(3000)
+ ));
+ controller.push("more prompt");
+ assert_eq!(controller.current_tail_lines(), prefix);
+ controller.push("\"}");
+ insta::assert_debug_snapshot!(visible_lines(controller.current_tail_lines()), @r#"
+ [
+ Line::from("…"),
+ ]
+ "#);
+ controller.push(" after.");
+ assert_eq!(
+ visible_lines(controller.current_tail_lines()),
+ vec![Line::from("…"), Line::from("after.")]
+ );
+}
+
+#[test]
+fn followup_preview_resumes_after_the_directive_leaves_the_window() {
+ let cwd = std::env::temp_dir();
+ let mut controller = StreamController::new(Some(40), &cwd, HistoryRenderMode::Rich);
+ controller.push(":codex-followup[First]{prompt=secret}");
+ let prose = "ordinary prose ".repeat(700);
+ controller.push(&prose);
+ controller.push(":codex-followup[Second]{prompt=hidden} after.");
+ let rendered = visible_lines(controller.current_tail_lines());
+ let text = rendered
+ .iter()
+ .map(Line::to_string)
+ .collect::<Vec<_>>()
+ .join(" ");
+ assert!(text.ends_with("Second after."), "{text}");
+ assert!(!text.contains("prompt="), "{text}");
+ assert_eq!(rendered.first(), Some(&Line::from("…")));
+}
+
+#[test]
+fn followup_preview_only_opens_quotes_at_value_boundaries() {
+ let cwd = std::env::temp_dir();
+ for attributes in [
+ "prompt=don't",
+ "prompt=foo='bar",
+ "prompt=don't other = \"quote } here\"",
+ "prompt = 'quote } here'other=don't",
+ ] {
+ let mut controller = StreamController::new(Some(40), &cwd, HistoryRenderMode::Rich);
+ controller.push("Before ");
+ let prefix = controller.current_tail_lines();
+ controller.push(":codex-followup[");
+ for character in format!("Open]{{{attributes}").chars() {
+ controller.push(&character.to_string());
+ assert_eq!(controller.current_tail_lines(), prefix);
+ }
+ controller.push("} after.");
+ assert_eq!(
+ visible_lines(controller.current_tail_lines()),
+ vec![Line::from("Before Open after.")],
+ "{attributes}",
+ );
+ }
+}
+
+#[test]
+fn followup_preview_handles_surrounding_unfinished_markdown() {
+ let cwd = std::env::temp_dir();
+ let mut controller = StreamController::new(Some(40), &cwd, HistoryRenderMode::Rich);
+ controller.push("[unfinished ");
+ let prefix = controller.current_tail_lines();
+ controller.push(":codex-followup[Inspect]{prompt=\"secret\"");
+ assert_eq!(controller.current_tail_lines(), prefix);
+ controller.push("}");
+ assert_eq!(
+ visible_lines(controller.current_tail_lines()),
+ vec![Line::from("[unfinished Inspect")]
+ );
+
+ for source in [
+ ":codex-followup[not a directive] continuing text",
+ ":codex-followup[not a directive] `code`",
+ ] {
+ let mut controller = StreamController::new(Some(80), &cwd, HistoryRenderMode::Rich);
+ for character in source.chars() {
+ controller.push(&character.to_string());
+ }
+ assert_eq!(
+ controller.current_tail_lines(),
+ render_source(
+ source,
+ Some(80),
+ &cwd,
+ HistoryRenderMode::Rich,
+ /*inline_visualization_context*/ None
+ )
+ );
+ }
+}
+
+#[test]
+fn unmatched_code_openers_do_not_expose_followup_prompts() {
+ let cwd = std::env::temp_dir();
+ let mut controller = StreamController::new(Some(80), &cwd, HistoryRenderMode::Rich);
+ controller.push("Type ` then ");
+ let prefix = controller.current_tail_lines();
+ controller.push(":codex-followup[");
+ for character in "Open]{prompt=secret}".chars() {
+ controller.push(&character.to_string());
+ assert_eq!(controller.current_tail_lines(), prefix);
+ }
+ insta::assert_debug_snapshot!(visible_lines(controller.current_tail_lines()), @r#"
+ [
+ Line::from("Type ` then"),
+ ]
+ "#);
+ // A matching closer establishes literal code; its full contents can then be previewed.
+ controller.push("`");
+ assert_eq!(
+ controller.current_tail_lines(),
+ render_source(
+ "Type ` then :codex-followup[Open]{prompt=secret}`",
+ Some(80),
+ &cwd,
+ HistoryRenderMode::Rich,
+ /*inline_visualization_context*/ None,
+ ),
+ );
+
+ // Extending a tentative closing run must restore the old, safely cropped preview.
+ let mut controller = StreamController::new(Some(80), &cwd, HistoryRenderMode::Rich);
+ controller.push(&format!(
+ ":codex-followup[Long]{{prompt={}}} ` then ",
+ "x".repeat(9000)
+ ));
+ let prefix = controller.current_tail_lines();
+ controller.push(&format!(
+ ":codex-followup[Open]{{prompt={}}}",
+ "y".repeat(9000)
+ ));
+ controller.push("`");
+ controller.push("`");
+ assert_eq!(controller.current_tail_lines(), prefix);
+}
+
#[test]
fn unfinished_link_destinations_do_not_expand_the_preview() {
let cwd = std::env::temp_dir();
@@ -46,39 +220,46 @@ fn unfinished_link_destinations_do_not_expand_the_preview() {
#[test]
fn unfinished_links_flush_on_newline_or_completion() {
let cwd = std::env::temp_dir();
- let source = "See [documentation](https://example.com/a/very/long/path";
- for ending in ["", "\n"] {
- let mut controller = StreamController::new(Some(24), &cwd, HistoryRenderMode::Rich);
- controller.push(source);
- insta::assert_debug_snapshot!(
- "unfinished_link_preview",
- visible_lines(controller.current_tail_lines()),
- );
- controller.set_width(Some(32));
- assert_eq!(
- visible_lines(controller.current_tail_lines()),
- vec![Line::from("See [documentation]")],
- );
- controller.push(ending);
- let (queued, _) = controller.on_commit_tick_batch(usize::MAX);
- let (remaining, finalized_source) = controller.finalize();
- let displayed = queued
- .into_iter()
- .chain(remaining)
- .flat_map(|cell| cell.display_lines(/*width*/ 34))
- .collect::<Vec<_>>();
- let expected = history_cell::AgentMessageCell::new_hyperlink_lines(
- render_source(
- source,
- Some(32),
- &cwd,
- HistoryRenderMode::Rich,
- /*inline_visualization_context*/ None,
- ),
- /*is_first_line*/ true,
- );
- assert_eq!(displayed, expected.display_lines(/*width*/ 34));
- assert_eq!(finalized_source, Some(format!("{source}\n")));
+ for (source, prefix) in [
+ (
+ "See [documentation](https://example.com/a/very/long/path",
+ "See [documentation]",
+ ),
+ ("See :codex-followup[Inspect]{prompt=\"unfinished", "See"),
+ ] {
+ for ending in ["", "\n"] {
+ let mut controller = StreamController::new(Some(24), &cwd, HistoryRenderMode::Rich);
+ controller.push(source);
+ assert_eq!(
+ visible_lines(controller.current_tail_lines()),
+ vec![Line::from(prefix)]
+ );
+ controller.set_width(Some(32));
+ assert_eq!(
+ visible_lines(controller.current_tail_lines()),
+ vec![Line::from(prefix)],
+ );
+ controller.push(ending);
+ let (queued, _) = controller.on_commit_tick_batch(usize::MAX);
+ let (remaining, finalized_source) = controller.finalize();
+ let displayed = queued
+ .into_iter()
+ .chain(remaining)
+ .flat_map(|cell| cell.display_lines(/*width*/ 34))
+ .collect::<Vec<_>>();
+ let expected = history_cell::AgentMessageCell::new_hyperlink_lines(
+ render_source(
+ source,
+ Some(32),
+ &cwd,
+ HistoryRenderMode::Rich,
+ /*inline_visualization_context*/ None,
+ ),
+ /*is_first_line*/ true,
+ );
+ assert_eq!(displayed, expected.display_lines(/*width*/ 34));
+ assert_eq!(finalized_source, Some(format!("{source}\n")));
+ }
}
}
@@ -91,6 +272,8 @@ fn link_preview_respects_code_escapes_and_raw_mode() {
"See `[label](https://example.com/unfinished`",
"See ``[la`bel](https://example.com/unfinished``",
"See [label](https://example.com/complete) and prose",
+ r"See \:codex-followup[label]{prompt=unfinished",
+ "See `:codex-followup[label]{prompt=unfinished`",
] {
let mut controller = StreamController::new(Some(24), &cwd, HistoryRenderMode::Rich);
for character in source.chars() {
diff --git a/codex-rs/tui/src/streaming/prose_preview.rs b/codex-rs/tui/src/streaming/prose_preview.rs
index ba464101ab..19ee271f23 100644
--- a/codex-rs/tui/src/streaming/prose_preview.rs
+++ b/codex-rs/tui/src/streaming/prose_preview.rs
@@ -1,7 +1,8 @@
//! Bounded, disposable previews of unterminated prose. Preview lines never enter scrollback;
//! newline commitment and finalization render the original source independently. Math previews
//! show wrapped source until closure, without interpreting TeX punctuation as Markdown.
-//! Rich prose withholds unfinished link destinations so closing a link cannot collapse URL rows.
+//! Rich prose withholds unfinished link destinations and follow-up directives until closure.
+//! Cropped previews skip completed directives atomically so their prompt text stays hidden.
use super::render::render_source;
use crate::history_cell::HistoryRenderMode;
@@ -9,6 +10,8 @@ use crate::inline_visualization::InlineVisualizationContext;
use crate::terminal_hyperlinks::HyperlinkLine;
use crate::terminal_hyperlinks::lines_with_sources_eq;
use ratatui::text::Line;
+use std::collections::VecDeque;
+use std::ops::Range;
use std::path::Path;
const MAX_PREVIEW_BYTES: usize = 8192;
@@ -40,31 +43,64 @@ impl ProsePreview {
) -> bool {
// Only scan newly arrived bytes, including on very long single-line responses.
self.has_pipe |= source[self.scanned_len..].contains('|');
- self.link.scan(source, self.scanned_len);
- self.scanned_len = source.len();
// Indented and quoted lines may belong to nested code blocks. Keep their
// existing newline holdback instead of guessing at the missing block context.
- if matches!(mode, PreviewMode::Math)
+ let advances = matches!(mode, PreviewMode::Math)
|| !(self.has_pipe
|| source.starts_with([' ', '\t', '>'])
|| source.starts_with("```")
|| source.starts_with("~~~")
- || matches!(source, "`" | "``" | "~" | "~~"))
- {
+ || matches!(source, "`" | "``" | "~" | "~~"));
+ // Keep tentative fence delimiters in the scanner, but stop collecting ranges once
+ // block structure freezes this tail until newline commitment.
+ if advances || source.len() <= 2 {
+ self.link.scan(source, self.scanned_len);
+ }
+ if advances {
self.safe_len = source.len();
self.rich_safe_len = source.len();
}
+ self.scanned_len = source.len();
// Keep Raw/Math's structural bound independent of Rich-only link holdback.
if let Some((start, _)) = self.link.destination {
self.rich_safe_len = self.rich_safe_len.min(start);
}
+ if let Some(start) = self.link.followup_start {
+ self.rich_safe_len = self.rich_safe_len.min(start);
+ }
+ if let Some(start) = self.link.code_followup_start
+ && self.link.pending_ticks != self.link.code_ticks
+ {
+ self.rich_safe_len = self.rich_safe_len.min(start);
+ }
// Retain the last safe text when tokens reveal structure, but still reflow it.
let source = &source[..if matches!(mode, PreviewMode::Prose(HistoryRenderMode::Rich)) {
self.rich_safe_len
} else {
self.safe_len
}];
- let start = source.ceil_char_boundary(source.len().saturating_sub(MAX_PREVIEW_BYTES));
+ let mut start = source.ceil_char_boundary(source.len().saturating_sub(MAX_PREVIEW_BYTES));
+ // A tentative closing backtick run can grow on the next update. Retain the old
+ // window's directive boundaries until a non-backtick confirms that the span closed.
+ let retained_len = self
+ .link
+ .code_followup_start
+ .map_or(self.rich_safe_len, |start| start.min(self.rich_safe_len));
+ while self
+ .link
+ .followups
+ .front()
+ .is_some_and(|range| range.end <= retained_len.saturating_sub(MAX_PREVIEW_BYTES))
+ {
+ self.link.followups.pop_front();
+ }
+ // A rolling preview may skip a completed directive, but must never start in its prompt.
+ if matches!(mode, PreviewMode::Prose(HistoryRenderMode::Rich))
+ && let Some(range) = self.link.followups.iter().find(|range| range.end > start)
+ && range.start < start
+ {
+ start = range.end;
+ }
let mut lines = match mode {
PreviewMode::Prose(render_mode) => render_source(
&source[start..],
@@ -101,11 +137,57 @@ struct LinkPreview {
escaped: bool,
code_ticks: usize,
pending_ticks: usize,
+ code_followup_start: Option<usize>,
+ followup_start: Option<usize>,
+ followup_brackets: usize,
+ followups: VecDeque<Range<usize>>,
+ followup_attributes: Option<AttributeState>,
+}
+
+enum AttributeState {
+ Key,
+ ValueStart,
+ Unquoted,
+ Quoted(u8),
}
impl LinkPreview {
fn scan(&mut self, source: &str, start: usize) {
for (index, byte) in source.bytes().enumerate().skip(start) {
+ if self.followup_start.is_some()
+ && self.closed_label
+ && self.brackets == self.followup_brackets
+ && byte != b'{'
+ {
+ self.followup_start = None;
+ }
+ if let Some(state) = self.followup_attributes.as_mut() {
+ if let AttributeState::Quoted(quote) = state {
+ if byte == *quote && !self.escaped {
+ *state = AttributeState::Key;
+ }
+ // Match the directive parser: only a backslash before the quote escapes it.
+ self.escaped = byte == b'\\';
+ } else if byte == b'}' {
+ if let Some(start) = self.followup_start.take() {
+ self.followups.push_back(start..index + 1);
+ }
+ self.followup_attributes = None;
+ } else {
+ *state = match (&*state, byte) {
+ (AttributeState::Key, b'=') => AttributeState::ValueStart,
+ (AttributeState::ValueStart, b'\'' | b'"') => AttributeState::Quoted(byte),
+ (AttributeState::ValueStart, b' ' | b'\t') => AttributeState::ValueStart,
+ (AttributeState::ValueStart, _) => AttributeState::Unquoted,
+ (AttributeState::Unquoted, b' ' | b'\t') => AttributeState::Key,
+ (AttributeState::Unquoted, _) => AttributeState::Unquoted,
+ (AttributeState::Key, _) | (AttributeState::Quoted(_), _) => {
+ AttributeState::Key
+ }
+ };
+ }
+ continue;
+ }
if self.destination.is_none() {
if byte == b'`' && (self.code_ticks > 0 || !self.escaped) {
self.pending_ticks += 1;
@@ -117,10 +199,19 @@ impl LinkPreview {
self.code_ticks = self.pending_ticks;
} else if self.code_ticks == self.pending_ticks {
self.code_ticks = 0;
+ self.code_followup_start = None;
}
self.pending_ticks = 0;
}
if self.code_ticks > 0 {
+ // Until the code span closes, its opener may still be a literal unmatched
+ // backtick. Withhold directive-like text rather than reveal a live prompt.
+ if byte == b'['
+ && self.code_followup_start.is_none()
+ && let Some(prefix) = source[..index].strip_suffix(":codex-followup")
+ {
+ self.code_followup_start = Some(prefix.len());
+ }
continue;
}
}
@@ -158,7 +249,23 @@ impl LinkPreview {
self.whitespace = byte.is_ascii_whitespace();
} else {
match byte {
- b'[' => self.brackets += 1,
+ b'[' => {
+ if self.followup_start.is_none()
+ && let Some(prefix) = source[..index].strip_suffix(":codex-followup")
+ && !prefix.ends_with(':')
+ && prefix
+ .bytes()
+ .rev()
+ .take_while(|byte| *byte == b'\\')
+ .count()
+ % 2
+ == 0
+ {
+ self.followup_start = Some(prefix.len());
+ self.followup_brackets = self.brackets;
+ }
+ self.brackets += 1;
+ }
b']' if self.brackets > 0 => {
self.brackets -= 1;
self.closed_label = true;
@@ -168,6 +275,12 @@ impl LinkPreview {
self.destination = Some((index, 1));
self.whitespace = false;
}
+ b'{' if self.closed_label
+ && self.followup_start.is_some()
+ && self.brackets == self.followup_brackets =>
+ {
+ self.followup_attributes = Some(AttributeState::Key);
+ }
_ => {}
}
self.closed_label = false;
diff --git a/codex-rs/tui/src/streaming/snapshots/codex_tui__streaming__controller__preview_tests__unfinished_link_preview.snap b/codex-rs/tui/src/streaming/snapshots/codex_tui__streaming__controller__preview_tests__unfinished_link_preview.snap
deleted file mode 100644
index 4895b40acf..0000000000
--- a/codex-rs/tui/src/streaming/snapshots/codex_tui__streaming__controller__preview_tests__unfinished_link_preview.snap
+++ /dev/null
@@ -1,8 +0,0 @@
----
-source: tui/src/streaming/controller_preview_tests.rs
-assertion_line: 52
-expression: visible_lines(controller.current_tail_lines())
----
-[
- Line::from("See [documentation]"),
-]