A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix prime agent formatting breaking when resizing to a small screen (#227)
kt43aa9044af9f
diff --git a/packages/coding-agent/src/modes/interactive/components/ipython-cell.ts b/packages/coding-agent/src/modes/interactive/components/ipython-cell.tsindex 67533cf233..3f9282aff2 100644--- a/packages/coding-agent/src/modes/interactive/components/ipython-cell.ts+++ b/packages/coding-agent/src/modes/interactive/components/ipython-cell.ts@@ -77,6 +77,43 @@ const COMMENT_LINE_PATTERN = /^\s*#/;// Strip a leading `cd … &&` to surface the real command.const CD_PREFIX_PATTERN = /^\s*cd\s+[^&;|]+(?:&&|;)\s*/;+const SGR_PATTERN = /\x1b\[([0-9;]*)m/g;++/**+ * Append `ESC[0m` when `line` ends with a foreground or background color still+ * open, so a span that wrapTextWithAnsi split across lines cannot bleed into the+ * trailing padding or the next line.+ */+function closeOpenSgr(line: string): string {+ let fgOpen = false;+ let bgOpen = false;+ for (const match of line.matchAll(SGR_PATTERN)) {+ const params = match[1] === "" ? ["0"] : match[1].split(";");+ for (let i = 0; i < params.length; i++) {+ const code = Number(params[i]);+ if (code === 0) {+ fgOpen = false;+ bgOpen = false;+ } else if (code === 38 || code === 48) {+ // Skip the color data of `38;5;n` / `38;2;r;g;b` so a component (e.g. 38) isn't read as a code.+ if (code === 38) fgOpen = true;+ else bgOpen = true;+ const mode = Number(params[i + 1]);+ i += mode === 2 ? 4 : mode === 5 ? 2 : 1;+ } else if (code === 39) {+ fgOpen = false;+ } else if (code === 49) {+ bgOpen = false;+ } else if ((code >= 30 && code <= 37) || (code >= 90 && code <= 97)) {+ fgOpen = true;+ } else if ((code >= 40 && code <= 47) || (code >= 100 && code <= 107)) {+ bgOpen = true;+ }+ }+ }+ return fgOpen || bgOpen ? `${line}\x1b[0m` : line;+}+function collapseWhitespace(text: string): string {return text.replace(/\s+/g, " ").trim();}@@ -693,7 +730,7 @@ export class IPythonCellComponent implements Component {const wrapped = wrapTextWithAnsi(text, available);for (const [index, line] of (wrapped.length > 0 ? wrapped : [""]).entries()) {const linePrefix = index === 0 ? prefix : " ".repeat(visibleWidth(prefix));- lines.push(toolPanelLine(linePrefix + line, width));+ lines.push(toolPanelLine(linePrefix + closeOpenSgr(line), width));}}diff --git a/packages/coding-agent/test/ipython-cell-wrap.test.ts b/packages/coding-agent/test/ipython-cell-wrap.test.tsnew file mode 100644index 0000000000..c359f1ad74--- /dev/null+++ b/packages/coding-agent/test/ipython-cell-wrap.test.ts@@ -0,0 +1,90 @@+import { visibleWidth } from "@earendil-works/pi-tui";+import { beforeAll, describe, expect, it } from "vitest";+import { IPythonCellComponent } from "../src/modes/interactive/components/ipython-cell.js";+import { initTheme } from "../src/modes/interactive/theme/theme.js";++type CellState = ConstructorParameters<typeof IPythonCellComponent>[0];++// True when `line` ends with a foreground color still open (a leak into the padding).+function foregroundLeftOpen(line: string): boolean {+ let fg = false;+ for (const match of line.matchAll(/\x1b\[([0-9;]*)m/g)) {+ const params = match[1] === "" ? ["0"] : match[1].split(";");+ for (let i = 0; i < params.length; i++) {+ const code = Number(params[i]);+ if (code === 0 || code === 39) {+ fg = false;+ } else if (code === 38) {+ // Skip the color data of 38;5;n / 38;2;r;g;b so a component isn't read as a code.+ fg = true;+ const mode = Number(params[i + 1]);+ i += mode === 2 ? 4 : mode === 5 ? 2 : 1;+ } else if (code === 48) {+ const mode = Number(params[i + 1]);+ i += mode === 2 ? 4 : mode === 5 ? 2 : 1;+ } else if ((code >= 30 && code <= 37) || (code >= 90 && code <= 97)) {+ fg = true;+ }+ }+ }+ return fg;+}++const WRAPPING_STATE: CellState = {+ code: "import numpy as np\nresult = np.linspace(0, 100, 50)\nprint('the first element of the linspace array is', result[0])",+ content: [+ {+ type: "text",+ text: "the first element of the linspace array is 0.0\nsecond line of output that is also fairly long and will wrap on a small terminal",+ },+ ],+ details: {+ status: "ok",+ durationMs: 12,+ stdout:+ "the first element of the linspace array is 0.0\nsecond line of output that is also fairly long and will wrap on a small terminal",+ },+ executionStarted: true,+ argsComplete: true,+ expanded: true,+};++describe("IPythonCellComponent wrapping", () => {+ beforeAll(() => {+ initTheme("dark");+ });++ it("never leaves a foreground color open at a wrapped line end", () => {+ for (let width = 20; width <= 60; width++) {+ const lines = new IPythonCellComponent(WRAPPING_STATE).render(width);+ const leaks = lines.filter(foregroundLeftOpen);+ expect(leaks, `width=${width} leaked foreground on ${leaks.length} line(s)`).toHaveLength(0);+ }+ });++ it("pads every wrapped line to exactly the panel width", () => {+ for (const width of [20, 30, 40, 50]) {+ const lines = new IPythonCellComponent(WRAPPING_STATE).render(width);+ expect(+ lines.every((line) => visibleWidth(line) === width),+ `width=${width}`,+ ).toBe(true);+ }+ });++ it("renders the same after a resize as a fresh render at the target width", () => {+ const resized = new IPythonCellComponent(WRAPPING_STATE);+ resized.render(100);+ resized.invalidate();+ const afterResize = resized.render(34);++ const fresh = new IPythonCellComponent(WRAPPING_STATE).render(34);+ expect(afterResize).toEqual(fresh);+ });++ it("leaves non-wrapping (wide) output untouched", () => {+ const lines = new IPythonCellComponent(WRAPPING_STATE).render(100);+ expect(lines.some(foregroundLeftOpen)).toBe(false);+ expect(lines.every((line) => visibleWidth(line) === 100)).toBe(true);+ });+});