Skip to content

refactor: eliminate NotebookState from save, clone, and reconnect#567

Merged
rgbkrk merged 2 commits intomainfrom
quill/remove-notebook-state
Mar 6, 2026
Merged

refactor: eliminate NotebookState from save, clone, and reconnect#567
rgbkrk merged 2 commits intomainfrom
quill/remove-notebook-state

Conversation

@rgbkrk
Copy link
Member

@rgbkrk rgbkrk commented Mar 6, 2026

Summary

Removes NotebookState usages from the save, clone, and reconnect paths. After PRs #554 (dead command removal) and #566 (metadata fallback migration), NotebookState was only used in 4 functions. This PR eliminates those usages:

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

Key Changes

  • save_notebook and save_notebook_as now use context.dirty instead of NotebookState.dirty
  • save_notebook_as reads cells/metadata from old room before disconnecting, then passes them to populate the new room (prevents data loss on room switch)
  • CloneNotebook protocol added - daemon handles clone with fresh env_id, cleared outputs, preserved cell metadata/attachments
  • initialize_notebook_sync signature changed to take explicit (notebook_id, cells, metadata) instead of Arc<Mutex<NotebookState>>
  • Added notebook_id: Arc<Mutex<String>> to WindowNotebookContext for reconnect tracking

Remaining Work

NotebookState field remains in WindowNotebookContext for fallback usages (get_kernelspec, get_dependencies, etc.) that read from local state when daemon isn't connected. Full removal requires either removing fallbacks or finding alternative sources - tracked for future PR.

Verification

  • Save clears dirty state
  • Save As preserves all cells after room reconnect
  • Save As to new path updates window title
  • Clone clears outputs but preserves source and cell metadata
  • Clone preserves markdown attachments (embedded images)
  • Clone generates fresh env_id
  • Reconnect after daemon restart preserves cells (frontend WASM doc)
  • New untitled notebook works correctly

PR submitted by @rgbkrk's agent, Quill

rgbkrk added 2 commits March 6, 2026 09:31
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.
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.
@rgbkrk rgbkrk marked this pull request as ready for review March 6, 2026 17:56
@rgbkrk rgbkrk merged commit 0159368 into main Mar 6, 2026
23 of 24 checks passed
@rgbkrk rgbkrk deleted the quill/remove-notebook-state branch March 6, 2026 18:19
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