MusePi

Notebook file runtime internals

This document describes current .ipynb handling in coding-agent and its relationship to the kernel-backed Python runtime.

The critical distinction: notebook support is file conversion/editing, not notebook execution. .ipynb files are exposed as editable cell-marked text through read and the edit pipeline; no notebook-specific tool starts or talks to a Python kernel.

Implementation files

1) Runtime boundary: editing vs executing

.ipynb file conversion (src/edit/notebook.ts)

No kernel lifecycle exists in this path:

Kernel-backed execution path (src/tools/eval.ts + src/eval/py/*)

When the agent needs to run cell-style Python code (sequential cells, persistent state, rich displays), that goes through the eval tool with per-cell language: "py", not through notebook file handling.

That path is where Python subprocess lifecycle, reset/cancel behavior, chunk streaming, rich displays, and output artifact truncation live.

2) Notebook cell handling semantics

Source normalization

Notebook JSON source is converted to virtual text by joining source arrays. When virtual text is serialized back, cell source is split with newline preservation:

This mirrors notebook JSON conventions and avoids accidental line concatenation on later edits.

Marker parsing and cell preservation

Error surfaces

Hard failures are thrown for:

These surface through the caller (read, edit, or write) as normal tool errors.

3) Kernel session semantics (where they actually exist)

Kernel semantics are implemented in executePython / PythonKernel and apply to the Python backend of the eval tool.

Modes

PythonKernelMode:

Reset behavior

Each eval cell has its own optional reset flag. reset: true resets the selected Python session before that cell executes; it is not a top-level tool parameter.

Kernel death / restart / retry

In session mode:

4) Environment/session variable injection

Kernel startup and per-execution environment patching can receive:

The runner initializes process state so code executes in the requested cwd, managed env entries are reflected in os.environ, and cwd is available on sys.path.

5) Streaming/chunk and display handling (kernel-backed path)

The Python backend uses an NDJSON subprocess runner. The host processes frames per execution:

Display text MIME precedence:

  1. text/markdown
  2. text/plain
  3. converted text/html

Structured outputs captured separately include:

Cancellation/timeout:

6) Truncation and artifact behavior

OutputSink in src/session/streaming-output.ts is used by kernel execution paths:

eval converts this metadata into result truncation notices and TUI warnings.

Notebook file conversion does not use OutputSink; it has no stream/artifact truncation pipeline because it does not execute code.

7) Renderer assumptions and formatting

Read/edit notebook representation

Notebook files are rendered to the model as text. The visible cell markers are part of the editable representation, not comments that are ignored during serialization.

Python renderer (for actual execution output)

Kernel-backed execution rendering expects:

This renderer behavior is unrelated to notebook JSON editing except that both reuse shared TUI primitives.

8) Practical workflow

If a workflow needs both notebook mutation and execution:

  1. read or edit the .ipynb file through the normal file tools
  2. copy the desired cell source into eval cells with language: "py" to execute it
  3. write resulting source changes back to the notebook if needed

Current implementation does not provide a single tool that both mutates .ipynb and executes notebook cells through kernel context.