The best way to control Claude Code, Codex and more from any computer or phone
fix(web): preserve saved notebook traceback frame boundaries (#36602)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 14, 2026 at 10:27 UTC
4574c6013d52867878038967297a885f1692383c
2 files changed
+69
-1
apps/web/src/components/files/FilePreviewPanel.test.ts
+58
@@ -296,6 +296,64 @@ Third}`;
296
},
297
],
298
});
299
+ // IPython traceback frames can omit trailing newlines or place ANSI resets after them.
300
+ const failed = openNotebook(
301
+ bytes(
302
+ JSON.stringify({
303
+ nbformat: 4,
304
+ cells: [
305
+ {
306
+ cell_type: "code",
307
+ source: "fail()",
308
+ outputs: [
309
+ {
310
+ output_type: "error",
311
+ traceback: [
312
+ "-----",
313
+ "ValueError Traceback (most recent call last)",
314
+ "Cell In[1], line 4\n----> 4 fail()\n",
315
+ "Cell In[1], line 2, in fail()\n----> 2 raise ValueError()\n",
316
+ "ValueError: preview proof",
317
+ ],
318
+ },
319
+ {
320
+ output_type: "error",
321
+ traceback: [
322
+ "Traceback (most recent call last):\n",
323
+ " Cell In[2], line 5\n fail()\n",
324
+ "ValueError: preview proof\n",
325
+ ],
326
+ },
327
+ {
328
+ output_type: "error",
329
+ traceback: [
330
+ "\u001b[31mTraceback (most recent call last):\n\u001b[0m",
331
+ "\u001b[32mCell In[3], line 1\n----> 1 fail()\n\u001b[0m\u001b[39m",
332
+ "\u001b[31mValueError: preview proof\u001b[0m",
333
+ "final entry",
334
+ ],
335
+ },
336
+ ],
337
+ },
338
+ ],
339
+ }),
340
+ ),
341
+ );
342
+ if (failed.type !== "notebook") throw new Error("Expected notebook");
343
+ expect(failed.cells[0]!.outputs).toEqual([
344
+ {
345
+ kind: "text",
346
+ text: "-----\nValueError Traceback (most recent call last)\nCell In[1], line 4\n----> 4 fail()\nCell In[1], line 2, in fail()\n----> 2 raise ValueError()\nValueError: preview proof",
347
+ },
348
+ {
349
+ kind: "text",
350
+ text: "Traceback (most recent call last):\n Cell In[2], line 5\n fail()\nValueError: preview proof\n",
351
+ },
352
+ {
353
+ kind: "text",
354
+ text: "\u001b[31mTraceback (most recent call last):\n\u001b[0m\u001b[32mCell In[3], line 1\n----> 1 fail()\n\u001b[0m\u001b[39m\u001b[31mValueError: preview proof\u001b[0m\nfinal entry",
355
+ },
356
+ ]);
357
expect(() => openNotebook(bytes('{"nbformat":3,"cells":[]}'))).toThrow("version 4");
358
expect(() =>
359
openNotebook(
apps/web/src/components/files/extendedDocumentPreview.ts
+11
-1
@@ -423,6 +423,14 @@ function notebookText(value: unknown, limit = 256_000): string {
423
if (result.length > limit) throw new Error("A notebook cell is too large to preview.");
424
return result;
425
}
426
+// IPython traceback frames may omit a final newline or follow it with ANSI SGR sequences.
427
+function tracebackLines(value: unknown): unknown {
428
+ if (!Array.isArray(value)) return value;
429
+ const frames = value.filter((frame): frame is string => typeof frame === "string");
430
+ return frames.map((frame, index) =>
431
+ index && !/\n(?:\u001b\[[\d;:]*m)*$/.test(frames[index - 1]!) ? `\n${frame}` : frame,
432
+ );
433
+}
434
export function openNotebook(bytes: ArrayBuffer): OfficeWorkerResult {
435
if (bytes.byteLength > 8 * 1024 * 1024)
436
throw new Error("Notebook preview limited to files under 8 MB.");
@@ -454,7 +462,9 @@ export function openNotebook(bytes: ArrayBuffer): OfficeWorkerResult {
462
if (html.trim() && htmlImagesFit(html)) return { kind: "html" as const, text: html };
463
return {
464
kind: "text" as const,
457
- text: notebookText(output.text ?? data["text/plain"] ?? output.traceback),
465
+ text: notebookText(
466
+ output.text ?? data["text/plain"] ?? tracebackLines(output.traceback),
467
+ ),
468
};
469
})
470
.filter((output) => output.text.trim().length > 0),