Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

Render ipython edit diffs in the TUI (#3744)

Sebastian Müller10f646da5e68
diff --git a/.changes/edit-diff-render.md b/.changes/edit-diff-render.md
new file mode 100644
index 0000000000..589760c8e9
--- /dev/null
+++ b/.changes/edit-diff-render.md
@@ -0,0 +1 @@
+- Edits made with the python tool's `edit` skill now show in the ipython cell card: one `path +N -M` summary line per edited file, and the numbered diff rows at the Details and All levels. The python highlighter no longer crashes the TUI on some non-ASCII text.
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 00a0737d55..5a9500926c 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -344,13 +344,13 @@ jobs:
- name: Bootstrap the kernel venv (ready before the kernel e2e units)
if: steps.selection.outputs.units != '0'
run: ./target/debug/prime-agent --prime-agent-bootstrap
- - name: Prepare pinned TS link-frame comparison binary
- if: matrix.shard == 1 && steps.selection.outputs.units != '0' && hashFiles('scripts/check_link_frames.py') != ''
+ - name: Prepare pinned TS frame comparison binary
+ if: matrix.shard == 1 && steps.selection.outputs.units != '0' && hashFiles('scripts/check_link_frames.py', 'scripts/check_ipython_diff_frames.py') != ''
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
- reference="${RUNNER_TEMP}/link-frame-ts-reference"
+ reference="${RUNNER_TEMP}/frame-ts-reference"
mkdir -p "${reference}/package"
gh release download v0.9.8 --repo "${GITHUB_REPOSITORY}" \
--pattern prime-agent-0.9.8-linux-x64.tar.gz --dir "${reference}"
diff --git a/crates/pa-core/src/session_engine/runtime_wiring.rs b/crates/pa-core/src/session_engine/runtime_wiring.rs
index 880bf5bf60..45ecb6d8be 100644
--- a/crates/pa-core/src/session_engine/runtime_wiring.rs
+++ b/crates/pa-core/src/session_engine/runtime_wiring.rs
@@ -456,6 +456,7 @@ fn convert_execute_result(
data: attachment.data,
})
.collect(),
+ diffs: result.diffs.unwrap_or_default(),
sent_agent_messages: result.sent_agent_messages.unwrap_or_default(),
bash_commands: result.bash_commands,
}
diff --git a/crates/pa-core/src/tools/golden_replay.rs b/crates/pa-core/src/tools/golden_replay.rs
index a88124c218..699940796e 100644
--- a/crates/pa-core/src/tools/golden_replay.rs
+++ b/crates/pa-core/src/tools/golden_replay.rs
@@ -273,6 +273,44 @@ async fn golden_ipython_group_matches_ts() {
assert_eq!(checked, corpus["caseCount"].as_u64().expect("caseCount"));
}
+/// Cell edits reach the result details as the edit tool's numbered diff text.
+#[tokio::test]
+async fn ipython_details_carry_the_cell_edit_diffs() {
+ let diff = |start_line| crate::kernel::shared::KernelDiffDisplay {
+ path: "src/app.py".to_string(),
+ old_str: "x = 1".to_string(),
+ new_str: "x = 2".to_string(),
+ start_line,
+ };
+ let result = ExecuteResult {
+ diffs: vec![diff(Some(10)), diff(None)],
+ ..ExecuteResult::default()
+ };
+ let options = IpythonToolOptions {
+ provisioner: Arc::new(MockProvisioner {
+ kernel: Arc::new(MockKernel {
+ executions: Mutex::new(vec![MockOutcome::Ok(Box::new(result))]),
+ exec_calls: AtomicUsize::new(0),
+ next_index: AtomicUsize::new(0),
+ }),
+ ensure_calls: AtomicUsize::new(0),
+ kill_calls: AtomicUsize::new(0),
+ }),
+ ui: None,
+ on_late_sent_agent_message: None,
+ };
+ let result = ipython::execute_ipython(&options, "edit()", None, None, None)
+ .await
+ .expect("cell executes");
+ assert_eq!(
+ result.details.expect("details")["diffs"],
+ serde_json::json!([
+ { "path": "src/app.py", "diff": "-10 x = 1\n+10 x = 2" },
+ { "path": "src/app.py", "diff": "-1 x = 1\n+1 x = 2" },
+ ])
+ );
+}
+
#[test]
fn golden_schema_group_matches_ts() {
let corpus = corpus("schema");
diff --git a/crates/pa-core/src/tools/ipython.rs b/crates/pa-core/src/tools/ipython.rs
index 727a89645c..ea3ae5dab0 100644
--- a/crates/pa-core/src/tools/ipython.rs
+++ b/crates/pa-core/src/tools/ipython.rs
@@ -10,10 +10,13 @@ use std::sync::Arc;
use serde_json::json;
+mod diff;
+
use crate::tools::tool_definition::{
AbortSignal, ExecutionMode, OnUpdate, ToolContentBlock, ToolDefinition, ToolExecutionResult,
ToolUpdate,
};
+use diff::generate_diff_string;
/// Mime types the model context accepts as images.
pub const IMAGE_MIME_TYPES: [&str; 4] = ["image/jpeg", "image/png", "image/gif", "image/webp"];
@@ -53,6 +56,8 @@ pub struct ExecuteResult {
pub background_output: Option<String>,
pub error: Option<KernelErrorInfo>,
pub attachments: Vec<KernelAttachment>,
+ /// File edits displayed by this cell, in order.
+ pub diffs: Vec<crate::kernel::shared::KernelDiffDisplay>,
/// Agent messages sent from this cell, in order (TS
/// `sentAgentMessages` on the tool-result details).
pub sent_agent_messages: Vec<crate::kernel::shared::KernelSentAgentMessage>,
@@ -470,6 +475,22 @@ pub async fn execute_ipython(
});
details["errorEname"] = json!(error.ename);
}
+ if !r.diffs.is_empty() {
+ details["diffs"] = json!(r
+ .diffs
+ .iter()
+ .map(|diff| {
+ let start_line = diff
+ .start_line
+ .and_then(|line| usize::try_from(line).ok())
+ .unwrap_or(1);
+ json!({
+ "path": diff.path,
+ "diff": generate_diff_string(&diff.old_str, &diff.new_str, 4, start_line),
+ })
+ })
+ .collect::<Vec<_>>());
+ }
if !r.sent_agent_messages.is_empty() {
details["sentAgentMessages"] = json!(r
.sent_agent_messages
diff --git a/crates/pa-core/src/tools/ipython/diff/lines.rs b/crates/pa-core/src/tools/ipython/diff/lines.rs
new file mode 100644
index 0000000000..360522de4a
--- /dev/null
+++ b/crates/pa-core/src/tools/ipython/diff/lines.rs
@@ -0,0 +1,350 @@
+//! Faithful port of jsdiff v9 `diffLines` (Myers O(ND) with the diagonal-bounds
+//! optimization) as the TS product vendors it (`Diff.diffLines`). Ordinary diffs
+//! match the TS algorithm; pathological searches use a bounded whole-block fallback.
+
+// Bound speculative Myers history. Beyond this budget, a whole-block
+// replacement preserves both texts without quadratic search memory.
+const MAX_DIFF_COMPONENTS: usize = 100_000;
+
+/// Tokenize a string the way jsdiff's `diffLines` does: split keeping `\n` / `\r\n` separators,
+/// drop a trailing empty element, and merge each newline into the preceding line token.
+fn jsdiff_line_tokenize(value: &str) -> Vec<String> {
+ let mut parts: Vec<String> = Vec::new();
+ let mut current = String::new();
+ let mut chars = value.chars().peekable();
+ while let Some(c) = chars.next() {
+ if c == '\n' {
+ parts.push(std::mem::take(&mut current));
+ parts.push("\n".to_string());
+ } else if c == '\r' && chars.peek() == Some(&'\n') {
+ chars.next();
+ parts.push(std::mem::take(&mut current));
+ parts.push("\r\n".to_string());
+ } else {
+ current.push(c);
+ }
+ }
+ parts.push(current);
+ // Ignore the final empty token that occurs if the string ends with a newline.
+ if parts.last().is_some_and(String::is_empty) {
+ parts.pop();
+ }
+ // Merge the content and line separators into single tokens.
+ let mut ret_lines: Vec<String> = Vec::new();
+ for (i, part) in parts.into_iter().enumerate() {
+ if i % 2 == 1 {
+ if let Some(last) = ret_lines.last_mut() {
+ last.push_str(&part);
+ }
+ } else {
+ ret_lines.push(part);
+ }
+ }
+ ret_lines
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(super) struct DiffPart {
+ pub value: String,
+ pub added: bool,
+ pub removed: bool,
+}
+
+#[derive(Debug, Clone, Copy)]
+struct Component {
+ count: usize,
+ added: bool,
+ removed: bool,
+ previous: Option<usize>,
+}
+
+#[derive(Debug, Clone, Copy)]
+struct PathEntry {
+ old_pos: isize,
+ last_component: Option<usize>,
+}
+
+struct DiffEngine<'a> {
+ old_tokens: &'a [String],
+ new_tokens: &'a [String],
+ components: Vec<Component>,
+}
+
+impl DiffEngine<'_> {
+ fn equals(left: &str, right: &str) -> bool {
+ left == right
+ }
+
+ fn add_to_path(
+ &mut self,
+ path: &PathEntry,
+ added: bool,
+ removed: bool,
+ old_pos_inc: isize,
+ ) -> PathEntry {
+ if let Some(last_idx) = path.last_component {
+ let last = self.components[last_idx];
+ if last.added == added && last.removed == removed {
+ let idx = self.components.len();
+ self.components.push(Component {
+ count: last.count + 1,
+ added,
+ removed,
+ previous: last.previous,
+ });
+ return PathEntry {
+ old_pos: path.old_pos + old_pos_inc,
+ last_component: Some(idx),
+ };
+ }
+ }
+ let idx = self.components.len();
+ self.components.push(Component {
+ count: 1,
+ added,
+ removed,
+ previous: path.last_component,
+ });
+ PathEntry {
+ old_pos: path.old_pos + old_pos_inc,
+ last_component: Some(idx),
+ }
+ }
+
+ fn extract_common(&mut self, base_path: &mut PathEntry, diagonal_path: isize) -> isize {
+ let new_len = self.new_tokens.len() as isize;
+ let old_len = self.old_tokens.len() as isize;
+ let mut old_pos = base_path.old_pos;
+ let mut new_pos = old_pos - diagonal_path;
+ let mut common_count = 0usize;
+ while new_pos + 1 < new_len
+ && old_pos + 1 < old_len
+ && Self::equals(
+ &self.old_tokens[(old_pos + 1) as usize],
+ &self.new_tokens[(new_pos + 1) as usize],
+ )
+ {
+ new_pos += 1;
+ old_pos += 1;
+ common_count += 1;
+ }
+ if common_count > 0 {
+ let idx = self.components.len();
+ self.components.push(Component {
+ count: common_count,
+ added: false,
+ removed: false,
+ previous: base_path.last_component,
+ });
+ base_path.last_component = Some(idx);
+ }
+ base_path.old_pos = old_pos;
+ new_pos
+ }
+
+ fn build_values(&mut self, last_component: Option<usize>) -> Vec<DiffPart> {
+ // Convert the linked list of components to an ordered array.
+ let mut component_indices: Vec<usize> = Vec::new();
+ let mut next = last_component;
+ while let Some(idx) = next {
+ component_indices.push(idx);
+ next = self.components[idx].previous;
+ }
+ component_indices.reverse();
+
+ let mut parts: Vec<DiffPart> = Vec::with_capacity(component_indices.len());
+ let mut new_pos = 0usize;
+ let mut old_pos = 0usize;
+ for idx in component_indices {
+ let component = self.components[idx];
+ if component.removed {
+ let slice: Vec<String> =
+ self.old_tokens[old_pos..(old_pos + component.count)].to_vec();
+ let value = slice.join("");
+ old_pos += component.count;
+ parts.push(DiffPart {
+ value,
+ added: component.added,
+ removed: component.removed,
+ });
+ } else {
+ let slice: Vec<String> =
+ self.new_tokens[new_pos..(new_pos + component.count)].to_vec();
+ let value = slice.join("");
+ new_pos += component.count;
+ if !component.added {
+ old_pos += component.count;
+ }
+ parts.push(DiffPart {
+ value,
+ added: component.added,
+ removed: component.removed,
+ });
+ }
+ }
+ parts
+ }
+
+ /// Port of jsdiff `Diff.diffWithOptionsObj` (no options, sync).
+ fn run(&mut self) -> Vec<DiffPart> {
+ let new_len = self.new_tokens.len() as isize;
+ let old_len = self.old_tokens.len() as isize;
+ let mut edit_length: isize = 1;
+ let max_edit_length = new_len + old_len;
+
+ // jsdiff stores best-path entries in a JS array/object indexed by the
+ // (possibly negative) diagonal path; emulate that with a map so
+ // negative indices work exactly like in JavaScript.
+ let mut best_path: std::collections::HashMap<isize, PathEntry> =
+ std::collections::HashMap::new();
+ best_path.insert(
+ 0,
+ PathEntry {
+ old_pos: -1,
+ last_component: None,
+ },
+ );
+ // Seed editLength = 0, i.e. the content starts with the same values.
+ let mut base = *best_path.get(&0).expect("seeded");
+ let mut new_pos = self.extract_common(&mut base, 0);
+ best_path.insert(0, base);
+ if base.old_pos + 1 >= old_len && new_pos + 1 >= new_len {
+ let last = base.last_component;
+ return self.build_values(last);
+ }
+
+ let mut min_diagonal_to_consider: isize = isize::MIN;
+ let mut max_diagonal_to_consider: isize = isize::MAX;
+
+ while edit_length <= max_edit_length {
+ let mut done: Option<Vec<DiffPart>> = None;
+ let diag_start = min_diagonal_to_consider.max(-edit_length);
+ let diag_end = max_diagonal_to_consider.min(edit_length);
+
+ let mut diagonal_path = diag_start;
+ while diagonal_path <= diag_end {
+ if self.components.len() >= MAX_DIFF_COMPONENTS {
+ return [
+ DiffPart {
+ value: self.old_tokens.concat(),
+ added: false,
+ removed: true,
+ },
+ DiffPart {
+ value: self.new_tokens.concat(),
+ added: true,
+ removed: false,
+ },
+ ]
+ .into_iter()
+ .filter(|part| !part.value.is_empty())
+ .collect();
+ }
+ let remove_path = best_path.get(&(diagonal_path - 1)).copied();
+ let add_path = best_path.get(&(diagonal_path + 1)).copied();
+
+ let mut can_add = false;
+ if let Some(add) = add_path {
+ let add_path_new_pos = add.old_pos - diagonal_path;
+ can_add = 0 <= add_path_new_pos && add_path_new_pos < new_len;
+ }
+ let can_remove = remove_path.is_some_and(|remove| remove.old_pos + 1 < old_len);
+
+ if !can_add && !can_remove {
+ // If this path is a terminal then prune.
+ best_path.remove(&diagonal_path);
+ diagonal_path += 2;
+ continue;
+ }
+
+ let mut base_path = if !can_remove
+ || (can_add
+ && remove_path.is_some_and(|remove| {
+ add_path.is_some_and(|add| remove.old_pos < add.old_pos)
+ })) {
+ self.add_to_path(&add_path.unwrap(), true, false, 0)
+ } else {
+ self.add_to_path(&remove_path.unwrap(), false, true, 1)
+ };
+
+ new_pos = self.extract_common(&mut base_path, diagonal_path);
+ if base_path.old_pos + 1 >= old_len && new_pos + 1 >= new_len {
+ // If we have hit the end of both strings, then we are done.
+ done = Some(self.build_values(base_path.last_component));
+ break;
+ }
+ best_path.insert(diagonal_path, base_path);
+ if base_path.old_pos + 1 >= old_len {
+ max_diagonal_to_consider = max_diagonal_to_consider.min(diagonal_path - 1);
+ }
+ if new_pos + 1 >= new_len {
+ min_diagonal_to_consider = min_diagonal_to_consider.max(diagonal_path + 1);
+ }
+ diagonal_path += 2;
+ }
+
+ if let Some(parts) = done {
+ return parts;
+ }
+ edit_length += 1;
+ }
+
+ // The loop always terminates via the done path for well-formed input (maxEditLength =
+ // newLen + oldLen bounds the Myers search), but the seed is kept as a safe terminal answer.
+ self.build_values(
+ best_path
+ .get(&(-max_edit_length))
+ .and_then(|p| p.last_component),
+ )
+ }
+}
+
+/// jsdiff `diffLines` with default options (case-sensitive, newlines merged
+/// into line tokens). Returns the change parts in order.
+pub(super) fn diff_lines(old_str: &str, new_str: &str) -> Vec<DiffPart> {
+ let mut old_tokens = jsdiff_line_tokenize(old_str);
+ let mut new_tokens = jsdiff_line_tokenize(new_str);
+ // removeEmpty: drop empty tokens.
+ old_tokens.retain(|t| !t.is_empty());
+ new_tokens.retain(|t| !t.is_empty());
+
+ let mut engine = DiffEngine {
+ old_tokens: &old_tokens,
+ new_tokens: &new_tokens,
+ components: Vec::new(),
+ };
+ engine.run()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn a_large_replacement_has_a_bounded_whole_block_fallback() {
+ use std::fmt::Write as _;
+
+ let (mut old, mut new) = (String::new(), String::new());
+ for n in 0..600 {
+ writeln!(old, "old-{n}").unwrap();
+ writeln!(new, "new-{n}").unwrap();
+ }
+ old.push_str("shared\n");
+ new.push_str("shared\n");
+ assert_eq!(
+ diff_lines(&old, &new),
+ vec![
+ DiffPart {
+ value: old,
+ added: false,
+ removed: true
+ },
+ DiffPart {
+ value: new,
+ added: true,
+ removed: false
+ },
+ ]
+ );
+ }
+}
diff --git a/crates/pa-core/src/tools/ipython/diff/mod.rs b/crates/pa-core/src/tools/ipython/diff/mod.rs
new file mode 100644
index 0000000000..0ae1ef25f8
--- /dev/null
+++ b/crates/pa-core/src/tools/ipython/diff/mod.rs
@@ -0,0 +1,122 @@
+//! Numbered cell-edit display, using the existing jsdiff-compatible line algorithm.
+
+mod lines;
+
+use lines::diff_lines;
+
+/// Generate the cell edit display with line numbers and bounded context.
+pub(super) fn generate_diff_string(
+ old_content: &str,
+ new_content: &str,
+ context_lines: usize,
+ start_line: usize,
+) -> String {
+ let parts = diff_lines(old_content, new_content);
+ let mut output: Vec<String> = Vec::new();
+
+ let old_lines: Vec<&str> = old_content.split('\n').collect();
+ let new_lines: Vec<&str> = new_content.split('\n').collect();
+ let max_line_num = start_line.saturating_sub(1) + old_lines.len().max(new_lines.len());
+ let line_num_width = max_line_num.to_string().len();
+
+ let mut old_line_num = start_line;
+ let mut new_line_num = start_line;
+ let mut last_was_change = false;
+
+ for (i, part) in parts.iter().enumerate() {
+ let mut raw: Vec<&str> = part.value.split('\n').collect();
+ if raw.last() == Some(&"") {
+ raw.pop();
+ }
+
+ if part.added || part.removed {
+ for line in &raw {
+ if part.added {
+ let line_num = format!("{new_line_num:>line_num_width$}");
+ output.push(format!("+{line_num} {line}"));
+ new_line_num += 1;
+ } else {
+ let line_num = format!("{old_line_num:>line_num_width$}");
+ output.push(format!("-{line_num} {line}"));
+ old_line_num += 1;
+ }
+ }
+ last_was_change = true;
+ } else {
+ let next_part_is_change =
+ i + 1 < parts.len() && (parts[i + 1].added || parts[i + 1].removed);
+ let has_leading_change = last_was_change;
+ let has_trailing_change = next_part_is_change;
+
+ if has_leading_change && has_trailing_change {
+ if raw.len() <= context_lines * 2 {
+ for line in &raw {
+ let line_num = format!("{old_line_num:>line_num_width$}");
+ output.push(format!(" {line_num} {line}"));
+ old_line_num += 1;
+ new_line_num += 1;
+ }
+ } else {
+ let leading_lines = &raw[..context_lines];
+ let trailing_lines = &raw[raw.len() - context_lines..];
+ let skipped_lines = raw.len() - leading_lines.len() - trailing_lines.len();
+
+ for line in leading_lines {
+ let line_num = format!("{old_line_num:>line_num_width$}");
+ output.push(format!(" {line_num} {line}"));
+ old_line_num += 1;
+ new_line_num += 1;
+ }
+
+ output.push(format!(" {:>line_num_width$} ...", ""));
+ old_line_num += skipped_lines;
+ new_line_num += skipped_lines;
+
+ for line in trailing_lines {
+ let line_num = format!("{old_line_num:>line_num_width$}");
+ output.push(format!(" {line_num} {line}"));
+ old_line_num += 1;
+ new_line_num += 1;
+ }
+ }
+ } else if has_leading_change {
+ let shown_lines = &raw[..context_lines.min(raw.len())];
+ let skipped_lines = raw.len() - shown_lines.len();
+
+ for line in shown_lines {
+ let line_num = format!("{old_line_num:>line_num_width$}");
+ output.push(format!(" {line_num} {line}"));
+ old_line_num += 1;
+ new_line_num += 1;
+ }
+
+ if skipped_lines > 0 {
+ output.push(format!(" {:>line_num_width$} ...", ""));
+ old_line_num += skipped_lines;
+ new_line_num += skipped_lines;
+ }
+ } else if has_trailing_change {
+ let skipped_lines = raw.len().saturating_sub(context_lines);
+ if skipped_lines > 0 {
+ output.push(format!(" {:>line_num_width$} ...", ""));
+ old_line_num += skipped_lines;
+ new_line_num += skipped_lines;
+ }
+
+ for line in raw.iter().skip(skipped_lines) {
+ let line_num = format!("{old_line_num:>line_num_width$}");
+ output.push(format!(" {line_num} {line}"));
+ old_line_num += 1;
+ new_line_num += 1;
+ }
+ } else {
+ old_line_num += raw.len();
+ new_line_num += raw.len();
+ }
+
+ last_was_change = false;
+ }
+ }
+
+ output.join("\n")
+}
diff --git a/crates/pa-tui/src/tool_card/highlight.rs b/crates/pa-tui/src/tool_card/highlight.rs
index 1342a2bbc6..ab79baa6ef 100644
--- a/crates/pa-tui/src/tool_card/highlight.rs
+++ b/crates/pa-tui/src/tool_card/highlight.rs
@@ -371,10 +371,9 @@ fn string_at(text: &str, i: usize) -> Option<usize> {
let mut cursor = end + if triple { 3 } else { 1 };
while cursor < text.len() {
let rest = &text[cursor..];
- if rest.starts_with('\\') {
+ if let Some(escaped) = rest.strip_prefix('\\') {
// Backslash escape: consumed inside the string.
- let escaped = rest.char_indices().nth(1).map_or(1, |(idx, _)| idx + 1);
- cursor += escaped;
+ cursor += 1 + escaped.chars().next().map_or(0, char::len_utf8);
continue;
}
let ch = rest.chars().next()?;
@@ -519,8 +518,12 @@ fn header_mode(
) -> Option<usize> {
let mut i = at + keyword_len;
let gap_start = i;
- while code[i..].starts_with(|c: char| c.is_whitespace()) && !code[i..].starts_with('\n') {
- i += 1;
+ while let Some(c) = code[i..]
+ .chars()
+ .next()
+ .filter(|c| c.is_whitespace() && *c != '\n')
+ {
+ i += c.len_utf8();
}
let name_len = code[i..]
.char_indices()
@@ -588,6 +591,8 @@ fn header_mode(
#[cfg(test)]
mod tests {
+ use proptest::prelude::*;
+
use super::*;
use crate::theme::{ColorMode, Theme};
@@ -617,6 +622,32 @@ mod tests {
);
}
+ #[test]
+ fn multibyte_chars_after_escapes_and_headers_stay_whole() {
+ assert!(scopes(r#"s = r"\é""#).contains(&(r#"r"\é""#.into(), Scope::String)));
+ assert!(scopes("def\u{a0}f():").contains(&("f".into(), Scope::Title)));
+ }
+
+ fn fragment() -> impl Strategy<Value = String> {
+ prop_oneof![
+ 3 => prop::sample::select(vec![
+ "'", "\"", "'''", "\"\"\"", "\\", "r", "b", "f", "u", "def", "class", "def ",
+ "class ", "#", "0", "7", "1.5e", "0x", "(", ")", "\n", " ", "é", "界", "\u{a0}",
+ "\u{3000}", "\u{1f980}",
+ ])
+ .prop_map(str::to_string),
+ 1 => any::<char>().prop_map(String::from),
+ ]
+ }
+
+ proptest! {
+ #[test]
+ fn highlight_python_never_panics(parts in prop::collection::vec(fragment(), 0..24)) {
+ let code = parts.concat();
+ prop_assert!(!highlight_python(&code, &SyntaxPalette::from_theme(&theme())).is_empty());
+ }
+ }
+
#[test]
fn fstring_is_one_token() {
let toks = scopes("print(f'line {i}')");
diff --git a/crates/pa-tui/src/tool_card/ipython.rs b/crates/pa-tui/src/tool_card/ipython.rs
index 4e15098ca0..8a24ff4fcf 100644
--- a/crates/pa-tui/src/tool_card/ipython.rs
+++ b/crates/pa-tui/src/tool_card/ipython.rs
@@ -1,10 +1,13 @@
//! The `ipython` tool-call card: a fixed collapsed summary line (marker,
-//! language, preview, line counts, duration, error name), plus in expanded
+//! language, preview, line counts, duration, error name), per-file edit
+//! summaries (with diff rows when edit diffs expand), plus in expanded
//! mode the full cell source and output. The top line never changes with
//! expansion, so toggling detail never shifts the layout.
+mod diffs;
mod output;
+use diffs::render_diffs;
use output::render_output;
use serde_json::Value;
@@ -106,9 +109,10 @@ fn cell_code(card: &ToolCallCard) -> &str {
.unwrap_or_default()
}
-/// Render the ipython cell card: the fixed summary line, then the expanded
-/// code and output rows when the card's `detail` is `all`. The sent agent
-/// messages' bodies follow the conversation `level` instead.
+/// Render the ipython cell card: the fixed summary line, the edit
+/// summaries, then the expanded code and output rows when the card's
+/// `detail` is `all`. Edit diff rows and the sent agent messages' bodies
+/// follow the conversation `level`, never a card's own click toggle.
#[must_use]
pub fn render(
card: &ToolCallCard,
@@ -156,6 +160,8 @@ pub(super) fn count(
lines.len()
}
+// One more than clippy's limit: the paint/count sink rides beside the
+// render inputs so both traversals share this single layout.
#[allow(clippy::too_many_arguments)]
fn layout(
card: &ToolCallCard,
@@ -188,16 +194,24 @@ fn layout(
code,
)
});
- // The sent-message receipt rows render even when the cell is collapsed. They are agent
- // messages of their own, so their bodies follow the conversation level, not a click on
- // the card.
- let messages_expanded = level.tool_output_expanded();
- if !detail.tool_output_expanded() {
- render_sent_agent_messages(lines, &details, messages_expanded, theme, width);
+ // Edit summaries and sent-message receipts show even when the cell is
+ // collapsed; code and output open up only when expanded. The sent
+ // messages are agent messages of their own, so their bodies follow the
+ // conversation level, not a click on the card.
+ let expanded = detail.tool_output_expanded();
+ let has_code = expanded && render_code(lines, code, theme, width);
+ render_diffs(
+ lines,
+ &details.diffs,
+ has_code,
+ level.edit_diffs_expanded(),
+ theme,
+ width,
+ );
+ render_sent_agent_messages(lines, &details, level.tool_output_expanded(), theme, width);
+ if !expanded {
return;
}
- let has_code = render_code(lines, code, theme, width);
- render_sent_agent_messages(lines, &details, messages_expanded, theme, width);
render_output(card, &details, lines, has_code, show_images, theme, width);
// Image blocks render below the card when shown (TS adds these rows
// for every tool shell).
diff --git a/crates/pa-tui/src/tool_card/ipython/diffs.rs b/crates/pa-tui/src/tool_card/ipython/diffs.rs
new file mode 100644
index 0000000000..66e414dce9
--- /dev/null
+++ b/crates/pa-tui/src/tool_card/ipython/diffs.rs
@@ -0,0 +1,234 @@
+//! Edit diffs in the cell card: one `╰─ <path> +N -M` summary line per edited file, always
+//! shown, and the numbered diff rows when edit diffs are expanded. Each `details.diffs` entry
+//! carries pa-core's edit-tool diff text, so the card only splits it into rows.
+
+use std::path::Path;
+
+use super::{highlight, Line, RowOutput, Span, Theme, ThemeColor, Value};
+use crate::theme::{ColorMode, ThemeBg};
+use crate::width::{
+ line_width, str_width, truncate_line, truncate_to_width, wrap_line, wrapped_line_count,
+};
+
+const SUMMARY_PREFIX: &str = " \u{2570}\u{2500} ";
+const DIFF_INDENT: &str = " ";
+
+/// Edits grouped by path in first-seen order, after a spacer row when code is shown.
+pub(super) fn render_diffs(
+ lines: &mut RowOutput,
+ diffs: &[Value],
+ has_code: bool,
+ expanded: bool,
+ theme: &Theme,
+ width: usize,
+) {
+ let mut by_path: Vec<(&str, Vec<&str>)> = Vec::new();
+ for entry in diffs {
+ let (Some(path), Some(diff)) = (
+ entry.get("path").and_then(Value::as_str),
+ entry.get("diff").and_then(Value::as_str),
+ ) else {
+ continue;
+ };
+ match by_path.iter_mut().find(|(seen, _)| *seen == path) {
+ Some((_, edits)) => edits.push(diff),
+ None => by_path.push((path, vec![diff])),
+ }
+ }
+ if by_path.is_empty() {
+ return;
+ }
+ if has_code {
+ lines.push(|| vec![Span::raw(" ")]);
+ }
+ for (path, edits) in &by_path {
+ render_file(lines, path, edits, expanded, theme, width);
+ }
+}
+
+/// The summary line, then (expanded) each edit's rows with a `⋮` separator between edits.
+fn render_file(
+ lines: &mut RowOutput,
+ path: &str,
+ edits: &[&str],
+ expanded: bool,
+ theme: &Theme,
+ width: usize,
+) {
+ let (mut added, mut removed) = (0usize, 0usize);
+ for line in edits.iter().flat_map(|diff| diff.split('\n')) {
+ if line.starts_with('+') {
+ added += 1;
+ } else if line.starts_with('-') {
+ removed += 1;
+ }
+ }
+ lines.push(|| summary_line(path, added, removed, theme, width));
+ if !expanded {
+ return;
+ }
+ let python = Path::new(path)
+ .extension()
+ .is_some_and(|ext| ext.eq_ignore_ascii_case("py"));
+ let indent = if width > 1 { DIFF_INDENT } else { "" };
+ let content_width = width.saturating_sub(indent.len()).max(1);
+ for (index, diff) in edits.iter().enumerate() {
+ if index > 0 {
+ lines.push(|| {
+ let mut row: Line = vec![Span::raw(indent)];
+ row.extend(diff_separator(theme, content_width));
+ truncate_line(&row, width, "")
+ });
+ }
+ diff_rows(lines, diff, indent, content_width, python, theme);
+ }
+}
+
+/// The raw path, truncated with `…` so the counts stay visible.
+fn summary_line(path: &str, added: usize, removed: usize, theme: &Theme, width: usize) -> Line {
+ let dim = theme.fg_style(ThemeColor::Dim);
+ let added = format!("+{added}");
+ let removed = format!("-{removed}");
+ let counts_width = 1 + str_width(&added) + 1 + str_width(&removed);
+ let width = width.max(1);
+ let available = width
+ .saturating_sub(str_width(SUMMARY_PREFIX) + counts_width)
+ .max(1);
+ let row: Line = vec![
+ Span::styled(SUMMARY_PREFIX, dim),
+ Span::styled(
+ truncate_to_width(path, available, "\u{2026}"),
+ theme.fg_style(ThemeColor::Muted),
+ ),
+ Span::styled(" ", dim),
+ Span::styled(added, theme.fg_style(ThemeColor::ToolDiffAdded)),
+ Span::raw(" "),
+ Span::styled(removed, theme.fg_style(ThemeColor::ToolDiffRemoved)),
+ ];
+ truncate_line(&row, width, "")
+}
+
+fn diff_separator(theme: &Theme, width: usize) -> Line {
+ let bg = theme.bg_style(ThemeBg::ToolPanelBg);
+ let marker = " \u{22ee}";
+ vec![
+ Span::styled(
+ marker,
+ theme.fg_style(ThemeColor::ToolDiffContext).patch(bg),
+ ),
+ Span::styled(" ".repeat(width.saturating_sub(str_width(marker))), bg),
+ ]
+}
+
+#[derive(Clone, Copy)]
+enum RowKind {
+ Added,
+ Removed,
+ Context,
+}
+
+/// One diff line split into kind, gutter, and text. Lines have the form
+/// `{prefix}{number:>w} {text}`; skip rows put spaces in the number column and `...` as text.
+/// Anything else renders whole as context behind a blank gutter.
+fn split_row(line: &str) -> (RowKind, String, &str) {
+ split_numbered(line).unwrap_or((RowKind::Context, " ".to_string(), line))
+}
+
+fn split_numbered(line: &str) -> Option<(RowKind, String, &str)> {
+ let kind = match line.bytes().next()? {
+ b'+' => RowKind::Added,
+ b'-' => RowKind::Removed,
+ b' ' => RowKind::Context,
+ _ => return None,
+ };
+ let (prefix, rest) = line.split_at(1);
+ let pad = rest.len() - rest.trim_start_matches(' ').len();
+ let digits = rest[pad..].bytes().take_while(u8::is_ascii_digit).count();
+ let split = if digits == 0 {
+ pad.checked_sub(1)?
+ } else {
+ pad + digits
+ };
+ let (number, text) = rest.split_at(split);
+ Some((
+ kind,
+ format!(" {number} {prefix} "),
+ text.strip_prefix(' ')?,
+ ))
+}
+
+/// Row background, gutter color, and the text color that replaces syntax colors when the
+/// terminal lacks truecolor (changed rows are then marked by text color, not background).
+fn row_colors(kind: RowKind, truecolor: bool) -> (ThemeBg, ThemeColor, Option<ThemeColor>) {
+ let (block, color) = match kind {
+ RowKind::Added => (ThemeBg::ToolDiffAddedBg, ThemeColor::ToolDiffAdded),
+ RowKind::Removed => (ThemeBg::ToolDiffRemovedBg, ThemeColor::ToolDiffRemoved),
+ RowKind::Context => return (ThemeBg::ToolPanelBg, ThemeColor::ToolDiffContext, None),
+ };
+ if truecolor {
+ (block, color, None)
+ } else {
+ (ThemeBg::ToolPanelBg, color, Some(color))
+ }
+}
+
+/// Full-width rows: the gutter, then the text wrapped onto blank-gutter continuation rows.
+fn diff_rows(
+ lines: &mut RowOutput,
+ diff: &str,
+ indent: &'static str,
+ width: usize,
+ python: bool,
+ theme: &Theme,
+) {
+ let truecolor = theme.mode == ColorMode::TrueColor;
+ for raw_line in diff.split('\n') {
+ let (kind, gutter, text) = split_row(raw_line);
+ let (bg, gutter_color, text_color) = row_colors(kind, truecolor);
+ let text = text.replace('\t', " ");
+ let content: Line = match text_color {
+ Some(color) => vec![Span::styled(text, theme.fg_style(color))],
+ None if text.is_empty() => Vec::new(),
+ None if python => {
+ highlight::highlight_python(&text, &highlight::SyntaxPalette::from_theme(theme))
+ .into_iter()
+ .next()
+ .unwrap_or_else(|| vec![Span::raw(text)])
+ }
+ None => vec![Span::styled(text, theme.fg_style(ThemeColor::MdCodeBlock))],
+ };
+ let gutter_width = str_width(&gutter);
+ let content_width = width.saturating_sub(gutter_width).max(1);
+ if lines.is_counting() {
+ lines.add_count(wrapped_line_count(&content, content_width).max(1));
+ continue;
+ }
+ let bg = theme.bg_style(bg);
+ let mut rows = wrap_line(&content, content_width);
+ if rows.is_empty() {
+ rows.push(Vec::new());
+ }
+ for (index, row) in rows.into_iter().enumerate() {
+ let lead = if index == 0 {
+ Span::styled(gutter.clone(), theme.fg_style(gutter_color).patch(bg))
+ } else {
+ Span::styled(" ".repeat(gutter_width), bg)
+ };
+ let mut line: Line = vec![lead];
+ line.extend(
+ row.into_iter()
+ .map(|span| Span::styled(span.content, span.style.patch(bg))),
+ );
+ let mut line = truncate_line(&line, width, "");
+ let pad = width.saturating_sub(line_width(&line));
+ if pad > 0 {
+ line.push(Span::styled(" ".repeat(pad), bg));
+ }
+ lines.push(|| {
+ let mut out: Line = vec![Span::raw(indent)];
+ out.extend(line);
+ out
+ });
+ }
+ }
+}
diff --git a/crates/pa-tui/src/tool_card/ipython/tests.rs b/crates/pa-tui/src/tool_card/ipython/tests.rs
index 7f905c6563..245a4eddae 100644
--- a/crates/pa-tui/src/tool_card/ipython/tests.rs
+++ b/crates/pa-tui/src/tool_card/ipython/tests.rs
@@ -510,6 +510,93 @@ fn bash_minority_cell_stays_python() {
assert_eq!(bash_dominated_stats(&card), None);
}
+fn edit_cell_card() -> ToolCallCard {
+ cell_card(
+ "await edit.run('src/app.py', 'x = 1', 'x = 2')",
+ json!({
+ "status": "ok",
+ "durationMs": 5,
+ "result": "'Edited src/app.py'",
+ "diffs": [{ "path": "src/app.py", "diff": "-10 x = 1\n+10 x = 2\n 11 y = 3\n ..." }],
+ }),
+ false,
+ false,
+ )
+}
+
+fn trimmed_rows(lines: &[Line]) -> Vec<String> {
+ lines
+ .iter()
+ .skip(1)
+ .map(|line| text_of(line).trim_end().to_string())
+ .collect()
+}
+
+#[test]
+fn edit_diffs_collapse_to_a_file_summary_line_in_overview() {
+ let card = edit_cell_card();
+ let summary = vec![" \u{2570}\u{2500} src/app.py +1 -1".to_string()];
+ let lines = render(
+ &card,
+ 0,
+ Detail::Overview,
+ Detail::Overview,
+ &theme(),
+ 40,
+ true,
+ );
+ assert_eq!(trimmed_rows(&lines), summary);
+ // A click-expanded card keeps the conversation level's collapsed diff.
+ let toggled = render(&card, 0, Detail::All, Detail::Overview, &theme(), 40, true);
+ assert!(!trimmed_rows(&toggled)
+ .iter()
+ .any(|row| row.contains("10 + x = 2")));
+}
+
+#[test]
+fn edit_diffs_expand_to_numbered_rows_in_details() {
+ let card = edit_cell_card();
+ let lines = render(
+ &card,
+ 0,
+ Detail::Details,
+ Detail::Details,
+ &theme(),
+ 40,
+ true,
+ );
+ assert_eq!(
+ trimmed_rows(&lines),
+ vec![
+ " \u{2570}\u{2500} src/app.py +1 -1".to_string(),
+ " 10 - x = 1".to_string(),
+ " 10 + x = 2".to_string(),
+ " 11 y = 3".to_string(),
+ " ...".to_string(),
+ ]
+ );
+ // Full-width rows on the diff backgrounds.
+ assert_eq!(text_of(&lines[2]).chars().count(), 40);
+ assert_eq!(
+ lines[3][1].style,
+ theme()
+ .fg_style(ThemeColor::ToolDiffAdded)
+ .patch(theme().bg_style(crate::theme::ThemeBg::ToolDiffAddedBg))
+ );
+ assert_eq!(
+ count(
+ &card,
+ 0,
+ Detail::Details,
+ Detail::Details,
+ &theme(),
+ 40,
+ true
+ ),
+ lines.len()
+ );
+}
+
#[test]
fn an_aborted_cell_freezes_its_duration_from_card_timestamps() {
let now = std::time::Instant::now();
diff --git a/crates/pa-tui/src/tool_card/mod.rs b/crates/pa-tui/src/tool_card/mod.rs
index 6382adbe40..576fb6d2fb 100644
--- a/crates/pa-tui/src/tool_card/mod.rs
+++ b/crates/pa-tui/src/tool_card/mod.rs
@@ -113,9 +113,9 @@ pub fn working_icon(frame: usize) -> &'static str {
}
/// Render one tool-call card through its tool shell. `detail` is the card's own level (a click
-/// flips it); `level` is the conversation level, which the python card's sent agent messages
-/// follow. `show_images` (the `terminal.showImages` setting) renders metadata rows or
-/// `[Image: ...]` placeholders.
+/// flips it); `level` is the conversation level, which the python card's edit diffs and sent
+/// agent messages follow. `show_images` (the `terminal.showImages` setting) renders metadata
+/// rows or `[Image: ...]` placeholders.
#[must_use]
pub fn render_tool_card(
card: &ToolCallCard,
diff --git a/crates/pa-tui/src/view/expansion.rs b/crates/pa-tui/src/view/expansion.rs
index 437b09cc39..58bb32214b 100644
--- a/crates/pa-tui/src/view/expansion.rs
+++ b/crates/pa-tui/src/view/expansion.rs
@@ -11,10 +11,11 @@ impl AgentView {
/// flipped for the entries a click toggled away from it. Exact for
/// both sides: the never-toggled entries get `self.detail` (a click
/// can only toggle the kinds `transcript_click_target` matches),
- /// and the card renderers read `Detail` only through
- /// `tool_output_expanded()` — the level's other readers (thinking
- /// blocks, edit diffs) belong to rows no click can toggle, so the
- /// flip never leaks into them.
+ /// and the card renderers read this `Detail` only through
+ /// `tool_output_expanded()`. The level's other readers never see the
+ /// flip: thinking blocks belong to rows no click can toggle, and the
+ /// ipython card's edit diffs read the conversation level (`self.detail`)
+ /// passed beside this one.
#[must_use]
pub(super) fn entry_detail(&self, index: usize) -> Detail {
if self.toggled_cards.contains(&index) {
diff --git a/crates/pa-tui/src/view/geometry.rs b/crates/pa-tui/src/view/geometry.rs
index 3e4be2e26f..d73a1f35fd 100644
--- a/crates/pa-tui/src/view/geometry.rs
+++ b/crates/pa-tui/src/view/geometry.rs
@@ -244,9 +244,9 @@ mod tests {
Just(serde_json::json!({"error": {"ename": "E", "evalue": "boom", "traceback": []}})),
Just(serde_json::json!({"error": {"ename": "E", "evalue": "boom",
"traceback": ["Traceback", "E: boom"]}, "status": "error"})),
- Just(
- serde_json::json!({"diffs": [{"path": "x", "diff": "-x\n+y"}], "stdout": "Edited x"})
- ),
+ Just(serde_json::json!({"diffs": [
+ {"path": "x.py", "diff": "-3 a\tb\n+3 a\n 4 c\n ...\n+9 d 界"},
+ {"path": "x.py", "diff": "-1 e\n+1 f"}], "stdout": "Edited x.py"})),
markdown_text().prop_map(|message| serde_json::json!({"sentAgentMessages": [
{"message": message, "deliveryStatus": "delivered", "receiverRole": "parent"},
{"id": "invalid"}]})),
diff --git a/scripts/check_ipython_diff_frames.py b/scripts/check_ipython_diff_frames.py
new file mode 100644
index 0000000000..d5e7e4a950
--- /dev/null
+++ b/scripts/check_ipython_diff_frames.py
@@ -0,0 +1,215 @@
+#!/usr/bin/env python3
+"""Compare actual TS/Rust edit-diff PTY frames and the Ctrl+O detail cycle.
+
+The captured region is the edited-file summary and its diff rows, excluding
+unchanged tool headers and app chrome. Semantic fixtures use each product's
+native details payload; the receipt records that wire difference explicitly.
+"""
+import argparse
+import copy
+import fcntl
+import json
+import os
+from pathlib import Path
+import pty
+import select
+import struct
+import subprocess
+import sys
+import tempfile
+import termios
+import time
+
+from lifecycle_lib import (DockScreen, OwnedProcesses, Supervisor, TAG, Terminal,
+ actual_hello, digest, env_for, finish)
+
+TS_ARCHIVE_SHA256 = '83fb09129bf78e3e60268212cd70932166591b15188caa70c1b0efbcc76235e2'
+TS_COMMIT = '7d442aafa985f9342134fac16c2ef41f03fb45c1'
+END = 'PARITY_DIFF_END'
+CASES = {
+ 'ascii': {'path': 'parity-diff.py', 'oldStr': 'x = 1\ny = 3\n',
+ 'newStr': 'x = 2\ny = 3\n', 'startLine': 10,
+ 'diff': '-10 x = 1\n+10 x = 2\n 11 y = 3'},
+ 'unicode': {'path': 'parity-unicode.py', 'oldStr': "label = 'café界'\n",
+ 'newStr': "label = 'hello界'\n", 'startLine': 1,
+ 'diff': "-1 label = 'café界'\n+1 label = 'hello界'"},
+}
+
+
+def messages(kind, case):
+ usage = {'input': 0, 'output': 0, 'cacheRead': 0, 'cacheWrite': 0,
+ 'totalTokens': 0, 'cost': {'input': 0, 'output': 0, 'cacheRead': 0,
+ 'cacheWrite': 0, 'total': 0}}
+ def assistant(content, stop):
+ return {'role': 'assistant', 'content': content, 'stopReason': stop,
+ 'api': 'openai-completions', 'provider': 'parity-local',
+ 'model': 'parity-1', 'timestamp': 1750000000000, 'usage': usage}
+ diff = {key: value for key, value in case.items()
+ if key in (('path', 'oldStr', 'newStr', 'startLine') if kind == 'ts'
+ else ('path', 'diff'))}
+ return [
+ assistant([{'type': 'toolCall', 'id': 'edit-cell', 'name': 'ipython',
+ 'arguments': {'code': 'edit_file()'}}], 'toolUse'),
+ {'role': 'toolResult', 'toolCallId': 'edit-cell', 'toolName': 'ipython',
+ 'content': [], 'isError': False, 'timestamp': 1750000000001,
+ 'details': {'status': 'ok', 'durationMs': 5,
+ 'result': 'Edited ' + case['path'], 'diffs': [diff]}},
+ assistant([{'type': 'text', 'text': END}], 'stop'),
+ ]
+
+
+def region(screen, path):
+ rows = [''.join(row) for row in screen.cells]
+ starts = [i for i, row in enumerate(rows) if path in row and '╰─' in row]
+ ends = [i for i, row in enumerate(rows) if END in row]
+ if len(starts) != 1 or len(ends) != 1 or starts[0] >= ends[0]:
+ raise ValueError('complete edited-file frame is not visible')
+ result = []
+ for y in range(starts[0], ends[0]):
+ text = rows[y]
+ cells = []
+ for x, char in enumerate(text):
+ style = copy.deepcopy(screen.styles[y][x])
+ # Foreground on a blank cell is invisible; background and inverse
+ # remain visible and are compared. No geometry is normalized.
+ if char.isspace() and not any(style.get(key) for key in ('4', '7', '9')):
+ style = {key: value for key, value in style.items() if key == 'bg'}
+ cells.append([char, style])
+ result.append(cells)
+ return result
+
+
+class FrameSupervisor(Supervisor):
+ def extra_command(self, command, response):
+ if command.get('type') == 'get_tool_definition' and command.get('name') == 'ipython':
+ # Pinned TS daemon-mode returns {toolDefinition: undefined} for
+ # a replay-only tool absent from the active registry. JSON omits
+ # that field; the actual client uses its built-in replay renderer.
+ response['data'] = {}
+ return True
+ return False
+
+
+class FrameTerminal(Terminal):
+ def __init__(self, command, root, env, width):
+ self.master, slave = pty.openpty()
+ attrs = termios.tcgetattr(slave)
+ attrs[3] &= ~(termios.ECHO | termios.ICANON)
+ termios.tcsetattr(slave, termios.TCSANOW, attrs)
+ fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', 40, width, 0, 0))
+ self.process = subprocess.Popen(command, cwd=root, env=env, stdin=slave,
+ stdout=slave, stderr=slave, start_new_session=True)
+ os.close(slave)
+ self.screen, self.raw = DockScreen(width), bytearray()
+
+
+def capture(binary, kind, case, width, root):
+ root.mkdir()
+ env = env_for(root)
+ env[TAG] = str(root)
+ env['COLORTERM'] = 'truecolor'
+ settings_path = root / 'agent/settings.json'
+ settings = json.loads(settings_path.read_text())
+ settings['chatDetail'] = 'overview'
+ settings_path.write_text(json.dumps(settings))
+ if kind == 'ts':
+ env['PI_PACKAGE_DIR'] = str(binary.parent)
+ hello = actual_hello(binary, root, env)
+ if kind == 'ts':
+ assert hello.get('appVersion') == '0.9.8', hello
+ assert hello.get('runtime', {}).get('buildId') == TS_COMMIT, hello
+ server = FrameSupervisor(root / 'ui.sock', hello, 'edit-diff-frames', root)
+ server.messages = messages(kind, case)
+ scope = OwnedProcesses(binary, root, server.path)
+ terminal = None
+ result = {'binary_sha256': digest(binary), 'hello': hello, 'frames': {},
+ 'fixture_messages': server.messages, 'initial_chat_detail': 'overview'}
+ try:
+ command = [str(binary), '--daemon-socket', str(server.path)]
+ terminal = FrameTerminal(command, root, env, width)
+ scope.register(terminal.process, command)
+ # Both real renderers place the line number before the change sign.
+ needle = str(case['startLine']) + ' - '
+ for index, (phase, expanded) in enumerate([
+ ('overview', False), ('details', True), ('all', True), ('overview_again', False)]):
+ barrier = f'FRAME{index}'
+ terminal.send(('\x0f' if index else '') + barrier)
+ terminal.wait(phase + ' input and diff painted', lambda:
+ barrier in terminal.screen.text() and case['path'] in terminal.screen.text()
+ and END in terminal.screen.text()
+ and (needle in terminal.screen.text()) == expanded, server)
+ result['frames'][phase] = region(terminal.screen, case['path'])
+ assert not server.prompts, 'frame check submitted a prompt'
+ assert not terminal.screen.unsupported_sgr, terminal.screen.unsupported_sgr
+ assert not terminal.screen.unsupported_terminal, terminal.screen.unsupported_terminal
+ result['executed'] = True
+ except Exception as error:
+ result['error'] = f'{type(error).__name__}: {error}'
+ finally:
+ try:
+ if terminal:
+ if terminal.process.poll() is None:
+ terminal.send('\x03\x03')
+ deadline = time.monotonic() + 8
+ while terminal.process.poll() is None and time.monotonic() < deadline:
+ terminal.pump()
+ while select.select([terminal.master], [], [], 0)[0]:
+ before = len(terminal.raw)
+ terminal.pump()
+ if len(terminal.raw) == before:
+ break
+ result['raw_ansi_hex'] = bytes(terminal.raw).hex()
+ result['exit_code'] = terminal.process.poll()
+ os.close(terminal.master)
+ finally:
+ try:
+ server.close()
+ finally:
+ result['commands'] = server.commands
+ result['fixture_errors'] = server.errors
+ finish(scope, None, None, result)
+ return result
+
+
+def frames_equal(pair):
+ return bool(set(pair) == {'ts', 'rust'} and all(
+ item.get('executed') and item.get('cleanup_verified')
+ and item.get('exit_code') == 0
+ and not item.get('error') and not item.get('fixture_errors')
+ for item in pair.values()) and pair['ts']['frames'] == pair['rust']['frames'])
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--receipt', type=Path, required=True)
+ args = parser.parse_args()
+ receipt = {'verified': False, 'scope': __doc__, 'cases': {}, 'comparisons': {},
+ 'not_verified': ['wire byte equality: TS carries raw old/new strings; Rust carries generated diff text',
+ 'real kernel edit execution', 'mouse selection', 'streaming'],
+ 'source_commit': TS_COMMIT}
+ try:
+ assert sys.platform == 'linux', 'hosted Linux only'
+ assert digest(Path(os.environ['PA_TS_ARCHIVE'])) == TS_ARCHIVE_SHA256
+ receipt['ts_archive_sha256'] = TS_ARCHIVE_SHA256
+ with tempfile.TemporaryDirectory(prefix='pa-diff-frames-', dir='/tmp') as folder:
+ for name, case in CASES.items():
+ for width in (80, 120):
+ key = f'{name}-{width}'
+ pair = receipt['cases'][key] = {}
+ for kind in ('ts', 'rust'):
+ binary = Path(os.environ[f'PA_{kind.upper()}_BINARY']).resolve()
+ pair[kind] = capture(binary, kind, case, width,
+ Path(folder) / f'{key}-{kind}')
+ receipt['comparisons'][key] = frames_equal(pair)
+ receipt['verified'] = bool(receipt['comparisons'] and all(receipt['comparisons'].values()))
+ except Exception as error:
+ receipt['error'] = f'{type(error).__name__}: {error}'
+ args.receipt.parent.mkdir(parents=True, exist_ok=True)
+ args.receipt.write_text(json.dumps(receipt, ensure_ascii=False, indent=2) + '\n')
+ print(json.dumps({'verified': receipt['verified'], 'comparisons': receipt['comparisons'],
+ 'error': receipt.get('error')}))
+ return 0 if receipt['verified'] else 1
+
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/scripts/lifecycle_lib.py b/scripts/lifecycle_lib.py
index 73dc320c6d..017805f79a 100644
--- a/scripts/lifecycle_lib.py
+++ b/scripts/lifecycle_lib.py
@@ -801,7 +801,13 @@ class DockScreen:
self.col = self.width - 1
self.cells[self.row][self.col] = char
self.styles[self.row][self.col] = dict(self.sgr)
- self.col += 2 if unicodedata.east_asian_width(char) in "WF" else 1
+ width = 2 if unicodedata.east_asian_width(char) in "WF" else 1
+ if width == 2 and self.col + 1 < self.width:
+ # A terminal paints both cells of a wide glyph, replacing
+ # any old text and background in its continuation cell.
+ self.cells[self.row][self.col + 1] = ' '
+ self.styles[self.row][self.col + 1] = dict(self.sgr)
+ self.col += width
elif char not in ('\x00', '\x07') and not unicodedata.combining(char):
self.unsupported_terminal.append(char)
return replies
diff --git a/scripts/test_check_ipython_diff_frames.py b/scripts/test_check_ipython_diff_frames.py
new file mode 100644
index 0000000000..632ef89cb9
--- /dev/null
+++ b/scripts/test_check_ipython_diff_frames.py
@@ -0,0 +1,64 @@
+import copy
+import unittest
+
+from check_ipython_diff_frames import END, FrameSupervisor, frames_equal, region
+from lifecycle_lib import DockScreen
+
+
+class EditFrameCaptureTests(unittest.TestCase):
+ def test_a_crashed_or_unexited_ui_cannot_pass(self):
+ item = {'executed': True, 'cleanup_verified': True, 'exit_code': 0,
+ 'frames': {'details': [[['x', {}]]]}}
+ pair = {'ts': copy.deepcopy(item), 'rust': copy.deepcopy(item)}
+ self.assertTrue(frames_equal(pair))
+ for code in (1, -11, None):
+ changed = copy.deepcopy(pair)
+ changed['rust']['exit_code'] = code
+ self.assertFalse(frames_equal(changed))
+
+ def test_wide_cell_repaint_clears_and_styles_its_continuation(self):
+ screen = DockScreen(width=8)
+ screen.feed('ab界\r\x1b[41mX界'.encode())
+ self.assertEqual(screen.cells[0][:4], ['X', '界', ' ', ' '])
+ self.assertEqual(screen.styles[0][:3], [{'bg': ['ansi', 41]}] * 3)
+
+ def test_capture_keeps_geometry_background_and_inverse(self):
+ screen = DockScreen(width=80)
+ screen.feed((' ╰─ parity.py +1 -1\r\n\x1b[41;7m-1 a b\x1b[0m\r\n'
+ + END).encode())
+ rows = region(screen, 'parity.py')
+ self.assertEqual(len(rows[0]), 80)
+ self.assertEqual(''.join(cell[0] for cell in rows[0]).rstrip(), ' ╰─ parity.py +1 -1')
+ self.assertEqual(rows[1][4], [' ', {'bg': ['ansi', 41], '7': True}])
+ changed = DockScreen(width=80)
+ changed.feed((' ╰─ parity.py +1 -1\r\n-1 a b\r\n' + END).encode())
+ self.assertNotEqual(region(changed, 'parity.py'), rows)
+
+ def test_trailing_colored_blanks_cannot_compare_equal(self):
+ plain = DockScreen(width=80)
+ plain.feed((' ╰─ parity.py +1 -1\r\n-1 x \r\n' + END).encode())
+ for sgr in ('41', '31;7'):
+ with self.subTest(sgr=sgr):
+ colored = DockScreen(width=80)
+ colored.feed((' ╰─ parity.py +1 -1\r\n-1 x\x1b[' + sgr
+ + 'm \x1b[0m\r\n' + END).encode())
+ self.assertNotEqual(region(colored, 'parity.py'), region(plain, 'parity.py'))
+
+ def test_only_the_actual_replay_tool_lookup_is_handled(self):
+ server = FrameSupervisor.__new__(FrameSupervisor)
+ response = {'data': {'stale': True}}
+ self.assertTrue(server.extra_command({'type': 'get_tool_definition', 'name': 'ipython'}, response))
+ self.assertEqual(response, {'data': {}})
+ response = {'success': False, 'error': 'unknown'}
+ self.assertFalse(server.extra_command({'type': 'unrelated_command', 'name': 'ipython'}, response))
+ self.assertEqual(response, {'success': False, 'error': 'unknown'})
+
+ def test_missing_frame_boundary_cannot_be_compared_as_empty(self):
+ screen = DockScreen(width=80)
+ screen.feed(b' summary without the actual edited-file row')
+ with self.assertRaises(ValueError):
+ region(screen, 'parity.py')
+
+
+if __name__ == '__main__':
+ unittest.main()