Skip to content

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

Rust

fix(coding-agent): preserve diff syntax highlighting (#405)

kt67aa09d407c4
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md
index 9a2424649f..43ec95b972 100644
--- a/packages/coding-agent/CHANGELOG.md
+++ b/packages/coding-agent/CHANGELOG.md
@@ -10,6 +10,7 @@
- Fixed update restarts starting concurrent daemon supervisors or unlinking a replacement supervisor's socket ([ENG-4600](https://linear.app/primeintellect/issue/ENG-4600/prevent-concurrent-daemon-supervisors-after-update-restart)).
- Changed provider, model, and MCP setup to use one tabbed configuration menu ([ENG-4539](https://linear.app/primeintellect/issue/ENG-4539/unify-providers-models-and-mcp-connections-menu)).
- Changed the shared configuration menu to show prominent, responsive tabs with configurable navigation shortcuts ([ENG-4534](https://linear.app/primeintellect/issue/ENG-4534/make-login-tabs-more-obvious)).
+- Fixed IPython edit diffs replacing syntax highlighting with a single foreground color ([ENG-4616](https://linear.app/primeintellect/issue/ENG-4616/syntax-highlighting-is-overridden-in-diff-view)).
- Fixed Prime Inference login leaving new sessions without a persisted model selection ([ENG-4573](https://linear.app/primeintellect/issue/ENG-4573/prompt-for-model-selection-after-prime-inference-login)).
- Fixed empty prompt placeholders hiding the input caret.
- Fixed automatic model selection preferring other configured providers over Prime Inference's GLM 5.2 default.
diff --git a/packages/coding-agent/src/modes/interactive/components/diff.ts b/packages/coding-agent/src/modes/interactive/components/diff.ts
index 5e22bc1110..93aff236ab 100644
--- a/packages/coding-agent/src/modes/interactive/components/diff.ts
+++ b/packages/coding-agent/src/modes/interactive/components/diff.ts
@@ -267,7 +267,7 @@ export function renderRichDiff(diffText: string, contentWidth: number, options:
content: text,
language,
width,
- contentFg: useBlocks ? "toolDiffText" : "toolDiffAdded",
+ contentFg: useBlocks ? undefined : "toolDiffAdded",
}),
);
} else if (prefix === "-") {
@@ -280,7 +280,7 @@ export function renderRichDiff(diffText: string, contentWidth: number, options:
content: text,
language,
width,
- contentFg: useBlocks ? "toolDiffText" : "toolDiffRemoved",
+ contentFg: useBlocks ? undefined : "toolDiffRemoved",
}),
);
} else {
diff --git a/packages/coding-agent/test/ipython-cell-diff.test.ts b/packages/coding-agent/test/ipython-cell-diff.test.ts
index 826e089619..1af51bba6f 100644
--- a/packages/coding-agent/test/ipython-cell-diff.test.ts
+++ b/packages/coding-agent/test/ipython-cell-diff.test.ts
@@ -1,7 +1,8 @@
import { visibleWidth } from "@earendil-works/pi-tui";
-import { beforeAll, describe, expect, it } from "vitest";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+import { renderRichDiff } from "../src/modes/interactive/components/diff.js";
import { IPythonCellComponent } from "../src/modes/interactive/components/ipython-cell.js";
-import { initTheme } from "../src/modes/interactive/theme/theme.js";
+import { initTheme, preloadCodeHighlighter, theme } from "../src/modes/interactive/theme/theme.js";
function stripAnsi(text: string): string {
return text.replace(/\x1b\[[0-9;]*m/g, "");
@@ -11,6 +12,23 @@ function hasBackground(line: string): boolean {
return /\x1b\[4[0-9]m|\x1b\[48[;:]/.test(line);
}
+type ThemeFg = Parameters<typeof theme.fg>[0];
+type ThemeBg = Parameters<typeof theme.bg>[0];
+
+function fgAnsi(color: ThemeFg): string {
+ return theme.fg(color, "").replace(/\x1b\[39m$/, "");
+}
+
+function bgAnsi(color: ThemeBg): string {
+ return theme.bg(color, "").replace(/\x1b\[49m$/, "");
+}
+
+function changedRow(rows: readonly string[], prefix: "+" | "-"): string {
+ const row = rows.find((line) => stripAnsi(line).startsWith(` 1 ${prefix} `));
+ expect(row).toBeDefined();
+ return row ?? "";
+}
+
function renderCell(state: ConstructorParameters<typeof IPythonCellComponent>[0]): string {
return stripAnsi(new IPythonCellComponent(state).render(80).join("\n"));
}
@@ -317,3 +335,111 @@ describe("IPythonCellComponent diff rendering", () => {
}
});
});
+
+describe("renderRichDiff syntax highlighting", () => {
+ let previousColorTerm: string | undefined;
+ let previousTerm: string | undefined;
+ let previousWtSession: string | undefined;
+
+ beforeAll(async () => {
+ previousColorTerm = process.env.COLORTERM;
+ previousTerm = process.env.TERM;
+ previousWtSession = process.env.WT_SESSION;
+ await preloadCodeHighlighter();
+ });
+
+ afterAll(() => {
+ if (previousColorTerm === undefined) {
+ delete process.env.COLORTERM;
+ } else {
+ process.env.COLORTERM = previousColorTerm;
+ }
+ if (previousTerm === undefined) {
+ delete process.env.TERM;
+ } else {
+ process.env.TERM = previousTerm;
+ }
+ if (previousWtSession === undefined) {
+ delete process.env.WT_SESSION;
+ } else {
+ process.env.WT_SESSION = previousWtSession;
+ }
+ initTheme("dark");
+ });
+
+ function useTruecolor(themeName: "dark" | "light" = "dark"): void {
+ process.env.COLORTERM = "truecolor";
+ process.env.TERM = "xterm-256color";
+ initTheme(themeName);
+ }
+
+ function use256Color(): void {
+ delete process.env.COLORTERM;
+ delete process.env.WT_SESSION;
+ process.env.TERM = "dumb";
+ initTheme("dark");
+ }
+
+ it("keeps syntax token foregrounds on truecolor added and removed backgrounds", () => {
+ useTruecolor();
+ const rows = renderRichDiff("-1 const answer: number = 1;\n+1 const answer: number = 2;", 72, {
+ language: "typescript",
+ });
+ const removed = changedRow(rows, "-");
+ const added = changedRow(rows, "+");
+
+ expect(removed).toContain(bgAnsi("toolDiffRemovedBg"));
+ expect(added).toContain(bgAnsi("toolDiffAddedBg"));
+ for (const row of [removed, added]) {
+ expect(row).toContain(fgAnsi("syntaxKeyword"));
+ expect(row).toContain(fgAnsi("syntaxType"));
+ expect(row).toContain(fgAnsi("syntaxNumber"));
+ expect(row).not.toContain(fgAnsi("toolDiffText"));
+ }
+ });
+
+ it("uses the code-block foreground when the language is unknown", () => {
+ useTruecolor();
+ const rows = renderRichDiff("-1 old value\n+1 new value", 48, { language: "not-a-language" });
+
+ expect(changedRow(rows, "-")).toContain(fgAnsi("mdCodeBlock"));
+ expect(changedRow(rows, "+")).toContain(fgAnsi("mdCodeBlock"));
+ });
+
+ it("keeps the flat red and green fallback in 256-color mode", () => {
+ use256Color();
+ const rows = renderRichDiff("-1 const answer: number = 1;\n+1 const answer: number = 2;", 72, {
+ language: "typescript",
+ });
+ const removed = changedRow(rows, "-");
+ const added = changedRow(rows, "+");
+
+ expect(removed).toContain(bgAnsi("toolPanelBg"));
+ expect(removed).toContain(fgAnsi("toolDiffRemoved"));
+ expect(added).toContain(bgAnsi("toolPanelBg"));
+ expect(added).toContain(fgAnsi("toolDiffAdded"));
+ expect(removed).not.toContain(fgAnsi("syntaxKeyword"));
+ expect(added).not.toContain(fgAnsi("syntaxKeyword"));
+ });
+
+ it("preserves themed syntax and the diff background across wrapped rows", () => {
+ const source = `const values: number[] = [${Array.from({ length: 20 }, (_, index) => index).join(", ")}];`;
+ for (const themeName of ["dark", "light"] as const) {
+ useTruecolor(themeName);
+ const keyword = fgAnsi("syntaxKeyword");
+ const background = bgAnsi("toolDiffAddedBg");
+ const rows = renderRichDiff(`+1 ${source}`, 28, { language: "typescript" });
+
+ expect(rows.length).toBeGreaterThan(1);
+ expect(rows.map(stripAnsi).join("")).toContain("19");
+ for (const row of rows) {
+ expect(visibleWidth(row)).toBe(28);
+ expect(row).toContain(background);
+ expect(row).not.toContain("\x1b[0m");
+ expect(row.match(/\x1b\[49m/g)).toHaveLength(1);
+ expect(row.endsWith("\x1b[49m")).toBe(true);
+ }
+ expect(rows.join("")).toContain(keyword);
+ }
+ });
+});