mirror of
https://github.com/stablyai/orca.git
synced 2026-09-27 00:02:37 +00:00
* feat(ipynb): run notebook cells in a persistent Jupyter kernel Replaces the fresh-process runner (which silently re-ran every earlier cell) with the user's own ipykernel, driven by a small bundled Python bridge over line-framed JSON. One kernel per open notebook: started on first Run, shut down when its tab closes or Orca exits (stdin EOF), and the kernel's own parent poller reaps it if the bridge dies. The header gains a kernel pill (workspace .venv/.conda recommended, PATH interpreters, Browse), Interrupt/Restart/Run all/Clear all, and a one-time missing-ipykernel dialog with Install. Outputs stream live per cell and are written into the document when the run finishes. * test(ipynb): cover the stalled-interrupt restart offer * refactor(ipynb): disable the kernel pill while settling; merge its classes with cn * refactor(ipynb): tie kernels to their renderer document and simplify the run flow - Main keys kernels per renderer, so a reload, renderer crash or closed window shuts them down, and two windows never share one notebook kernel. One close-driven cleanup replaces the separate exit and start-failure deletions. - The first run uses the nearest workspace env, else the first Python on PATH; the picker no longer opens itself, so its open state stays in the toolbar. Closing the picker brings the missing-ipykernel dialog back instead of dropping the queue, which also keeps a Browse pick's run. - Discovery marks the kernel starting, so a second run during it queues instead of starting a second kernel, and a tab closed mid-discovery no longer leaks one. - Running an nbformat 4.4 notebook gives its cells ids (upgrading to 4.5), so moving a cell mid-run cannot misroute its output. - The death notice drops stderr from before the kernel was ready (the unencrypted-TCP warning). - SSH and non-Python runs toast instead of writing a notice into the cell. - Windows conda envs are named after their folder. * fix(ipynb): install ipykernel into envs without pip uv-created venvs ship without pip, so Install failed with 'No module named pip' there. When pip is missing, bootstrap it with the stdlib's ensurepip and retry. The install moves beside the other interpreter probes, and the copyable install command comes from one helper. * fix(ipynb): address PR review comments on stream errors, old jupyter_client and the Windows venv hint - Swallow stdout/stderr stream errors on the bridge child, as spawnProcess requires, so a broken pipe cannot crash main. - The bridge exits (reporting the death) even when cleanup_resources is missing (jupyter_client < 6.1.5) or raises. - The install-failure hint suggests `py -m venv .venv` on Windows. * feat(ipynb): add Cancel to the missing-ipykernel dialog It does what Esc does: drops the cells waiting on the kernel. * fix(ipynb): recover from a rejected kernel start; quote the install command per shell - Discovery moves into start, so one catch turns a rejected listPythonEnvironments or startKernel into the usual failed start: the session returns to off with the error in the cell, instead of sticking at starting. - The copyable ipykernel command quotes the interpreter only when its path has whitespace, prefixing PowerShell's call operator on Windows. Install itself still spawns without a shell. * fix(ipynb): always shell-quote the copyable ipykernel install command Quote the interpreter path for every path, not only ones with whitespace, so paths with shell metacharacters like & copy as a working command. Single quotes are literal in POSIX shells and PowerShell; embedded quotes are escaped per shell, and PowerShell keeps its & call operator.
110 lines
3.1 KiB
Python
110 lines
3.1 KiB
Python
"""Runs one Jupyter kernel for Orca in the interpreter that launched this script.
|
|
|
|
Protocol: one JSON object per line. Orca writes {"op": "execute", "code": ...} and
|
|
{"op": "interrupt"} to stdin; this writes {"type": ...} frames to stdout. Closing
|
|
stdin shuts the kernel down, so the kernel never outlives Orca. When the kernel
|
|
dies this process exits, so its exit is the one death signal Orca watches.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import queue
|
|
import sys
|
|
import threading
|
|
import time
|
|
|
|
# Frames own the real stdout; anything else writing to fd 1 (imports, C code) lands on stderr.
|
|
_frames = os.fdopen(os.dup(1), "wb", buffering=0)
|
|
os.dup2(2, 1)
|
|
sys.stdout = sys.stderr
|
|
_frames_lock = threading.Lock()
|
|
|
|
OUTPUT_TYPES = {
|
|
"stream",
|
|
"display_data",
|
|
"execute_result",
|
|
"update_display_data",
|
|
"clear_output",
|
|
"error",
|
|
}
|
|
|
|
|
|
def send(frame):
|
|
line = (json.dumps(frame) + "\n").encode("utf-8")
|
|
with _frames_lock:
|
|
_frames.write(line)
|
|
|
|
|
|
try:
|
|
import ipykernel # noqa: F401
|
|
from jupyter_client.kernelspec import KernelSpec
|
|
from jupyter_client.manager import KernelManager
|
|
except ImportError:
|
|
send({"type": "missing"})
|
|
sys.exit(0)
|
|
|
|
|
|
def forward(msg):
|
|
if msg["header"]["msg_type"] in OUTPUT_TYPES:
|
|
send({"type": msg["header"]["msg_type"], "content": msg["content"]})
|
|
|
|
|
|
def execute_all(client, codes):
|
|
while True:
|
|
code = codes.get()
|
|
# allow_stdin=False: input() raises a clear error instead of reading Orca's command pipe.
|
|
reply = client.execute_interactive(code, allow_stdin=False, output_hook=forward)
|
|
content = reply["content"]
|
|
send(
|
|
{
|
|
"type": "done",
|
|
"status": content.get("status"),
|
|
"execution_count": content.get("execution_count"),
|
|
}
|
|
)
|
|
|
|
|
|
def exit_when_dead(manager):
|
|
while manager.is_alive():
|
|
time.sleep(0.5)
|
|
# Exit even if cleanup fails (cleanup_resources is missing before jupyter_client 6.1.5).
|
|
try:
|
|
manager.cleanup_resources()
|
|
finally:
|
|
os._exit(1)
|
|
|
|
|
|
def main():
|
|
manager = KernelManager()
|
|
# Why: a user-level "python3" kernelspec may point at another interpreter; run this one.
|
|
manager._kernel_spec = KernelSpec(
|
|
argv=[sys.executable, "-m", "ipykernel_launcher", "-f", "{connection_file}"],
|
|
display_name="Python",
|
|
language="python",
|
|
)
|
|
manager.start_kernel()
|
|
client = manager.client()
|
|
client.start_channels()
|
|
try:
|
|
client.wait_for_ready(timeout=60)
|
|
except RuntimeError:
|
|
manager.shutdown_kernel(now=True)
|
|
sys.exit(1)
|
|
|
|
codes = queue.Queue()
|
|
threading.Thread(target=execute_all, args=(client, codes), daemon=True).start()
|
|
threading.Thread(target=exit_when_dead, args=(manager,), daemon=True).start()
|
|
send({"type": "ready"})
|
|
|
|
for line in sys.stdin.buffer:
|
|
command = json.loads(line)
|
|
if command["op"] == "execute":
|
|
codes.put(command["code"])
|
|
elif command["op"] == "interrupt":
|
|
manager.interrupt_kernel()
|
|
manager.shutdown_kernel(now=True)
|
|
os._exit(0)
|
|
|
|
|
|
main()
|