Commit 612d578
authored
feat(python): redesign runtimed public API with high-level wrappers (#1030)
* feat(python): redesign runtimed public API with high-level wrappers
Replace the flat Session/AsyncSession API with a layered design:
- `Client` wraps `NativeAsyncClient`, returns `Notebook` objects
- `Notebook` wraps `AsyncSession` with sync reads and async writes
- `CellHandle` provides sync property access (source, cell_type,
outputs) and async mutations (set_source, run, delete)
- `CellCollection` on `notebook.cells` for sync iteration and
async cell creation
- `NotebookInfo` dataclass for structured room metadata
Rust changes:
- Add sync read methods to AsyncSession (blocking_lock for local
Automerge reads)
- Rename PyRuntimeState/PyKernelState/PyEnvState to clean names
via #[pyclass(name)]
- Rename Client/AsyncClient to NativeClient/NativeAsyncClient
- Rename list_rooms to list_active_notebooks
- Fix AsyncClient.list_rooms returning strings for int/bool fields
- Delete DaemonClient and deprecated Session/AsyncSession constructors
Closes #983
* fix(python): update demo files to use new runtimed API
Replace DaemonClient/Session references with NativeClient in
presence_cursor.py and test-presence.ipynb demos.
* fix(python): address review — add start_kernel, fix Windows path detection
- Add start_kernel() and shutdown_kernel() to Notebook so non-Python
runtimes (e.g. deno) can be launched through the high-level API
- Fix NotebookInfo.path to use Path.is_absolute() instead of checking
for "/" — works on both POSIX and Windows paths
* fix(runtimed): restart_kernel preserves kernel_type and env_source
Previously restart_kernel hardcoded kernel_type="python" and
env_source="auto", so restarting a deno or conda:inline kernel
would silently switch it to a prewarmed Python kernel.
Now captures the running kernel's type and env_source before
shutdown and re-launches with the same configuration.
* fix(python): address review — close lifecycle, docs, repr strings
- Make Session.close() and AsyncSession.close() actually disconnect by
dropping the doc handle and broadcast receiver
- Fix README examples to use asyncio.run() wrapper instead of bare await
- Update all first-party docs to use new API (docs/python-bindings.md,
contributing/runtimed.md, contributing/testing.md, SKILL.md)
- Fix NativeClient/NativeAsyncClient __repr__ to match their class names
* fix(session): hydrate kernel state, type mismatch error, queue_cell auto-start
Incorporates fixes from #1033:
- hydrate_kernel_state() reads RuntimeStateDoc at connect time to
populate kernel_started/kernel_type/env_source from the daemon's
source of truth (fixes stale state after auto-launch)
- start_kernel() now errors when requesting a different kernel type
than what's already running instead of silently succeeding
- queue_cell() gains ensure_kernel_started guard matching execute_cell
* style(runtimed): fix cargo fmt in queue_cell
* fix(python): add missing CellHandle properties and harden deleted-cell behavior (#1035)
* fix(python): add missing CellHandle properties and harden deleted-cell behavior
- Add tags, is_source_hidden, is_outputs_hidden sync properties to CellHandle
- Make outputs/metadata return defaults on deleted cells instead of raising
- Fix NotebookInfo.join() client parameter typing (Any -> Client)
- Make test_all_exports an exact set match against __all__ (27 items)
* docs: add supervisor-first guidance for agents managing dev daemons
CLAUDE.md, contributing/development.md, and contributing/runtimed.md all
documented the two-terminal cargo xtask dev-daemon workflow without
mentioning the supervisor as the preferred path. Agents with supervisor_*
tools available should use those instead of manual terminal commands.
- Add 'If you have supervisor tools, use them' section to CLAUDE.md
- Label manual commands as fallback for when supervisor isn't available
- Add supervisor workflow to runtimed.md fast iteration section
- Update development.md daemon section with supervisor-first note
* docs: clarify agents should not launch the notebook app
The app is a GUI process that blocks until the user cmd-q's it.
An agent running it in a terminal will misinterpret the exit.
Let the human launch it from their own terminal or Zed task.
* refactor(python): use Rust Cell helpers for tags/hidden, narrow exception catches
- tags, is_source_hidden, is_outputs_hidden delegate to Rust Cell
properties instead of manually parsing JSON metadata keys
- metadata property uses get_cell_metadata_sync (no blob I/O)
- except Exception narrowed to except RuntimedError so real errors surface
- Addresses copilot review feedback on #1035
* fix(python): remove Notebook.run() — ambiguous semantics (#1037)
* fix(python): remove Notebook.run() — ambiguous semantics
Notebook.run(code) created a persistent cell as a side effect, and the
name suggests 'run all cells' to anyone coming from Jupyter conventions.
The explicit two-step is clear and discoverable:
cell = await notebook.cells.create(source)
result = await cell.run()
Session.run() / session_core::run are kept — they're session-level
primitives used heavily in integration tests.
* docs: update examples to use cells.create + cell.run instead of notebook.run
* chore(python): clean up stale references and dead tests (#1039)
* chore(python): clean up stale references and dead tests
- Update runtimed-py lib.rs doc comment: DaemonClient -> NativeClient/NativeAsyncClient, list rooms -> list active notebooks
- Remove 4 unconditionally-skipped presence tests that tested a removed AsyncSession() constructor
- Fix stale DaemonClient reference in integration test comment
* fix(test): clarify pool warmup comment — socket != pools ready
* refactor(python): use runtime terminology in user-facing API (#1040)
* refactor(python): use runtime terminology in user-facing API
Jupyter kernels are an implementation detail — the wrapper layer should
speak in terms of runtimes. Native Session/AsyncSession methods keep
their kernel terminology (they ARE kernel operations).
Notebook:
start_kernel(kernel_type=) -> start(runtime=)
shutdown_kernel() -> shutdown()
restart_kernel() -> restart()
NotebookInfo:
kernel_type -> runtime_type
kernel_status -> status
has_kernel -> has_runtime
_from_dict still reads the daemon's kernel_* dict keys — the mapping
is internal to NotebookInfo.
* refactor(python): explicit notebook methods, drop is_ prefix on hidden props
Client:
create() -> create_notebook()
open() -> open_notebook()
join() -> join_notebook()
CellHandle:
is_source_hidden -> source_hidden
is_outputs_hidden -> outputs_hidden
Fix test_from_dict_ephemeral to use daemon's raw has_kernel key
and assert the mapped has_runtime field.
* fix: use session runtime type in ensure_kernel_started instead of hardcoding python (#1043)
ensure_kernel_started() hardcoded "python" as the kernel type when
auto-launching kernels. This caused a type mismatch error for deno
sessions: the daemon already had a deno kernel running, but the
Python binding requested "python".
Add a `runtime` field to SessionState that tracks the intended kernel
type, set at connection time:
- connect_create: from the runtime parameter
- connect_open: inferred from the notebook's kernelspec
- connect_with_socket (join): inferred from the notebook's kernelspec
ensure_kernel_started now reads state.runtime instead of hardcoding
"python".
* refactor(python): split save into save() and save_as(path) (#1042)
save() saves to the current path (in-place).
save_as(path) saves to a new location.
Clearer intent than a single method with an optional path arg.
* refactor(mcp): migrate nteract server to wrapper API, expand API surface (#1044)
* refactor(mcp): migrate nteract server to Client/Notebook wrapper API
Replace NativeAsyncClient/AsyncSession with the high-level wrapper:
- _client: NativeAsyncClient -> Client
- _session: AsyncSession -> _notebook: Notebook
- join/open/create use Client.join_notebook(), etc.
- save uses notebook.save() / notebook.save_as(path)
- restart/interrupt use notebook.restart() / notebook.interrupt()
- list_active_notebooks returns NotebookInfo objects with runtime terminology
- _get_session() helper preserved as escape hatch for advanced ops
(splice_source, presence, deps, streaming, cell metadata)
This dogfoods the wrapper API in a real consumer and validates
the escape hatch pattern works for operations not on the wrapper.
* refactor(python): expand wrapper API surface, dogfood in MCP server
Add to Notebook:
- add_dependency/remove_dependency/get_dependencies (auto-detect pkg mgr)
- sync_environment
- run_all
- presence (Presence object for cursor/selection/focus)
Add to CellHandle:
- set_tags, set_source_hidden, set_outputs_hidden (async mutations)
- stream (streaming execution returning async iterator)
New Presence class (notebook.presence):
- set_cursor, set_selection, focus
- clear_cursor, clear_selection
- get_remote_cursors
Migrate nteract MCP server to use wrapper API throughout:
- Dependencies go through notebook.add_dependency() etc.
- Cell tags/hidden go through CellHandle methods
- Create/delete/move/clear_outputs use CellCollection/CellHandle
- replace_match/replace_regex use cell.source + cell.splice
- Streaming execution uses cell.stream()
- Presence uses notebook.presence
- Only remaining escape hatch: get_cells/get_cell for full Cell
snapshots (needed by formatting helpers that want resolved outputs)
Exports updated: Presence added to __all__ (26 items), stubs, and tests.
* refactor(mcp): eliminate all session escape hatches
Migrate formatting helpers from Cell (Rust snapshot) to CellHandle
(live wrapper). All formatters now accept CellHandle, which has every
property they need (id, source, cell_type, execution_count, outputs).
Removed _get_session() — no callers remain. The MCP server now uses
the wrapper API exclusively:
- notebook.cells for iteration and lookup
- CellHandle properties for reads
- CellHandle methods for mutations
- notebook.presence for cursor/selection/focus
- notebook.session only for queue_state and is_connected (status resource)
* fix(python): correct get_remote_cursors return type
* refactor(python): trim public surface to wrapper-reachable types only (#1047)
__all__ goes from 26 to 17 items — only types reachable through the
wrapper API are publicly advertised.
Removed from __all__ (still importable via runtimed.runtimed):
- NativeAsyncClient, NativeClient, AsyncSession, Session
- CompletionItem, CompletionResult, HistoryEntry
- QueueState, NotebookConnectionInfo
Removed notebook.session property — no public escape hatch. Internal
code uses notebook._session directly.
Added to Notebook:
- is_connected() — eliminates last session.is_connected() usage
- queue_state() — eliminates last session.get_queue_state() usage
The nteract MCP server now has zero notebook.session references.
* chore: delete stale presence demos — will be rewritten with wrapper API1 parent b4b55a7 commit 612d578
File tree
30 files changed
+1744
-2139
lines changed- .claude/skills/python-bindings
- contributing
- crates/runtimed-py/src
- docs
- python
- gremlin
- nteract/src/nteract
- runtimed
- demos
- src/runtimed
- tests
30 files changed
+1744
-2139
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
40 | 41 | | |
41 | 42 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
58 | 63 | | |
59 | 64 | | |
60 | 65 | | |
| |||
72 | 77 | | |
73 | 78 | | |
74 | 79 | | |
75 | | - | |
| 80 | + | |
76 | 81 | | |
77 | | - | |
| 82 | + | |
78 | 83 | | |
79 | 84 | | |
80 | | - | |
81 | | - | |
82 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
83 | 88 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
| 89 | + | |
| 90 | + | |
90 | 91 | | |
91 | 92 | | |
92 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
93 | 102 | | |
94 | 103 | | |
95 | 104 | | |
96 | 105 | | |
97 | 106 | | |
98 | 107 | | |
99 | | - | |
100 | | - | |
| 108 | + | |
101 | 109 | | |
102 | 110 | | |
103 | 111 | | |
| |||
106 | 114 | | |
107 | 115 | | |
108 | 116 | | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | 117 | | |
125 | 118 | | |
126 | 119 | | |
| |||
159 | 152 | | |
160 | 153 | | |
161 | 154 | | |
162 | | - | |
| 155 | + | |
163 | 156 | | |
164 | | - | |
| 157 | + | |
165 | 158 | | |
166 | | - | |
| 159 | + | |
167 | 160 | | |
168 | 161 | | |
169 | 162 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
10 | 26 | | |
11 | 27 | | |
12 | 28 | | |
| |||
59 | 75 | | |
60 | 76 | | |
61 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
62 | 86 | | |
63 | 87 | | |
64 | 88 | | |
| |||
183 | 207 | | |
184 | 208 | | |
185 | 209 | | |
186 | | - | |
187 | | - | |
| 210 | + | |
| 211 | + | |
188 | 212 | | |
189 | | - | |
| 213 | + | |
190 | 214 | | |
191 | 215 | | |
192 | 216 | | |
| |||
206 | 230 | | |
207 | 231 | | |
208 | 232 | | |
209 | | - | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
210 | 236 | | |
211 | 237 | | |
212 | 238 | | |
| |||
216 | 242 | | |
217 | 243 | | |
218 | 244 | | |
219 | | - | |
| 245 | + | |
220 | 246 | | |
221 | 247 | | |
222 | 248 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
170 | 181 | | |
171 | 182 | | |
172 | 183 | | |
| |||
205 | 216 | | |
206 | 217 | | |
207 | 218 | | |
208 | | - | |
| 219 | + | |
209 | 220 | | |
210 | 221 | | |
211 | 222 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
87 | 106 | | |
88 | 107 | | |
89 | 108 | | |
| |||
274 | 293 | | |
275 | 294 | | |
276 | 295 | | |
| 296 | + | |
277 | 297 | | |
278 | 298 | | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
310 | 316 | | |
311 | 317 | | |
312 | | - | |
| 318 | + | |
313 | 319 | | |
314 | 320 | | |
315 | 321 | | |
| |||
342 | 348 | | |
343 | 349 | | |
344 | 350 | | |
345 | | - | |
346 | | - | |
| 351 | + | |
347 | 352 | | |
348 | 353 | | |
349 | 354 | | |
| |||
367 | 372 | | |
368 | 373 | | |
369 | 374 | | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
| 375 | + | |
389 | 376 | | |
390 | 377 | | |
391 | 378 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
215 | 215 | | |
216 | 216 | | |
217 | 217 | | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
223 | 221 | | |
224 | | - | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
225 | 226 | | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
| |||
0 commit comments