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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix(ai): preserve child-cell traceback context (#11035)
Shahmir Varqha4ea8982c44de
diff --git a/marimo/_server/scratchpad.py b/marimo/_server/scratchpad.pyindex 85754b1851..4372846af5 100644--- a/marimo/_server/scratchpad.py+++ b/marimo/_server/scratchpad.py@@ -102,8 +102,11 @@ class ScratchCellListener(EventAwareExtension):super().__init__()self._queue: asyncio.Queue[CellNotification | None] = asyncio.Queue()self._run_id = run_id+ self._scratch_started = False+ self._completed = Falseself.timed_out = Falseself.child_error_summaries: list[str] = []+ self.stderr: list[str] = []def on_notification_sent(self, session: Session, notification: KernelMessage@@ -118,6 +121,7 @@ class ScratchCellListener(EventAwareExtension):# unrelated commands — skip them.if isinstance(msg, CompletedRunNotification):if msg.run_id == self._run_id:+ self._completed = Trueself._queue.put_nowait(None)return@@ -125,12 +129,25 @@ class ScratchCellListener(EventAwareExtension):returnif msg.cell_id == SCRATCH_CELL_ID:+ self._scratch_started = Trueself._queue.put_nowait(msg)else:+ # Ignore notifications from an execution that was already active+ # when this listener was attached. Child cells run by _code_mode+ # cannot execute before the scratch cell itself starts.+ if not self._scratch_started:+ returnif msg.console is not None:# Stream console output from cells run by _code_mode# during this scratchpad execution.self._queue.put_nowait(msg)++ # Keep the existing SSE grace period for trailing console output, but+ # freeze structured diagnostics at this request's completion boundary.+ if self._completed:+ return++ if msg.cell_id != SCRATCH_CELL_ID:if (msg.output is not Noneand msg.output.channel == CellChannel.MARIMO_ERROR@@ -149,10 +166,34 @@ class ScratchCellListener(EventAwareExtension):getattr(err, "exception_type", None)or type(err).__name__)+ raw_detail = getattr(err, "msg", "")+ detail = str(raw_detail or "").strip()+ empty_detail = f"This cell raised an exception: {exc_type}"+ if detail == empty_detail:+ detail = ""+ if detail == exc_type or detail.startswith(f"{exc_type}:"):+ diagnostic = detail+ elif detail:+ diagnostic = f"{exc_type}: {detail}"+ else:+ diagnostic = exc_typeself.child_error_summaries.append(- f"cell '{msg.cell_id}' raised {exc_type}"+ f"cell '{msg.cell_id}' raised {diagnostic}")+ if msg.console is not None:+ console_outputs = (+ msg.console if isinstance(msg.console, list) else [msg.console]+ )+ # `wait()` consumes queued notifications without formatting them,+ # so retain stderr in notification order for the structured tool+ # result.+ self.stderr.extend(+ str(output.data)+ for output in console_outputs+ if output is not None and output.channel == CellChannel.STDERR+ )+async def stream(self) -> AsyncGenerator[str, None]:"""Yield SSE-formatted stdout/stderr events until execution completes.@@ -306,7 +347,7 @@ def extract_result(continueif out.channel == CellChannel.STDOUT:stdout.append(str(out.data))- elif out.channel == CellChannel.STDERR:+ elif out.channel == CellChannel.STDERR and listener is None:stderr.append(str(out.data))errors: list[str] = []@@ -318,6 +359,7 @@ def extract_result(# Include child cell error summaries.if listener:+ stderr.extend(listener.stderr)errors.extend(listener.child_error_summaries)return CodeExecutionResult(diff --git a/tests/_server/test_scratchpad.py b/tests/_server/test_scratchpad.pyindex 678fa57a4c..1f6212943f 100644--- a/tests/_server/test_scratchpad.py+++ b/tests/_server/test_scratchpad.py@@ -312,6 +312,29 @@ class TestExtractResult:assert result.stdout == ["out1", "out2"]assert result.stderr == ["err1"]+ def test_listener_stderr_preserves_cross_cell_order(self) -> None:+ notif = CellNotification(+ cell_id=SCRATCH_CELL_ID,+ output=None,+ console=[+ CellOutput.stderr("scratch before\n"),+ CellOutput.stderr("scratch after\n"),+ ],+ status="idle",+ )+ listener = ScratchCellListener(run_id=_TEST_RUN_ID)+ listener.stderr.extend(+ ["scratch before\n", "child\n", "scratch after\n"]+ )++ result = extract_result(_make_session(notif), listener)++ assert result.stderr == [+ "scratch before\n",+ "child\n",+ "scratch after\n",+ ]+def test_errors(self) -> None:err_obj = MagicMock()err_obj.msg = "NameError: x is not defined"@@ -644,6 +667,12 @@ class TestScratchCellListener:session = MagicMock()listener.on_attach(session, event_bus)+ listener.on_notification_sent(+ session,+ serialize_kernel_message(+ CellNotification(cell_id=SCRATCH_CELL_ID, status="running")+ ),+ )other_console = CellNotification(cell_id="other_cell_id",console=CellOutput.stderr("error trace\n"),@@ -666,6 +695,81 @@ class TestScratchCellListener:assert name == "stderr"assert payload["data"] == "error trace\n"+ assert listener.stderr == ["error trace\n"]++ @pytest.mark.asyncio+ async def test_captures_stderr_in_emission_order(self) -> None:+ """Scratch and child stderr retain their notification order."""+ from marimo._messaging.serde import serialize_kernel_message++ listener = ScratchCellListener(run_id=_TEST_RUN_ID)+ session = MagicMock()++ notifications = [+ CellNotification(+ cell_id=SCRATCH_CELL_ID,+ console=CellOutput.stderr("scratch before\n"),+ ),+ CellNotification(+ cell_id="other_cell_id",+ console=[+ CellOutput.stderr("first child error\n"),+ CellOutput.stdout("ordinary output\n"),+ CellOutput.stderr("second child error\n"),+ ],+ ),+ CellNotification(+ cell_id=SCRATCH_CELL_ID,+ console=CellOutput.stderr("scratch after\n"),+ ),+ ]+ for notification in notifications:+ listener.on_notification_sent(+ session, serialize_kernel_message(notification)+ )++ assert listener.stderr == [+ "scratch before\n",+ "first child error\n",+ "second child error\n",+ "scratch after\n",+ ]++ @pytest.mark.asyncio+ async def test_ignores_stderr_outside_scratch_run(self) -> None:+ """Unrelated stderr before or after the scratch run is ignored."""+ from marimo._messaging.serde import serialize_kernel_message++ listener = ScratchCellListener(run_id=_TEST_RUN_ID)+ session = MagicMock()++ notifications: list[NotificationMessage] = [+ CellNotification(+ cell_id="browser-cell",+ console=CellOutput.stderr("before\n"),+ ),+ CellNotification(cell_id=SCRATCH_CELL_ID, status="running"),+ CellNotification(+ cell_id="child-cell",+ console=CellOutput.stderr("during\n"),+ ),+ _completed_run(),+ CellNotification(+ cell_id="child-cell",+ console=CellOutput.stderr("trailing\n"),+ ),+ CellNotification(+ cell_id="browser-cell",+ console=CellOutput.stderr("after\n"),+ ),+ ]+ for notification in notifications:+ listener.on_notification_sent(+ session, serialize_kernel_message(notification)+ )++ assert listener.stderr == ["during\n"]+@pytest.mark.asyncioasync def test_stream_cancelled_on_disconnect(self) -> None:"""stream() can be cancelled externally (simulating client disconnect)."""@@ -914,12 +1018,37 @@ class TestRunScratchpadCode:assert lock_held_during_interrupt == [True]@pytest.mark.asyncio- async def test_child_cell_errors_flow_into_result_errors(self) -> None:+ @pytest.mark.parametrize(+ ("message", "expected_error"),+ [+ (+ "division by zero",+ "cell 'child-cell' raised ZeroDivisionError: division by zero",+ ),+ (+ "ZeroDivisionError: division by zero",+ "cell 'child-cell' raised ZeroDivisionError: division by zero",+ ),+ (+ "ZeroDivisionError",+ "cell 'child-cell' raised ZeroDivisionError",+ ),+ ("", "cell 'child-cell' raised ZeroDivisionError"),+ (+ "This cell raised an exception: ZeroDivisionError",+ "cell 'child-cell' raised ZeroDivisionError",+ ),+ ],+ )+ async def test_child_cell_diagnostics_flow_into_result(+ self, message: str, expected_error: str+ ) -> None:"""End-to-end: child-cell errors captured by the listener during- execution must surface in `result.errors` — otherwise the AI- never learns its `run_cell` calls failed. This pins down the- `extract_result(session, listener)` plumbing as well; dropping- the `listener` arg silently loses every child-cell error."""+ execution must surface in the result with their message and traceback.+ Otherwise the AI learns only the exception type, not what failed or+ where. This also pins down the `extract_result(session, listener)`+ plumbing; dropping the listener silently loses every child-cell error.+ """from marimo._types.ids import CellId_tsession = _FakeSession()@@ -938,13 +1067,28 @@ class TestRunScratchpadCode:status="idle",))+ session.emit(+ CellNotification(cell_id=SCRATCH_CELL_ID, status="running")+ )+ session.emit(+ CellNotification(+ cell_id=CellId_t("child-cell"),+ console=CellOutput.stderr(+ "Traceback (most recent call last):\n"+ ' File "<cell-child-cell>", line 2, in <module>\n'+ " return 1 / 0\n"+ " ~~^~~\n"+ "ZeroDivisionError: division by zero\n"+ ),+ )+ )session.emit(CellNotification(cell_id=CellId_t("child-cell"),output=CellOutput.errors([MarimoExceptionRaisedError(- msg="division by zero",+ msg=message,exception_type="ZeroDivisionError",raising_cell=None,)@@ -964,7 +1108,16 @@ class TestRunScratchpadCode:)assert result.success is False- assert result.errors == ["cell 'child-cell' raised ZeroDivisionError"]+ assert result.errors == [expected_error]+ assert result.stderr == [+ (+ "Traceback (most recent call last):\n"+ ' File "<cell-child-cell>", line 2, in <module>\n'+ " return 1 / 0\n"+ " ~~^~~\n"+ "ZeroDivisionError: division by zero\n"+ )+ ]@pytest.mark.asyncioasync def test_listener_registered_only_while_lock_held(self) -> None: