Skip to content

refactor: move notebook metadata types to shared notebook-doc crate#566

Merged
rgbkrk merged 1 commit intomainfrom
540/metadata-to-notebook-doc
Mar 6, 2026
Merged

refactor: move notebook metadata types to shared notebook-doc crate#566
rgbkrk merged 1 commit intomainfrom
540/metadata-to-notebook-doc

Conversation

@rgbkrk
Copy link
Member

@rgbkrk rgbkrk commented Mar 6, 2026

Moves notebook metadata types (NotebookMetadataSnapshot, RuntMetadata, KernelspecSnapshot, etc.) from runtimed to the shared notebook-doc crate. All peers now share one definition.

Why

Every peer (daemon, Tauri relay, WASM frontend, Python bindings) needs to read and write notebook metadata — kernelspec, dependencies, trust signatures. Previously these types lived in runtimed, forcing all consumers to depend on the full daemon crate. Now they're in notebook-doc alongside the Automerge doc they describe.

What changed

  • git mv runtimed/src/notebook_metadata.rsnotebook-doc/src/metadata.rs (history preserved)
  • Re-export from runtimed as notebook_metadata — zero breakage for existing callers
  • Add typed helpers on NotebookDoc:
    • get_metadata_snapshot() — deserialize NotebookMetadataSnapshot from doc
    • set_metadata_snapshot() — serialize and write back
    • detect_runtime() — kernelspec + language_info detection ("python", "deno", or None)

What this enables

Any consumer with a NotebookDoc (or WASM NotebookHandle) can do:

let snapshot = doc.get_metadata_snapshot();
let runtime = doc.detect_runtime(); // Some("python")

Without manual JSON parsing or depending on runtimed. This is the foundation for the frontend reading metadata directly from its WASM doc instead of via Tauri commands.

QA

  • Existing notebook operations work (kernel launch, save, dependency management)
  • Runtime detection produces correct results

31 notebook-doc tests, 225 runtimed tests, all passing.

All peers (daemon, Tauri relay, WASM frontend, Python bindings) now
share one definition of notebook metadata via notebook-doc.

- git mv notebook_metadata.rs → notebook-doc/src/metadata.rs
- Re-export from runtimed as notebook_metadata (backwards compatible)
- Add typed helpers on NotebookDoc:
  - get_metadata_snapshot() / set_metadata_snapshot()
  - detect_runtime() — kernelspec + language_info detection
- 31 notebook-doc tests (23 doc + 8 metadata), all passing
- 225 runtimed tests, all passing

Any consumer with a NotebookDoc can now do typed metadata operations
without manual JSON parsing or depending on runtimed directly.
@rgbkrk rgbkrk enabled auto-merge (squash) March 6, 2026 16:55
@rgbkrk
Copy link
Member Author

rgbkrk commented Mar 6, 2026

Follow-on work enabled by this PR

With metadata types in notebook-doc, two paths open up for further relay simplification:

1. Frontend reads metadata from its own WASM doc

The Tauri metadata commands (get_notebook_dependencies, set_notebook_dependencies, get_notebook_runtime, verify_notebook_trust, etc.) currently go through handle.get_metadata() — an async hop to the relay's sync task to read from its local Automerge doc.

The frontend's WASM NotebookHandle has the same data. Once we expose get_metadata_snapshot() and detect_runtime() from the WASM bindings, these Tauri commands can be replaced by direct frontend reads. No IPC, no relay involvement.

Scope: Rebuild runtimed-wasm with the new notebook-doc metadata API, then migrate frontend hooks (useDependencies, useCondaDependencies, useDenoDependencies, useTrust) to read from WASM instead of invoke().

2. Daemon-owned initial population

initialize_notebook_sync in lib.rs populates the relay's Automerge doc with cells from NotebookState via handle.add_cell() + handle.update_source(). This is the last path that writes cells through the handle.

Alternative: the daemon reads the ipynb file directly (it already does for saved notebooks via notebook-watch) and populates its own doc. The relay connects with an empty doc and syncs from the daemon. This eliminates the NotebookState → relay → daemon bootstrap path entirely.

Scope: Larger change — touches the connection handshake protocol and the untitled-notebook creation flow. Good candidate for a dedicated spike.

@rgbkrk rgbkrk merged commit 83a5f30 into main Mar 6, 2026
14 checks passed
@rgbkrk rgbkrk deleted the 540/metadata-to-notebook-doc branch March 6, 2026 17:11
rgbkrk added a commit that referenced this pull request Mar 6, 2026
After PR #554 (dead command removal) and #566 (metadata fallback migration),
NotebookState is only used in 4 functions. This PR removes those usages by:

1. Add dirty flag to WindowNotebookContext - tracks unsaved changes in context
   instead of NotebookState
2. Migrate clone_notebook_to_path to daemon - daemon reads from Automerge,
   clears outputs, generates fresh env_id
3. Decouple initialize_notebook_sync from NotebookState - daemon is source of
   truth; pass explicit notebook_id and cells rather than NotebookState

Step 4 (removing notebook_state field from WindowNotebookContext) is deferred
because fallback usages (get_kernelspec, get_dependencies, etc.) read from
NotebookState when daemon isn't connected. Removing these requires either
removing fallbacks or finding alternative sources.

The daemon now owns notebook content via Automerge; NotebookState remains only
as a local window cache and for fallback cases.
rgbkrk added a commit that referenced this pull request Mar 6, 2026
* refactor: eliminate NotebookState from save, clone, and reconnect

After PR #554 (dead command removal) and #566 (metadata fallback migration),
NotebookState is only used in 4 functions. This PR removes those usages by:

1. Add dirty flag to WindowNotebookContext - tracks unsaved changes in context
   instead of NotebookState
2. Migrate clone_notebook_to_path to daemon - daemon reads from Automerge,
   clears outputs, generates fresh env_id
3. Decouple initialize_notebook_sync from NotebookState - daemon is source of
   truth; pass explicit notebook_id and cells rather than NotebookState

Step 4 (removing notebook_state field from WindowNotebookContext) is deferred
because fallback usages (get_kernelspec, get_dependencies, etc.) read from
NotebookState when daemon isn't connected. Removing these requires either
removing fallbacks or finding alternative sources.

The daemon now owns notebook content via Automerge; NotebookState remains only
as a local window cache and for fallback cases.

* fix: address reviewer feedback on save-as and clone

1. Fix save_notebook_as data loss risk: Read cells and metadata from the
   old room BEFORE disconnecting. The new room won't have them since it's
   a different notebook_id, so we need to carry them across to populate
   the new room if it's empty.

2. Fix clone_notebook_to_disk dropping metadata: Read existing notebook
   to preserve cell metadata, attachments, and non-snapshot notebook
   metadata fields. Use split_inclusive for cleaner source line splitting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant