Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: limit console outputs to last 5000 lines per cell (#1424)

Akshay Agrawal82df49587fe0
diff --git a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts
index e5e0c65bdc..3d9af46b4b 100644
--- a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts
+++ b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts
@@ -144,4 +144,96 @@ describe("collapseConsoleOutputs", () => {
expect(result[0]).not.toBe(consoleOutputs[0]);
expect(result[1]).not.toBe(consoleOutputs[1]);
});
+
+ it("should truncate head of text/plain single message", () => {
+ const consoleOutputs: OutputMessage[] = [
+ {
+ mimetype: "text/plain",
+ channel: "output",
+ data: "Hello\nWorld\nBye\nWorld",
+ timestamp: 0,
+ },
+ ];
+ const result = collapseConsoleOutputs(consoleOutputs, 2);
+ expect(result.length).toBe(2);
+ // First result is a warning re: truncation
+ expect(result[0].data).toContain("Streaming output truncated");
+ // First two lines are truncated
+ expect(result[1].data).toBe("Bye\nWorld");
+ });
+
+ it("should truncate head of text/plain multiple channels", () => {
+ const consoleOutputs: OutputMessage[] = [
+ {
+ mimetype: "text/plain",
+ channel: "stdout",
+ data: "A\nB\nC\nD\n",
+ timestamp: 0,
+ },
+ {
+ mimetype: "text/plain",
+ channel: "stderr",
+ data: "E\nF\nG\nH\n",
+ timestamp: 0,
+ },
+ ];
+ const result = collapseConsoleOutputs(consoleOutputs, 7);
+ expect(result.length).toBe(3);
+ // First result is a warning re: truncation
+ expect(result[0].data).toContain("Streaming output truncated");
+ // First two lines are truncated, leaving 2 lines
+ expect(result[1].data).toBe("D\n");
+ // No truncation: 5 lines, for a total of 2 + 5 = 7 lines
+ expect(result[2].data).toBe("E\nF\nG\nH\n");
+ });
+
+ it("should truncate head of text/plain same channel", () => {
+ const consoleOutputs: OutputMessage[] = [
+ {
+ mimetype: "text/plain",
+ channel: "stdout",
+ data: "A\nB\nC\nD\n",
+ timestamp: 0,
+ },
+ {
+ mimetype: "text/plain",
+ channel: "stdout",
+ data: "E\nF\nG\nH\n",
+ timestamp: 0,
+ },
+ ];
+ const result = collapseConsoleOutputs(consoleOutputs, 7);
+ // 1 warning message, and 1 message containing merged stdout
+ expect(result.length).toBe(2);
+ // First result is a warning re: truncation
+ expect(result[0].data).toContain("Streaming output truncated");
+ // First two lines are truncated
+ expect(result[1].data).toBe("C\nD\nE\nF\nG\nH\n");
+ });
+
+ it("should truncate head with text/html counting as one line", () => {
+ const consoleOutputs: OutputMessage[] = [
+ {
+ mimetype: "text/plain",
+ channel: "stdout",
+ data: "A\nB\nC\nD\n",
+ timestamp: 0,
+ },
+ {
+ mimetype: "text/html",
+ channel: "output",
+ data: "<pre>E\nF\nG\nH\n</pre>",
+ timestamp: 0,
+ },
+ ];
+ const result = collapseConsoleOutputs(consoleOutputs, 3);
+ // 1 warning message, and 1 message containing merged stdout
+ expect(result.length).toBe(3);
+ // First result is a warning re: truncation
+ expect(result[0].data).toContain("Streaming output truncated");
+ // 2 lines
+ expect(result[1].data).toBe("D\n");
+ // heuristic: non-text counts as 1 line ...
+ expect(result[2].data).toBe("<pre>E\nF\nG\nH\n</pre>");
+ });
});
diff --git a/frontend/src/core/cells/collapseConsoleOutputs.tsx b/frontend/src/core/cells/collapseConsoleOutputs.tsx
index e9b6ca52b5..092e72eff5 100644
--- a/frontend/src/core/cells/collapseConsoleOutputs.tsx
+++ b/frontend/src/core/cells/collapseConsoleOutputs.tsx
@@ -8,11 +8,12 @@ import { invariant } from "@/utils/invariant";
*/
export function collapseConsoleOutputs(
consoleOutputs: OutputMessage[],
+ maxLines = 5000,
): OutputMessage[] {
const newConsoleOutputs = [...consoleOutputs];
if (newConsoleOutputs.length < 2) {
- return handleCarriageReturns(newConsoleOutputs);
+ return truncateHead(handleCarriageReturns(newConsoleOutputs), maxLines);
}
const lastOutput = newConsoleOutputs[newConsoleOutputs.length - 1];
@@ -26,7 +27,7 @@ export function collapseConsoleOutputs(
newConsoleOutputs.pop();
}
- return handleCarriageReturns(newConsoleOutputs);
+ return truncateHead(handleCarriageReturns(newConsoleOutputs), maxLines);
}
function shouldCollapse(
@@ -44,6 +45,7 @@ function shouldCollapse(
return isTextPlain && isSameChannel && isNotStdin;
}
+
function handleCarriageReturns(
consoleOutputs: OutputMessage[],
): OutputMessage[] {
@@ -89,3 +91,41 @@ function handleCarriageReturns(
};
return newConsoleOutputs;
}
+
+function truncateHead(consoleOutputs: OutputMessage[], limit: number) {
+ let nLines = 0;
+ let i;
+ for (i = consoleOutputs.length - 1; i >= 0 && nLines < limit; i--) {
+ const output = consoleOutputs[i];
+ if (output.mimetype === "text/plain") {
+ nLines += output.data.split("\n").length;
+ } else {
+ nLines++;
+ }
+ }
+
+ if (nLines < limit) {
+ return consoleOutputs;
+ }
+
+ const cutoff = i + 1;
+ const warningOutput: OutputMessage = {
+ channel: "stderr",
+ mimetype: "text/html",
+ data: `<pre>Streaming output truncated to last ${limit} lines.\n</pre>`,
+ timestamp: -1,
+ };
+ const output = consoleOutputs[cutoff];
+ if (output.mimetype == "text/plain") {
+ const output_lines = output.data.split("\n");
+ const nLinesAfterOutput = nLines - output_lines.length;
+ const nLinesToKeep = limit - nLinesAfterOutput;
+ return [
+ warningOutput,
+ { ...output, data: output_lines.slice(-nLinesToKeep).join("\n") },
+ ...consoleOutputs.slice(cutoff + 1),
+ ];
+ } else {
+ return [warningOutput, ...consoleOutputs.slice(cutoff + 1)];
+ }
+}