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: Refactor console outputs to use a copy of the input array (#1289)

Myles Scolnick61c0c70b0cd9
diff --git a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts
index dcd4da638e..e5e0c65bdc 100644
--- a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts
+++ b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts
@@ -123,4 +123,25 @@ describe("collapseConsoleOutputs", () => {
!orld"
`);
});
+
+ it("doesn't mutate the input", () => {
+ const consoleOutputs: OutputMessage[] = [
+ {
+ mimetype: "text/plain",
+ channel: "output",
+ data: "Hello ",
+ timestamp: 0,
+ },
+ {
+ mimetype: "text/plain",
+ channel: "output",
+ data: "World",
+ timestamp: 0,
+ },
+ ];
+ const result = collapseConsoleOutputs(consoleOutputs);
+ expect(result).not.toBe(consoleOutputs);
+ expect(result[0]).not.toBe(consoleOutputs[0]);
+ expect(result[1]).not.toBe(consoleOutputs[1]);
+ });
});
diff --git a/frontend/src/core/cells/collapseConsoleOutputs.tsx b/frontend/src/core/cells/collapseConsoleOutputs.tsx
index 968b08df4d..e9b6ca52b5 100644
--- a/frontend/src/core/cells/collapseConsoleOutputs.tsx
+++ b/frontend/src/core/cells/collapseConsoleOutputs.tsx
@@ -9,22 +9,24 @@ import { invariant } from "@/utils/invariant";
export function collapseConsoleOutputs(
consoleOutputs: OutputMessage[],
): OutputMessage[] {
- if (consoleOutputs.length < 2) {
- return handleCarriageReturns(consoleOutputs);
+ const newConsoleOutputs = [...consoleOutputs];
+
+ if (newConsoleOutputs.length < 2) {
+ return handleCarriageReturns(newConsoleOutputs);
}
- const lastOutput = consoleOutputs[consoleOutputs.length - 1];
- const secondLastOutput = consoleOutputs[consoleOutputs.length - 2];
+ const lastOutput = newConsoleOutputs[newConsoleOutputs.length - 1];
+ const secondLastOutput = newConsoleOutputs[newConsoleOutputs.length - 2];
if (shouldCollapse(lastOutput, secondLastOutput)) {
invariant(typeof lastOutput.data === "string", "expected string");
invariant(typeof secondLastOutput.data === "string", "expected string");
secondLastOutput.data += lastOutput.data;
- consoleOutputs.pop();
+ newConsoleOutputs.pop();
}
- return handleCarriageReturns(consoleOutputs);
+ return handleCarriageReturns(newConsoleOutputs);
}
function shouldCollapse(
@@ -42,17 +44,17 @@ function shouldCollapse(
return isTextPlain && isSameChannel && isNotStdin;
}
-
function handleCarriageReturns(
consoleOutputs: OutputMessage[],
): OutputMessage[] {
- if (consoleOutputs.length === 0) {
- return consoleOutputs;
+ const newConsoleOutputs = [...consoleOutputs];
+ if (newConsoleOutputs.length === 0) {
+ return newConsoleOutputs;
}
- const lastOutput = consoleOutputs[consoleOutputs.length - 1];
+ const lastOutput = newConsoleOutputs[newConsoleOutputs.length - 1];
if (lastOutput.mimetype !== "text/plain") {
- return consoleOutputs;
+ return newConsoleOutputs;
}
// eslint-disable-next-line no-control-regex
@@ -81,6 +83,9 @@ function handleCarriageReturns(
carriageIdx = text.search(carriagePattern);
}
- lastOutput.data = text;
- return consoleOutputs;
+ newConsoleOutputs[newConsoleOutputs.length - 1] = {
+ ...lastOutput,
+ data: text,
+ };
+ return newConsoleOutputs;
}