-
Notifications
You must be signed in to change notification settings - Fork 7
fix: Detect external changes to notebooks #319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Artmann
wants to merge
6
commits into
main
Choose a base branch
from
chris/detect-external-changes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+531
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
56ae054
fix: Detect external changes to notebooks
Artmann 1edb1ec
Improvements
Artmann 72ed472
format
Artmann be9c94e
fix: Use validYaml in debounce test to avoid false cell match
Artmann efa5666
refactor: Extract block ID helper and fix test teardown cleanup
Artmann d6d36cc
fix: Address CodeRabbit review comments
Artmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { | ||
| CancellationTokenSource, | ||
| NotebookCellData, | ||
| NotebookCellOutput, | ||
| NotebookDocument, | ||
| NotebookEdit, | ||
| NotebookRange, | ||
| Uri, | ||
| WorkspaceEdit, | ||
| workspace | ||
| } from 'vscode'; | ||
| import { inject, injectable, optional } from 'inversify'; | ||
|
|
||
| import { IExtensionSyncActivationService } from '../../platform/activation/types'; | ||
| import { IDisposableRegistry } from '../../platform/common/types'; | ||
| import { logger } from '../../platform/logging'; | ||
| import { IDeepnoteNotebookManager } from '../types'; | ||
| import { DeepnoteNotebookSerializer } from './deepnoteSerializer'; | ||
| import { isSnapshotFile } from './snapshots/snapshotFiles'; | ||
| import { SnapshotService } from './snapshots/snapshotService'; | ||
|
|
||
| const debounceTimeInMilliseconds = 500; | ||
|
|
||
| /** | ||
| * Watches .deepnote files for external changes and reloads open notebook editors. | ||
| * | ||
| * When AI agents (Cursor, Claude Code) modify a .deepnote file on disk, | ||
| * VS Code's NotebookSerializer does not reliably detect and reload the notebook. | ||
| * This service bridges that gap by watching the filesystem and applying edits | ||
| * to open notebook documents when their underlying files change externally. | ||
| */ | ||
| @injectable() | ||
| export class DeepnoteFileChangeWatcher implements IExtensionSyncActivationService { | ||
| private readonly debounceTimers = new Map<string, ReturnType<typeof setTimeout>>(); | ||
| private readonly serializer: DeepnoteNotebookSerializer; | ||
|
|
||
| constructor( | ||
| @inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry, | ||
| @inject(IDeepnoteNotebookManager) private readonly notebookManager: IDeepnoteNotebookManager, | ||
| @inject(SnapshotService) @optional() private readonly snapshotService?: SnapshotService | ||
| ) { | ||
| this.serializer = new DeepnoteNotebookSerializer(this.notebookManager, this.snapshotService); | ||
| } | ||
|
|
||
| public activate(): void { | ||
| const watcher = workspace.createFileSystemWatcher('**/*.deepnote'); | ||
|
|
||
| this.disposables.push(watcher); | ||
| this.disposables.push(watcher.onDidChange((uri) => this.handleFileChange(uri))); | ||
| this.disposables.push({ dispose: () => this.clearAllTimers() }); | ||
| } | ||
|
|
||
| private cellsMatchNotebook(notebook: NotebookDocument, newCells: NotebookCellData[]): boolean { | ||
| const liveCells = notebook.getCells(); | ||
|
|
||
| if (liveCells.length !== newCells.length) { | ||
| return false; | ||
| } | ||
|
|
||
| return liveCells.every( | ||
| (live, i) => live.document.getText() === newCells[i].value && live.kind === newCells[i].kind | ||
| ); | ||
| } | ||
|
|
||
| private clearAllTimers(): void { | ||
| for (const timer of this.debounceTimers.values()) { | ||
| clearTimeout(timer); | ||
| } | ||
|
|
||
| this.debounceTimers.clear(); | ||
| } | ||
|
|
||
| private getBlockIdFromMetadata(metadata: Record<string, unknown> | undefined): string | undefined { | ||
| return (metadata?.id ?? metadata?.__deepnoteBlockId) as string | undefined; | ||
| } | ||
|
|
||
| private handleFileChange(uri: Uri): void { | ||
| if (isSnapshotFile(uri)) { | ||
| return; | ||
| } | ||
|
|
||
| const key = uri.toString(); | ||
| const existing = this.debounceTimers.get(key); | ||
|
|
||
| if (existing) { | ||
| clearTimeout(existing); | ||
| } | ||
|
|
||
| this.debounceTimers.set( | ||
| key, | ||
| setTimeout(() => { | ||
| this.debounceTimers.delete(key); | ||
|
|
||
| void this.reloadNotebooksForFile(uri); | ||
| }, debounceTimeInMilliseconds) | ||
| ); | ||
| } | ||
|
|
||
| private async reloadNotebooksForFile(uri: Uri): Promise<void> { | ||
| const uriString = uri.toString(); | ||
| const affectedNotebooks = workspace.notebookDocuments.filter( | ||
| (doc) => | ||
| doc.notebookType === 'deepnote' && doc.uri.with({ query: '', fragment: '' }).toString() === uriString | ||
| ); | ||
|
|
||
| if (affectedNotebooks.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| let content: Uint8Array; | ||
|
|
||
| try { | ||
| content = await workspace.fs.readFile(uri); | ||
| } catch (error) { | ||
| logger.warn(`[FileChangeWatcher] Failed to read changed file: ${uri.path}`, error); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const tokenSource = new CancellationTokenSource(); | ||
| let newData; | ||
| try { | ||
| newData = await this.serializer.deserializeNotebook(content, tokenSource.token); | ||
| } catch (error) { | ||
| logger.warn(`[FileChangeWatcher] Failed to parse changed file: ${uri.path}`, error); | ||
|
|
||
| return; | ||
| } finally { | ||
| tokenSource.dispose(); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
Artmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (const notebook of affectedNotebooks) { | ||
| try { | ||
| const newCells = newData.cells.map((cell) => ({ ...cell })); | ||
|
|
||
| if (this.cellsMatchNotebook(notebook, newCells)) { | ||
| continue; | ||
| } | ||
|
|
||
| // Preserve outputs from live cells that the deserialized data may lack. | ||
| // In snapshot mode the main file has outputs stripped; AI agents | ||
| // typically don't preserve outputs when editing code. | ||
| const liveCells = notebook.getCells(); | ||
| const liveOutputsByBlockId = new Map<string, readonly NotebookCellOutput[]>(); | ||
| for (const liveCell of liveCells) { | ||
| const blockId = this.getBlockIdFromMetadata(liveCell.metadata); | ||
| if (blockId && liveCell.outputs.length > 0) { | ||
| liveOutputsByBlockId.set(blockId, liveCell.outputs); | ||
| } | ||
| } | ||
|
|
||
| for (const cell of newCells) { | ||
| const blockId = this.getBlockIdFromMetadata(cell.metadata); | ||
| if (blockId && (!cell.outputs || cell.outputs.length === 0)) { | ||
| const liveOutputs = liveOutputsByBlockId.get(blockId); | ||
| if (liveOutputs) { | ||
| cell.outputs = [...liveOutputs]; | ||
| } | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const edit = new WorkspaceEdit(); | ||
| edit.set(notebook.uri, [NotebookEdit.replaceCells(new NotebookRange(0, notebook.cellCount), newCells)]); | ||
| const applied = await workspace.applyEdit(edit); | ||
| if (!applied) { | ||
| logger.warn(`[FileChangeWatcher] Failed to apply edit: ${notebook.uri.path}`); | ||
| continue; | ||
| } | ||
|
|
||
| // Save immediately so VS Code updates its internal mtime for the file. | ||
| // Without this, the user gets a "content is newer" conflict dialog on | ||
| // their next manual save because VS Code still remembers the old mtime. | ||
| await workspace.save(notebook.uri); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| logger.info(`[FileChangeWatcher] Reloaded notebook from external change: ${notebook.uri.path}`); | ||
| } catch (error) { | ||
| logger.error(`[FileChangeWatcher] Failed to reload notebook: ${notebook.uri.path}`, error); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious if you figured out why this is the case / root cause? What makes a
.deepnotefile behave differently?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's mostly because our multiple editors (notebooks) are in a single file setup.