Skip to content

Fix console output routing for code_mode run_cell#8790

Merged
manzt merged 1 commit intomainfrom
push-vzpqqqknkonp
Mar 20, 2026
Merged

Fix console output routing for code_mode run_cell#8790
manzt merged 1 commit intomainfrom
push-vzpqqqknkonp

Conversation

@manzt
Copy link
Copy Markdown
Collaborator

@manzt manzt commented Mar 20, 2026

When code_mode runs cells via ctx.run_cell(), execution happens inside
the scratchpad's redirect_streams context. The nested
redirect_streams call for the target cell was a complete no-op, so all
print output was tagged with SCRATCH_CELL_ID instead of the actual
cell ID. The frontend never associated it with the correct cell.

The fix is narrowly scoped: only when the outer context is the
scratchpad do we swap stream.cell_id to the target cell. Regular
nested contexts (e.g. post-execution hooks running within a cell's
context) preserve the existing no-op behavior.

@manzt manzt requested a review from dmadisetti as a code owner March 20, 2026 15:03
Copilot AI review requested due to automatic review settings March 20, 2026 15:03
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Mar 20, 2026 3:16pm

Request Review

@manzt manzt added enhancement New feature or request bug Something isn't working and removed enhancement New feature or request labels Mar 20, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect console output cell attribution when redirect_streams is entered recursively (e.g., code_mode executing a target cell while already inside the scratchpad’s redirect context), by swapping stream.cell_id for the nested scope and restoring it on exit.

Changes:

  • Update redirect_streams to swap stream.cell_id in nested contexts and restore it in a finally block.
  • Add runtime tests covering nested/non-nested behavior and restoration on exceptions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
marimo/_runtime/redirect_streams.py Implements nested-context cell_id swapping/restoration so console routing targets the correct cell.
tests/_runtime/test_redirect_streams.py Adds regression tests validating nested swap + restoration (including exception paths).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

When code_mode runs cells via `ctx.run_cell()`, execution happens inside
the scratchpad's `redirect_streams` context. The nested
`redirect_streams` call for the target cell was a complete no-op, so all
print output was tagged with `SCRATCH_CELL_ID` instead of the actual
cell ID. The frontend never associated it with the correct cell.

The fix is narrowly scoped: only when the outer context is the
scratchpad do we swap `stream.cell_id` to the target cell. Regular
nested contexts (e.g. post-execution hooks running within a cell's
context) preserve the existing no-op behavior.
@manzt manzt changed the title Fix console output routing in nested redirect_streams Fix console output routing for code_mode run_cell Mar 20, 2026
@manzt manzt force-pushed the push-vzpqqqknkonp branch from a59dc2a to ab7425b Compare March 20, 2026 15:15
@manzt manzt merged commit 928a2e2 into main Mar 20, 2026
45 checks passed
@manzt manzt deleted the push-vzpqqqknkonp branch March 20, 2026 17:29
@github-actions
Copy link
Copy Markdown

🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.21.2-dev20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants